1
0

BxDolModuleQuery.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. $sSql = $this->prepare("SELECT * FROM `sys_modules` WHERE `name`=? LIMIT 1", $sName);
  43. return $bFromCache ? $this->fromMemory('sys_modules_' . $sName, 'getRow', $sSql) : $this->getRow($sSql);
  44. }
  45. function getModuleByUri($sUri, $bFromCache = true)
  46. {
  47. $sSql = $this->prepare("SELECT * FROM `sys_modules` WHERE `uri`=? LIMIT 1", $sUri);
  48. return $bFromCache ? $this->fromMemory('sys_modules_' . $sUri, 'getRow', $sSql) : $this->getRow($sSql);
  49. }
  50. function enableModuleByUri($sUri)
  51. {
  52. $sSql = $this->prepare("UPDATE `sys_modules` SET `enabled`='1' WHERE `uri`=? LIMIT 1", $sUri);
  53. return (int)$this->query($sSql) > 0;
  54. }
  55. function disableModuleByUri($sUri)
  56. {
  57. $sSql = $this->prepare("UPDATE `sys_modules` SET `enabled`='0' WHERE `uri`=? LIMIT 1", $sUri);
  58. return (int)$this->query($sSql) > 0;
  59. }
  60. function setModulePendingUninstall($sUri, $bPendingUninstall)
  61. {
  62. $sSql = $this->prepare("UPDATE `sys_modules` SET `pending_uninstall` = ? WHERE `uri` = ? LIMIT 1", $bPendingUninstall ? 1 : 0, $sUri);
  63. return $this->query($sSql);
  64. }
  65. function isModule($sUri)
  66. {
  67. $sSql = $this->prepare("SELECT `id` FROM `sys_modules` WHERE `uri`=? LIMIT 1", $sUri);
  68. return (int)$this->fromMemory('sys_module_' . $sUri, 'getOne', $sSql) > 0;
  69. }
  70. function isModuleByName($sName)
  71. {
  72. $sSql = $this->prepare("SELECT `id` FROM `sys_modules` WHERE `name`=? LIMIT 1", $sName);
  73. return (int)$this->fromMemory('sys_module_' . $sName, 'getOne', $sSql) > 0;
  74. }
  75. function isModuleParamsUsed($sName, $sUri, $sPath, $sPrefixDb, $sPrefixClass)
  76. {
  77. $sSql = "SELECT `id` FROM `sys_modules` WHERE `name`=:name || `uri`=:uri || `path`=:path || `db_prefix`=:db_prefix || `class_prefix`=:class_prefix LIMIT 1";
  78. return (int)$this->getOne($sSql, [
  79. 'name' => $sName,
  80. 'uri' => $sUri,
  81. 'path' => $sPath,
  82. 'db_prefix' => $sPrefixDb,
  83. 'class_prefix' => $sPrefixClass
  84. ]) > 0;
  85. }
  86. function isEnabled($sUri)
  87. {
  88. $sSql = $this->prepare("SELECT `id` FROM `sys_modules` WHERE `uri`=? AND `enabled`='1' LIMIT 1", $sUri);
  89. return (int)$this->fromMemory('sys_module_enabled_' . $sUri, 'getOne', $sSql) > 0;
  90. }
  91. function isEnabledByName($sName)
  92. {
  93. $sSql = $this->prepare("SELECT `id` FROM `sys_modules` WHERE `name`=? AND `enabled`='1' LIMIT 1", $sName);
  94. return (int)$this->fromMemory('sys_module_enabled_' . $sName, 'getOne', $sSql) > 0;
  95. }
  96. function getModules()
  97. {
  98. $sSql = "SELECT * FROM `sys_modules` ORDER BY `title`";
  99. return $this->fromMemory('sys_modules', 'getAll', $sSql);
  100. }
  101. function getModulesBy($aParams = array(), $bFromCache = true)
  102. {
  103. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  104. $sPostfix = $sWhereClause = $sOrderByClause = "";
  105. $sSelectClause = "`id`, `type`, `name`, `title`, `vendor`, `version`, `help_url`, `path`, `uri`, `class_prefix`, `db_prefix`, `lang_category`, `date`, `enabled`";
  106. $aBindings = [];
  107. switch($aParams['type']) {
  108. case 'type':
  109. if(!is_array($aParams['value']))
  110. $aParams['value'] = array($aParams['value']);
  111. $sPostfix .= '_type_' . implode('_', $aParams['value']);
  112. $sWhereClause .= " AND `type` IN (" . $this->implode_escape($aParams['value']) . ")";
  113. break;
  114. case 'modules':
  115. $sPostfix .= '_modules';
  116. $aBindings['type'] = BX_DOL_MODULE_TYPE_MODULE;
  117. $sWhereClause .= " AND `type`=:type";
  118. break;
  119. case 'modules_subtypes':
  120. $sPostfix .= '_modules_subtypes_';
  121. if(isset($aParams['id_as_key']) && $aParams['id_as_key'] === true) {
  122. $sPostfix .= 'id_as_key_';
  123. $aMethod['name'] = 'getAllWithKey';
  124. $aMethod['params'][1] = 'id';
  125. }
  126. if(isset($aParams['name_as_key']) && $aParams['name_as_key'] === true) {
  127. $sPostfix .= 'name_as_key_';
  128. $aMethod['name'] = 'getAllWithKey';
  129. $aMethod['params'][1] = 'name';
  130. }
  131. if(!is_array($aParams['value']))
  132. $aParams['value'] = [$aParams['value']];
  133. $sPostfix .= implode('_', $aParams['value']);
  134. $aBindings['type'] = BX_DOL_MODULE_TYPE_MODULE;
  135. $iSubtypes = 0;
  136. foreach($aParams['value'] as $iSubtype)
  137. $iSubtypes |= pow(2, $iSubtype);
  138. $aBindings['subtypes'] = $iSubtypes;
  139. $sWhereClause .= " AND `type`=:type AND `subtypes` & :subtypes";
  140. break;
  141. case 'is_module_subtype':
  142. $sPostfix .= '_is_module_' . $aParams['subtype'] . '_' . $aParams['name'];
  143. $aMethod['name'] = 'getOne';
  144. $aBindings = [
  145. 'type' => BX_DOL_MODULE_TYPE_MODULE,
  146. 'subtypes' => pow(2, (int)$aParams['subtype']),
  147. 'name' => $aParams['name']
  148. ];
  149. $sSelectClause = "`id`";
  150. $sWhereClause .= " AND `type`=:type AND `subtypes` & :subtypes AND `name`=:name";
  151. break;
  152. case 'languages':
  153. $sPostfix .= '_languages';
  154. $aBindings['type'] = BX_DOL_MODULE_TYPE_LANGUAGE;
  155. $sWhereClause .= " AND `type`=:type";
  156. break;
  157. case 'templates':
  158. $sPostfix .= '_templates';
  159. $aBindings['type'] = BX_DOL_MODULE_TYPE_TEMPLATE;
  160. $sWhereClause .= " AND `type`=:type";
  161. break;
  162. case 'path_and_uri':
  163. $sPostfix .= '_path_and_uri_' . $aParams['path'] . '_' . $aParams['uri'];
  164. $aMethod['name'] = 'getRow';
  165. $aBindings = array_merge($aBindings, array(
  166. 'path' => $aParams['path'],
  167. 'uri' => $aParams['uri']
  168. ));
  169. $sWhereClause .= " AND `path`=:path AND `uri`=:uri";
  170. break;
  171. case 'all_pairs_name_uri':
  172. $sPostfix .= 'all_pairs_name_uri';
  173. $aMethod['name'] = 'getPairs';
  174. $aMethod['params'][1] = 'name';
  175. $aMethod['params'][2] = 'uri';
  176. break;
  177. case 'all_key_name':
  178. $sPostfix .= 'all_key_name';
  179. $aMethod['name'] = 'getAllWithKey';
  180. $aMethod['params'][1] = 'name';
  181. break;
  182. case 'all':
  183. break;
  184. }
  185. if(isset($aParams['active'])) {
  186. $sPostfix .= "_active";
  187. $aBindings['enabled'] = (int)$aParams['active'];
  188. $sWhereClause .= " AND `enabled`=:enabled";
  189. }
  190. $sOrderByClause = " ORDER BY " . (isset($aParams['order_by']) ? $aParams['order_by'] : '`title`');
  191. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  192. FROM `sys_modules`
  193. WHERE 1 " . $sWhereClause . $sOrderByClause;
  194. $aMethod['params'][] = $aBindings;
  195. if(!$bFromCache || empty($sPostfix))
  196. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  197. return call_user_func_array(array($this, 'fromMemory'), array_merge(array('sys_modules' . $sPostfix, $aMethod['name']), $aMethod['params']));
  198. }
  199. function isModuleContent($sName)
  200. {
  201. return (int)$this->getModulesBy(['type' => 'is_module_subtype', 'subtype' => BX_DOL_MODULE_SUBTYPE_TEXT, 'name' => $sName]) != 0;
  202. }
  203. function isModuleContext($sName)
  204. {
  205. return (int)$this->getModulesBy(['type' => 'is_module_subtype', 'subtype' => BX_DOL_MODULE_SUBTYPE_CONTEXT, 'name' => $sName]) != 0;
  206. }
  207. function isModuleProfile($sName)
  208. {
  209. return (int)$this->getModulesBy(['type' => 'is_module_subtype', 'subtype' => BX_DOL_MODULE_SUBTYPE_PROFILE, 'name' => $sName]) != 0;
  210. }
  211. function getModulesUri()
  212. {
  213. return $this->fromMemory('sys_modules_uri', 'getColumn', 'SELECT `uri` FROM `sys_modules` ORDER BY `uri`');
  214. }
  215. function getDependent($sName, $sUri)
  216. {
  217. $aResults = array();
  218. $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'");
  219. foreach($aModules as $aModule) {
  220. $aDependencies = explode(',', $aModule['dependencies']);
  221. if(in_array($sName, $aDependencies) || in_array($sUri, $aDependencies))
  222. $aResults[] = $aModule;
  223. }
  224. return $aResults;
  225. }
  226. public function updateModule($aParamsSet, $aParamsWhere = array())
  227. {
  228. if(empty($aParamsSet))
  229. return false;
  230. $sWhereClause = !empty($aParamsWhere) ? $this->arrayToSQL($aParamsWhere, " AND ") : "1";
  231. $sSql = "UPDATE `sys_modules` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause;
  232. return $this->query($sSql);
  233. }
  234. public function checkModulesSubtypes()
  235. {
  236. return (int)$this->getOne("SELECT COUNT(`id`) FROM `sys_modules` WHERE `type` = :type AND `subtypes` <> 0", [
  237. 'type' => BX_DOL_MODULE_TYPE_MODULE
  238. ]) > 0;
  239. }
  240. }
  241. /** @} */