BxDolCmtsQuery.php 11 KB

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