1
0

BxDolModule.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Base class for Module classes in modules engine.
  11. *
  12. * The object of the class contains major objects of the whole module. They are:
  13. * a. An object of config class
  14. * @see BxDolModuleConfig
  15. *
  16. * b. An object of database class.
  17. * @see BxDolModuleQuery
  18. *
  19. * c. An object of template class.
  20. * @see BxDolTemplate
  21. *
  22. *
  23. * Example of usage:
  24. * refer to any BoonEx module
  25. *
  26. *
  27. * Static Methods:
  28. *
  29. * Get an instance of a module's class.
  30. * @see BxDolModule::getInstance($sClassName)
  31. *
  32. *
  33. * Memberships/ACL:
  34. * Doesn't depend on user's membership.
  35. *
  36. *
  37. * Alerts:
  38. * no alerts available
  39. *
  40. */
  41. class BxDolModule extends BxDol
  42. {
  43. public $_aModule;
  44. private $_aInstancesStorage = [];
  45. private $_aInstancesToStore = ['_oDb' => '_initDb', '_oTemplate' => '_initTemplate', '_oConfig' => '_initConfig'];
  46. /**
  47. * constructor
  48. */
  49. public function __construct($aModule)
  50. {
  51. parent::__construct();
  52. $this->_aModule = $aModule;
  53. }
  54. public function __get($sName) {
  55. if (isset($this->_aInstancesToStore[$sName])) {
  56. $sMethod = $this->_aInstancesToStore[$sName];
  57. return $this->_aInstancesStorage[$sName] ??= $this->$sMethod();
  58. }
  59. throw new \Exception("Unknown property: $sName");
  60. }
  61. public function __set($sName, $o) {
  62. if (isset($this->_aInstancesToStore[$sName])) {
  63. $this->_aInstancesStorage[$sName] = $o;
  64. }
  65. throw new \Exception("Unknown property: $sName");
  66. }
  67. private function _initDb():BxDolModuleDb {
  68. $sClassPrefix = $this->_aModule['class_prefix'];
  69. $sClassPath = BX_DIRECTORY_PATH_MODULES . $this->_aModule['path'] . 'classes/';
  70. $sClassName = $sClassPrefix . 'Db';
  71. require_once($sClassPath . $sClassName . '.php');
  72. /** @var BxDolDb $oDb */
  73. $oDb = new $sClassName($this->_oConfig);
  74. return $oDb;
  75. }
  76. private function _initConfig():BxDolModuleConfig {
  77. $sClassPrefix = $this->_aModule['class_prefix'];
  78. $sClassPath = BX_DIRECTORY_PATH_MODULES . $this->_aModule['path'] . 'classes/';
  79. $sClassName = $sClassPrefix . 'Config';
  80. require_once($sClassPath . $sClassName . '.php');
  81. /** @var BxDolModuleConfig $oConfig */
  82. $oConfig = new $sClassName($this->_aModule);
  83. return $oConfig;
  84. }
  85. private function _initTemplate():BxDolModuleProxy {
  86. $sClassPrefix = $this->_aModule['class_prefix'];
  87. $sClassPath = BX_DIRECTORY_PATH_MODULES . $this->_aModule['path'] . 'classes/';
  88. $sClassName = $sClassPrefix . 'Template';
  89. require_once($sClassPath . $sClassName . '.php');
  90. /** @var BxDolTemplate $oTemplate */
  91. $oTemplate = new $sClassName($this->_oConfig, $this->_oDb);
  92. $oTemplate->init();
  93. return new BxDolModuleProxy('module_template', $oTemplate);
  94. }
  95. /**
  96. * Static method to get an instance of a module's class.
  97. *
  98. * @param $sName module name.
  99. */
  100. public static function getInstance($sName)
  101. {
  102. if (empty($sName))
  103. return null;
  104. $aModule = BxDolModuleQuery::getInstance()->getModuleByName($sName);
  105. if (empty($aModule) || !is_array($aModule))
  106. return null;
  107. $sClassName = $aModule['class_prefix'] . 'Module';
  108. if ('system' != $sName) {
  109. if (isset($GLOBALS['bxDolClasses'][$sClassName]))
  110. return $GLOBALS['bxDolClasses'][$sClassName];
  111. $sClassPath = BX_DIRECTORY_PATH_MODULES . $aModule['path'] . '/classes/' . $sClassName . '.php';
  112. if (!file_exists($sClassPath))
  113. return null;
  114. require_once($sClassPath);
  115. $GLOBALS['bxDolClasses'][$sClassName] = new $sClassName($aModule);
  116. return $GLOBALS['bxDolClasses'][$sClassName];
  117. } else {
  118. $sClassName = 'BxTemplServices';
  119. return bx_instance($sClassName, array($aModule));
  120. }
  121. }
  122. public static function getTitle($sUri)
  123. {
  124. return _t(self::getTitleKey($sUri));
  125. }
  126. public static function getTitleKey($sUri)
  127. {
  128. return 'system' == $sUri ? '_adm_txt_module_system' : '_sys_module_' . strtolower(str_replace(' ', '_', $sUri));
  129. }
  130. /**
  131. * is module enabled
  132. */
  133. function isEnabled()
  134. {
  135. return (int)$this->_aModule['enabled'] == 1;
  136. }
  137. /**
  138. * get module name
  139. */
  140. function getName()
  141. {
  142. return $this->_aModule['name'];
  143. }
  144. /**
  145. * Check whether user logged in or not.
  146. *
  147. * @return boolean result of operation.
  148. */
  149. public function isLogged()
  150. {
  151. return isLogged();
  152. }
  153. /**
  154. * Get currently logged in user ID.
  155. *
  156. * @return integer user ID.
  157. */
  158. public function getUserId()
  159. {
  160. return getLoggedId();
  161. }
  162. /**
  163. * Get currently logged in user password.
  164. *
  165. * @return string user password.
  166. */
  167. public function getUserPassword ()
  168. {
  169. return getLoggedPassword();
  170. }
  171. public function serviceIsSafeService($s)
  172. {
  173. $sService = bx_gen_method_name($s);
  174. $aServices = $this->serviceGetSafeServices();
  175. return isset($aServices[$sService]);
  176. }
  177. public function serviceGetSafeServices()
  178. {
  179. return array();
  180. }
  181. public function serviceIsPublicService($s)
  182. {
  183. $sService = bx_gen_method_name($s);
  184. $aServices = $this->serviceGetPublicServices();
  185. return isset($aServices[$sService]);
  186. }
  187. public function serviceGetPublicServices()
  188. {
  189. return array();
  190. }
  191. }
  192. /** @} */