BxDolCmtsQuery.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident 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 'latest':
  153. if(!empty($aParams['author'])) {
  154. $aMethod['params'][1]['cmt_author_id'] = (int)$aParams['author'];
  155. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` " . (isset($aParams['others']) && (int)$aParams['others'] == 1 ? "<>" : "=") . " :cmt_author_id";
  156. }
  157. $sOrderClause = "ORDER BY `{$this->_sTable}`.`cmt_time` DESC";
  158. $sLimitClause = "";
  159. if(isset($aParams['per_page']))
  160. $sLimitClause = $this->prepareAsString("LIMIT ?, ?", $aParams['start'], $aParams['per_page']);
  161. break;
  162. }
  163. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `{$this->_sTable}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  164. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  165. }
  166. function getComment ($iId, $iCmtId)
  167. {
  168. $sFields = $sJoin = "";
  169. $oVote = $this->_oMain->getVoteObject($iCmtId);
  170. if($oVote !== false) {
  171. $aSql = $oVote->getSqlParts($this->_sTableIds, 'id');
  172. $sFields .= $aSql['fields'];
  173. $sJoin .= $aSql['join'];
  174. }
  175. $sQuery = $this->prepare("SELECT
  176. `{$this->_sTable}`.`cmt_id`,
  177. `{$this->_sTableIds}`.`id` AS `cmt_unique_id`,
  178. `{$this->_sTable}`.`cmt_parent_id`,
  179. `{$this->_sTable}`.`cmt_vparent_id`,
  180. `{$this->_sTable}`.`cmt_object_id`,
  181. `{$this->_sTable}`.`cmt_author_id`,
  182. `{$this->_sTable}`.`cmt_level`,
  183. `{$this->_sTable}`.`cmt_text`,
  184. `{$this->_sTable}`.`cmt_replies`,
  185. `{$this->_sTable}`.`cmt_time`
  186. $sFields
  187. FROM `{$this->_sTable}`
  188. LEFT JOIN `{$this->_sTableIds}` ON (`{$this->_sTable}`.`cmt_id` = `{$this->_sTableIds}`.`cmt_id` AND `{$this->_sTableIds}`.`system_id` = ?)
  189. $sJoin
  190. WHERE `{$this->_sTable}`.`cmt_object_id` = ? AND `{$this->_sTable}`.`cmt_id` = ?
  191. LIMIT 1", $this->_oMain->getSystemId(), $iId, $iCmtId);
  192. return $this->getRow($sQuery);
  193. }
  194. function getCommentSimple ($iId, $iCmtId)
  195. {
  196. $sQuery = $this->prepare("SELECT * FROM {$this->_sTable} AS `c` WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  197. return $this->getRow($sQuery);
  198. }
  199. function removeComment ($iId, $iCmtId, $iCmtParentId)
  200. {
  201. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  202. if (!$this->query($sQuery))
  203. return false;
  204. if($iCmtParentId)
  205. $this->updateRepliesCount($iCmtParentId, -1);
  206. return true;
  207. }
  208. function saveImages($iSystemId, $iCmtId, $iImageId)
  209. {
  210. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableFiles2Entries}` SET `system_id`=?, `cmt_id`=?, `image_id`=?", $iSystemId, $iCmtId, $iImageId);
  211. return (int)$this->query($sQuery) > 0;
  212. }
  213. function getFiles($iSystemId, $iCmtId, $iId = false)
  214. {
  215. $aBindings = array(
  216. 'system_id' => $iSystemId
  217. );
  218. $sJoin = "";
  219. $sWhere = " AND `tf2e`.`system_id` = :system_id ";
  220. if($iCmtId !== false) {
  221. $aBindings['cmt_id'] = $iCmtId;
  222. $sWhere .= " AND `tf2e`.`cmt_id` = :cmt_id ";
  223. }
  224. if($iId !== false) {
  225. $aBindings['cmt_object_id'] = $iId;
  226. $sWhere .= " AND `te`.`cmt_object_id` = :cmt_object_id";
  227. $sJoin .= " INNER JOIN `{$this->_sTable}` AS `te` ON (`tf2e`.`cmt_id` = `te`.`cmt_id`) ";
  228. }
  229. $sQuery = "SELECT
  230. `tf2e`.*,
  231. `tf`.`file_name` AS `file_name`,
  232. `tf`.`mime_type` AS `mime_type`
  233. FROM `{$this->_sTableFiles2Entries}` AS `tf2e`
  234. LEFT JOIN `{$this->_sTableFiles}` AS `tf` ON (`tf2e`.`image_id` = `tf`.`id`) " . $sJoin . "
  235. WHERE 1 " . $sWhere;
  236. return $this->getAll($sQuery, $aBindings);
  237. }
  238. function deleteImages($iSystemId, $iCmtId)
  239. {
  240. $aBindings = array(
  241. 'system_id' => $iSystemId
  242. );
  243. $sWhereAddon = "";
  244. if ($iCmtId !== false) {
  245. $aBindings['cmt_id'] = $iCmtId;
  246. $sWhereAddon = " AND `cmt_id` = :cmt_id ";
  247. }
  248. return $this->query("DELETE FROM `{$this->_sTableFiles2Entries}` WHERE `system_id` = :system_id" . $sWhereAddon, $aBindings);
  249. }
  250. function updateRepliesCount($iCmtId, $iCount)
  251. {
  252. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `cmt_replies`=`cmt_replies`+? WHERE `cmt_id`=? LIMIT 1", $iCount, $iCmtId);
  253. return $this->query($sQuery);
  254. }
  255. function deleteAuthorComments ($iAuthorId, &$aFiles = null, &$aCmtIds = null)
  256. {
  257. $aSystem = $this->_oMain->getSystemInfo();
  258. $isDelOccured = 0;
  259. $sQuery = $this->prepare("SELECT `cmt_id`, `cmt_parent_id` FROM {$this->_sTable} WHERE `cmt_author_id` = ? AND `cmt_replies` = 0", $iAuthorId);
  260. $a = $this->getAll ($sQuery);
  261. for ( reset($a) ; list (, $r) = each ($a) ; ) {
  262. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_id` = ?", $r['cmt_id']);
  263. $this->query ($sQuery);
  264. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = ?", $r['cmt_parent_id']);
  265. $this->query ($sQuery);
  266. $aFilesMore = $this->convertImagesArray($this->getFiles($aSystem['system_id'], $r['cmt_id']));
  267. $this->deleteImages($aSystem['system_id'], $r['cmt_id']);
  268. if ($aFilesMore && null !== $aFiles)
  269. $aFiles = array_merge($aFiles, $aFilesMore);
  270. if (null !== $aCmtIds)
  271. $aCmtIds[] = $r['cmt_id'];
  272. $isDelOccured = 1;
  273. }
  274. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_author_id` = 0 WHERE `cmt_author_id` = ? AND `cmt_replies` != 0", $iAuthorId);
  275. $this->query ($sQuery);
  276. if ($isDelOccured)
  277. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  278. }
  279. function deleteObjectComments ($iObjectId, &$aFilesReturn = null, &$aCmtIds = null)
  280. {
  281. $aSystem = $this->_oMain->getSystemInfo();
  282. $aFiles = $this->convertImagesArray($this->getFiles($aSystem['system_id'], false, $iObjectId));
  283. if ($aFiles) {
  284. $sQuery = $this->prepare("DELETE FROM {$this->_sTableFiles2Entries} WHERE `system_id` = ? AND `image_id` IN(" . $this->implode_escape($aFiles) . ")", $aSystem['system_id']);
  285. $this->query($sQuery);
  286. }
  287. if (null !== $aCmtIds) {
  288. $sQuery = $this->prepare("SELECT `cmt_id` FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  289. $aCmtIds = $this->getColumn ($sQuery);
  290. }
  291. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  292. $this->query ($sQuery);
  293. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  294. if (null !== $aFilesReturn)
  295. $aFilesReturn = $aFiles;
  296. }
  297. function deleteAll ($iSystemId, &$aFiles = null, &$aCmtIds = null)
  298. {
  299. // get files
  300. if (null !== $aFiles)
  301. $aFiles = $this->convertImagesArray($this->getFiles($iSystemId, false));
  302. // delete files
  303. $this->deleteImages($iSystemId, false);
  304. if (null !== $aCmtIds)
  305. $aCmtIds = $this->getColumn ("SELECT `cmt_id` FROM {$this->_sTable}");
  306. // delete comments
  307. $sQuery = $this->prepare("TRUNCATE TABLE {$this->_sTable}");
  308. $this->query ($sQuery);
  309. }
  310. function deleteCmtIds ($iSystemId, $iCmtId)
  311. {
  312. $sQuery = $this->prepare("DELETE FROM {$this->_sTableIds} WHERE `system_id` = ? AND `cmt_id` = ?", $iSystemId, $iCmtId);
  313. return $this->query ($sQuery);
  314. }
  315. function getObjectAuthorId($iId)
  316. {
  317. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldAuthor}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  318. return $this->getOne($sQuery);
  319. }
  320. function getObjectTitle($iId)
  321. {
  322. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldTitle}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  323. return $this->getOne($sQuery);
  324. }
  325. function updateTriggerTable($iId, $iCount)
  326. {
  327. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldComments}` = ? WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iCount, $iId);
  328. return $this->query($sQuery);
  329. }
  330. function getUniqId($iSystemId, $iCmtId)
  331. {
  332. $sQuery = $this->prepare("SELECT `id` FROM `{$this->_sTableIds}` WHERE `system_id` = ? AND `cmt_id` = ?", $iSystemId, $iCmtId);
  333. if ($iUniqId = $this->getOne($sQuery))
  334. return $iUniqId;
  335. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableIds}` SET `system_id` = ?, `cmt_id` = ?", $iSystemId, $iCmtId);
  336. if (!$this->query($sQuery))
  337. return false;
  338. return $this->lastId();
  339. }
  340. protected function convertImagesArray($a)
  341. {
  342. if (!$a || !is_array($a))
  343. return array();
  344. $aFiles = array ();
  345. foreach ($a as $aFile)
  346. $aFiles[] = $aFile['image_id'];
  347. return $aFiles;
  348. }
  349. }
  350. /** @} */