BxDolModuleQuery.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. class BxDolModuleQuery extends BxDolDb implements iBxDolSingleton
  10. {
  11. 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)
  36. {
  37. $sSql = $this->prepare("SELECT * FROM `sys_modules` WHERE `id`=? LIMIT 1", $iId);
  38. return $this->fromMemory('sys_modules_' . $iId, 'getRow', $sSql);
  39. }
  40. function getModuleByName($sName)
  41. {
  42. $sSql = $this->prepare("SELECT * FROM `sys_modules` WHERE `name`=? LIMIT 1", $sName);
  43. return $this->fromMemory('sys_modules_' . $sName, 'getRow', $sSql);
  44. }
  45. function getModuleByUri($sUri)
  46. {
  47. $sSql = $this->prepare("SELECT * FROM `sys_modules` WHERE `uri`=? LIMIT 1", $sUri);
  48. return $this->fromMemory('sys_modules_' . $sUri, '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->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->getOne($sSql) > 0;
  74. }
  75. function isModuleParamsUsed($sUri, $sPath, $sPrefixDb, $sPrefixClass)
  76. {
  77. $sSql = "SELECT `id` FROM `sys_modules` WHERE `uri`='" . $sUri . "' || `path`='" . $sPath . "' || `db_prefix`='" . $sPrefixDb . "' || `class_prefix`='" . $sPrefixClass . "' LIMIT 1";
  78. return (int)$this->getOne($sSql) > 0;
  79. }
  80. function isEnabled($sUri)
  81. {
  82. $sSql = $this->prepare("SELECT `id` FROM `sys_modules` WHERE `uri`=? AND `enabled`='1' LIMIT 1", $sUri);
  83. return (int)$this->getOne($sSql) > 0;
  84. }
  85. function isEnabledByName($sName)
  86. {
  87. $sSql = $this->prepare("SELECT `id` FROM `sys_modules` WHERE `name`=? AND `enabled`='1' LIMIT 1", $sName);
  88. return (int)$this->getOne($sSql) > 0;
  89. }
  90. function getModules()
  91. {
  92. $sSql = "SELECT * FROM `sys_modules` ORDER BY `title`";
  93. return $this->fromMemory('sys_modules', 'getAll', $sSql);
  94. }
  95. function getModulesBy($aParams = array())
  96. {
  97. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  98. $sPostfix = $sWhereClause = $sOrderByClause = "";
  99. switch($aParams['type']) {
  100. case 'modules':
  101. $sPostfix .= '_modules';
  102. $sWhereClause .= $this->prepare(" AND `type`=?", BX_DOL_MODULE_TYPE_MODULE);
  103. break;
  104. case 'languages':
  105. $sPostfix .= '_languages';
  106. $sWhereClause .= $this->prepare(" AND `type`=?", BX_DOL_MODULE_TYPE_LANGUAGE);
  107. break;
  108. case 'templates':
  109. $sPostfix .= '_templates';
  110. $sWhereClause .= $this->prepare(" AND `type`=?", BX_DOL_MODULE_TYPE_TEMPLATE);
  111. break;
  112. case 'path_and_uri':
  113. $aMethod['name'] = 'getRow';
  114. $sWhereClause .= $this->prepare(" AND `path`=? AND `uri`=?", $aParams['path'], $aParams['uri']);
  115. break;
  116. }
  117. if(isset($aParams['active'])) {
  118. $sPostfix .= "_active";
  119. $sWhereClause .= $this->prepare(" AND `enabled`=?", (int)$aParams['active']);
  120. }
  121. $sOrderByClause = " ORDER BY " . (isset($aParams['order_by']) ? $aParams['order_by'] : '`title`');
  122. $aMethod['params'][0] = "SELECT
  123. `id`,
  124. `type`,
  125. `name`,
  126. `title`,
  127. `vendor`,
  128. `version`,
  129. `help_url`,
  130. `path`,
  131. `uri`,
  132. `class_prefix`,
  133. `db_prefix`,
  134. `lang_category`,
  135. `date`,
  136. `enabled`
  137. FROM `sys_modules`
  138. WHERE 1 " . $sWhereClause . $sOrderByClause;
  139. if(empty($sPostfix))
  140. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  141. return call_user_func_array(array($this, 'fromMemory'), array_merge(array('sys_modules' . $sPostfix, $aMethod['name']), $aMethod['params']));
  142. }
  143. function getModulesUri()
  144. {
  145. $sSql = "SELECT `uri` FROM `sys_modules` ORDER BY `uri`";
  146. return $this->fromMemory('sys_modules_uri', 'getColumn', $sSql);
  147. }
  148. function getDependent($sUri)
  149. {
  150. $sSql = "SELECT `id`, `title` FROM `sys_modules` WHERE `dependencies` LIKE '%" . $this->escape($sUri) . "%'";
  151. return $this->getAll($sSql);
  152. }
  153. public function updateModule($aParamsSet, $aParamsWhere = array())
  154. {
  155. if(empty($aParamsSet))
  156. return false;
  157. $sWhereClause = !empty($aParamsWhere) ? $this->arrayToSQL($aParamsWhere, " AND ") : "1";
  158. $sSql = "UPDATE `sys_modules` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause;
  159. return $this->query($sSql);
  160. }
  161. }
  162. /** @} */