BxBaseModGeneralDb.php 16 KB

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