BxDolCmtsQuery.php 14 KB

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