BxDolCmtsQuery.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 getComments ($iId, $iCmtParentId = 0, $iAuthorId = 0, $sCmtOrder = 'ASC', $iStart = 0, $iCount = -1)
  29. {
  30. global $sHomeUrl;
  31. $iTimestamp = time();
  32. $sFields = "'' AS `cmt_rated`,";
  33. $sJoin = '';
  34. if ($iAuthorId) {
  35. $sFields = '`r`.`cmt_rate` AS `cmt_rated`,';
  36. $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);
  37. }
  38. $sQuery = $this->prepare("SELECT
  39. $sFields
  40. `c`.`cmt_id`,
  41. `c`.`cmt_parent_id`,
  42. `c`.`cmt_object_id`,
  43. `c`.`cmt_author_id`,
  44. `c`.`cmt_text`,
  45. `c`.`cmt_mood`,
  46. `c`.`cmt_rate`,
  47. `c`.`cmt_rate_count`,
  48. `c`.`cmt_replies`,
  49. ($iTimestamp - UNIX_TIMESTAMP(`c`.`cmt_time`)) AS `cmt_secs_ago`,
  50. `p`.`id` AS `cmt_author_name`
  51. FROM {$this->_sTable} AS `c`
  52. LEFT JOIN `sys_profiles` AS `p` ON (`p`.`id` = `c`.`cmt_author_id`)
  53. $sJoin
  54. WHERE `c`.`cmt_object_id` = ? AND `c`.`cmt_parent_id` = ?
  55. ORDER BY `c`.`cmt_time` " . (strtoupper($sCmtOrder) == 'ASC' ? 'ASC' : 'DESC') . ($iCount != -1 ? ' LIMIT ' . (int)$iStart . ', ' . (int)$iCount : ''), $iId, $iCmtParentId);
  56. $a = $this->getAll($sQuery);
  57. for(reset($a) ; list ($k) = each ($a) ; ) {
  58. $a[$k]['cmt_text'] = str_replace("[ray_url]", $sHomeUrl, $a[$k]['cmt_text']);
  59. $a[$k]['cmt_ago'] = _format_when ($a[$k]['cmt_secs_ago']);
  60. }
  61. return $a;
  62. }
  63. function getComment ($iId, $iCmtId, $iAuthorId = 0)
  64. {
  65. global $sHomeUrl;
  66. $iTimestamp = time();
  67. $sFields = "'' AS `cmt_rated`,";
  68. $sJoin = '';
  69. if ($iAuthorId) {
  70. $sFields = '`r`.`cmt_rate` AS `cmt_rated`,';
  71. $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);
  72. }
  73. $sQuery = $this->prepare("SELECT
  74. $sFields
  75. `c`.`cmt_id`,
  76. `c`.`cmt_parent_id`,
  77. `c`.`cmt_object_id`,
  78. `c`.`cmt_author_id`,
  79. `c`.`cmt_text`,
  80. `c`.`cmt_mood`,
  81. `c`.`cmt_rate`,
  82. `c`.`cmt_rate_count`,
  83. `c`.`cmt_replies`,
  84. ($iTimestamp - UNIX_TIMESTAMP(`c`.`cmt_time`)) AS `cmt_secs_ago`,
  85. `p`.`NickName` AS `cmt_author_name`
  86. FROM {$this->_sTable} AS `c`
  87. LEFT JOIN `Profiles` AS `p` ON (`p`.`ID` = `c`.`cmt_author_id`)
  88. $sJoin
  89. WHERE `c`.`cmt_object_id` = ? AND `c`.`cmt_id` = ?
  90. LIMIT 1", $iId, $iCmtId);
  91. $aComment = $this->getRow($sQuery);
  92. $aComment['cmt_text'] = str_replace("[ray_url]", $sHomeUrl, $aComment['cmt_text']);
  93. $aComment['cmt_ago'] = _format_when($aComment['cmt_secs_ago']);
  94. return $aComment;
  95. }
  96. function getCommentSimple ($iId, $iCmtId)
  97. {
  98. $iTimestamp = time();
  99. $sQuery = $this->prepare("
  100. SELECT
  101. *, ($iTimestamp - UNIX_TIMESTAMP(`c`.`cmt_time`)) AS `cmt_secs_ago`
  102. FROM {$this->_sTable} AS `c`
  103. WHERE `cmt_object_id` = ? AND `cmt_id` = ?
  104. LIMIT 1", $iId, $iCmtId);
  105. return $this->getRow($sQuery);
  106. }
  107. function addComment ($iId, $iCmtParentId, $iAuthorId, $sText, $iMood)
  108. {
  109. $sQuery = $this->prepare("INSERT INTO {$this->_sTable} SET
  110. `cmt_parent_id` = ?,
  111. `cmt_object_id` = ?,
  112. `cmt_author_id` = ?,
  113. `cmt_text` = ?,
  114. `cmt_mood` = ?,
  115. `cmt_time` = NOW()", $iCmtParentId, $iId, $iAuthorId, $sText, $iMood);
  116. if (!$this->query($sQuery))
  117. {
  118. return false;
  119. }
  120. $iRet = $this->lastId();
  121. if ($iCmtParentId) {
  122. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` + 1 WHERE `cmt_id` = ? LIMIT 1", $iCmtParentId);
  123. $this->query ($sQuery);
  124. }
  125. return $iRet;
  126. }
  127. function removeComment ($iId, $iCmtId, $iCmtParentId)
  128. {
  129. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  130. if (!$this->query($sQuery))
  131. return false;
  132. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = ? LIMIT 1", $iCmtParentId);
  133. $this->query ($sQuery);
  134. return true;
  135. }
  136. function updateComment ($iId, $iCmtId, $sText, $iMood)
  137. {
  138. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_text` = ?, `cmt_mood` = ? WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $sText, $iMood, $iId, $iCmtId);
  139. return $this->query($sQuery);
  140. }
  141. function rateComment ($iSystemId, $iCmtId, $iRate, $iAuthorId, $sAuthorIp)
  142. {
  143. $iTimestamp = time();
  144. $sQuery = $this->prepare("INSERT IGNORE INTO {$this->_sTableTrack} SET
  145. `cmt_system_id` = ?,
  146. `cmt_id` = ?,
  147. `cmt_rate` = ?,
  148. `cmt_rate_author_id` = ?,
  149. `cmt_rate_author_nip` = INET_ATON(?),
  150. `cmt_rate_ts` = ?", $iSystemId, $iCmtId, $iRate, $iAuthorId, $sAuthorIp, $iTimestamp);
  151. if ($this->query($sQuery))
  152. {
  153. $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);
  154. $this->query($sQuery);
  155. return true;
  156. }
  157. return false;
  158. }
  159. function deleteAuthorComments ($iAuthorId)
  160. {
  161. $isDelOccured = 0;
  162. $sQuery = $this->prepare("SELECT `cmt_id`, `cmt_parent_id` FROM {$this->_sTable} WHERE `cmt_author_id` = ? AND `cmt_replies` = 0", $iAuthorId);
  163. $a = $this->getAll ($sQuery);
  164. for ( reset($a) ; list (, $r) = each ($a) ; )
  165. {
  166. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_id` = ?", $r['cmt_id']);
  167. $this->query ($sQuery);
  168. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = ?", $r['cmt_parent_id']);
  169. $this->query ($sQuery);
  170. $isDelOccured = 1;
  171. }
  172. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_author_id` = 0 WHERE `cmt_author_id` = ? AND `cmt_replies` != 0", $iAuthorId);
  173. $this->query ($sQuery);
  174. if ($isDelOccured)
  175. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  176. }
  177. function deleteObjectComments ($iObjectId)
  178. {
  179. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  180. $this->query ($sQuery);
  181. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  182. }
  183. function getObjectCommentsCount ($iObjectId, $iParentId = -1)
  184. {
  185. $sWhere = '';
  186. if ($iParentId != -1)
  187. $sWhere = $this->prepare(" AND `cmt_parent_id` = ?", $iParentId);
  188. $sQuery = $this->prepare("SELECT COUNT(*) FROM `" . $this->_sTable ."` WHERE `cmt_object_id` = ? " . $sWhere, $iObjectId);
  189. return $this->getOne ($sQuery);
  190. }
  191. function updateTriggerTable($iId, $iCount)
  192. {
  193. $sQuery = $this->prepare("UPDATE `{$this->_aSystem['trigger_table']}` SET `{$this->_aSystem['trigger_field_comments']}` = ? WHERE `{$this->_aSystem['trigger_field_id']}` = ? LIMIT 1", $iCount, $iId);
  194. return $this->query($sQuery);
  195. }
  196. function maintenance() {
  197. $iTimestamp = time();
  198. $iDeletedRecords = $this->query("DELETE FROM {$this->_sTableTrack} WHERE `cmt_rate_ts` < ($iTimestamp - " . (int)BX_OLD_CMT_VOTES . ")");
  199. if ($iDeletedRecords)
  200. $this->query("OPTIMIZE TABLE {$this->_sTableTrack}");
  201. return $iDeletedRecords;
  202. }
  203. }