BxDolModuleQuery.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. class BxDolModuleQuery extends BxDolDb implements iBxDolSingleton
  10. {
  11. protected function __construct()
  12. {
  13. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  14. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  15. parent::__construct();
  16. }
  17. /**
  18. * Prevent cloning the instance
  19. */
  20. public function __clone()
  21. {
  22. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  23. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  24. }
  25. /**
  26. * Get singleton instance of the class
  27. */
  28. public static function getInstance()
  29. {
  30. $sClass = __CLASS__;
  31. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  32. $GLOBALS['bxDolClasses'][__CLASS__] = new $sClass();
  33. return $GLOBALS['bxDolClasses'][__CLASS__];
  34. }
  35. function getModuleById($iId, $bFromCache = true)
  36. {
  37. $sSql = $this->prepare("SELECT * FROM `sys_modules` WHERE `id`=? LIMIT 1", $iId);
  38. return $bFromCache ? $this->fromMemory('sys_modules_' . $iId, 'getRow', $sSql) : $this->getRow($sSql);
  39. }
  40. function getModuleByName($sName, $bFromCache = true)
  41. {
  42. if ($bFromCache) {
  43. $sSql = "SELECT * FROM `sys_modules`";
  44. $a = $this->fromCache('sys_modules_list', 'getAllWithKey', $sSql, 'name');
  45. if ($a && isset($a[$sName]))
  46. return $a[$sName];
  47. return false;
  48. } else {
  49. $sSql = $this->prepare("SELECT * FROM `sys_modules` WHERE `name`=? LIMIT 1", $sName);
  50. return $this->getRow($sSql);
  51. }
  52. }
  53. function getModuleByUri($sUri, $bFromCache = true)
  54. {
  55. $sSql = $this->prepare("SELECT * FROM `sys_modules` WHERE `uri`=? LIMIT 1", $sUri);
  56. return $bFromCache ? $this->fromMemory('sys_modules_' . $sUri, 'getRow', $sSql) : $this->getRow($sSql);
  57. }
  58. function enableModuleByUri($sUri)
  59. {
  60. $sSql = $this->prepare("UPDATE `sys_modules` SET `enabled`='1' WHERE `uri`=? LIMIT 1", $sUri);
  61. $bRet = (int)$this->query($sSql) > 0;
  62. if ($bRet)
  63. $this->cleanCache('sys_modules_list');
  64. return $bRet;
  65. }
  66. function disableModuleByUri($sUri)
  67. {
  68. $sSql = $this->prepare("UPDATE `sys_modules` SET `enabled`='0' WHERE `uri`=? LIMIT 1", $sUri);
  69. $bRet = (int)$this->query($sSql) > 0;
  70. if ($bRet)
  71. $this->cleanCache('sys_modules_list');
  72. return $bRet;
  73. }
  74. function setModulePendingUninstall($sUri, $bPendingUninstall)
  75. {
  76. $sSql = $this->prepare("UPDATE `sys_modules` SET `pending_uninstall` = ? WHERE `uri` = ? LIMIT 1", $bPendingUninstall ? 1 : 0, $sUri);
  77. $bRet = $this->query($sSql);
  78. if ($bRet)
  79. $this->cleanCache('sys_modules_list');
  80. return $bRet;
  81. }
  82. function isModule($sUri)
  83. {
  84. $sSql = $this->prepare("SELECT `id` FROM `sys_modules` WHERE `uri`=? LIMIT 1", $sUri);
  85. return (int)$this->fromMemory('sys_module_' . $sUri, 'getOne', $sSql) > 0;
  86. }
  87. function isModuleByName($sName)
  88. {
  89. $sSql = $this->prepare("SELECT `id` FROM `sys_modules` WHERE `name`=? LIMIT 1", $sName);
  90. return (int)$this->fromMemory('sys_module_' . $sName, 'getOne', $sSql) > 0;
  91. }
  92. function isModuleParamsUsed($sName, $sUri, $sPath, $sPrefixDb, $sPrefixClass)
  93. {
  94. $sSql = "SELECT `id` FROM `sys_modules` WHERE `name`=:name || `uri`=:uri || `path`=:path || `db_prefix`=:db_prefix || `class_prefix`=:class_prefix LIMIT 1";
  95. return (int)$this->getOne($sSql, [
  96. 'name' => $sName,
  97. 'uri' => $sUri,
  98. 'path' => $sPath,
  99. 'db_prefix' => $sPrefixDb,
  100. 'class_prefix' => $sPrefixClass
  101. ]) > 0;
  102. }
  103. function isEnabled($sUri)
  104. {
  105. $sSql = $this->prepare("SELECT `id` FROM `sys_modules` WHERE `uri`=? AND `enabled`='1' LIMIT 1", $sUri);
  106. return (int)$this->fromMemory('sys_module_enabled_' . $sUri, 'getOne', $sSql) > 0;
  107. }
  108. function isEnabledByName($sName)
  109. {
  110. $sSql = $this->prepare("SELECT `id` FROM `sys_modules` WHERE `name`=? AND `enabled`='1' LIMIT 1", $sName);
  111. return (int)$this->fromMemory('sys_module_enabled_' . $sName, 'getOne', $sSql) > 0;
  112. }
  113. function getModules()
  114. {
  115. $sSql = "SELECT * FROM `sys_modules` ORDER BY `title`";
  116. return $this->fromMemory('sys_modules', 'getAll', $sSql);
  117. }
  118. function getModulesBy($aParams = array(), $bFromCache = true)
  119. {
  120. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  121. $sPostfix = $sWhereClause = $sOrderByClause = "";
  122. $sSelectClause = "`id`, `type`, `name`, `title`, `vendor`, `version`, `help_url`, `path`, `uri`, `class_prefix`, `db_prefix`, `lang_category`, `date`, `enabled`";
  123. $aBindings = [];
  124. switch($aParams['type']) {
  125. case 'type':
  126. if(!is_array($aParams['value']))
  127. $aParams['value'] = array($aParams['value']);
  128. $sPostfix .= '_type_' . implode('_', $aParams['value']);
  129. $sWhereClause .= " AND `type` IN (" . $this->implode_escape($aParams['value']) . ")";
  130. break;
  131. case 'modules':
  132. $sPostfix .= '_modules';
  133. $aBindings['type'] = BX_DOL_MODULE_TYPE_MODULE;
  134. $sWhereClause .= " AND `type`=:type";
  135. break;
  136. case 'modules_subtypes':
  137. $sPostfix .= '_modules_subtypes_';
  138. if(isset($aParams['id_as_key']) && $aParams['id_as_key'] === true) {
  139. $sPostfix .= 'id_as_key_';
  140. $aMethod['name'] = 'getAllWithKey';
  141. $aMethod['params'][1] = 'id';
  142. }
  143. if(isset($aParams['name_as_key']) && $aParams['name_as_key'] === true) {
  144. $sPostfix .= 'name_as_key_';
  145. $aMethod['name'] = 'getAllWithKey';
  146. $aMethod['params'][1] = 'name';
  147. }
  148. if(!is_array($aParams['value']))
  149. $aParams['value'] = [$aParams['value']];
  150. $sPostfix .= implode('_', $aParams['value']);
  151. $aBindings['type'] = BX_DOL_MODULE_TYPE_MODULE;
  152. $iSubtypes = 0;
  153. foreach($aParams['value'] as $iSubtype)
  154. $iSubtypes |= pow(2, $iSubtype);
  155. $aBindings['subtypes'] = $iSubtypes;
  156. $sWhereClause .= " AND `type`=:type AND `subtypes` & :subtypes";
  157. break;
  158. case 'is_module_subtype':
  159. $sPostfix .= '_is_module_' . $aParams['subtype'] . '_' . $aParams['name'];
  160. $aMethod['name'] = 'getOne';
  161. $aBindings = [
  162. 'type' => BX_DOL_MODULE_TYPE_MODULE,
  163. 'subtypes' => pow(2, (int)$aParams['subtype']),
  164. 'name' => $aParams['name']
  165. ];
  166. $sSelectClause = "`id`";
  167. $sWhereClause .= " AND `type`=:type AND `subtypes` & :subtypes AND `name`=:name";
  168. break;
  169. case 'languages':
  170. $sPostfix .= '_languages';
  171. $aBindings['type'] = BX_DOL_MODULE_TYPE_LANGUAGE;
  172. $sWhereClause .= " AND `type`=:type";
  173. break;
  174. case 'templates':
  175. $sPostfix .= '_templates';
  176. $aBindings['type'] = BX_DOL_MODULE_TYPE_TEMPLATE;
  177. $sWhereClause .= " AND `type`=:type";
  178. break;
  179. case 'path_and_uri':
  180. $sPostfix .= '_path_and_uri_' . $aParams['path'] . '_' . $aParams['uri'];
  181. $aMethod['name'] = 'getRow';
  182. $aBindings = array_merge($aBindings, array(
  183. 'path' => $aParams['path'],
  184. 'uri' => $aParams['uri']
  185. ));
  186. $sWhereClause .= " AND `path`=:path AND `uri`=:uri";
  187. break;
  188. case 'all_pairs_name_uri':
  189. $sPostfix .= 'all_pairs_name_uri';
  190. $aMethod['name'] = 'getPairs';
  191. $aMethod['params'][1] = 'name';
  192. $aMethod['params'][2] = 'uri';
  193. break;
  194. case 'all_key_name':
  195. $sPostfix .= 'all_key_name';
  196. $aMethod['name'] = 'getAllWithKey';
  197. $aMethod['params'][1] = 'name';
  198. break;
  199. case 'all':
  200. break;
  201. }
  202. if(isset($aParams['active'])) {
  203. $sPostfix .= "_active";
  204. $aBindings['enabled'] = (int)$aParams['active'];
  205. $sWhereClause .= " AND `enabled`=:enabled";
  206. }
  207. $sOrderByClause = " ORDER BY " . (isset($aParams['order_by']) ? $aParams['order_by'] : '`title`');
  208. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  209. FROM `sys_modules`
  210. WHERE 1 " . $sWhereClause . $sOrderByClause;
  211. $aMethod['params'][] = $aBindings;
  212. if(!$bFromCache || empty($sPostfix))
  213. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  214. return call_user_func_array(array($this, 'fromMemory'), array_merge(array('sys_modules' . $sPostfix, $aMethod['name']), $aMethod['params']));
  215. }
  216. function isModuleContent($sName)
  217. {
  218. return (int)$this->getModulesBy(['type' => 'is_module_subtype', 'subtype' => BX_DOL_MODULE_SUBTYPE_TEXT, 'name' => $sName]) != 0;
  219. }
  220. function isModuleContext($sName)
  221. {
  222. return (int)$this->getModulesBy(['type' => 'is_module_subtype', 'subtype' => BX_DOL_MODULE_SUBTYPE_CONTEXT, 'name' => $sName]) != 0;
  223. }
  224. function isModuleProfile($sName)
  225. {
  226. return (int)$this->getModulesBy(['type' => 'is_module_subtype', 'subtype' => BX_DOL_MODULE_SUBTYPE_PROFILE, 'name' => $sName]) != 0;
  227. }
  228. function getModulesUri()
  229. {
  230. return $this->fromMemory('sys_modules_uri', 'getColumn', 'SELECT `uri` FROM `sys_modules` ORDER BY `uri`');
  231. }
  232. function getDependent($sName, $sUri)
  233. {
  234. $aResults = array();
  235. $aModules = $this->getAll("SELECT `id`, `title`, `dependencies`, `enabled` FROM `sys_modules` WHERE (`dependencies` LIKE " . $this->escape('%' . $sName . '%') . " OR `dependencies` LIKE " . $this->escape('%' . $sUri . '%') . ") AND `enabled`='1'");
  236. foreach($aModules as $aModule) {
  237. $aDependencies = explode(',', $aModule['dependencies']);
  238. if(in_array($sName, $aDependencies) || in_array($sUri, $aDependencies))
  239. $aResults[] = $aModule;
  240. }
  241. return $aResults;
  242. }
  243. public function updateModule($aParamsSet, $aParamsWhere = array())
  244. {
  245. if(empty($aParamsSet))
  246. return false;
  247. $sWhereClause = !empty($aParamsWhere) ? $this->arrayToSQL($aParamsWhere, " AND ") : "1";
  248. $sSql = "UPDATE `sys_modules` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause;
  249. $mixedRet = $this->query($sSql);
  250. if ($mixedRet)
  251. $this->cleanCache('sys_modules_list');
  252. return $mixedRet;
  253. }
  254. public function checkModulesSubtypes()
  255. {
  256. return (int)$this->getOne("SELECT COUNT(`id`) FROM `sys_modules` WHERE `type` = :type AND `subtypes` <> 0", [
  257. 'type' => BX_DOL_MODULE_TYPE_MODULE
  258. ]) > 0;
  259. }
  260. }
  261. /** @} */