BxBaseModGeneralDb.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 BaseGeneral Base classes for modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module database queries
  13. */
  14. class BxBaseModGeneralDb extends BxDolModuleDb
  15. {
  16. protected $_oConfig;
  17. public function __construct(&$oConfig)
  18. {
  19. parent::__construct($oConfig);
  20. $this->_oConfig = $oConfig;
  21. }
  22. public function getContentInfoById ($iContentId)
  23. {
  24. $CNF = &$this->_oConfig->CNF;
  25. if(empty($CNF['TABLE_ENTRIES']) || empty($CNF['FIELD_ID']))
  26. return array();
  27. $sQuery = $this->prepare ("SELECT * FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `" . $CNF['FIELD_ID'] . "` = ?", $iContentId);
  28. return $this->getRow($sQuery);
  29. }
  30. function getEntriesBy($aParams = array())
  31. {
  32. $CNF = &$this->_oConfig->CNF;
  33. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  34. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  35. $sSelectClause = "`" . $CNF['TABLE_ENTRIES'] . "`.*";
  36. switch($aParams['type']) {
  37. case 'id':
  38. $aMethod['name'] = 'getRow';
  39. $aMethod['params'][1]['id'] = (int)$aParams['id'];
  40. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "` = :id";
  41. break;
  42. case 'author':
  43. $aMethod['params'][1]['author'] = (int)$aParams['author'];
  44. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_AUTHOR'] . "` = :author";
  45. break;
  46. case 'conditions':
  47. if(empty($aParams['conditions']))
  48. break;
  49. if(isset($aParams['count']) && $aParams['count'] === true) {
  50. $aMethod['name'] = 'getOne';
  51. $sSelectClause = 'COUNT(`' . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . '`)';
  52. }
  53. if(is_array($aParams['conditions']))
  54. $sWhereClause .= " AND " . $this->arrayToSQL($aParams['conditions'], ' AND ');
  55. else if(is_string($aParams['conditions']) && !empty($aParams['bindings']) && is_array($aParams['bindings'])) {
  56. $sWhereClause .= $aParams['conditions'];
  57. if(!is_array($aMethod['params'][1]))
  58. $aMethod['params'][1] = array();
  59. $aMethod['params'][1] = array_merge($aMethod['params'][1], $aParams['bindings']);
  60. }
  61. break;
  62. case 'search_ids':
  63. $this->_getEntriesBySearchIds($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  64. bx_alert('system', 'search_ids', 0, 0, array('module_name' => $this->_oConfig->getName(), 'params' => &$aParams, 'select_clause' => &$sSelectClause, 'join_clause' => &$sJoinClause, 'where_clause' => &$sWhereClause, 'order_clause' => &$sOrderClause, 'limit_clause' => &$sLimitClause));
  65. break;
  66. case 'all_ids':
  67. $aMethod['name'] = 'getColumn';
  68. $sSelectClause = "`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "`";
  69. $sOrderClause .= "`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ADDED'] . "` ASC";
  70. break;
  71. case 'all':
  72. $sOrderClause .= "`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ADDED'] . "` ASC";
  73. break;
  74. }
  75. if(!empty($sOrderClause))
  76. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  77. if(!empty($sLimitClause))
  78. $sLimitClause = 'LIMIT ' . $sLimitClause;
  79. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_ENTRIES'] . "` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  80. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  81. }
  82. public function getEntriesNumByContext ($iProfileId)
  83. {
  84. $sQuery = $this->prepare ("SELECT COUNT(*) FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO'] . "` = ?", - $iProfileId);
  85. return $this->getOne($sQuery);
  86. }
  87. public function getEntriesByAuthor ($iProfileId)
  88. {
  89. $sQuery = $this->prepare ("SELECT * FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_AUTHOR'] . "` = ? ORDER BY `" . $this->_oConfig->CNF['FIELD_ADDED'] . "` DESC", $iProfileId);
  90. return $this->getAll($sQuery);
  91. }
  92. public function getEntriesNumByAuthor ($iProfileId)
  93. {
  94. $sQuery = $this->prepare ("SELECT COUNT(*) FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_AUTHOR'] . "` = ?", $iProfileId);
  95. return $this->getOne($sQuery);
  96. }
  97. public function updateEntriesBy($aParamsSet, $aParamsWhere)
  98. {
  99. $CNF = &$this->_oConfig->CNF;
  100. if(empty($CNF['TABLE_ENTRIES']) || empty($aParamsSet) || empty($aParamsWhere))
  101. return false;
  102. return $this->query("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  103. }
  104. public function updateStatusAdmin($iContentId, $isActive)
  105. {
  106. $CNF = $this->_oConfig->CNF;
  107. if (!isset($CNF['FIELD_STATUS_ADMIN']))
  108. return false;
  109. $sQuery = $this->prepare("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET `" . $CNF['FIELD_STATUS_ADMIN'] . "` = ? WHERE `" . $CNF['FIELD_ID'] . "` = ?", $isActive ? 'active' : 'hidden', $iContentId);
  110. return $this->query($sQuery);
  111. }
  112. public function publishEntries()
  113. {
  114. $CNF = $this->_oConfig->CNF;
  115. $aEntries = $this->getAll("SELECT `id`, `" . $CNF['FIELD_PUBLISHED'] . "`, FROM_UNIXTIME(`" . $CNF['FIELD_PUBLISHED'] . "`) FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `" . $CNF['FIELD_PUBLISHED'] . "` > `" . $CNF['FIELD_ADDED'] . "` AND `" . $CNF['FIELD_STATUS'] . "` = 'awaiting'");
  116. if(empty($aEntries) || !is_array($aEntries))
  117. return false;
  118. $iNow = time();
  119. $aResult = array();
  120. foreach($aEntries as $aEntry)
  121. if($aEntry[$CNF['FIELD_PUBLISHED']] <= $iNow)
  122. $aResult[] = $aEntry[$CNF['FIELD_ID']];
  123. $sSet = "`" . $CNF['FIELD_ADDED'] . "`=`" . $CNF['FIELD_PUBLISHED'] . "`, `" . $CNF['FIELD_STATUS'] . "` = 'active'";
  124. if(isset($CNF['FIELD_CHANGED']))
  125. $sSet .= ", `" . $CNF['FIELD_CHANGED'] . "`=`" . $CNF['FIELD_PUBLISHED'] . "`";
  126. return count($aResult) == (int)$this->query("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET " . $sSet . " WHERE `id` IN (" . $this->implode_escape($aResult) . ")") ? $aResult : false;
  127. }
  128. public function alterFulltextIndex ()
  129. {
  130. $CNF = $this->_oConfig->CNF;
  131. $bFulltextIndex = false;
  132. $aIndexes = $this->getAll("SHOW INDEXES FROM `" . $CNF['TABLE_ENTRIES'] . "`");
  133. foreach ($aIndexes as $r) {
  134. if ($CNF['TABLE_ENTRIES_FULLTEXT'] == $r['Key_name']) {
  135. $bFulltextIndex = true;
  136. break;
  137. }
  138. }
  139. if ($bFulltextIndex)
  140. $this->query("ALTER TABLE `" . $CNF['TABLE_ENTRIES'] . "` DROP INDEX `" . $CNF['TABLE_ENTRIES_FULLTEXT'] . "`");
  141. $sFields = getParam($CNF['PARAM_SEARCHABLE_FIELDS']);
  142. if (!$sFields || !($aFields = explode(',', $sFields)))
  143. return true;
  144. $sFields = '';
  145. foreach ($aFields as $s)
  146. $sFields .= "`$s`,";
  147. return $this->query("ALTER TABLE `" . $CNF['TABLE_ENTRIES'] . "` ADD FULLTEXT `" . $CNF['TABLE_ENTRIES_FULLTEXT'] . "` (" . trim($sFields, ', ') . ")");
  148. }
  149. public function deleteNestedById ($iNestedId, $sTableKey, $sTableName)
  150. {
  151. return $this->query("DELETE FROM `" . $sTableName . "` WHERE `" . $sTableKey . "` = :content_id", array('content_id' => $iNestedId));
  152. }
  153. function getNestedBy($aParams, $sTableName)
  154. {
  155. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  156. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  157. $sSelectClause = "`" . $sTableName . "`.*";
  158. switch($aParams['type']) {
  159. case 'id':
  160. $aMethod['name'] = 'getRow';
  161. $aMethod['params'][1]['id'] = (int)$aParams['id'];
  162. $sWhereClause .= " AND `" . $sTableName . "`.`" . $aParams['key_name'] . "` = :id";
  163. break;
  164. case 'content_id':
  165. $aMethod['name'] = 'getAllWithKey';
  166. $aMethod['params'][2]['id'] = (int)$aParams['id'];
  167. $aMethod['params'][1] = $aParams['key_name'];
  168. $sWhereClause .= " AND `" . $sTableName . "`.`content_id` = :id";
  169. break;
  170. }
  171. if(!empty($sOrderClause))
  172. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  173. if(!empty($sLimitClause))
  174. $sLimitClause = 'LIMIT ' . $sLimitClause;
  175. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $sTableName . "` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  176. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  177. }
  178. protected function _getEntriesBySearchIds($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  179. {
  180. $CNF = &$this->_oConfig->CNF;
  181. $aMethod['name'] = 'getColumn';
  182. $sSelectClause = " DISTINCT `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "`";
  183. if (!empty($aParams['start']) && !empty($aParams['per_page']))
  184. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  185. elseif (!empty($aParams['per_page']))
  186. $sLimitClause = $this->prepareAsString("?", $aParams['per_page']);
  187. $sWhereConditions = "1";
  188. foreach($aParams['search_params'] as $sSearchParam => $aSearchParam) {
  189. if(empty($aSearchParam['operator']))
  190. continue;
  191. $sSearchValue = "";
  192. switch ($aSearchParam['operator']) {
  193. case 'like':
  194. if(is_array($aSearchParam['value'])) {
  195. $sSubCondition = "0";
  196. foreach($aSearchParam['value'] as $sValue)
  197. if(!empty($sValue))
  198. $sSubCondition .= " OR `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "` LIKE " . $this->_getEbsiLike($sValue);
  199. if($sSubCondition != "0")
  200. $sWhereConditions .= " AND (" . $sSubCondition . ")";
  201. }
  202. else
  203. $sSearchValue = " LIKE " . $this->_getEbsiLike($aSearchParam['value']);
  204. break;
  205. case 'in':
  206. $sSearchValue = " IN (" . $this->implode_escape($aSearchParam['value']) . ")";
  207. break;
  208. case 'not in':
  209. $sSearchValue = " NOT IN (" . $this->implode_escape($aSearchParam['value']) . ")";
  210. break;
  211. case 'and':
  212. $iResult = 0;
  213. if (is_array($aSearchParam['value']))
  214. foreach ($aSearchParam['value'] as $iValue)
  215. $iResult |= pow (2, $iValue - 1);
  216. else
  217. $iResult = (int)$aSearchParam['value'];
  218. $sSearchValue = " & " . $iResult . "";
  219. break;
  220. case 'locate':
  221. if(!isset($CNF['OBJECT_METATAGS']))
  222. break;
  223. if ($aSearchParam['type'] == 'location_radius'){
  224. if ($aSearchParam['value']['string'] != ''){
  225. list($fLatitude, $fLongitude, $sCountry, $sState, $sCity, $sZip, $sStreet, $sStreetNumber, $iRadius) = $aSearchParam['value']['array'];
  226. $aBounds = bx_get_location_bounds_latlng($fLatitude, $fLongitude, $iRadius);
  227. $aSql = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])->locationsGetAsSQLPart($CNF['TABLE_ENTRIES'], $CNF['FIELD_ID'], '', '', '', '', $aBounds);
  228. }
  229. }
  230. else{
  231. list($fLatitude, $fLongitude, $sCountry, $sState, $sCity, $sZip) = $aSearchParam['value']['array'];
  232. $aSql = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])->locationsGetAsSQLPart($CNF['TABLE_ENTRIES'], $CNF['FIELD_ID'], $sCountry, $sState, $sCity, $sZip);
  233. }
  234. if(!empty($aSql['where'])) {
  235. $sWhereConditions .= $aSql['where'];
  236. if(!empty($aSql['join']))
  237. $sJoinClause .= $aSql['join'];
  238. }
  239. break;
  240. case 'between':
  241. if(!is_array($aSearchParam['value']) || count($aSearchParam['value']) != 2)
  242. break;
  243. list($mixedMin, $mixedMax) = $aSearchParam['value'];
  244. if(!empty($mixedMin)) {
  245. $sWhereConditions .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "` >= :" . $sSearchParam . "_from";
  246. $aMethod['params'][1][$sSearchParam . "_from"] = $mixedMin;
  247. }
  248. if(!empty($mixedMax)) {
  249. $sWhereConditions .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "` <= :" . $sSearchParam . "_to";
  250. $aMethod['params'][1][$sSearchParam . "_to"] = $mixedMax;
  251. }
  252. break;
  253. default:
  254. $sSearchValue = " " . $aSearchParam['operator'] . " :" . $sSearchParam;
  255. $aMethod['params'][1][$sSearchParam] = $aSearchParam['value'];
  256. }
  257. if(!empty($sSearchValue))
  258. $sWhereConditions .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "`" . $sSearchValue;
  259. }
  260. $sWhereClause .= " AND (" . $sWhereConditions . ")";
  261. $this->_getEntriesBySearchIdsOrder($aParams, $sOrderClause);
  262. }
  263. protected function _getEntriesBySearchIdsOrder($aParams, &$sOrderClause)
  264. {
  265. $CNF = &$this->_oConfig->CNF;
  266. if(empty($aParams['search_params']['order']) || !is_array($aParams['search_params']['order']))
  267. $aParams['search_params']['order'] = array(
  268. array('table' => $CNF['TABLE_ENTRIES'], 'field' => $CNF['FIELD_ADDED'], 'direction' => 'DESC')
  269. );
  270. $aOrders = array();
  271. foreach($aParams['search_params']['order'] as $aOrder)
  272. $aOrders[] = "`" . (isset($aOrder['table']) ? $aOrder['table'] : $CNF['TABLE_ENTRIES']) . "`.`" . (!empty($aOrder['field']) ? $aOrder['field'] : $CNF['FIELD_ADDED']) . "` " . (!empty($aOrder['direction']) ? strtoupper($aOrder['direction']) : 'DESC');
  273. $sOrderClause .= implode(', ', $aOrders);
  274. }
  275. protected function _getEbsiLike($sValue)
  276. {
  277. return $this->escape("%" . preg_replace('/\s+/', '%', $sValue) . "%");
  278. }
  279. }
  280. /** @} */