BxBaseModGeneralDb.php 30 KB

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