BxBaseModGeneralDb.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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, $bForceGet = false)
  23. {
  24. $CNF = &$this->_oConfig->CNF;
  25. if(empty($CNF['TABLE_ENTRIES']) || empty($CNF['FIELD_ID']))
  26. return [];
  27. return $this->getRow("SELECT * FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `" . $CNF['FIELD_ID'] . "` = :id", [
  28. 'id' => $iContentId
  29. ]);
  30. }
  31. function getEntriesBy($aParams = array())
  32. {
  33. $CNF = &$this->_oConfig->CNF;
  34. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  35. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  36. $sSelectClause = "`" . $CNF['TABLE_ENTRIES'] . "`.*";
  37. switch($aParams['type']) {
  38. case 'id':
  39. $aMethod['name'] = 'getRow';
  40. $aMethod['params'][1]['id'] = (int)$aParams['id'];
  41. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "` = :id";
  42. break;
  43. case 'author':
  44. $aMethod['params'][1]['author'] = (int)$aParams['author'];
  45. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_AUTHOR'] . "` = :author";
  46. break;
  47. case 'conditions':
  48. if(empty($aParams['conditions']))
  49. break;
  50. if(isset($aParams['count']) && $aParams['count'] === true) {
  51. $aMethod['name'] = 'getOne';
  52. $sSelectClause = 'COUNT(`' . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . '`)';
  53. }
  54. if(is_array($aParams['conditions']))
  55. $sWhereClause .= " AND " . $this->arrayToSQL($aParams['conditions'], ' AND ');
  56. else if(is_string($aParams['conditions']) && !empty($aParams['bindings']) && is_array($aParams['bindings'])) {
  57. $sWhereClause .= $aParams['conditions'];
  58. if(!is_array($aMethod['params'][1]))
  59. $aMethod['params'][1] = array();
  60. $aMethod['params'][1] = array_merge($aMethod['params'][1], $aParams['bindings']);
  61. }
  62. break;
  63. case 'search_ids':
  64. $this->_getEntriesBySearchIds($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  65. /**
  66. * @hooks
  67. * @hookdef hook-system-search_ids 'system', 'search_ids' - hook to modify a list of found content ids
  68. * - $unit_name - equals `system`
  69. * - $action - equals `search_ids`
  70. * - $object_id - not used
  71. * - $sender_id - not used
  72. * - $extra_params - array of additional params with the following array keys:
  73. * - `module_name` - [string] content's module name
  74. * - `params` - [array] by ref, override search params
  75. * - `select_clause` - [string] by ref, 'select' part of SQL query, can be overridden in hook processing
  76. * - `join_clause` - [string] by ref, 'join' part of SQL query, can be overridden in hook processing
  77. * - `where_clause` - [string] by ref, 'where' part of SQL query, can be overridden in hook processing
  78. * - `order_clause` - [string] by ref, 'order' part of SQL query, can be overridden in hook processing
  79. * - `limit_clause` - [string] by ref, 'limit' part of SQL query, can be overridden in hook processing
  80. * - `bindings` - [array] by ref, bindings of SQL query, can be overridden in hook processing
  81. * @hook @ref hook-system-search_ids
  82. */
  83. bx_alert('system', 'search_ids', 0, 0, [
  84. 'module_name' => $this->_oConfig->getName(),
  85. 'params' => &$aParams,
  86. 'select_clause' => &$sSelectClause,
  87. 'join_clause' => &$sJoinClause,
  88. 'where_clause' => &$sWhereClause,
  89. 'order_clause' => &$sOrderClause,
  90. 'limit_clause' => &$sLimitClause,
  91. 'bindings' => &$aMethod['params'][1],
  92. 'params_ref' => &$aParams,
  93. 'select_clause_ref' => &$sSelectClause,
  94. 'join_clause_ref' => &$sJoinClause,
  95. 'where_clause_ref' => &$sWhereClause,
  96. 'order_clause_ref' => &$sOrderClause,
  97. 'limit_clause_ref' => &$sLimitClause,
  98. 'bindings_ref' => &$aMethod['params'][1]
  99. ]);
  100. break;
  101. case 'all_ids':
  102. $aMethod['name'] = 'getColumn';
  103. $sSelectClause = "`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "`";
  104. $sOrderClause .= "`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ADDED'] . "` ASC";
  105. break;
  106. case 'all':
  107. if(isset($aParams['count']) && $aParams['count'] === true) {
  108. $aMethod['name'] = 'getOne';
  109. $sSelectClause = 'COUNT(`' . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . '`)';
  110. }
  111. $sOrderClause .= "`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ADDED'] . "` ASC";
  112. break;
  113. }
  114. if(!empty($sOrderClause))
  115. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  116. if(!empty($sLimitClause))
  117. $sLimitClause = 'LIMIT ' . $sLimitClause;
  118. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_ENTRIES'] . "` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  119. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  120. }
  121. public function getEntriesNumByContext ($iProfileId)
  122. {
  123. $sQuery = $this->prepare ("SELECT COUNT(*) FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_ALLOW_VIEW_TO'] . "` = ?", - $iProfileId);
  124. return $this->getOne($sQuery);
  125. }
  126. public function getEntriesByAuthor ($iProfileId)
  127. {
  128. $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);
  129. return $this->getAll($sQuery);
  130. }
  131. public function getEntriesNumByAuthor ($iProfileId)
  132. {
  133. $sQuery = $this->prepare ("SELECT COUNT(*) FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_AUTHOR'] . "` = ?", $iProfileId);
  134. return $this->getOne($sQuery);
  135. }
  136. public function getEntriesNumByParams ($aParams = [])
  137. {
  138. $sSql = "SELECT COUNT(*) FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE 1";
  139. foreach($aParams as $aValue){
  140. if (!$this->isValidOperator($aValue['operator'])) {
  141. throw new Exception('Invalid operator in getEntriesNumByParams method');
  142. }
  143. $sSql .= ' AND `' . $aValue['key'] ."` " . $aValue['operator'] . " '" . $aValue['value'] . "'";
  144. }
  145. $sQuery = $this->prepare($sSql);
  146. return $this->getOne($sQuery);
  147. }
  148. public function updateEntriesBy($aParamsSet, $aParamsWhere)
  149. {
  150. $CNF = &$this->_oConfig->CNF;
  151. if(empty($CNF['TABLE_ENTRIES']) || empty($aParamsSet) || empty($aParamsWhere))
  152. return false;
  153. return $this->query("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  154. }
  155. public function updateStatusAdmin($iContentId, $isActive)
  156. {
  157. $CNF = $this->_oConfig->CNF;
  158. if (!isset($CNF['FIELD_STATUS_ADMIN']))
  159. return false;
  160. $sQuery = $this->prepare("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET `" . $CNF['FIELD_STATUS_ADMIN'] . "` = ? WHERE `" . $CNF['FIELD_ID'] . "` = ?", $isActive ? 'active' : 'hidden', $iContentId);
  161. return $this->query($sQuery);
  162. }
  163. public function publishEntries()
  164. {
  165. $CNF = $this->_oConfig->CNF;
  166. $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'");
  167. if(empty($aEntries) || !is_array($aEntries))
  168. return false;
  169. $iNow = time();
  170. $aResult = array();
  171. foreach($aEntries as $aEntry)
  172. if($aEntry[$CNF['FIELD_PUBLISHED']] <= $iNow)
  173. $aResult[] = $aEntry[$CNF['FIELD_ID']];
  174. $sSet = "`" . $CNF['FIELD_ADDED'] . "`=`" . $CNF['FIELD_PUBLISHED'] . "`, `" . $CNF['FIELD_STATUS'] . "` = 'active'";
  175. if(isset($CNF['FIELD_CHANGED']))
  176. $sSet .= ", `" . $CNF['FIELD_CHANGED'] . "`=`" . $CNF['FIELD_PUBLISHED'] . "`";
  177. return count($aResult) == (int)$this->query("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET " . $sSet . " WHERE `id` IN (" . $this->implode_escape($aResult) . ")") ? $aResult : false;
  178. }
  179. public function alterFulltextIndex ()
  180. {
  181. $CNF = $this->_oConfig->CNF;
  182. $bFulltextIndex = false;
  183. $aIndexes = $this->getAll("SHOW INDEXES FROM `" . $CNF['TABLE_ENTRIES'] . "`");
  184. foreach ($aIndexes as $r) {
  185. if ($CNF['TABLE_ENTRIES_FULLTEXT'] == $r['Key_name']) {
  186. $bFulltextIndex = true;
  187. break;
  188. }
  189. }
  190. if ($bFulltextIndex)
  191. $this->query("ALTER TABLE `" . $CNF['TABLE_ENTRIES'] . "` DROP INDEX `" . $CNF['TABLE_ENTRIES_FULLTEXT'] . "`");
  192. $sFields = getParam($CNF['PARAM_SEARCHABLE_FIELDS']);
  193. if (!$sFields || !($aFields = explode(',', $sFields)))
  194. return true;
  195. $sFields = '';
  196. foreach ($aFields as $s)
  197. $sFields .= "`$s`,";
  198. return $this->query("ALTER TABLE `" . $CNF['TABLE_ENTRIES'] . "` ADD FULLTEXT `" . $CNF['TABLE_ENTRIES_FULLTEXT'] . "` (" . trim($sFields, ', ') . ")");
  199. }
  200. public function deleteNestedById ($iNestedId, $sTableKey, $sTableName)
  201. {
  202. return $this->query("DELETE FROM `" . $sTableName . "` WHERE `" . $sTableKey . "` = :content_id", array('content_id' => $iNestedId));
  203. }
  204. public function getNestedBy($aParams, $sTableName)
  205. {
  206. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  207. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  208. $sSelectClause = "`" . $sTableName . "`.*";
  209. switch($aParams['type']) {
  210. case 'id':
  211. $aMethod['name'] = 'getRow';
  212. $aMethod['params'][1]['id'] = (int)$aParams['id'];
  213. $sWhereClause .= " AND `" . $sTableName . "`.`" . $aParams['key_name'] . "` = :id";
  214. break;
  215. case 'content_id':
  216. $aMethod['name'] = 'getAllWithKey';
  217. $aMethod['params'][2]['id'] = (int)$aParams['id'];
  218. $aMethod['params'][1] = $aParams['key_name'];
  219. $sWhereClause .= " AND `" . $sTableName . "`.`content_id` = :id";
  220. $sOrderClause = $aParams['key_name'];
  221. break;
  222. }
  223. if(!empty($sOrderClause))
  224. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  225. if(!empty($sLimitClause))
  226. $sLimitClause = 'LIMIT ' . $sLimitClause;
  227. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $sTableName . "` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  228. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  229. }
  230. /**
  231. *
  232. * Internal Polls related methods.
  233. *
  234. */
  235. public function isPollPerformed($iPollId, $iAuthorId, $iAuthorIp = 0)
  236. {
  237. $CNF = &$this->_oConfig->CNF;
  238. if(empty($CNF['TABLE_POLLS_ANSWERS_VOTES_TRACK']))
  239. return false;
  240. $iAuthorId = (int)$iAuthorId;
  241. $aBindings = array('author_id' => $iAuthorId);
  242. $sWhereClause = "AND `author_id`=:author_id";
  243. if(empty($iAuthorId)) {
  244. $aBindings['author_nip'] = $iAuthorIp;
  245. $sWhereClause .= " AND `author_nip`=:author_nip";
  246. }
  247. $aAnswers = $this->getPollAnswers([
  248. 'type' => 'poll_id_pairs',
  249. 'poll_id' => $iPollId
  250. ]);
  251. return (int)$this->getOne("SELECT `object_id` FROM `" . $CNF['TABLE_POLLS_ANSWERS_VOTES_TRACK'] . "` WHERE `object_id` IN (" . $this->implode_escape(array_keys($aAnswers)) . ") " . $sWhereClause . " LIMIT 1", $aBindings) != 0;
  252. }
  253. public function getPollPerformedValue($iPollId, $iAuthorId)
  254. {
  255. $CNF = &$this->_oConfig->CNF;
  256. $aBindings = ['author_id' => $iAuthorId];
  257. $sWhereClause = "AND `author_id`=:author_id";
  258. $aSubentries = $this->getPollAnswers([
  259. 'type' => 'poll_id_pairs',
  260. 'poll_id' => $iPollId
  261. ]);
  262. return (int)$this->getOne("SELECT `object_id` FROM `" . $CNF['TABLE_POLLS_ANSWERS_VOTES_TRACK'] . "` WHERE `object_id` IN (" . $this->implode_escape(array_keys($aSubentries)) . ") " . $sWhereClause . " LIMIT 1", $aBindings);
  263. }
  264. public function getPolls($aParams)
  265. {
  266. $CNF = &$this->_oConfig->CNF;
  267. if(empty($CNF['TABLE_POLLS']))
  268. return false;
  269. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  270. $sJoinClause = $sWhereClause = $sOrderByClause = "";
  271. $sSelectClause = "`tp`.*";
  272. switch($aParams['type']) {
  273. case 'id':
  274. $aMethod['name'] = 'getRow';
  275. $aMethod['params'][1] = array(
  276. 'id' => $aParams['id']
  277. );
  278. $sWhereClause .= " AND `tp`.`id`=:id";
  279. break;
  280. case 'answer_id':
  281. $aMethod['name'] = 'getRow';
  282. $aMethod['params'][1] = array(
  283. 'answer_id' => $aParams['answer_id']
  284. );
  285. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_POLLS_ANSWERS'] . "` AS `ta` ON `tp`.`" . $CNF['FIELD_POLL_ID'] . "`=`ta`.`poll_id`";
  286. $sWhereClause .= " AND `ta`.`id`=:answer_id";
  287. break;
  288. case 'content_id':
  289. $aMethod['params'][1] = array(
  290. 'content_id' => $aParams['content_id']
  291. );
  292. $sWhereClause .= " AND `tp`.`content_id`=:content_id";
  293. break;
  294. case 'content_id_ids':
  295. $aMethod['name'] = 'getColumn';
  296. $aMethod['params'][1] = array(
  297. 'content_id' => $aParams['content_id']
  298. );
  299. $sSelectClause = "`tp`.`" . $CNF['FIELD_POLL_ID'] . "`";
  300. $sWhereClause .= " AND `tp`.`content_id`=:content_id";
  301. break;
  302. case 'author_id':
  303. $aMethod['params'][1] = array(
  304. 'author_id' => $aParams['author_id']
  305. );
  306. $sWhereClause .= " AND `tp`.`author_id`=:author_id";
  307. if(isset($aParams['unused']) && $aParams['unused'] === true) {
  308. $aMethod['params'][1]['content_id'] = 0;
  309. $sWhereClause .= " AND `tp`.`content_id`=:content_id";
  310. }
  311. break;
  312. }
  313. if(!empty($sOrderByClause))
  314. $sOrderByClause = "ORDER BY " . $sOrderByClause;
  315. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_POLLS'] . "` AS `tp` " . $sJoinClause . " WHERE 1 " . $sWhereClause . $sOrderByClause;
  316. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  317. }
  318. public function getPollInfoById($iId)
  319. {
  320. return $this->getPolls(array('type' => 'id', 'id' => $iId));
  321. }
  322. public function updatePolls($aParamsSet, $aParamsWhere)
  323. {
  324. $CNF = &$this->_oConfig->CNF;
  325. if(empty($CNF['TABLE_POLLS']) || empty($aParamsSet) || empty($aParamsWhere))
  326. return false;
  327. return $this->query("UPDATE `" . $CNF['TABLE_POLLS'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  328. }
  329. public function deletePolls($aParams)
  330. {
  331. $CNF = &$this->_oConfig->CNF;
  332. if(empty($CNF['TABLE_POLLS']))
  333. return false;
  334. $sWhereClause = $this->arrayToSQL($aParams, " AND ");
  335. $aPolls = $this->getAll("SELECT * FROM `" . $CNF['TABLE_POLLS'] . "` WHERE " . $sWhereClause);
  336. if(empty($aPolls) || !is_array($aPolls))
  337. return true;
  338. return $this->_deletePolls($aPolls);
  339. }
  340. public function deletePollsByIds($mixedId)
  341. {
  342. $CNF = &$this->_oConfig->CNF;
  343. if(empty($CNF['TABLE_POLLS']))
  344. return false;
  345. $sWhereClause = "`id` IN (" . $this->implode_escape(is_array($mixedId) ? $mixedId : array($mixedId)) . ")";
  346. $aPolls = $this->getAll("SELECT * FROM `" . $CNF['TABLE_POLLS'] . "` WHERE " . $sWhereClause);
  347. if(empty($aPolls) || !is_array($aPolls))
  348. return true;
  349. return $this->_deletePolls($aPolls);
  350. }
  351. public function getPollAnswers($aParams = array())
  352. {
  353. $CNF = &$this->_oConfig->CNF;
  354. if(empty($CNF['TABLE_POLLS_ANSWERS']))
  355. return false;
  356. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  357. $sWhereClause = $sOrderByClause = "";
  358. $sSelectClause = "*";
  359. switch($aParams['type']) {
  360. case 'id':
  361. $aMethod['name'] = 'getRow';
  362. $aMethod['params'][1] = array(
  363. 'id' => $aParams['id']
  364. );
  365. $sWhereClause .= " AND `id`=:id";
  366. break;
  367. case 'poll_id_pairs':
  368. $aMethod['name'] = 'getPairs';
  369. $aMethod['params'][1] = 'id';
  370. $aMethod['params'][2] = 'title';
  371. $aMethod['params'][3] = array(
  372. 'poll_id' => $aParams['poll_id']
  373. );
  374. $sWhereClause .= " AND `poll_id`=:poll_id";
  375. break;
  376. case 'poll_id':
  377. $aMethod['params'][1] = array(
  378. 'poll_id' => $aParams['poll_id']
  379. );
  380. $sWhereClause .= " AND `poll_id`=:poll_id";
  381. break;
  382. case 'poll_id_max_order':
  383. $aMethod['name'] = 'getOne';
  384. $aMethod['params'][1] = array(
  385. 'poll_id' => $aParams['poll_id']
  386. );
  387. $sSelectClause = "MAX(`order`)";
  388. $sWhereClause .= " AND `poll_id`=:poll_id";
  389. break;
  390. case 'all':
  391. break;
  392. }
  393. $sOrderByClause = " ORDER BY " . (isset($aParams['order_by']) ? '`' . $aParams['order_by'] . '`' : '`order`');
  394. $sOrderByClause .= " " . (isset($aParams['order_way']) ? strtoupper($aParams['order_way']) : 'ASC');
  395. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_POLLS_ANSWERS'] . "` WHERE 1 " . $sWhereClause . $sOrderByClause;
  396. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  397. }
  398. public function insertPollAnswer($aParamsSet)
  399. {
  400. $CNF = &$this->_oConfig->CNF;
  401. if(empty($CNF['TABLE_POLLS_ANSWERS']) || empty($aParamsSet))
  402. return false;
  403. return $this->query("INSERT INTO `" . $CNF['TABLE_POLLS_ANSWERS'] . "` SET " . $this->arrayToSQL($aParamsSet));
  404. }
  405. public function updatePollAnswers($aParamsSet, $aParamsWhere)
  406. {
  407. $CNF = &$this->_oConfig->CNF;
  408. if(empty($CNF['TABLE_POLLS_ANSWERS']) || empty($aParamsSet) || empty($aParamsWhere))
  409. return false;
  410. return $this->query("UPDATE `" . $CNF['TABLE_POLLS_ANSWERS'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  411. }
  412. public function deletePollAnswers($aParams)
  413. {
  414. $CNF = &$this->_oConfig->CNF;
  415. if(empty($CNF['TABLE_POLLS_ANSWERS']))
  416. return false;
  417. $sWhereClause = $this->arrayToSQL($aParams, " AND ");
  418. $aAnswers = $this->getAll("SELECT * FROM `" . $CNF['TABLE_POLLS_ANSWERS'] . "` WHERE " . $sWhereClause);
  419. if(empty($aAnswers) || !is_array($aAnswers))
  420. return true;
  421. return $this->_deletePollAnswers($aAnswers);
  422. }
  423. public function deletePollAnswersByIds($mixedId)
  424. {
  425. $CNF = &$this->_oConfig->CNF;
  426. if(empty($CNF['TABLE_POLLS_ANSWERS']))
  427. return false;
  428. $sWhereClause = "`id` IN (" . $this->implode_escape(is_array($mixedId) ? $mixedId : array($mixedId)) . ")";
  429. $aAnswers = $this->getAll("SELECT * FROM `" . $CNF['TABLE_POLLS_ANSWERS'] . "` WHERE " . $sWhereClause);
  430. if(empty($aAnswers) || !is_array($aAnswers))
  431. return true;
  432. return $this->_deletePollAnswers($aAnswers);
  433. }
  434. protected function _deletePolls(&$aPolls)
  435. {
  436. $CNF = &$this->_oConfig->CNF;
  437. $aAffected = array();
  438. foreach($aPolls as $aPoll) {
  439. if(!$this->deletePollAnswers(array('poll_id' => $aPoll[$CNF['FIELD_POLL_ID']])))
  440. continue;
  441. $aAffected[] = $aPoll[$CNF['FIELD_POLL_ID']];
  442. }
  443. return $this->query("DELETE FROM `" . $CNF['TABLE_POLLS'] . "` WHERE `id` IN (" . $this->implode_escape($aAffected) . ")");
  444. }
  445. protected function _deletePollAnswers(&$aAnswers)
  446. {
  447. $CNF = &$this->_oConfig->CNF;
  448. $aAffected = array();
  449. foreach($aAnswers as $aAnswer) {
  450. BxDolVote::getObjectInstance($CNF['OBJECT_VOTES_POLL_ANSWERS'], $aAnswer['id'])->onObjectDelete();
  451. $aAffected[] = $aAnswer['id'];
  452. }
  453. return $this->query("DELETE FROM `" . $CNF['TABLE_POLLS_ANSWERS'] . "` WHERE `id` IN (" . $this->implode_escape($aAffected) . ")");
  454. }
  455. protected function _getEntriesBySearchIds($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  456. {
  457. $CNF = &$this->_oConfig->CNF;
  458. $aMethod['name'] = 'getColumn';
  459. $sSelectClause = " DISTINCT `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "`";
  460. if (!empty($aParams['start']) && !empty($aParams['per_page']))
  461. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  462. elseif (!empty($aParams['per_page']))
  463. $sLimitClause = $this->prepareAsString("?", $aParams['per_page']);
  464. $sWhereConditions = "1";
  465. foreach($aParams['search_params'] as $sSearchParam => $aSearchParam) {
  466. if(empty($aSearchParam['operator']))
  467. continue;
  468. $sSearchValue = "";
  469. switch ($aSearchParam['operator']) {
  470. case 'like':
  471. if(is_array($aSearchParam['value'])) {
  472. $sSubCondition = "0";
  473. foreach($aSearchParam['value'] as $sValue)
  474. if(!empty($sValue))
  475. $sSubCondition .= " OR `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "` LIKE " . $this->_getEbsiLike($sValue);
  476. if($sSubCondition != "0")
  477. $sWhereConditions .= " AND (" . $sSubCondition . ")";
  478. }
  479. else
  480. $sSearchValue = " LIKE " . $this->_getEbsiLike($aSearchParam['value']);
  481. break;
  482. case 'in':
  483. $sSearchValue = " IN (" . $this->implode_escape($aSearchParam['value']) . ")";
  484. break;
  485. case 'not in':
  486. $sSearchValue = " NOT IN (" . $this->implode_escape($aSearchParam['value']) . ")";
  487. break;
  488. case 'and':
  489. $iResult = 0;
  490. if (is_array($aSearchParam['value']))
  491. foreach ($aSearchParam['value'] as $iValue)
  492. $iResult |= pow (2, $iValue - 1);
  493. else
  494. $iResult = (int)$aSearchParam['value'];
  495. $sSearchValue = " & " . $iResult . "";
  496. break;
  497. case 'locate':
  498. if(!isset($CNF['OBJECT_METATAGS']))
  499. break;
  500. if ($aSearchParam['type'] == 'location_radius'){
  501. list($fLatitude, $fLongitude, $sCountry, $sState, $sCity, $sZip, $sStreet, $sStreetNumber, $iRadius) = $aSearchParam['value']['array'];
  502. if ($fLatitude && $fLongitude && $iRadius) {
  503. $aBounds = bx_get_location_bounds_latlng($fLatitude, $fLongitude, $iRadius);
  504. $aSql = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])->locationsGetAsSQLPart($CNF['TABLE_ENTRIES'], $CNF['FIELD_ID'], '', '', '', '', $aBounds);
  505. }
  506. }
  507. else{
  508. list($fLatitude, $fLongitude, $sCountry, $sState, $sCity, $sZip) = $aSearchParam['value']['array'];
  509. $aSql = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])->locationsGetAsSQLPart($CNF['TABLE_ENTRIES'], $CNF['FIELD_ID'], $sCountry, $sState, $sCity, $sZip);
  510. }
  511. if(!empty($aSql['where'])) {
  512. $sWhereConditions .= $aSql['where'];
  513. if(!empty($aSql['join']))
  514. $sJoinClause .= $aSql['join'];
  515. }
  516. break;
  517. case 'between':
  518. if(!is_array($aSearchParam['value']) || count($aSearchParam['value']) != 2)
  519. break;
  520. list($mixedMin, $mixedMax) = $aSearchParam['value'];
  521. if(!empty($mixedMin)) {
  522. $sWhereConditions .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "` >= :" . $sSearchParam . "_from";
  523. $aMethod['params'][1][$sSearchParam . "_from"] = $mixedMin;
  524. }
  525. if(!empty($mixedMax)) {
  526. $sWhereConditions .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "` <= :" . $sSearchParam . "_to";
  527. $aMethod['params'][1][$sSearchParam . "_to"] = $mixedMax;
  528. }
  529. break;
  530. default:
  531. if (!$this->isValidOperator($aSearchParam['operator'])) {
  532. throw new Exception('Invalid operator in _getEntriesBySearchIds method');
  533. }
  534. $sSearchValue = " " . $aSearchParam['operator'] . " :" . $sSearchParam;
  535. $aMethod['params'][1][$sSearchParam] = $aSearchParam['value'];
  536. }
  537. if(!empty($sSearchValue))
  538. $sWhereConditions .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "`" . $sSearchValue;
  539. }
  540. $sWhereClause .= " AND (" . $sWhereConditions . ")";
  541. $this->_addCustomConditions($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  542. $this->_getEntriesBySearchIdsOrder($aParams, $sOrderClause);
  543. }
  544. protected function _addCustomConditions($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  545. {
  546. $this->_addConditionsForAuthorStatus($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  547. $this->_addConditionsForCf($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  548. }
  549. protected function _addConditionsForAuthorStatus($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  550. {
  551. $CNF = &$this->_oConfig->CNF;
  552. if (empty($CNF['FIELD_AUTHOR']))
  553. return;
  554. if (!empty($aParams['show_all_content']))
  555. return;
  556. $sJoinClause .= " INNER JOIN `sys_profiles` as `p` ON (`p`.`id` = `{$CNF['TABLE_ENTRIES']}`.`{$CNF['FIELD_AUTHOR']}` AND `p`.`status` = 'active') ";
  557. }
  558. protected function _addConditionsForCf($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  559. {
  560. $CNF = &$this->_oConfig->CNF;
  561. if(empty($CNF['FIELD_CF']))
  562. return;
  563. $oCf = BxDolContentFilter::getInstance();
  564. if(!$oCf->isEnabled())
  565. return;
  566. $sWhereClause .= $oCf->getSQLParts($CNF['TABLE_ENTRIES'], $CNF['FIELD_CF']);
  567. }
  568. protected function _getEntriesBySearchIdsOrder($aParams, &$sOrderClause)
  569. {
  570. $CNF = &$this->_oConfig->CNF;
  571. if(empty($aParams['search_params']['order']) || !is_array($aParams['search_params']['order']))
  572. $aParams['search_params']['order'] = array(
  573. array('table' => $CNF['TABLE_ENTRIES'], 'field' => $CNF['FIELD_ADDED'], 'direction' => 'DESC')
  574. );
  575. $aOrders = array();
  576. foreach($aParams['search_params']['order'] as $aOrder) {
  577. $sTable = (isset($aOrder['table']) ? $aOrder['table'] : $CNF['TABLE_ENTRIES']);
  578. $sField = $CNF['FIELD_ADDED'];
  579. if (!empty($aOrder['field'])) {
  580. if (!$this->isFieldExists($sTable, $aOrder['field'])) {
  581. throw new Exception('Invalid field ' . $aOrder['field'] . ' in table ' . $sTable . ' in _getEntriesBySearchIdsOrder method');
  582. }
  583. $sField = $aOrder['field'];
  584. }
  585. $sDir = 'DESC';
  586. if (!empty($aOrder['direction'])) {
  587. $sDir = strtoupper($aOrder['direction']) == 'ASC' ? 'ASC' : 'DESC';
  588. }
  589. $aOrders[] = "`" . $sTable . "`.`" . $sField . "` " . $sDir;
  590. }
  591. $sOrderClause .= implode(', ', $aOrders);
  592. }
  593. protected function _getEbsiLike($sValue)
  594. {
  595. return $this->escape("%" . preg_replace('/\s+/', '%', $sValue) . "%");
  596. }
  597. public function getStatByProfile($iAuthorId)
  598. {
  599. $aBindings = array(
  600. 'author' => $iAuthorId
  601. );
  602. $CNF = &$this->_oConfig->CNF;
  603. return $this->getRow("SELECT COUNT(id) AS count, SUM(views) AS views, SUM(votes) AS votes, SUM(score) AS score FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `author`=:author", $aBindings);
  604. }
  605. }
  606. /** @} */