BxDolCmtsQuery.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. protected $_oMain;
  15. protected $_sTable;
  16. protected $_sTableTrack;
  17. protected $_sTriggerTable;
  18. protected $_sTriggerFieldId;
  19. protected $_sTriggerFieldTitle;
  20. protected $_sTriggerFieldComments;
  21. protected $_sTableImages;
  22. protected $_sTableImages2Entries;
  23. function BxDolCmtsQuery(&$oMain)
  24. {
  25. $this->_oMain = $oMain;
  26. $aSystem = $this->_oMain->getSystemInfo();
  27. $this->_sTable = $aSystem['table_cmts'];
  28. $this->_sTableTrack = $aSystem['table_track'];
  29. $this->_sTriggerTable = $aSystem['trigger_table'];
  30. $this->_sTriggerFieldId = $aSystem['trigger_field_id'];
  31. $this->_sTriggerFieldTitle = $aSystem['trigger_field_title'];
  32. $this->_sTriggerFieldComments = $aSystem['trigger_field_comments'];
  33. $this->_sTableImages = $aSystem['table_images'];
  34. $this->_sTableImages2Entries = $aSystem['table_images2entries'];
  35. parent::BxDolDb();
  36. }
  37. function getTableName ()
  38. {
  39. return $this->_sTable;
  40. }
  41. function getCommentsCount ($iId, $iCmtVParentId = 0) {
  42. $sWhereParent = '';
  43. if((int)$iCmtVParentId >= 0)
  44. $sWhereParent = $this->prepare(" AND `cmt_vparent_id` = ?", $iCmtVParentId);
  45. $sQuery = $this->prepare("SELECT COUNT(*) FROM `{$this->_sTable}` WHERE `cmt_object_id` = ?" . $sWhereParent, $iId);
  46. return (int)$this->getOne($sQuery);
  47. }
  48. function getComments ($iId, $iCmtVParentId = 0, $iAuthorId = 0, $aOrder = array(), $iStart = 0, $iCount = -1)
  49. {
  50. global $sHomeUrl;
  51. $iTimestamp = time();
  52. $sFields = "'' AS `cmt_rated`,";
  53. $sJoin = "";
  54. if ($iAuthorId) {
  55. $sFields = "`r`.`cmt_rate` AS `cmt_rated`,";
  56. $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->_oMain->getSystemId(), $iAuthorId);
  57. }
  58. $sWhereParent = '';
  59. if((int)$iCmtVParentId >= 0)
  60. $sWhereParent = $this->prepare(" AND `c`.`cmt_vparent_id` = ?", $iCmtVParentId);
  61. $sOder = " ORDER BY `c`.`cmt_time` ASC";
  62. if(isset($aOrder['by']) && isset($aOrder['way'])) {
  63. $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);
  64. switch($aOrder['by']) {
  65. case BX_CMT_ORDER_BY_DATE:
  66. $sOder = " ORDER BY `c`.`cmt_time` " . $aOrder['way'];
  67. break;
  68. case BX_CMT_ORDER_BY_POPULAR:
  69. $sOder = " ORDER BY `c`.`cmt_rate` " . $aOrder['way'];
  70. break;
  71. case BX_CMT_ORDER_BY_CONNECTION:
  72. $sFields .= " IF(NOT ISNULL(`tcc`.`id`), 1, 0) AS `cmt_author_contact`,";
  73. $sFields .= " IF(NOT ISNULL(`tcf`.`id`), 1, 0) AS `cmt_author_friend`,";
  74. $sJoin .= $this->prepare(" LEFT JOIN `sys_profiles_conn_contacts` AS `tcc` ON ((`c`.`cmt_author_id`=`tcc`.`initiator` AND `tcc`.`content`=?) OR (`c`.`cmt_author_id`=`tcc`.`content` AND `tcc`.`initiator`=?))", $iAuthorId, $iAuthorId);
  75. $sJoin .= $this->prepare(" LEFT JOIN `sys_profiles_conn_friends` AS `tcf` ON ((`c`.`cmt_author_id`=`tcf`.`initiator` AND `tcf`.`content`=?) OR (`c`.`cmt_author_id`=`tcf`.`content` AND `tcf`.`initiator`=?))", $iAuthorId, $iAuthorId);
  76. $sOder = " ORDER BY `cmt_author_friend` " . $aOrder['way'] . ",`cmt_author_contact` " . $aOrder['way'] . ",`c`.`cmt_time` ASC";
  77. break;
  78. }
  79. }
  80. $sLimit = $iCount != -1 ? $this->prepare(" LIMIT ?, ?", (int)$iStart, (int)$iCount) : '';
  81. $sQuery = $this->prepare("SELECT
  82. $sFields
  83. `c`.`cmt_id`,
  84. `c`.`cmt_parent_id`,
  85. `c`.`cmt_vparent_id`,
  86. `c`.`cmt_object_id`,
  87. `c`.`cmt_author_id`,
  88. `c`.`cmt_level`,
  89. `c`.`cmt_text`,
  90. `c`.`cmt_rate`,
  91. `c`.`cmt_rate_count`,
  92. `c`.`cmt_replies`,
  93. (? - `c`.`cmt_time`) AS `cmt_secs_ago`
  94. FROM `{$this->_sTable}` AS `c`
  95. $sJoin
  96. WHERE `c`.`cmt_object_id` = ?" . $sWhereParent . $sOder . $sLimit, $iTimestamp, $iId);
  97. $a = $this->getAll($sQuery);
  98. for(reset($a) ; list ($k) = each ($a) ; ) {
  99. $a[$k]['cmt_text'] = str_replace("[ray_url]", $sHomeUrl, $a[$k]['cmt_text']);
  100. $a[$k]['cmt_ago'] = _format_when ($a[$k]['cmt_secs_ago']);
  101. }
  102. return $a;
  103. }
  104. function getComment ($iId, $iCmtId, $iAuthorId = 0)
  105. {
  106. global $sHomeUrl;
  107. $iTimestamp = time();
  108. $sFields = "'' AS `cmt_rated`,";
  109. $sJoin = '';
  110. if ($iAuthorId) {
  111. $sFields = '`r`.`cmt_rate` AS `cmt_rated`,';
  112. $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->_oMain->getSystemId(), $iAuthorId);
  113. }
  114. $sQuery = $this->prepare("SELECT
  115. $sFields
  116. `c`.`cmt_id`,
  117. `c`.`cmt_parent_id`,
  118. `c`.`cmt_vparent_id`,
  119. `c`.`cmt_object_id`,
  120. `c`.`cmt_author_id`,
  121. `c`.`cmt_level`,
  122. `c`.`cmt_text`,
  123. `c`.`cmt_rate`,
  124. `c`.`cmt_rate_count`,
  125. `c`.`cmt_replies`,
  126. ($iTimestamp - `c`.`cmt_time`) AS `cmt_secs_ago`
  127. FROM {$this->_sTable} AS `c`
  128. $sJoin
  129. WHERE `c`.`cmt_object_id` = ? AND `c`.`cmt_id` = ?
  130. LIMIT 1", $iId, $iCmtId);
  131. $aComment = $this->getRow($sQuery);
  132. $aComment['cmt_text'] = str_replace("[ray_url]", $sHomeUrl, $aComment['cmt_text']);
  133. $aComment['cmt_ago'] = _format_when($aComment['cmt_secs_ago']);
  134. return $aComment;
  135. }
  136. function getCommentSimple ($iId, $iCmtId)
  137. {
  138. $iTimestamp = time();
  139. $sQuery = $this->prepare("
  140. SELECT
  141. *, ($iTimestamp - `c`.`cmt_time`) AS `cmt_secs_ago`
  142. FROM {$this->_sTable} AS `c`
  143. WHERE `cmt_object_id` = ? AND `cmt_id` = ?
  144. LIMIT 1", $iId, $iCmtId);
  145. return $this->getRow($sQuery);
  146. }
  147. function removeComment ($iId, $iCmtId, $iCmtParentId)
  148. {
  149. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  150. if (!$this->query($sQuery))
  151. return false;
  152. if($iCmtParentId)
  153. $this->updateRepliesCount($iCmtParentId, -1);
  154. return true;
  155. }
  156. function saveImages($iSystemId, $iCmtId, $iImageId)
  157. {
  158. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableImages2Entries}` SET `system_id`=?, `cmt_id`=?, `image_id`=?", $iSystemId, $iCmtId, $iImageId);
  159. return (int)$this->query($sQuery) > 0;
  160. }
  161. function updateRepliesCount($iCmtId, $iCount)
  162. {
  163. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `cmt_replies`=`cmt_replies`+? WHERE `cmt_id`=? LIMIT 1", $iCount, $iCmtId);
  164. return $this->query($sQuery);
  165. }
  166. function rateComment ($iSystemId, $iCmtId, $iRate, $iAuthorId, $sAuthorIp)
  167. {
  168. $iCount = 0;
  169. $iTimestamp = time();
  170. if($iRate == BX_CMT_RATE_VALUE_PLUS) {
  171. $iCount = 1;
  172. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableTrack}` SET
  173. `cmt_system_id` = ?,
  174. `cmt_id` = ?,
  175. `cmt_rate` = ?,
  176. `cmt_rate_author_id` = ?,
  177. `cmt_rate_author_nip` = INET_ATON(?),
  178. `cmt_rate_ts` = ?", $iSystemId, $iCmtId, $iRate, $iAuthorId, $sAuthorIp, $iTimestamp);
  179. }
  180. else if($iRate == BX_CMT_RATE_VALUE_MINUS) {
  181. $iCount = -1;
  182. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableTrack}` WHERE
  183. `cmt_system_id` = ? AND
  184. `cmt_id` = ? AND
  185. `cmt_rate_author_id` = ?", $iSystemId, $iCmtId, $iAuthorId);
  186. }
  187. if ($this->query($sQuery)) {
  188. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET
  189. `cmt_rate` = `cmt_rate` + ?,
  190. `cmt_rate_count` = `cmt_rate_count` + ?
  191. WHERE `cmt_id` = ?
  192. LIMIT 1", $iRate, $iCount, $iCmtId);
  193. $this->query($sQuery);
  194. return true;
  195. }
  196. return false;
  197. }
  198. function getRatedBy ($iSystemId, $iCmtId)
  199. {
  200. $sQuery = $this->prepare("SELECT `cmt_rate_author_id` FROM `{$this->_sTableTrack}` WHERE `cmt_system_id`=? AND `cmt_id`=?", $iSystemId, $iCmtId);
  201. return $this->getColumn($sQuery);
  202. }
  203. function deleteAuthorComments ($iAuthorId)
  204. {
  205. $isDelOccured = 0;
  206. $sQuery = $this->prepare("SELECT `cmt_id`, `cmt_parent_id` FROM {$this->_sTable} WHERE `cmt_author_id` = ? AND `cmt_replies` = 0", $iAuthorId);
  207. $a = $this->getAll ($sQuery);
  208. for ( reset($a) ; list (, $r) = each ($a) ; )
  209. {
  210. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_id` = ?", $r['cmt_id']);
  211. $this->query ($sQuery);
  212. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = ?", $r['cmt_parent_id']);
  213. $this->query ($sQuery);
  214. $isDelOccured = 1;
  215. }
  216. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_author_id` = 0 WHERE `cmt_author_id` = ? AND `cmt_replies` != 0", $iAuthorId);
  217. $this->query ($sQuery);
  218. if ($isDelOccured)
  219. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  220. }
  221. function deleteObjectComments ($iObjectId)
  222. {
  223. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  224. $this->query ($sQuery);
  225. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  226. }
  227. function getObjectCommentsCount ($iObjectId, $iParentId = -1)
  228. {
  229. $sWhere = '';
  230. if ($iParentId != -1)
  231. $sWhere = $this->prepare(" AND `cmt_parent_id` = ?", $iParentId);
  232. $sQuery = $this->prepare("SELECT COUNT(*) FROM `" . $this->_sTable ."` WHERE `cmt_object_id` = ? " . $sWhere, $iObjectId);
  233. return $this->getOne ($sQuery);
  234. }
  235. function getObjectTitle($iId)
  236. {
  237. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldTitle}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  238. return $this->getOne($sQuery);
  239. }
  240. function updateTriggerTable($iId, $iCount)
  241. {
  242. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldComments}` = ? WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iCount, $iId);
  243. return $this->query($sQuery);
  244. }
  245. function maintenance()
  246. {
  247. $iTimestamp = time();
  248. $iDeletedRecords = $this->query("DELETE FROM {$this->_sTableTrack} WHERE `cmt_rate_ts` < ($iTimestamp - " . (int)BX_OLD_CMT_VOTES . ")");
  249. if ($iDeletedRecords)
  250. $this->query("OPTIMIZE TABLE {$this->_sTableTrack}");
  251. return $iDeletedRecords;
  252. }
  253. }