BxDolModuleQuery.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolDb');
  10. class BxDolModuleQuery extends BxDolDb implements iBxDolSingleton
  11. {
  12. function __construct()
  13. {
  14. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  15. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  16. parent::__construct();
  17. }
  18. /**
  19. * Prevent cloning the instance
  20. */
  21. public function __clone()
  22. {
  23. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  24. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  25. }
  26. /**
  27. * Get singleton instance of the class
  28. */
  29. public static function getInstance()
  30. {
  31. $sClass = __CLASS__;
  32. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  33. $GLOBALS['bxDolClasses'][__CLASS__] = new $sClass();
  34. return $GLOBALS['bxDolClasses'][__CLASS__];
  35. }
  36. function getModuleById($iId)
  37. {
  38. $sSql = $this->prepare("SELECT * FROM `sys_modules` WHERE `id`=? LIMIT 1", $iId);
  39. return $this->fromMemory('sys_modules_' . $iId, 'getRow', $sSql);
  40. }
  41. function getModuleByName($sName)
  42. {
  43. $sSql = $this->prepare("SELECT * FROM `sys_modules` WHERE `name`=? LIMIT 1", $sName);
  44. return $this->fromMemory('sys_modules_' . $sName, 'getRow', $sSql);
  45. }
  46. function getModuleByUri($sUri)
  47. {
  48. $sSql = $this->prepare("SELECT * FROM `sys_modules` WHERE `uri`=? LIMIT 1", $sUri);
  49. return $this->fromMemory('sys_modules_' . $sUri, 'getRow', $sSql);
  50. }
  51. function enableModuleByUri($sUri)
  52. {
  53. $sSql = $this->prepare("UPDATE `sys_modules` SET `enabled`='1' WHERE `uri`=? LIMIT 1", $sUri);
  54. return (int)$this->query($sSql) > 0;
  55. }
  56. function disableModuleByUri($sUri)
  57. {
  58. $sSql = $this->prepare("UPDATE `sys_modules` SET `enabled`='0' WHERE `uri`=? LIMIT 1", $sUri);
  59. return (int)$this->query($sSql) > 0;
  60. }
  61. function setModulePendingUninstall($sUri, $bPendingUninstall)
  62. {
  63. $sSql = $this->prepare("UPDATE `sys_modules` SET `pending_uninstall` = ? WHERE `uri` = ? LIMIT 1", $bPendingUninstall ? 1 : 0, $sUri);
  64. return $this->query($sSql);
  65. }
  66. function isModule($sUri)
  67. {
  68. $sSql = $this->prepare("SELECT `id` FROM `sys_modules` WHERE `uri`=? LIMIT 1", $sUri);
  69. return (int)$this->getOne($sSql) > 0;
  70. }
  71. function isModuleByName($sName)
  72. {
  73. $sSql = $this->prepare("SELECT `id` FROM `sys_modules` WHERE `name`=? LIMIT 1", $sName);
  74. return (int)$this->getOne($sSql) > 0;
  75. }
  76. function isModuleParamsUsed($sUri, $sPath, $sPrefixDb, $sPrefixClass)
  77. {
  78. $sSql = "SELECT `id` FROM `sys_modules` WHERE `uri`='" . $sUri . "' || `path`='" . $sPath . "' || `db_prefix`='" . $sPrefixDb . "' || `class_prefix`='" . $sPrefixClass . "' LIMIT 1";
  79. return (int)$this->getOne($sSql) > 0;
  80. }
  81. function isEnabled($sUri)
  82. {
  83. $sSql = $this->prepare("SELECT `id` FROM `sys_modules` WHERE `uri`=? AND `enabled`='1' LIMIT 1", $sUri);
  84. return (int)$this->getOne($sSql) > 0;
  85. }
  86. function isEnabledByName($sName)
  87. {
  88. $sSql = $this->prepare("SELECT `id` FROM `sys_modules` WHERE `name`=? AND `enabled`='1' LIMIT 1", $sName);
  89. return (int)$this->getOne($sSql) > 0;
  90. }
  91. function getModules()
  92. {
  93. $sSql = "SELECT * FROM `sys_modules` ORDER BY `title`";
  94. return $this->fromMemory('sys_modules', 'getAll', $sSql);
  95. }
  96. function getModulesBy($aParams = array())
  97. {
  98. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  99. $sPostfix = $sWhereClause = $sOrderByClause = "";
  100. switch($aParams['type']) {
  101. case 'modules':
  102. $sPostfix .= '_modules';
  103. $sWhereClause .= $this->prepare(" AND `type`=?", BX_DOL_MODULE_TYPE_MODULE);
  104. break;
  105. case 'languages':
  106. $sPostfix .= '_languages';
  107. $sWhereClause .= $this->prepare(" AND `type`=?", BX_DOL_MODULE_TYPE_LANGUAGE);
  108. break;
  109. case 'templates':
  110. $sPostfix .= '_templates';
  111. $sWhereClause .= $this->prepare(" AND `type`=?", BX_DOL_MODULE_TYPE_TEMPLATE);
  112. break;
  113. case 'path_and_uri':
  114. $aMethod['name'] = 'getRow';
  115. $sWhereClause .= $this->prepare(" AND `path`=? AND `uri`=?", $aParams['path'], $aParams['uri']);
  116. break;
  117. }
  118. if(isset($aParams['active'])) {
  119. $sPostfix .= "_active";
  120. $sWhereClause .= $this->prepare(" AND `enabled`=?", (int)$aParams['active']);
  121. }
  122. $sOrderByClause = " ORDER BY " . (isset($aParams['order_by']) ? $aParams['order_by'] : '`title`');
  123. $aMethod['params'][0] = "SELECT
  124. `id`,
  125. `type`,
  126. `name`,
  127. `title`,
  128. `vendor`,
  129. `version`,
  130. `help_url`,
  131. `path`,
  132. `uri`,
  133. `class_prefix`,
  134. `db_prefix`,
  135. `lang_category`,
  136. `date`,
  137. `enabled`
  138. FROM `sys_modules`
  139. WHERE 1 " . $sWhereClause . $sOrderByClause;
  140. if(empty($sPostfix))
  141. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  142. return call_user_func_array(array($this, 'fromMemory'), array_merge(array('sys_modules' . $sPostfix, $aMethod['name']), $aMethod['params']));
  143. }
  144. function getModulesUri()
  145. {
  146. $sSql = "SELECT `uri` FROM `sys_modules` ORDER BY `uri`";
  147. return $this->fromMemory('sys_modules_uri', 'getColumn', $sSql);
  148. }
  149. function getDependent($sUri)
  150. {
  151. $sSql = "SELECT `id`, `title` FROM `sys_modules` WHERE `dependencies` LIKE '%" . $this->escape($sUri) . "%'";
  152. return $this->getAll($sSql);
  153. }
  154. public function updateModule($aParamsSet, $aParamsWhere = array())
  155. {
  156. if(empty($aParamsSet))
  157. return false;
  158. $sWhereClause = !empty($aParamsWhere) ? $this->arrayToSQL($aParamsWhere, " AND ") : "1";
  159. $sSql = "UPDATE `sys_modules` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $sWhereClause;
  160. return $this->query($sSql);
  161. }
  162. }
  163. /** @} */