BxDolCmtsQuery.php 11 KB

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