BxDolCmtsQuery.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * @see BxDolCmts
  11. */
  12. class BxDolCmtsQuery extends BxDolDb
  13. {
  14. protected $_oMain;
  15. protected $_sTable;
  16. protected $_sTriggerTable;
  17. protected $_sTriggerFieldId;
  18. protected $_sTriggerFieldAuthor;
  19. protected $_sTriggerFieldTitle;
  20. protected $_sTriggerFieldComments;
  21. protected $_sTableFiles;
  22. protected $_sTableFiles2Entries;
  23. protected $_sTableIds;
  24. public function __construct(&$oMain)
  25. {
  26. $this->_oMain = $oMain;
  27. $aSystem = $this->_oMain->getSystemInfo();
  28. $this->_sTable = $aSystem['table'];
  29. $this->_sTriggerTable = $aSystem['trigger_table'];
  30. $this->_sTriggerFieldId = $aSystem['trigger_field_id'];
  31. $this->_sTriggerFieldAuthor = $aSystem['trigger_field_author'];
  32. $this->_sTriggerFieldTitle = $aSystem['trigger_field_title'];
  33. $this->_sTriggerFieldComments = $aSystem['trigger_field_comments'];
  34. $this->_sTableFiles = $aSystem['table_images'];
  35. $this->_sTableFiles2Entries = $aSystem['table_images2entries'];
  36. $this->_sTableIds = $aSystem['table_ids'];
  37. parent::__construct();
  38. }
  39. function getTableName ()
  40. {
  41. return $this->_sTable;
  42. }
  43. function setTableNameFiles($sTable)
  44. {
  45. $this->_sTableFiles = $sTable;
  46. }
  47. function setTableNameFiles2Entries($sTable)
  48. {
  49. $this->_sTableFiles2Entries = $sTable;
  50. }
  51. function getCommentsCountAll ($iId)
  52. {
  53. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldComments}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ?", $iId);
  54. return (int)$this->getOne($sQuery);
  55. }
  56. function getCommentsCount ($iId, $iCmtVParentId = -1, $iAuthorId = 0, $sFilter = '')
  57. {
  58. $aBindings = array(
  59. 'cmt_object_id' => $iId
  60. );
  61. $sWhereClause = '';
  62. if((int)$iCmtVParentId >= 0) {
  63. $aBindings['cmt_vparent_id'] = $iCmtVParentId;
  64. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_vparent_id` = :cmt_vparent_id";
  65. }
  66. $sJoinClause = '';
  67. switch($sFilter) {
  68. case BX_CMT_FILTER_FRIENDS:
  69. case BX_CMT_FILTER_SUBSCRIPTIONS:
  70. $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
  71. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  72. $sJoinClause .= ' ' . $aQueryParts['join'];
  73. break;
  74. case BX_CMT_FILTER_OTHERS:
  75. $aBindings['cmt_author_id'] = $iAuthorId;
  76. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` <> :cmt_author_id";
  77. break;
  78. }
  79. $sQuery = "SELECT COUNT(*) FROM `{$this->_sTable}` $sJoinClause WHERE `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id" . $sWhereClause;
  80. return (int)$this->getOne($sQuery, $aBindings);
  81. }
  82. function getComments ($iId, $iCmtVParentId = 0, $iAuthorId = 0, $sFilter = '', $aOrder = array(), $iStart = 0, $iCount = -1)
  83. {
  84. $aBindings = array(
  85. 'cmt_object_id' => $iId
  86. );
  87. $sFields = $sJoin = "";
  88. $oVote = $this->_oMain->getVoteObject(0);
  89. if($oVote !== false) {
  90. $aSql = $oVote->getSqlParts($this->_sTable, 'cmt_id');
  91. $sFields .= $aSql['fields'];
  92. $sJoin .= $aSql['join'];
  93. }
  94. $sWhereParent = '';
  95. if((int)$iCmtVParentId >= 0) {
  96. $aBindings['cmt_vparent_id'] = $iCmtVParentId;
  97. $sWhereParent = " AND `{$this->_sTable}`.`cmt_vparent_id` = :cmt_vparent_id";
  98. }
  99. if(in_array($sFilter, array(BX_CMT_FILTER_FRIENDS, BX_CMT_FILTER_SUBSCRIPTIONS))) {
  100. $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
  101. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  102. $sJoin .= ' ' . $aQueryParts['join'];
  103. }
  104. $sOder = " ORDER BY `{$this->_sTable}`.`cmt_time` ASC";
  105. if(isset($aOrder['by']) && isset($aOrder['way'])) {
  106. $aOrder['way'] = strtoupper(in_array($aOrder['way'], array(BX_CMT_ORDER_WAY_ASC, BX_CMT_ORDER_WAY_DESC)) ? $aOrder['way'] : BX_CMT_ORDER_WAY_ASC);
  107. switch($aOrder['by']) {
  108. case BX_CMT_ORDER_BY_DATE:
  109. $sOder = " ORDER BY `{$this->_sTable}`.`cmt_time` " . $aOrder['way'];
  110. break;
  111. case BX_CMT_ORDER_BY_POPULAR:
  112. $sOder = " ORDER BY `{$this->_sTable}`.`cmt_rate` " . $aOrder['way'];
  113. break;
  114. }
  115. }
  116. $sLimit = $iCount != -1 ? $this->prepareAsString(" LIMIT ?, ?", (int)$iStart, (int)$iCount) : '';
  117. $sQuery = "SELECT
  118. `{$this->_sTable}`.`cmt_id`,
  119. `{$this->_sTable}`.`cmt_parent_id`,
  120. `{$this->_sTable}`.`cmt_vparent_id`,
  121. `{$this->_sTable}`.`cmt_object_id`,
  122. `{$this->_sTable}`.`cmt_author_id`,
  123. `{$this->_sTable}`.`cmt_level`,
  124. `{$this->_sTable}`.`cmt_text`,
  125. `{$this->_sTable}`.`cmt_replies`,
  126. `{$this->_sTable}`.`cmt_time`
  127. $sFields
  128. FROM `{$this->_sTable}`
  129. $sJoin
  130. WHERE `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id" . $sWhereParent . $sOder . $sLimit;
  131. return $this->getAll($sQuery, $aBindings);
  132. }
  133. function getCommentsBy($aParams = array())
  134. {
  135. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  136. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  137. $sSelectClause = "
  138. `{$this->_sTable}`.`cmt_id`,
  139. `{$this->_sTable}`.`cmt_parent_id`,
  140. `{$this->_sTable}`.`cmt_vparent_id`,
  141. `{$this->_sTable}`.`cmt_object_id`,
  142. `{$this->_sTable}`.`cmt_author_id`,
  143. `{$this->_sTable}`.`cmt_level`,
  144. `{$this->_sTable}`.`cmt_text`,
  145. `{$this->_sTable}`.`cmt_replies`,
  146. `{$this->_sTable}`.`cmt_time`";
  147. if(isset($aParams['object_id'])) {
  148. $aMethod['params'][1]['cmt_object_id'] = (int)$aParams['object_id'];
  149. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id";
  150. }
  151. switch($aParams['type']) {
  152. case 'id':
  153. $aMethod['name'] = 'getRow';
  154. $aMethod['params'][1]['id'] = (int)$aParams['id'];
  155. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_id` = :id";
  156. break;
  157. case 'latest':
  158. if(!empty($aParams['author'])) {
  159. $aMethod['params'][1]['cmt_author_id'] = (int)$aParams['author'];
  160. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` " . (isset($aParams['others']) && (int)$aParams['others'] == 1 ? "<>" : "=") . " :cmt_author_id";
  161. }
  162. $sOrderClause = "`{$this->_sTable}`.`cmt_time` DESC";
  163. $sLimitClause = "";
  164. if(isset($aParams['per_page']))
  165. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  166. break;
  167. case 'object_id':
  168. $aMethod['params'][1]['cmt_object_id'] = (int)$aParams['object_id'];
  169. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id";
  170. $sOrderClause = "`{$this->_sTable}`.`cmt_time` ASC";
  171. $sLimitClause = "";
  172. if(isset($aParams['per_page']))
  173. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  174. break;
  175. case 'search_ids':
  176. $aMethod['name'] = 'getColumn';
  177. $sSelectClause = "`{$this->_sTable}`.`cmt_id`";
  178. $sWhereConditions = "1";
  179. foreach($aParams['search_params'] as $sSearchParam => $aSearchParam) {
  180. $sSearchValue = "";
  181. switch ($aSearchParam['operator']) {
  182. case 'like':
  183. $sSearchValue = " LIKE " . $this->escape("%" . $aSearchParam['value'] . "%");
  184. break;
  185. case 'in':
  186. $sSearchValue = " IN (" . $this->implode_escape($aSearchParam['value']) . ")";
  187. break;
  188. default:
  189. $sSearchValue = " " . $aSearchParam['operator'] . " :" . $sSearchParam;
  190. $aMethod['params'][1][$sSearchParam] = $aSearchParam['value'];
  191. }
  192. $sWhereConditions .= " AND `{$this->_sTable}`.`" . $sSearchParam . "`" . $sSearchValue;
  193. }
  194. $sWhereClause .= " AND (" . $sWhereConditions . ")";
  195. $sOrderClause .= "`{$this->_sTable}`.`cmt_time` ASC";
  196. break;
  197. case 'all_ids':
  198. $aMethod['name'] = 'getColumn';
  199. $sSelectClause = "`{$this->_sTable}`.`cmt_id`";
  200. $sOrderClause = "`{$this->_sTable}`.`cmt_time` ASC";
  201. break;
  202. }
  203. if(!empty($sOrderClause))
  204. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  205. if(!empty($sLimitClause))
  206. $sLimitClause = 'LIMIT ' . $sLimitClause;
  207. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `{$this->_sTable}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  208. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  209. }
  210. function getComment ($iId, $iCmtId)
  211. {
  212. $sFields = $sJoin = "";
  213. $oVote = $this->_oMain->getVoteObject($iCmtId);
  214. if($oVote !== false) {
  215. $aSql = $oVote->getSqlParts($this->_sTableIds, 'id');
  216. $sFields .= $aSql['fields'];
  217. $sJoin .= $aSql['join'];
  218. }
  219. $sQuery = $this->prepare("SELECT
  220. `{$this->_sTable}`.`cmt_id`,
  221. `{$this->_sTableIds}`.`id` AS `cmt_unique_id`,
  222. `{$this->_sTable}`.`cmt_parent_id`,
  223. `{$this->_sTable}`.`cmt_vparent_id`,
  224. `{$this->_sTable}`.`cmt_object_id`,
  225. `{$this->_sTable}`.`cmt_author_id`,
  226. `{$this->_sTable}`.`cmt_level`,
  227. `{$this->_sTable}`.`cmt_text`,
  228. `{$this->_sTable}`.`cmt_replies`,
  229. `{$this->_sTable}`.`cmt_time`
  230. $sFields
  231. FROM `{$this->_sTable}`
  232. LEFT JOIN `{$this->_sTableIds}` ON (`{$this->_sTable}`.`cmt_id` = `{$this->_sTableIds}`.`cmt_id` AND `{$this->_sTableIds}`.`system_id` = ?)
  233. $sJoin
  234. WHERE `{$this->_sTable}`.`cmt_object_id` = ? AND `{$this->_sTable}`.`cmt_id` = ?
  235. LIMIT 1", $this->_oMain->getSystemId(), $iId, $iCmtId);
  236. return $this->getRow($sQuery);
  237. }
  238. function getCommentSimple ($iId, $iCmtId)
  239. {
  240. $sQuery = $this->prepare("SELECT * FROM {$this->_sTable} AS `c` WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  241. return $this->getRow($sQuery);
  242. }
  243. function removeComment ($iId, $iCmtId, $iCmtParentId)
  244. {
  245. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  246. if (!$this->query($sQuery))
  247. return false;
  248. if($iCmtParentId)
  249. $this->updateRepliesCount($iCmtParentId, -1);
  250. return true;
  251. }
  252. function saveImages($iSystemId, $iCmtId, $iImageId)
  253. {
  254. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableFiles2Entries}` SET `system_id`=?, `cmt_id`=?, `image_id`=?", $iSystemId, $iCmtId, $iImageId);
  255. return (int)$this->query($sQuery) > 0;
  256. }
  257. function getFiles($iSystemId, $iCmtId, $iId = false)
  258. {
  259. $aBindings = array(
  260. 'system_id' => $iSystemId
  261. );
  262. $sJoin = "";
  263. $sWhere = " AND `tf2e`.`system_id` = :system_id ";
  264. if($iCmtId !== false) {
  265. $aBindings['cmt_id'] = $iCmtId;
  266. $sWhere .= " AND `tf2e`.`cmt_id` = :cmt_id ";
  267. }
  268. if($iId !== false) {
  269. $aBindings['cmt_object_id'] = $iId;
  270. $sWhere .= " AND `te`.`cmt_object_id` = :cmt_object_id";
  271. $sJoin .= " INNER JOIN `{$this->_sTable}` AS `te` ON (`tf2e`.`cmt_id` = `te`.`cmt_id`) ";
  272. }
  273. $sQuery = "SELECT
  274. `tf2e`.*,
  275. `tf`.`file_name` AS `file_name`,
  276. `tf`.`mime_type` AS `mime_type`,
  277. `tf`.`size` AS `size`
  278. FROM `{$this->_sTableFiles2Entries}` AS `tf2e`
  279. LEFT JOIN `{$this->_sTableFiles}` AS `tf` ON (`tf2e`.`image_id` = `tf`.`id`) " . $sJoin . "
  280. WHERE 1 " . $sWhere;
  281. return $this->getAll($sQuery, $aBindings);
  282. }
  283. function deleteImages($iSystemId, $iCmtId)
  284. {
  285. $aBindings = array(
  286. 'system_id' => $iSystemId
  287. );
  288. $sWhereAddon = "";
  289. if ($iCmtId !== false) {
  290. $aBindings['cmt_id'] = $iCmtId;
  291. $sWhereAddon = " AND `cmt_id` = :cmt_id ";
  292. }
  293. return $this->query("DELETE FROM `{$this->_sTableFiles2Entries}` WHERE `system_id` = :system_id" . $sWhereAddon, $aBindings);
  294. }
  295. function updateRepliesCount($iCmtId, $iCount)
  296. {
  297. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `cmt_replies`=`cmt_replies`+? WHERE `cmt_id`=? LIMIT 1", $iCount, $iCmtId);
  298. return $this->query($sQuery);
  299. }
  300. function deleteAuthorComments ($iAuthorId, &$aFiles = null, &$aCmtIds = null)
  301. {
  302. $aSystem = $this->_oMain->getSystemInfo();
  303. $isDelOccured = 0;
  304. $sQuery = $this->prepare("SELECT `cmt_id`, `cmt_parent_id` FROM {$this->_sTable} WHERE `cmt_author_id` = ? AND `cmt_replies` = 0", $iAuthorId);
  305. $a = $this->getAll ($sQuery);
  306. for ( reset($a) ; list (, $r) = each ($a) ; ) {
  307. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_id` = ?", $r['cmt_id']);
  308. $this->query ($sQuery);
  309. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = ?", $r['cmt_parent_id']);
  310. $this->query ($sQuery);
  311. $aFilesMore = $this->convertImagesArray($this->getFiles($aSystem['system_id'], $r['cmt_id']));
  312. $this->deleteImages($aSystem['system_id'], $r['cmt_id']);
  313. if ($aFilesMore && null !== $aFiles)
  314. $aFiles = array_merge($aFiles, $aFilesMore);
  315. if (null !== $aCmtIds)
  316. $aCmtIds[] = $r['cmt_id'];
  317. $isDelOccured = 1;
  318. }
  319. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_author_id` = 0 WHERE `cmt_author_id` = ? AND `cmt_replies` != 0", $iAuthorId);
  320. $this->query ($sQuery);
  321. if ($isDelOccured)
  322. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  323. }
  324. function deleteObjectComments ($iObjectId, &$aFilesReturn = null, &$aCmtIds = null)
  325. {
  326. $aSystem = $this->_oMain->getSystemInfo();
  327. $aFiles = $this->convertImagesArray($this->getFiles($aSystem['system_id'], false, $iObjectId));
  328. if ($aFiles) {
  329. $sQuery = $this->prepare("DELETE FROM {$this->_sTableFiles2Entries} WHERE `system_id` = ? AND `image_id` IN(" . $this->implode_escape($aFiles) . ")", $aSystem['system_id']);
  330. $this->query($sQuery);
  331. }
  332. if (null !== $aCmtIds) {
  333. $sQuery = $this->prepare("SELECT `cmt_id` FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  334. $aCmtIds = $this->getColumn ($sQuery);
  335. }
  336. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  337. $this->query ($sQuery);
  338. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  339. if (null !== $aFilesReturn)
  340. $aFilesReturn = $aFiles;
  341. }
  342. function deleteAll ($iSystemId, &$aFiles = null, &$aCmtIds = null)
  343. {
  344. // get files
  345. if (null !== $aFiles)
  346. $aFiles = $this->convertImagesArray($this->getFiles($iSystemId, false));
  347. // delete files
  348. $this->deleteImages($iSystemId, false);
  349. if (null !== $aCmtIds)
  350. $aCmtIds = $this->getColumn ("SELECT `cmt_id` FROM {$this->_sTable}");
  351. // delete comments
  352. $sQuery = $this->prepare("TRUNCATE TABLE {$this->_sTable}");
  353. $this->query ($sQuery);
  354. }
  355. function deleteCmtIds ($iSystemId, $iCmtId)
  356. {
  357. $sQuery = $this->prepare("DELETE FROM {$this->_sTableIds} WHERE `system_id` = ? AND `cmt_id` = ?", $iSystemId, $iCmtId);
  358. return $this->query ($sQuery);
  359. }
  360. function getObjectAuthorId($iId)
  361. {
  362. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldAuthor}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  363. return $this->getOne($sQuery);
  364. }
  365. function getObjectTitle($iId)
  366. {
  367. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldTitle}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  368. return $this->getOne($sQuery);
  369. }
  370. function updateTriggerTable($iId, $iCount)
  371. {
  372. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldComments}` = ? WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iCount, $iId);
  373. return $this->query($sQuery);
  374. }
  375. function getUniqId($iSystemId, $iCmtId)
  376. {
  377. $sQuery = $this->prepare("SELECT `id` FROM `{$this->_sTableIds}` WHERE `system_id` = ? AND `cmt_id` = ?", $iSystemId, $iCmtId);
  378. if ($iUniqId = $this->getOne($sQuery))
  379. return $iUniqId;
  380. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableIds}` SET `system_id` = ?, `cmt_id` = ?", $iSystemId, $iCmtId);
  381. if (!$this->query($sQuery))
  382. return false;
  383. return $this->lastId();
  384. }
  385. protected function convertImagesArray($a)
  386. {
  387. if (!$a || !is_array($a))
  388. return array();
  389. $aFiles = array ();
  390. foreach ($a as $aFile)
  391. $aFiles[] = $aFile['image_id'];
  392. return $aFiles;
  393. }
  394. }
  395. /** @} */