BxDolCmtsQuery.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolDb');
  10. /**
  11. * @see BxDolCmts
  12. */
  13. class BxDolCmtsQuery extends BxDolDb
  14. {
  15. protected $_oMain;
  16. protected $_sTable;
  17. protected $_sTriggerTable;
  18. protected $_sTriggerFieldId;
  19. protected $_sTriggerFieldTitle;
  20. protected $_sTriggerFieldComments;
  21. protected $_sTableImages;
  22. protected $_sTableImages2Entries;
  23. function __construct(&$oMain)
  24. {
  25. $this->_oMain = $oMain;
  26. $aSystem = $this->_oMain->getSystemInfo();
  27. $this->_sTable = $aSystem['table'];
  28. $this->_sTriggerTable = $aSystem['trigger_table'];
  29. $this->_sTriggerFieldId = $aSystem['trigger_field_id'];
  30. $this->_sTriggerFieldTitle = $aSystem['trigger_field_title'];
  31. $this->_sTriggerFieldComments = $aSystem['trigger_field_comments'];
  32. $this->_sTableImages = $aSystem['table_images'];
  33. $this->_sTableImages2Entries = $aSystem['table_images2entries'];
  34. parent::__construct();
  35. }
  36. function getTableName ()
  37. {
  38. return $this->_sTable;
  39. }
  40. function getCommentsCount ($iId, $iCmtVParentId = 0, $iAuthorId = 0, $sFilter = '')
  41. {
  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 = $sJoin = "";
  62. $oVote = $this->_oMain->getVoteObject(0);
  63. if($oVote !== false) {
  64. $aSql = $oVote->getSqlParts($this->_sTable, 'cmt_id');
  65. $sFields .= $aSql['fields'];
  66. $sJoin .= $aSql['join'];
  67. }
  68. $sWhereParent = '';
  69. if((int)$iCmtVParentId >= 0)
  70. $sWhereParent = $this->prepare(" AND `{$this->_sTable}`.`cmt_vparent_id` = ?", $iCmtVParentId);
  71. if(in_array($sFilter, array(BX_CMT_FILTER_FRIENDS, BX_CMT_FILTER_SUBSCRIPTIONS))) {
  72. bx_import('BxDolConnection');
  73. $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
  74. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  75. $sJoin .= ' ' . $aQueryParts['join'];
  76. }
  77. $sOder = " ORDER BY `{$this->_sTable}`.`cmt_time` ASC";
  78. if(isset($aOrder['by']) && isset($aOrder['way'])) {
  79. $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);
  80. switch($aOrder['by']) {
  81. case BX_CMT_ORDER_BY_DATE:
  82. $sOder = " ORDER BY `{$this->_sTable}`.`cmt_time` " . $aOrder['way'];
  83. break;
  84. case BX_CMT_ORDER_BY_POPULAR:
  85. $sOder = " ORDER BY `{$this->_sTable}`.`cmt_rate` " . $aOrder['way'];
  86. break;
  87. }
  88. }
  89. $sLimit = $iCount != -1 ? $this->prepare(" LIMIT ?, ?", (int)$iStart, (int)$iCount) : '';
  90. $sQuery = $this->prepare("SELECT
  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_replies`,
  99. `{$this->_sTable}`.`cmt_time`
  100. $sFields
  101. FROM `{$this->_sTable}`
  102. $sJoin
  103. WHERE `{$this->_sTable}`.`cmt_object_id` = ?" . $sWhereParent . $sOder . $sLimit, $iId);
  104. return $this->getAll($sQuery);
  105. }
  106. function getComment ($iId, $iCmtId)
  107. {
  108. $sFields = $sJoin = "";
  109. $oVote = $this->_oMain->getVoteObject($iCmtId);
  110. if($oVote !== false) {
  111. $aSql = $oVote->getSqlParts($this->_sTable, 'cmt_id');
  112. $sFields .= $aSql['fields'];
  113. $sJoin .= $aSql['join'];
  114. }
  115. $sQuery = $this->prepare("SELECT
  116. `{$this->_sTable}`.`cmt_id`,
  117. `{$this->_sTable}`.`cmt_parent_id`,
  118. `{$this->_sTable}`.`cmt_vparent_id`,
  119. `{$this->_sTable}`.`cmt_object_id`,
  120. `{$this->_sTable}`.`cmt_author_id`,
  121. `{$this->_sTable}`.`cmt_level`,
  122. `{$this->_sTable}`.`cmt_text`,
  123. `{$this->_sTable}`.`cmt_replies`,
  124. `{$this->_sTable}`.`cmt_time`
  125. $sFields
  126. FROM `{$this->_sTable}`
  127. $sJoin
  128. WHERE `{$this->_sTable}`.`cmt_object_id` = ? AND `{$this->_sTable}`.`cmt_id` = ?
  129. LIMIT 1", $iId, $iCmtId);
  130. return $this->getRow($sQuery);
  131. }
  132. function getCommentSimple ($iId, $iCmtId)
  133. {
  134. $sQuery = $this->prepare("SELECT * FROM {$this->_sTable} AS `c` WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  135. return $this->getRow($sQuery);
  136. }
  137. function removeComment ($iId, $iCmtId, $iCmtParentId)
  138. {
  139. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  140. if (!$this->query($sQuery))
  141. return false;
  142. if($iCmtParentId)
  143. $this->updateRepliesCount($iCmtParentId, -1);
  144. return true;
  145. }
  146. function saveImages($iSystemId, $iCmtId, $iImageId)
  147. {
  148. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableImages2Entries}` SET `system_id`=?, `cmt_id`=?, `image_id`=?", $iSystemId, $iCmtId, $iImageId);
  149. return (int)$this->query($sQuery) > 0;
  150. }
  151. function getImages($iSystemId, $iCmtId, $iId = false)
  152. {
  153. $sJoin = '';
  154. $sWhere = $this->prepare(" AND `i`.`system_id` = ? ", $iSystemId);
  155. if (false !== $iCmtId) {
  156. $sWhere .= $this->prepare(" AND `i`.`cmt_id` = ? ", $iCmtId);
  157. }
  158. if (false !== $iId) {
  159. $sWhere .= $this->prepare(" AND `c`.`cmt_object_id` = ?", $iId);
  160. $sJoin .= " INNER JOIN `{$this->_sTable}` AS `c` ON (`i`.`cmt_id` = `c`.`cmt_id`) ";
  161. }
  162. return $this->getAll("SELECT * FROM `{$this->_sTableImages2Entries}` AS `i` " . $sJoin . " WHERE 1 " . $sWhere);
  163. }
  164. function deleteImages($iSystemId, $iCmtId)
  165. {
  166. $sWhereAddon = '';
  167. if (false !== $iCmtId)
  168. $sWhereAddon = $this->prepare(" AND `cmt_id` = ? ", $iCmtId);
  169. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableImages2Entries}` WHERE `system_id` = ?", $iSystemId);
  170. $sQuery .= $sWhereAddon;
  171. return $this->query($sQuery);
  172. }
  173. function updateRepliesCount($iCmtId, $iCount)
  174. {
  175. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `cmt_replies`=`cmt_replies`+? WHERE `cmt_id`=? LIMIT 1", $iCount, $iCmtId);
  176. return $this->query($sQuery);
  177. }
  178. function deleteAuthorComments ($iAuthorId, &$aFiles = null)
  179. {
  180. $aSystem = $this->_oMain->getSystemInfo();
  181. $isDelOccured = 0;
  182. $sQuery = $this->prepare("SELECT `cmt_id`, `cmt_parent_id` FROM {$this->_sTable} WHERE `cmt_author_id` = ? AND `cmt_replies` = 0", $iAuthorId);
  183. $a = $this->getAll ($sQuery);
  184. for ( reset($a) ; list (, $r) = each ($a) ; ) {
  185. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_id` = ?", $r['cmt_id']);
  186. $this->query ($sQuery);
  187. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = ?", $r['cmt_parent_id']);
  188. $this->query ($sQuery);
  189. $aFilesMore = $this->convertImagesArray($this->getImages($aSystem['system_id'], $r['cmt_id']));
  190. $this->deleteImages($aSystem['system_id'], $r['cmt_id']);
  191. if ($aFilesMore && null !== $aFiles)
  192. $aFiles = array_merge($aFiles, $aFilesMore);
  193. $isDelOccured = 1;
  194. }
  195. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_author_id` = 0 WHERE `cmt_author_id` = ? AND `cmt_replies` != 0", $iAuthorId);
  196. $this->query ($sQuery);
  197. if ($isDelOccured)
  198. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  199. }
  200. function deleteObjectComments ($iObjectId, &$aFilesReturn = null)
  201. {
  202. $aSystem = $this->_oMain->getSystemInfo();
  203. $aFiles = $this->convertImagesArray($this->getImages($aSystem['system_id'], false, $iObjectId));
  204. if ($aFiles) {
  205. $sQuery = $this->prepare("DELETE FROM {$this->_sTableImages2Entries} WHERE `system_id` = ? ", $aSystem['system_id']);
  206. $sQuery .= " AND `image_id` IN(" . $this->implode_escape($aFiles) . ") ";
  207. $this->query ($sQuery);
  208. }
  209. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  210. $this->query ($sQuery);
  211. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  212. if (null !== $aFilesReturn)
  213. $aFilesReturn = $aFiles;
  214. }
  215. function deleteAll ($iSystemId, &$aFiles = null)
  216. {
  217. // get files
  218. if (null !== $aFiles)
  219. $aFiles = $this->convertImagesArray($this->getImages($iSystemId, false));
  220. // delete files
  221. $this->deleteImages($iSystemId, false);
  222. // delete comments
  223. $sQuery = $this->prepare("TRUNCATE TABLE {$this->_sTable}");
  224. $this->query ($sQuery);
  225. }
  226. function getObjectTitle($iId)
  227. {
  228. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldTitle}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  229. return $this->getOne($sQuery);
  230. }
  231. function updateTriggerTable($iId, $iCount)
  232. {
  233. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldComments}` = ? WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iCount, $iId);
  234. return $this->query($sQuery);
  235. }
  236. protected function convertImagesArray($a)
  237. {
  238. if (!$a || !is_array($a))
  239. return array();
  240. $aFiles = array ();
  241. foreach ($a as $aFile)
  242. $aFiles[] = $aFile['image_id'];
  243. return $aFiles;
  244. }
  245. }
  246. /** @} */