BxBaseModGeneralDb.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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([
  237. 'type' => 'poll_id_pairs',
  238. 'poll_id' => $iPollId
  239. ]);
  240. 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;
  241. }
  242. public function getPollPerformedValue($iPollId, $iAuthorId)
  243. {
  244. $CNF = &$this->_oConfig->CNF;
  245. $aBindings = ['author_id' => $iAuthorId];
  246. $sWhereClause = "AND `author_id`=:author_id";
  247. $aSubentries = $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($aSubentries)) . ") " . $sWhereClause . " LIMIT 1", $aBindings);
  252. }
  253. public function getPolls($aParams)
  254. {
  255. $CNF = &$this->_oConfig->CNF;
  256. if(empty($CNF['TABLE_POLLS']))
  257. return false;
  258. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  259. $sJoinClause = $sWhereClause = $sOrderByClause = "";
  260. $sSelectClause = "`tp`.*";
  261. switch($aParams['type']) {
  262. case 'id':
  263. $aMethod['name'] = 'getRow';
  264. $aMethod['params'][1] = array(
  265. 'id' => $aParams['id']
  266. );
  267. $sWhereClause .= " AND `tp`.`id`=:id";
  268. break;
  269. case 'answer_id':
  270. $aMethod['name'] = 'getRow';
  271. $aMethod['params'][1] = array(
  272. 'answer_id' => $aParams['answer_id']
  273. );
  274. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_POLLS_ANSWERS'] . "` AS `ta` ON `tp`.`" . $CNF['FIELD_POLL_ID'] . "`=`ta`.`poll_id`";
  275. $sWhereClause .= " AND `ta`.`id`=:answer_id";
  276. break;
  277. case 'content_id':
  278. $aMethod['params'][1] = array(
  279. 'content_id' => $aParams['content_id']
  280. );
  281. $sWhereClause .= " AND `tp`.`content_id`=:content_id";
  282. break;
  283. case 'content_id_ids':
  284. $aMethod['name'] = 'getColumn';
  285. $aMethod['params'][1] = array(
  286. 'content_id' => $aParams['content_id']
  287. );
  288. $sSelectClause = "`tp`.`" . $CNF['FIELD_POLL_ID'] . "`";
  289. $sWhereClause .= " AND `tp`.`content_id`=:content_id";
  290. break;
  291. case 'author_id':
  292. $aMethod['params'][1] = array(
  293. 'author_id' => $aParams['author_id']
  294. );
  295. $sWhereClause .= " AND `tp`.`author_id`=:author_id";
  296. if(isset($aParams['unused']) && $aParams['unused'] === true) {
  297. $aMethod['params'][1]['content_id'] = 0;
  298. $sWhereClause .= " AND `tp`.`content_id`=:content_id";
  299. }
  300. break;
  301. }
  302. if(!empty($sOrderByClause))
  303. $sOrderByClause = "ORDER BY " . $sOrderByClause;
  304. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_POLLS'] . "` AS `tp` " . $sJoinClause . " WHERE 1 " . $sWhereClause . $sOrderByClause;
  305. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  306. }
  307. public function getPollInfoById($iId)
  308. {
  309. return $this->getPolls(array('type' => 'id', 'id' => $iId));
  310. }
  311. public function updatePolls($aParamsSet, $aParamsWhere)
  312. {
  313. $CNF = &$this->_oConfig->CNF;
  314. if(empty($CNF['TABLE_POLLS']) || empty($aParamsSet) || empty($aParamsWhere))
  315. return false;
  316. return $this->query("UPDATE `" . $CNF['TABLE_POLLS'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  317. }
  318. public function deletePolls($aParams)
  319. {
  320. $CNF = &$this->_oConfig->CNF;
  321. if(empty($CNF['TABLE_POLLS']))
  322. return false;
  323. $sWhereClause = $this->arrayToSQL($aParams, " AND ");
  324. $aPolls = $this->getAll("SELECT * FROM `" . $CNF['TABLE_POLLS'] . "` WHERE " . $sWhereClause);
  325. if(empty($aPolls) || !is_array($aPolls))
  326. return true;
  327. return $this->_deletePolls($aPolls);
  328. }
  329. public function deletePollsByIds($mixedId)
  330. {
  331. $CNF = &$this->_oConfig->CNF;
  332. if(empty($CNF['TABLE_POLLS']))
  333. return false;
  334. $sWhereClause = "`id` IN (" . $this->implode_escape(is_array($mixedId) ? $mixedId : array($mixedId)) . ")";
  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 getPollAnswers($aParams = array())
  341. {
  342. $CNF = &$this->_oConfig->CNF;
  343. if(empty($CNF['TABLE_POLLS_ANSWERS']))
  344. return false;
  345. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  346. $sWhereClause = $sOrderByClause = "";
  347. $sSelectClause = "*";
  348. switch($aParams['type']) {
  349. case 'id':
  350. $aMethod['name'] = 'getRow';
  351. $aMethod['params'][1] = array(
  352. 'id' => $aParams['id']
  353. );
  354. $sWhereClause .= " AND `id`=:id";
  355. break;
  356. case 'poll_id_pairs':
  357. $aMethod['name'] = 'getPairs';
  358. $aMethod['params'][1] = 'id';
  359. $aMethod['params'][2] = 'title';
  360. $aMethod['params'][3] = array(
  361. 'poll_id' => $aParams['poll_id']
  362. );
  363. $sWhereClause .= " AND `poll_id`=:poll_id";
  364. break;
  365. case 'poll_id':
  366. $aMethod['params'][1] = array(
  367. 'poll_id' => $aParams['poll_id']
  368. );
  369. $sWhereClause .= " AND `poll_id`=:poll_id";
  370. break;
  371. case 'poll_id_max_order':
  372. $aMethod['name'] = 'getOne';
  373. $aMethod['params'][1] = array(
  374. 'poll_id' => $aParams['poll_id']
  375. );
  376. $sSelectClause = "MAX(`order`)";
  377. $sWhereClause .= " AND `poll_id`=:poll_id";
  378. break;
  379. case 'all':
  380. break;
  381. }
  382. $sOrderByClause = " ORDER BY " . (isset($aParams['order_by']) ? '`' . $aParams['order_by'] . '`' : '`order`');
  383. $sOrderByClause .= " " . (isset($aParams['order_way']) ? strtoupper($aParams['order_way']) : 'ASC');
  384. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_POLLS_ANSWERS'] . "` WHERE 1 " . $sWhereClause . $sOrderByClause;
  385. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  386. }
  387. public function insertPollAnswer($aParamsSet)
  388. {
  389. $CNF = &$this->_oConfig->CNF;
  390. if(empty($CNF['TABLE_POLLS_ANSWERS']) || empty($aParamsSet))
  391. return false;
  392. return $this->query("INSERT INTO `" . $CNF['TABLE_POLLS_ANSWERS'] . "` SET " . $this->arrayToSQL($aParamsSet));
  393. }
  394. public function updatePollAnswers($aParamsSet, $aParamsWhere)
  395. {
  396. $CNF = &$this->_oConfig->CNF;
  397. if(empty($CNF['TABLE_POLLS_ANSWERS']) || empty($aParamsSet) || empty($aParamsWhere))
  398. return false;
  399. return $this->query("UPDATE `" . $CNF['TABLE_POLLS_ANSWERS'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  400. }
  401. public function deletePollAnswers($aParams)
  402. {
  403. $CNF = &$this->_oConfig->CNF;
  404. if(empty($CNF['TABLE_POLLS_ANSWERS']))
  405. return false;
  406. $sWhereClause = $this->arrayToSQL($aParams, " AND ");
  407. $aAnswers = $this->getAll("SELECT * FROM `" . $CNF['TABLE_POLLS_ANSWERS'] . "` WHERE " . $sWhereClause);
  408. if(empty($aAnswers) || !is_array($aAnswers))
  409. return true;
  410. return $this->_deletePollAnswers($aAnswers);
  411. }
  412. public function deletePollAnswersByIds($mixedId)
  413. {
  414. $CNF = &$this->_oConfig->CNF;
  415. if(empty($CNF['TABLE_POLLS_ANSWERS']))
  416. return false;
  417. $sWhereClause = "`id` IN (" . $this->implode_escape(is_array($mixedId) ? $mixedId : array($mixedId)) . ")";
  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. protected function _deletePolls(&$aPolls)
  424. {
  425. $CNF = &$this->_oConfig->CNF;
  426. $aAffected = array();
  427. foreach($aPolls as $aPoll) {
  428. if(!$this->deletePollAnswers(array('poll_id' => $aPoll[$CNF['FIELD_POLL_ID']])))
  429. continue;
  430. $aAffected[] = $aPoll[$CNF['FIELD_POLL_ID']];
  431. }
  432. return $this->query("DELETE FROM `" . $CNF['TABLE_POLLS'] . "` WHERE `id` IN (" . $this->implode_escape($aAffected) . ")");
  433. }
  434. protected function _deletePollAnswers(&$aAnswers)
  435. {
  436. $CNF = &$this->_oConfig->CNF;
  437. $aAffected = array();
  438. foreach($aAnswers as $aAnswer) {
  439. BxDolVote::getObjectInstance($CNF['OBJECT_VOTES_POLL_ANSWERS'], $aAnswer['id'])->onObjectDelete();
  440. $aAffected[] = $aAnswer['id'];
  441. }
  442. return $this->query("DELETE FROM `" . $CNF['TABLE_POLLS_ANSWERS'] . "` WHERE `id` IN (" . $this->implode_escape($aAffected) . ")");
  443. }
  444. protected function _getEntriesBySearchIds($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  445. {
  446. $CNF = &$this->_oConfig->CNF;
  447. $aMethod['name'] = 'getColumn';
  448. $sSelectClause = " DISTINCT `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "`";
  449. if (!empty($aParams['start']) && !empty($aParams['per_page']))
  450. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  451. elseif (!empty($aParams['per_page']))
  452. $sLimitClause = $this->prepareAsString("?", $aParams['per_page']);
  453. $sWhereConditions = "1";
  454. foreach($aParams['search_params'] as $sSearchParam => $aSearchParam) {
  455. if(empty($aSearchParam['operator']))
  456. continue;
  457. $sSearchValue = "";
  458. switch ($aSearchParam['operator']) {
  459. case 'like':
  460. if(is_array($aSearchParam['value'])) {
  461. $sSubCondition = "0";
  462. foreach($aSearchParam['value'] as $sValue)
  463. if(!empty($sValue))
  464. $sSubCondition .= " OR `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "` LIKE " . $this->_getEbsiLike($sValue);
  465. if($sSubCondition != "0")
  466. $sWhereConditions .= " AND (" . $sSubCondition . ")";
  467. }
  468. else
  469. $sSearchValue = " LIKE " . $this->_getEbsiLike($aSearchParam['value']);
  470. break;
  471. case 'in':
  472. $sSearchValue = " IN (" . $this->implode_escape($aSearchParam['value']) . ")";
  473. break;
  474. case 'not in':
  475. $sSearchValue = " NOT IN (" . $this->implode_escape($aSearchParam['value']) . ")";
  476. break;
  477. case 'and':
  478. $iResult = 0;
  479. if (is_array($aSearchParam['value']))
  480. foreach ($aSearchParam['value'] as $iValue)
  481. $iResult |= pow (2, $iValue - 1);
  482. else
  483. $iResult = (int)$aSearchParam['value'];
  484. $sSearchValue = " & " . $iResult . "";
  485. break;
  486. case 'locate':
  487. if(!isset($CNF['OBJECT_METATAGS']))
  488. break;
  489. if ($aSearchParam['type'] == 'location_radius'){
  490. list($fLatitude, $fLongitude, $sCountry, $sState, $sCity, $sZip, $sStreet, $sStreetNumber, $iRadius) = $aSearchParam['value']['array'];
  491. if ($fLatitude && $fLongitude && $iRadius) {
  492. $aBounds = bx_get_location_bounds_latlng($fLatitude, $fLongitude, $iRadius);
  493. $aSql = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])->locationsGetAsSQLPart($CNF['TABLE_ENTRIES'], $CNF['FIELD_ID'], '', '', '', '', $aBounds);
  494. }
  495. }
  496. else{
  497. list($fLatitude, $fLongitude, $sCountry, $sState, $sCity, $sZip) = $aSearchParam['value']['array'];
  498. $aSql = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])->locationsGetAsSQLPart($CNF['TABLE_ENTRIES'], $CNF['FIELD_ID'], $sCountry, $sState, $sCity, $sZip);
  499. }
  500. if(!empty($aSql['where'])) {
  501. $sWhereConditions .= $aSql['where'];
  502. if(!empty($aSql['join']))
  503. $sJoinClause .= $aSql['join'];
  504. }
  505. break;
  506. case 'between':
  507. if(!is_array($aSearchParam['value']) || count($aSearchParam['value']) != 2)
  508. break;
  509. list($mixedMin, $mixedMax) = $aSearchParam['value'];
  510. if(!empty($mixedMin)) {
  511. $sWhereConditions .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "` >= :" . $sSearchParam . "_from";
  512. $aMethod['params'][1][$sSearchParam . "_from"] = $mixedMin;
  513. }
  514. if(!empty($mixedMax)) {
  515. $sWhereConditions .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "` <= :" . $sSearchParam . "_to";
  516. $aMethod['params'][1][$sSearchParam . "_to"] = $mixedMax;
  517. }
  518. break;
  519. default:
  520. $sSearchValue = " " . $aSearchParam['operator'] . " :" . $sSearchParam;
  521. $aMethod['params'][1][$sSearchParam] = $aSearchParam['value'];
  522. }
  523. if(!empty($sSearchValue))
  524. $sWhereConditions .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "`" . $sSearchValue;
  525. }
  526. $sWhereClause .= " AND (" . $sWhereConditions . ")";
  527. $this->_addCustomConditions($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  528. $this->_getEntriesBySearchIdsOrder($aParams, $sOrderClause);
  529. }
  530. protected function _addCustomConditions($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  531. {
  532. $this->_addConditionsForAuthorStatus($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  533. $this->_addConditionsForCf($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  534. }
  535. protected function _addConditionsForAuthorStatus($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  536. {
  537. $CNF = &$this->_oConfig->CNF;
  538. if (empty($CNF['FIELD_AUTHOR']))
  539. return;
  540. if (!empty($aParams['show_all_content']))
  541. return;
  542. $sJoinClause .= " INNER JOIN `sys_profiles` as `p` ON (`p`.`id` = `{$CNF['TABLE_ENTRIES']}`.`{$CNF['FIELD_AUTHOR']}` AND `p`.`status` = 'active') ";
  543. }
  544. protected function _addConditionsForCf($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  545. {
  546. $CNF = &$this->_oConfig->CNF;
  547. if(empty($CNF['FIELD_CF']))
  548. return;
  549. $oCf = BxDolContentFilter::getInstance();
  550. if(!$oCf->isEnabled())
  551. return;
  552. $sWhereClause .= $oCf->getSQLParts($CNF['TABLE_ENTRIES'], $CNF['FIELD_CF']);
  553. }
  554. protected function _getEntriesBySearchIdsOrder($aParams, &$sOrderClause)
  555. {
  556. $CNF = &$this->_oConfig->CNF;
  557. if(empty($aParams['search_params']['order']) || !is_array($aParams['search_params']['order']))
  558. $aParams['search_params']['order'] = array(
  559. array('table' => $CNF['TABLE_ENTRIES'], 'field' => $CNF['FIELD_ADDED'], 'direction' => 'DESC')
  560. );
  561. $aOrders = array();
  562. foreach($aParams['search_params']['order'] as $aOrder)
  563. $aOrders[] = "`" . (isset($aOrder['table']) ? $aOrder['table'] : $CNF['TABLE_ENTRIES']) . "`.`" . (!empty($aOrder['field']) ? $aOrder['field'] : $CNF['FIELD_ADDED']) . "` " . (!empty($aOrder['direction']) ? strtoupper($aOrder['direction']) : 'DESC');
  564. $sOrderClause .= implode(', ', $aOrders);
  565. }
  566. protected function _getEbsiLike($sValue)
  567. {
  568. return $this->escape("%" . preg_replace('/\s+/', '%', $sValue) . "%");
  569. }
  570. public function getStatByProfile($iAuthorId)
  571. {
  572. $aBindings = array(
  573. 'author' => $iAuthorId
  574. );
  575. $CNF = &$this->_oConfig->CNF;
  576. 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);
  577. }
  578. }
  579. /** @} */