BxDolCmtsQuery.php 10 KB

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