BxBaseModTextDb.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 BaseText Base classes for text modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module database queries
  13. */
  14. class BxBaseModTextDb extends BxBaseModGeneralDb
  15. {
  16. public function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function searchByAuthorTerm($iAuthor, $sTerm, $iLimit)
  21. {
  22. $CNF = &$this->_oConfig->CNF;
  23. if (empty($CNF['FIELDS_QUICK_SEARCH']))
  24. return array();
  25. $aBindings = array(
  26. 'author' => $iAuthor
  27. );
  28. $sWhere = '';
  29. foreach ($CNF['FIELDS_QUICK_SEARCH'] as $sField) {
  30. $aBindings[$sField] = '%' . $sTerm . '%';
  31. $sWhere .= " OR `c`.`$sField` LIKE :" . $sField;
  32. }
  33. $sOrderBy = $this->prepareAsString(" ORDER BY `c`.`added` DESC LIMIT ?", (int)$iLimit);
  34. $sQuery = "SELECT `c`.* FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` AS `c` WHERE `c`.`" . $CNF['FIELD_AUTHOR'] . "`=:author AND (0 $sWhere)" . $sOrderBy;
  35. return $this->getAll($sQuery, $aBindings);
  36. }
  37. /**
  38. *
  39. * Internal Polls related methods.
  40. *
  41. */
  42. public function isPollPerformed($iPollId, $iAuthorId, $iAuthorIp = 0)
  43. {
  44. $CNF = &$this->_oConfig->CNF;
  45. if(empty($CNF['TABLE_POLLS_ANSWERS_VOTES_TRACK']))
  46. return false;
  47. $iAuthorId = (int)$iAuthorId;
  48. $aBindings = array('author_id' => $iAuthorId);
  49. $sWhereClause = "AND `author_id`=:author_id";
  50. if(empty($iAuthorId)) {
  51. $aBindings['author_nip'] = $iAuthorIp;
  52. $sWhereClause .= " AND `author_nip`=:author_nip";
  53. }
  54. $aAnswers = $this->getPollAnswers(array('type' => 'poll_id_pairs', 'poll_id' => $iPollId));
  55. 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;
  56. }
  57. public function getPolls($aParams)
  58. {
  59. $CNF = &$this->_oConfig->CNF;
  60. if(empty($CNF['TABLE_POLLS']))
  61. return false;
  62. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  63. $sJoinClause = $sWhereClause = $sOrderByClause = "";
  64. $sSelectClause = "`tp`.*";
  65. switch($aParams['type']) {
  66. case 'id':
  67. $aMethod['name'] = 'getRow';
  68. $aMethod['params'][1] = array(
  69. 'id' => $aParams['id']
  70. );
  71. $sWhereClause .= " AND `tp`.`id`=:id";
  72. break;
  73. case 'answer_id':
  74. $aMethod['name'] = 'getRow';
  75. $aMethod['params'][1] = array(
  76. 'answer_id' => $aParams['answer_id']
  77. );
  78. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_POLLS_ANSWERS'] . "` AS `ta` ON `tp`.`" . $CNF['FIELD_POLL_ID'] . "`=`ta`.`poll_id`";
  79. $sWhereClause .= " AND `ta`.`id`=:answer_id";
  80. break;
  81. case 'content_id':
  82. $aMethod['params'][1] = array(
  83. 'content_id' => $aParams['content_id']
  84. );
  85. $sWhereClause .= " AND `tp`.`content_id`=:content_id";
  86. break;
  87. case 'content_id_ids':
  88. $aMethod['name'] = 'getColumn';
  89. $aMethod['params'][1] = array(
  90. 'content_id' => $aParams['content_id']
  91. );
  92. $sSelectClause = "`tp`.`" . $CNF['FIELD_POLL_ID'] . "`";
  93. $sWhereClause .= " AND `tp`.`content_id`=:content_id";
  94. break;
  95. case 'author_id':
  96. $aMethod['params'][1] = array(
  97. 'author_id' => $aParams['author_id']
  98. );
  99. $sWhereClause .= " AND `tp`.`author_id`=:author_id";
  100. if(isset($aParams['unused']) && $aParams['unused'] === true) {
  101. $aMethod['params'][1]['content_id'] = 0;
  102. $sWhereClause .= " AND `tp`.`content_id`=:content_id";
  103. }
  104. break;
  105. }
  106. if(!empty($sOrderByClause))
  107. $sOrderByClause = "ORDER BY " . $sOrderByClause;
  108. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_POLLS'] . "` AS `tp` " . $sJoinClause . " WHERE 1 " . $sWhereClause . $sOrderByClause;
  109. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  110. }
  111. public function getPollInfoById($iId)
  112. {
  113. return $this->getPolls(array('type' => 'id', 'id' => $iId));
  114. }
  115. public function updatePolls($aParamsSet, $aParamsWhere)
  116. {
  117. $CNF = &$this->_oConfig->CNF;
  118. if(empty($CNF['TABLE_POLLS']) || empty($aParamsSet) || empty($aParamsWhere))
  119. return false;
  120. return $this->query("UPDATE `" . $CNF['TABLE_POLLS'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  121. }
  122. public function deletePolls($aParams)
  123. {
  124. $CNF = &$this->_oConfig->CNF;
  125. if(empty($CNF['TABLE_POLLS']))
  126. return false;
  127. $sWhereClause = $this->arrayToSQL($aParams, " AND ");
  128. $aPolls = $this->getAll("SELECT * FROM `" . $CNF['TABLE_POLLS'] . "` WHERE " . $sWhereClause);
  129. if(empty($aPolls) || !is_array($aPolls))
  130. return true;
  131. return $this->_deletePolls($aPolls);
  132. }
  133. public function deletePollsByIds($mixedId)
  134. {
  135. $CNF = &$this->_oConfig->CNF;
  136. if(empty($CNF['TABLE_POLLS']))
  137. return false;
  138. $sWhereClause = "`id` IN (" . $this->implode_escape(is_array($mixedId) ? $mixedId : array($mixedId)) . ")";
  139. $aPolls = $this->getAll("SELECT * FROM `" . $CNF['TABLE_POLLS'] . "` WHERE " . $sWhereClause);
  140. if(empty($aPolls) || !is_array($aPolls))
  141. return true;
  142. return $this->_deletePolls($aPolls);
  143. }
  144. public function getPollAnswers($aParams = array())
  145. {
  146. $CNF = &$this->_oConfig->CNF;
  147. if(empty($CNF['TABLE_POLLS_ANSWERS']))
  148. return false;
  149. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  150. $sWhereClause = $sOrderByClause = "";
  151. $sSelectClause = "*";
  152. switch($aParams['type']) {
  153. case 'id':
  154. $aMethod['name'] = 'getRow';
  155. $aMethod['params'][1] = array(
  156. 'id' => $aParams['id']
  157. );
  158. $sWhereClause .= " AND `id`=:id";
  159. break;
  160. case 'poll_id_pairs':
  161. $aMethod['name'] = 'getPairs';
  162. $aMethod['params'][1] = 'id';
  163. $aMethod['params'][2] = 'title';
  164. $aMethod['params'][3] = array(
  165. 'poll_id' => $aParams['poll_id']
  166. );
  167. $sWhereClause .= " AND `poll_id`=:poll_id";
  168. break;
  169. case 'poll_id':
  170. $aMethod['params'][1] = array(
  171. 'poll_id' => $aParams['poll_id']
  172. );
  173. $sWhereClause .= " AND `poll_id`=:poll_id";
  174. break;
  175. case 'poll_id_max_order':
  176. $aMethod['name'] = 'getOne';
  177. $aMethod['params'][1] = array(
  178. 'poll_id' => $aParams['poll_id']
  179. );
  180. $sSelectClause = "MAX(`order`)";
  181. $sWhereClause .= " AND `poll_id`=:poll_id";
  182. break;
  183. case 'all':
  184. break;
  185. }
  186. $sOrderByClause = " ORDER BY " . (isset($aParams['order_by']) ? '`' . $aParams['order_by'] . '`' : '`order`');
  187. $sOrderByClause .= " " . (isset($aParams['order_way']) ? strtoupper($aParams['order_way']) : 'ASC');
  188. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_POLLS_ANSWERS'] . "` WHERE 1 " . $sWhereClause . $sOrderByClause;
  189. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  190. }
  191. public function insertPollAnswer($aParamsSet)
  192. {
  193. $CNF = &$this->_oConfig->CNF;
  194. if(empty($CNF['TABLE_POLLS_ANSWERS']) || empty($aParamsSet))
  195. return false;
  196. return $this->query("INSERT INTO `" . $CNF['TABLE_POLLS_ANSWERS'] . "` SET " . $this->arrayToSQL($aParamsSet));
  197. }
  198. public function updatePollAnswers($aParamsSet, $aParamsWhere)
  199. {
  200. $CNF = &$this->_oConfig->CNF;
  201. if(empty($CNF['TABLE_POLLS_ANSWERS']) || empty($aParamsSet) || empty($aParamsWhere))
  202. return false;
  203. return $this->query("UPDATE `" . $CNF['TABLE_POLLS_ANSWERS'] . "` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  204. }
  205. public function deletePollAnswers($aParams)
  206. {
  207. $CNF = &$this->_oConfig->CNF;
  208. if(empty($CNF['TABLE_POLLS_ANSWERS']))
  209. return false;
  210. $sWhereClause = $this->arrayToSQL($aParams, " AND ");
  211. $aAnswers = $this->getAll("SELECT * FROM `" . $CNF['TABLE_POLLS_ANSWERS'] . "` WHERE " . $sWhereClause);
  212. if(empty($aAnswers) || !is_array($aAnswers))
  213. return true;
  214. return $this->_deletePollAnswers($aAnswers);
  215. }
  216. public function deletePollAnswersByIds($mixedId)
  217. {
  218. $CNF = &$this->_oConfig->CNF;
  219. if(empty($CNF['TABLE_POLLS_ANSWERS']))
  220. return false;
  221. $sWhereClause = "`id` IN (" . $this->implode_escape(is_array($mixedId) ? $mixedId : array($mixedId)) . ")";
  222. $aAnswers = $this->getAll("SELECT * FROM `" . $CNF['TABLE_POLLS_ANSWERS'] . "` WHERE " . $sWhereClause);
  223. if(empty($aAnswers) || !is_array($aAnswers))
  224. return true;
  225. return $this->_deletePollAnswers($aAnswers);
  226. }
  227. protected function _deletePolls(&$aPolls)
  228. {
  229. $CNF = &$this->_oConfig->CNF;
  230. $aAffected = array();
  231. foreach($aPolls as $aPoll) {
  232. if(!$this->deletePollAnswers(array('poll_id' => $aPoll[$CNF['FIELD_POLL_ID']])))
  233. continue;
  234. $aAffected[] = $aPoll[$CNF['FIELD_POLL_ID']];
  235. }
  236. return $this->query("DELETE FROM `" . $CNF['TABLE_POLLS'] . "` WHERE `id` IN (" . $this->implode_escape($aAffected) . ")");
  237. }
  238. protected function _deletePollAnswers(&$aAnswers)
  239. {
  240. $CNF = &$this->_oConfig->CNF;
  241. $aAffected = array();
  242. foreach($aAnswers as $aAnswer) {
  243. BxDolVote::getObjectInstance($CNF['OBJECT_VOTES_POLL_ANSWERS'], $aAnswer['id'])->onObjectDelete();
  244. $aAffected[] = $aAnswer['id'];
  245. }
  246. return $this->query("DELETE FROM `" . $CNF['TABLE_POLLS_ANSWERS'] . "` WHERE `id` IN (" . $this->implode_escape($aAffected) . ")");
  247. }
  248. //--- Link attach related methods ---//
  249. public function getUnusedLinks($iUserId)
  250. {
  251. return $this->getLinksBy(array(
  252. 'type' => 'unused',
  253. 'profile_id' => $iUserId
  254. ));
  255. }
  256. public function deleteUnusedLinks($iUserId, $iLinkId = 0)
  257. {
  258. if(!$this->_oConfig->isAttachLinks())
  259. return false;
  260. $CNF = &$this->_oConfig->CNF;
  261. $aBindings = [
  262. 'profile_id' => $iUserId
  263. ];
  264. $sWhereAddon = '';
  265. if(!empty($iLinkId)) {
  266. $aBindings['id'] = $iLinkId;
  267. $sWhereAddon = " AND `id`=:id";
  268. }
  269. return $this->query("DELETE FROM `tl`, `tlc` USING `" . $CNF['TABLE_LINKS'] . "` AS `tl` LEFT JOIN `" . $CNF['TABLE_LINKS2CONTENT'] . "` AS `tlc` ON `tl`.`id`=`tlc`.`link_id` WHERE `tl`.`profile_id`=:profile_id AND ISNULL(`tlc`.`content_id`)" . $sWhereAddon, $aBindings);
  270. }
  271. public function saveLink($iContentId, $iLinkId)
  272. {
  273. if(!$this->_oConfig->isAttachLinks())
  274. return false;
  275. $CNF = &$this->_oConfig->CNF;
  276. $aBindings = array(
  277. 'content_id' => $iContentId,
  278. 'link_id' => $iLinkId
  279. );
  280. $iId = $this->getOne("SELECT `id` FROM `" . $CNF['TABLE_LINKS2CONTENT'] . "` WHERE `content_id`=:content_id AND `link_id`=:link_id LIMIT 1", $aBindings);
  281. if(!empty($iId))
  282. return true;
  283. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_LINKS2CONTENT'] . "` SET `content_id`=:content_id, `link_id`=:link_id", $aBindings) > 0;
  284. }
  285. public function deleteLink($iId)
  286. {
  287. if(!$this->_oConfig->isAttachLinks())
  288. return false;
  289. $CNF = &$this->_oConfig->CNF;
  290. return (int)$this->query("DELETE FROM `tl`, `tlc` USING `" . $CNF['TABLE_LINKS'] . "` AS `tl` LEFT JOIN `" . $CNF['TABLE_LINKS2CONTENT'] . "` AS `tlc` ON `tl`.`id`=`tlc`.`link_id` WHERE `tl`.`id` = :id", array(
  291. 'id' => $iId
  292. )) > 0;
  293. }
  294. public function deleteLinks($iContentId)
  295. {
  296. if(!$this->_oConfig->isAttachLinks())
  297. return false;
  298. $CNF = &$this->_oConfig->CNF;
  299. return (int)$this->query("DELETE FROM `tl`, `tlc` USING `" . $CNF['TABLE_LINKS'] . "` AS `tl` LEFT JOIN `" . $CNF['TABLE_LINKS2CONTENT'] . "` AS `tlc` ON `tl`.`id`=`tlc`.`link_id` WHERE `tlc`.`content_id` = :content_id", array(
  300. 'content_id' => $iContentId
  301. )) > 0;
  302. }
  303. public function getLinks($iContentId)
  304. {
  305. return $this->getLinksBy(array('type' => 'content_id', 'content_id' => $iContentId));
  306. }
  307. public function getStatByProfile($iAuthorId)
  308. {
  309. $aBindings = array(
  310. 'author' => $iAuthorId
  311. );
  312. $CNF = &$this->_oConfig->CNF;
  313. return $this->getRow("SELECT COUNT(id) AS count, SUM(views) AS views, SUM(votes) AS votes, SUM(rvotes) AS rvotes FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `author`=:author", $aBindings);
  314. }
  315. public function getLinksBy($aParams = array())
  316. {
  317. if(!$this->_oConfig->isAttachLinks())
  318. return [];
  319. $CNF = &$this->_oConfig->CNF;
  320. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  321. $sSelectClause = "`tl`.*";
  322. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  323. switch($aParams['type']) {
  324. case 'id':
  325. $aMethod['name'] = 'getRow';
  326. $aMethod['params'][1] = array(
  327. 'id' => $aParams['id']
  328. );
  329. $sWhereClause = " AND `tl`.`id`=:id";
  330. if(!empty($aParams['profile_id'])) {
  331. $aMethod['params'][1]['profile_id'] = $aParams['profile_id'];
  332. $sWhereClause .= " AND `tl`.`profile_id`=:profile_id";
  333. }
  334. break;
  335. case 'content_id':
  336. $aMethod['params'][1] = array(
  337. 'content_id' => $aParams['content_id']
  338. );
  339. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_LINKS2CONTENT'] . "` AS `tlc` ON `tl`.`id`=`tlc`.`link_id`";
  340. $sWhereClause = " AND `tlc`.`content_id`=:content_id";
  341. break;
  342. case 'unused':
  343. $aBindings = array(
  344. 'profile_id' => $aParams['profile_id']
  345. );
  346. if(isset($aParams['short']) && $aParams['short'] === true) {
  347. $aMethod['name'] = 'getPairs';
  348. $aMethod['params'][1] = 'url';
  349. $aMethod['params'][2] = 'id';
  350. $aMethod['params'][3] = $aBindings;
  351. }
  352. else
  353. $aMethod['params'][1] = $aBindings;
  354. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_LINKS2CONTENT'] . "` AS `tlc` ON `tl`.`id`=`tlc`.`link_id`";
  355. $sWhereClause = " AND `tl`.`profile_id`=:profile_id AND ISNULL(`tlc`.`content_id`)";
  356. $sOrderClause = "`tl`.`added` DESC";
  357. break;
  358. }
  359. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  360. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  361. $aMethod['params'][0] = "SELECT
  362. " . $sSelectClause . "
  363. FROM `" . $CNF['TABLE_LINKS'] . "` AS `tl` " . $sJoinClause . "
  364. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  365. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  366. }
  367. protected function _getEntriesBySearchIds($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  368. {
  369. $CNF = &$this->_oConfig->CNF;
  370. if($CNF['FIELD_STATUS'])
  371. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_STATUS'] . "`='active'";
  372. if($CNF['FIELD_STATUS_ADMIN'])
  373. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_STATUS_ADMIN'] . "`='active'";
  374. parent::_getEntriesBySearchIds($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  375. }
  376. }
  377. /** @} */