BxBaseModGeneralDb.php 15 KB

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