BxDolCmtsQuery.php 14 KB

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