BxDolCmtsQuery.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. bx_import('BxDolDb');
  9. /**
  10. * @see BxDolCmts
  11. */
  12. class BxDolCmtsQuery extends BxDolDb
  13. {
  14. var $_aSystem; ///< current voting system
  15. var $_sTable;
  16. var $_sTableTrack;
  17. function BxDolCmtsQuery(&$aSystem)
  18. {
  19. $this->_aSystem = &$aSystem;
  20. $this->_sTable = $this->_aSystem['table_cmts'];
  21. $this->_sTableTrack = $this->_aSystem['table_track'];
  22. parent::BxDolDb();
  23. }
  24. function getTableName ()
  25. {
  26. return $this->_sTable;
  27. }
  28. function getCommentsCount ($iId, $iCmtParentId = 0) {
  29. $sQuery = $this->prepare("SELECT COUNT(*) FROM `{$this->_sTable}` WHERE `cmt_object_id` = ? AND `cmt_parent_id` = ?", $iId, $iCmtParentId);
  30. return (int)$this->getOne($sQuery);
  31. }
  32. function getComments ($iId, $iCmtParentId = 0, $iAuthorId = 0, $sCmtOrder = 'ASC', $iStart = 0, $iCount = -1)
  33. {
  34. global $sHomeUrl;
  35. $iTimestamp = time();
  36. $sFields = "'' AS `cmt_rated`,";
  37. $sJoin = '';
  38. if ($iAuthorId) {
  39. $sFields = '`r`.`cmt_rate` AS `cmt_rated`,';
  40. $sJoin = $this->prepare("LEFT JOIN {$this->_sTableTrack} AS `r` ON (`r`.`cmt_system_id` = ? AND `r`.`cmt_id` = `c`.`cmt_id` AND `r`.`cmt_rate_author_id` = ?)", $this->_aSystem['system_id'], $iAuthorId);
  41. }
  42. $sLimit = $iCount != -1 ? $this->prepare(' LIMIT ?, ?', (int)$iStart, (int)$iCount) : '';
  43. $sQuery = $this->prepare("SELECT
  44. $sFields
  45. `c`.`cmt_id`,
  46. `c`.`cmt_parent_id`,
  47. `c`.`cmt_object_id`,
  48. `c`.`cmt_author_id`,
  49. `c`.`cmt_text`,
  50. `c`.`cmt_mood`,
  51. `c`.`cmt_rate`,
  52. `c`.`cmt_rate_count`,
  53. `c`.`cmt_replies`,
  54. (? - UNIX_TIMESTAMP(`c`.`cmt_time`)) AS `cmt_secs_ago`,
  55. `p`.`id` AS `cmt_author_name`
  56. FROM {$this->_sTable} AS `c`
  57. LEFT JOIN `sys_profiles` AS `p` ON (`p`.`id` = `c`.`cmt_author_id`)
  58. $sJoin
  59. WHERE `c`.`cmt_object_id` = ? AND `c`.`cmt_parent_id` = ?
  60. ORDER BY `c`.`cmt_time` " . (strtoupper($sCmtOrder) == 'ASC' ? 'ASC' : 'DESC') . $sLimit, $iTimestamp, $iId, $iCmtParentId);
  61. $a = $this->getAll($sQuery);
  62. for(reset($a) ; list ($k) = each ($a) ; ) {
  63. $a[$k]['cmt_text'] = str_replace("[ray_url]", $sHomeUrl, $a[$k]['cmt_text']);
  64. $a[$k]['cmt_ago'] = _format_when ($a[$k]['cmt_secs_ago']);
  65. }
  66. return $a;
  67. }
  68. function getComment ($iId, $iCmtId, $iAuthorId = 0)
  69. {
  70. global $sHomeUrl;
  71. $iTimestamp = time();
  72. $sFields = "'' AS `cmt_rated`,";
  73. $sJoin = '';
  74. if ($iAuthorId) {
  75. $sFields = '`r`.`cmt_rate` AS `cmt_rated`,';
  76. $sJoin = $this->prepare("LEFT JOIN {$this->_sTableTrack} AS `r` ON (`r`.`cmt_system_id` = ? AND `r`.`cmt_id` = `c`.`cmt_id` AND `r`.`cmt_rate_author_id` = ?)", $this->_aSystem['system_id'], $iAuthorId);
  77. }
  78. $sQuery = $this->prepare("SELECT
  79. $sFields
  80. `c`.`cmt_id`,
  81. `c`.`cmt_parent_id`,
  82. `c`.`cmt_object_id`,
  83. `c`.`cmt_author_id`,
  84. `c`.`cmt_text`,
  85. `c`.`cmt_mood`,
  86. `c`.`cmt_rate`,
  87. `c`.`cmt_rate_count`,
  88. `c`.`cmt_replies`,
  89. ($iTimestamp - UNIX_TIMESTAMP(`c`.`cmt_time`)) AS `cmt_secs_ago`,
  90. `p`.`NickName` AS `cmt_author_name`
  91. FROM {$this->_sTable} AS `c`
  92. LEFT JOIN `Profiles` AS `p` ON (`p`.`ID` = `c`.`cmt_author_id`)
  93. $sJoin
  94. WHERE `c`.`cmt_object_id` = ? AND `c`.`cmt_id` = ?
  95. LIMIT 1", $iId, $iCmtId);
  96. $aComment = $this->getRow($sQuery);
  97. $aComment['cmt_text'] = str_replace("[ray_url]", $sHomeUrl, $aComment['cmt_text']);
  98. $aComment['cmt_ago'] = _format_when($aComment['cmt_secs_ago']);
  99. return $aComment;
  100. }
  101. function getCommentSimple ($iId, $iCmtId)
  102. {
  103. $iTimestamp = time();
  104. $sQuery = $this->prepare("
  105. SELECT
  106. *, ($iTimestamp - UNIX_TIMESTAMP(`c`.`cmt_time`)) AS `cmt_secs_ago`
  107. FROM {$this->_sTable} AS `c`
  108. WHERE `cmt_object_id` = ? AND `cmt_id` = ?
  109. LIMIT 1", $iId, $iCmtId);
  110. return $this->getRow($sQuery);
  111. }
  112. function addComment ($iId, $iCmtParentId, $iAuthorId, $sText)
  113. {
  114. $sQuery = $this->prepare("INSERT INTO {$this->_sTable} SET
  115. `cmt_parent_id` = ?,
  116. `cmt_object_id` = ?,
  117. `cmt_author_id` = ?,
  118. `cmt_text` = ?,
  119. `cmt_time` = NOW()", $iCmtParentId, $iId, $iAuthorId, $sText);
  120. if (!$this->query($sQuery))
  121. {
  122. return false;
  123. }
  124. $iRet = $this->lastId();
  125. if ($iCmtParentId) {
  126. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` + 1 WHERE `cmt_id` = ? LIMIT 1", $iCmtParentId);
  127. $this->query ($sQuery);
  128. }
  129. return $iRet;
  130. }
  131. function removeComment ($iId, $iCmtId, $iCmtParentId)
  132. {
  133. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  134. if (!$this->query($sQuery))
  135. return false;
  136. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = ? LIMIT 1", $iCmtParentId);
  137. $this->query ($sQuery);
  138. return true;
  139. }
  140. function updateComment ($iId, $iCmtId, $sText)
  141. {
  142. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_text` = ? WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $sText, $iId, $iCmtId);
  143. return $this->query($sQuery);
  144. }
  145. function rateComment ($iSystemId, $iCmtId, $iRate, $iAuthorId, $sAuthorIp)
  146. {
  147. $iTimestamp = time();
  148. $sQuery = $this->prepare("INSERT IGNORE INTO {$this->_sTableTrack} SET
  149. `cmt_system_id` = ?,
  150. `cmt_id` = ?,
  151. `cmt_rate` = ?,
  152. `cmt_rate_author_id` = ?,
  153. `cmt_rate_author_nip` = INET_ATON(?),
  154. `cmt_rate_ts` = ?", $iSystemId, $iCmtId, $iRate, $iAuthorId, $sAuthorIp, $iTimestamp);
  155. if ($this->query($sQuery))
  156. {
  157. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_rate` = `cmt_rate` + ?, `cmt_rate_count` = `cmt_rate_count` + 1 WHERE `cmt_id` = ? LIMIT 1", $iRate, $iCmtId);
  158. $this->query($sQuery);
  159. return true;
  160. }
  161. return false;
  162. }
  163. function deleteAuthorComments ($iAuthorId)
  164. {
  165. $isDelOccured = 0;
  166. $sQuery = $this->prepare("SELECT `cmt_id`, `cmt_parent_id` FROM {$this->_sTable} WHERE `cmt_author_id` = ? AND `cmt_replies` = 0", $iAuthorId);
  167. $a = $this->getAll ($sQuery);
  168. for ( reset($a) ; list (, $r) = each ($a) ; )
  169. {
  170. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_id` = ?", $r['cmt_id']);
  171. $this->query ($sQuery);
  172. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = ?", $r['cmt_parent_id']);
  173. $this->query ($sQuery);
  174. $isDelOccured = 1;
  175. }
  176. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_author_id` = 0 WHERE `cmt_author_id` = ? AND `cmt_replies` != 0", $iAuthorId);
  177. $this->query ($sQuery);
  178. if ($isDelOccured)
  179. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  180. }
  181. function deleteObjectComments ($iObjectId)
  182. {
  183. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  184. $this->query ($sQuery);
  185. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  186. }
  187. function getObjectCommentsCount ($iObjectId, $iParentId = -1)
  188. {
  189. $sWhere = '';
  190. if ($iParentId != -1)
  191. $sWhere = $this->prepare(" AND `cmt_parent_id` = ?", $iParentId);
  192. $sQuery = $this->prepare("SELECT COUNT(*) FROM `" . $this->_sTable ."` WHERE `cmt_object_id` = ? " . $sWhere, $iObjectId);
  193. return $this->getOne ($sQuery);
  194. }
  195. function updateTriggerTable($iId, $iCount)
  196. {
  197. $sQuery = $this->prepare("UPDATE `{$this->_aSystem['trigger_table']}` SET `{$this->_aSystem['trigger_field_comments']}` = ? WHERE `{$this->_aSystem['trigger_field_id']}` = ? LIMIT 1", $iCount, $iId);
  198. return $this->query($sQuery);
  199. }
  200. function maintenance() {
  201. $iTimestamp = time();
  202. $iDeletedRecords = $this->query("DELETE FROM {$this->_sTableTrack} WHERE `cmt_rate_ts` < ($iTimestamp - " . (int)BX_OLD_CMT_VOTES . ")");
  203. if ($iDeletedRecords)
  204. $this->query("OPTIMIZE TABLE {$this->_sTableTrack}");
  205. return $iDeletedRecords;
  206. }
  207. }