BxDolCmtsQuery.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 TridentCore Trident 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. protected $_sTableIds;
  24. function __construct(&$oMain)
  25. {
  26. $this->_oMain = $oMain;
  27. $aSystem = $this->_oMain->getSystemInfo();
  28. $this->_sTable = $aSystem['table'];
  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. $this->_sTableIds = $aSystem['table_ids'];
  36. parent::__construct();
  37. }
  38. function getTableName ()
  39. {
  40. return $this->_sTable;
  41. }
  42. function getCommentsCount ($iId, $iCmtVParentId = 0, $iAuthorId = 0, $sFilter = '')
  43. {
  44. $sWhereClause = '';
  45. if((int)$iCmtVParentId >= 0)
  46. $sWhereClause .= $this->prepare(" AND `{$this->_sTable}`.`cmt_vparent_id` = ?", $iCmtVParentId);
  47. $sJoinClause = '';
  48. switch($sFilter) {
  49. case BX_CMT_FILTER_FRIENDS:
  50. case BX_CMT_FILTER_SUBSCRIPTIONS:
  51. bx_import('BxDolConnection');
  52. $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
  53. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  54. $sJoinClause .= ' ' . $aQueryParts['join'];
  55. break;
  56. case BX_CMT_FILTER_OTHERS:
  57. $sWhereClause .= $this->prepare(" AND `{$this->_sTable}`.`cmt_author_id` <> ?", $iAuthorId);
  58. break;
  59. }
  60. $sQuery = $this->prepare("SELECT
  61. COUNT(*)
  62. FROM `{$this->_sTable}`
  63. $sJoinClause
  64. WHERE `{$this->_sTable}`.`cmt_object_id` = ?" . $sWhereClause, $iId);
  65. return (int)$this->getOne($sQuery);
  66. }
  67. function getComments ($iId, $iCmtVParentId = 0, $iAuthorId = 0, $sFilter = '', $aOrder = array(), $iStart = 0, $iCount = -1)
  68. {
  69. $sFields = $sJoin = "";
  70. $oVote = $this->_oMain->getVoteObject(0);
  71. if($oVote !== false) {
  72. $aSql = $oVote->getSqlParts($this->_sTable, 'cmt_id');
  73. $sFields .= $aSql['fields'];
  74. $sJoin .= $aSql['join'];
  75. }
  76. $sWhereParent = '';
  77. if((int)$iCmtVParentId >= 0)
  78. $sWhereParent = $this->prepare(" AND `{$this->_sTable}`.`cmt_vparent_id` = ?", $iCmtVParentId);
  79. if(in_array($sFilter, array(BX_CMT_FILTER_FRIENDS, BX_CMT_FILTER_SUBSCRIPTIONS))) {
  80. bx_import('BxDolConnection');
  81. $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
  82. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  83. $sJoin .= ' ' . $aQueryParts['join'];
  84. }
  85. $sOder = " ORDER BY `{$this->_sTable}`.`cmt_time` ASC";
  86. if(isset($aOrder['by']) && isset($aOrder['way'])) {
  87. $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);
  88. switch($aOrder['by']) {
  89. case BX_CMT_ORDER_BY_DATE:
  90. $sOder = " ORDER BY `{$this->_sTable}`.`cmt_time` " . $aOrder['way'];
  91. break;
  92. case BX_CMT_ORDER_BY_POPULAR:
  93. $sOder = " ORDER BY `{$this->_sTable}`.`cmt_rate` " . $aOrder['way'];
  94. break;
  95. }
  96. }
  97. $sLimit = $iCount != -1 ? $this->prepare(" LIMIT ?, ?", (int)$iStart, (int)$iCount) : '';
  98. $sQuery = $this->prepare("SELECT
  99. `{$this->_sTable}`.`cmt_id`,
  100. `{$this->_sTable}`.`cmt_parent_id`,
  101. `{$this->_sTable}`.`cmt_vparent_id`,
  102. `{$this->_sTable}`.`cmt_object_id`,
  103. `{$this->_sTable}`.`cmt_author_id`,
  104. `{$this->_sTable}`.`cmt_level`,
  105. `{$this->_sTable}`.`cmt_text`,
  106. `{$this->_sTable}`.`cmt_replies`,
  107. `{$this->_sTable}`.`cmt_time`
  108. $sFields
  109. FROM `{$this->_sTable}`
  110. $sJoin
  111. WHERE `{$this->_sTable}`.`cmt_object_id` = ?" . $sWhereParent . $sOder . $sLimit, $iId);
  112. return $this->getAll($sQuery);
  113. }
  114. function getCommentsBy($aParams = array())
  115. {
  116. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  117. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  118. $sSelectClause = "
  119. `{$this->_sTable}`.`cmt_id`,
  120. `{$this->_sTable}`.`cmt_parent_id`,
  121. `{$this->_sTable}`.`cmt_vparent_id`,
  122. `{$this->_sTable}`.`cmt_object_id`,
  123. `{$this->_sTable}`.`cmt_author_id`,
  124. `{$this->_sTable}`.`cmt_level`,
  125. `{$this->_sTable}`.`cmt_text`,
  126. `{$this->_sTable}`.`cmt_replies`,
  127. `{$this->_sTable}`.`cmt_time`";
  128. if(isset($aParams['object_id']))
  129. $sWhereClause .= $this->prepare(" AND `{$this->_sTable}`.`cmt_object_id` = ?" , (int)$aParams['object_id']);
  130. switch($aParams['type']) {
  131. case 'latest_ids':
  132. $aMethod['name'] = 'getColumn';
  133. $sSelectClause = "`{$this->_sTable}`.`cmt_id`";
  134. $sOrderClause = "ORDER BY `{$this->_sTable}`.`cmt_time` DESC";
  135. $sLimitClause = isset($aParams['per_page']) ? "LIMIT " . $aParams['start'] . ", " . $aParams['per_page'] : "";
  136. break;
  137. }
  138. $aMethod['params'][0] = "SELECT " . $sSelectClause . "
  139. FROM `{$this->_sTable}` " . $sJoinClause . "
  140. WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  141. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  142. }
  143. function getComment ($iId, $iCmtId)
  144. {
  145. $sFields = $sJoin = "";
  146. $oVote = $this->_oMain->getVoteObject($iCmtId);
  147. if($oVote !== false) {
  148. $aSql = $oVote->getSqlParts($this->_sTable, 'cmt_id');
  149. $sFields .= $aSql['fields'];
  150. $sJoin .= $aSql['join'];
  151. }
  152. $sQuery = $this->prepare("SELECT
  153. `{$this->_sTable}`.`cmt_id`,
  154. `{$this->_sTable}`.`cmt_parent_id`,
  155. `{$this->_sTable}`.`cmt_vparent_id`,
  156. `{$this->_sTable}`.`cmt_object_id`,
  157. `{$this->_sTable}`.`cmt_author_id`,
  158. `{$this->_sTable}`.`cmt_level`,
  159. `{$this->_sTable}`.`cmt_text`,
  160. `{$this->_sTable}`.`cmt_replies`,
  161. `{$this->_sTable}`.`cmt_time`
  162. $sFields
  163. FROM `{$this->_sTable}`
  164. $sJoin
  165. WHERE `{$this->_sTable}`.`cmt_object_id` = ? AND `{$this->_sTable}`.`cmt_id` = ?
  166. LIMIT 1", $iId, $iCmtId);
  167. return $this->getRow($sQuery);
  168. }
  169. function getCommentSimple ($iId, $iCmtId)
  170. {
  171. $sQuery = $this->prepare("SELECT * FROM {$this->_sTable} AS `c` WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  172. return $this->getRow($sQuery);
  173. }
  174. function removeComment ($iId, $iCmtId, $iCmtParentId)
  175. {
  176. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  177. if (!$this->query($sQuery))
  178. return false;
  179. if($iCmtParentId)
  180. $this->updateRepliesCount($iCmtParentId, -1);
  181. return true;
  182. }
  183. function saveImages($iSystemId, $iCmtId, $iImageId)
  184. {
  185. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableImages2Entries}` SET `system_id`=?, `cmt_id`=?, `image_id`=?", $iSystemId, $iCmtId, $iImageId);
  186. return (int)$this->query($sQuery) > 0;
  187. }
  188. function getImages($iSystemId, $iCmtId, $iId = false)
  189. {
  190. $sJoin = '';
  191. $sWhere = $this->prepare(" AND `i`.`system_id` = ? ", $iSystemId);
  192. if (false !== $iCmtId) {
  193. $sWhere .= $this->prepare(" AND `i`.`cmt_id` = ? ", $iCmtId);
  194. }
  195. if (false !== $iId) {
  196. $sWhere .= $this->prepare(" AND `c`.`cmt_object_id` = ?", $iId);
  197. $sJoin .= " INNER JOIN `{$this->_sTable}` AS `c` ON (`i`.`cmt_id` = `c`.`cmt_id`) ";
  198. }
  199. return $this->getAll("SELECT * FROM `{$this->_sTableImages2Entries}` AS `i` " . $sJoin . " WHERE 1 " . $sWhere);
  200. }
  201. function deleteImages($iSystemId, $iCmtId)
  202. {
  203. $sWhereAddon = '';
  204. if (false !== $iCmtId)
  205. $sWhereAddon = $this->prepare(" AND `cmt_id` = ? ", $iCmtId);
  206. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableImages2Entries}` WHERE `system_id` = ?", $iSystemId);
  207. $sQuery .= $sWhereAddon;
  208. return $this->query($sQuery);
  209. }
  210. function updateRepliesCount($iCmtId, $iCount)
  211. {
  212. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `cmt_replies`=`cmt_replies`+? WHERE `cmt_id`=? LIMIT 1", $iCount, $iCmtId);
  213. return $this->query($sQuery);
  214. }
  215. function deleteAuthorComments ($iAuthorId, &$aFiles = null, &$aCmtIds = null)
  216. {
  217. $aSystem = $this->_oMain->getSystemInfo();
  218. $isDelOccured = 0;
  219. $sQuery = $this->prepare("SELECT `cmt_id`, `cmt_parent_id` FROM {$this->_sTable} WHERE `cmt_author_id` = ? AND `cmt_replies` = 0", $iAuthorId);
  220. $a = $this->getAll ($sQuery);
  221. for ( reset($a) ; list (, $r) = each ($a) ; ) {
  222. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_id` = ?", $r['cmt_id']);
  223. $this->query ($sQuery);
  224. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = ?", $r['cmt_parent_id']);
  225. $this->query ($sQuery);
  226. $aFilesMore = $this->convertImagesArray($this->getImages($aSystem['system_id'], $r['cmt_id']));
  227. $this->deleteImages($aSystem['system_id'], $r['cmt_id']);
  228. if ($aFilesMore && null !== $aFiles)
  229. $aFiles = array_merge($aFiles, $aFilesMore);
  230. if (null !== $aCmtIds)
  231. $aCmtIds[] = $r['cmt_id'];
  232. $isDelOccured = 1;
  233. }
  234. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_author_id` = 0 WHERE `cmt_author_id` = ? AND `cmt_replies` != 0", $iAuthorId);
  235. $this->query ($sQuery);
  236. if ($isDelOccured)
  237. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  238. }
  239. function deleteObjectComments ($iObjectId, &$aFilesReturn = null, &$aCmtIds = null)
  240. {
  241. $aSystem = $this->_oMain->getSystemInfo();
  242. $aFiles = $this->convertImagesArray($this->getImages($aSystem['system_id'], false, $iObjectId));
  243. if ($aFiles) {
  244. $sQuery = $this->prepare("DELETE FROM {$this->_sTableImages2Entries} WHERE `system_id` = ? ", $aSystem['system_id']);
  245. $sQuery .= " AND `image_id` IN(" . $this->implode_escape($aFiles) . ") ";
  246. $this->query ($sQuery);
  247. }
  248. if (null !== $aCmtIds) {
  249. $sQuery = $this->prepare("SELECT `cmt_id` FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  250. $aCmtIds = $this->getColumn ($sQuery);
  251. }
  252. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  253. $this->query ($sQuery);
  254. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  255. if (null !== $aFilesReturn)
  256. $aFilesReturn = $aFiles;
  257. }
  258. function deleteAll ($iSystemId, &$aFiles = null, &$aCmtIds = null)
  259. {
  260. // get files
  261. if (null !== $aFiles)
  262. $aFiles = $this->convertImagesArray($this->getImages($iSystemId, false));
  263. // delete files
  264. $this->deleteImages($iSystemId, false);
  265. if (null !== $aCmtIds)
  266. $aCmtIds = $this->getColumn ("SELECT `cmt_id` FROM {$this->_sTable}");
  267. // delete comments
  268. $sQuery = $this->prepare("TRUNCATE TABLE {$this->_sTable}");
  269. $this->query ($sQuery);
  270. }
  271. function deleteCmtIds ($iSystemId, $iCmtId)
  272. {
  273. $sQuery = $this->prepare("DELETE FROM {$this->_sTableIds} WHERE `system_id` = ? AND `cmt_id` = ?", $iSystemId, $iCmtId);
  274. return $this->query ($sQuery);
  275. }
  276. function getObjectTitle($iId)
  277. {
  278. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldTitle}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  279. return $this->getOne($sQuery);
  280. }
  281. function updateTriggerTable($iId, $iCount)
  282. {
  283. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldComments}` = ? WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iCount, $iId);
  284. return $this->query($sQuery);
  285. }
  286. function getUniqId($iSystemId, $iCmtId)
  287. {
  288. $sQuery = $this->prepare("SELECT `id` FROM `{$this->_sTableIds}` WHERE `system_id` = ? AND `cmt_id` = ?", $iSystemId, $iCmtId);
  289. if ($iUniqId = $this->getOne($sQuery))
  290. return $iUniqId;
  291. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableIds}` SET `system_id` = ?, `cmt_id` = ?", $iSystemId, $iCmtId);
  292. if (!$this->query($sQuery))
  293. return false;
  294. return $this->lastId();
  295. }
  296. protected function convertImagesArray($a)
  297. {
  298. if (!$a || !is_array($a))
  299. return array();
  300. $aFiles = array ();
  301. foreach ($a as $aFile)
  302. $aFiles[] = $aFile['image_id'];
  303. return $aFiles;
  304. }
  305. }
  306. /** @} */