BxBaseModGeneralDb.php 12 KB

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