BxDolCmtsQuery.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA 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 $_sTriggerFieldAuthor;
  19. protected $_sTriggerFieldTitle;
  20. protected $_sTriggerFieldComments;
  21. protected $_sTableFiles;
  22. protected $_sTableFiles2Entries;
  23. protected $_sTableIds;
  24. public 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->_sTriggerFieldAuthor = $aSystem['trigger_field_author'];
  32. $this->_sTriggerFieldTitle = $aSystem['trigger_field_title'];
  33. $this->_sTriggerFieldComments = $aSystem['trigger_field_comments'];
  34. $this->_sTableFiles = $aSystem['table_images'];
  35. $this->_sTableFiles2Entries = $aSystem['table_images2entries'];
  36. $this->_sTableIds = $aSystem['table_ids'];
  37. parent::__construct();
  38. }
  39. function getTableName ()
  40. {
  41. return $this->_sTable;
  42. }
  43. function setTableNameFiles($sTable)
  44. {
  45. $this->_sTableFiles = $sTable;
  46. }
  47. function setTableNameFiles2Entries($sTable)
  48. {
  49. $this->_sTableFiles2Entries = $sTable;
  50. }
  51. function getCommentsCountAll ($iId)
  52. {
  53. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldComments}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ?", $iId);
  54. return (int)$this->getOne($sQuery);
  55. }
  56. function getCommentsCount ($iId, $iCmtVParentId = -1, $iAuthorId = 0, $sFilter = '')
  57. {
  58. $aBindings = array(
  59. 'cmt_object_id' => $iId
  60. );
  61. $sWhereClause = '';
  62. if((int)$iCmtVParentId >= 0) {
  63. $aBindings['cmt_vparent_id'] = $iCmtVParentId;
  64. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_vparent_id` = :cmt_vparent_id";
  65. }
  66. $sJoinClause = '';
  67. switch($sFilter) {
  68. case BX_CMT_FILTER_FRIENDS:
  69. case BX_CMT_FILTER_SUBSCRIPTIONS:
  70. $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
  71. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  72. $sJoinClause .= ' ' . $aQueryParts['join'];
  73. break;
  74. case BX_CMT_FILTER_OTHERS:
  75. $aBindings['cmt_author_id'] = $iAuthorId;
  76. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` <> :cmt_author_id";
  77. break;
  78. }
  79. $sQuery = "SELECT COUNT(*) FROM `{$this->_sTable}` $sJoinClause WHERE `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id" . $sWhereClause;
  80. return (int)$this->getOne($sQuery, $aBindings);
  81. }
  82. function getComments ($iId, $iCmtVParentId = 0, $iAuthorId = 0, $sFilter = '', $aOrder = array(), $iStart = 0, $iCount = -1)
  83. {
  84. $aBindings = array(
  85. 'cmt_object_id' => $iId
  86. );
  87. $sFields = $sJoin = "";
  88. $oVote = $this->_oMain->getVoteObject(0);
  89. if($oVote !== false) {
  90. $aSql = $oVote->getSqlParts($this->_sTable, 'cmt_id');
  91. $sFields .= $aSql['fields'];
  92. $sJoin .= $aSql['join'];
  93. }
  94. $sWhereParent = '';
  95. if((int)$iCmtVParentId >= 0) {
  96. $aBindings['cmt_vparent_id'] = $iCmtVParentId;
  97. $sWhereParent = " AND `{$this->_sTable}`.`cmt_vparent_id` = :cmt_vparent_id";
  98. }
  99. if(in_array($sFilter, array(BX_CMT_FILTER_FRIENDS, BX_CMT_FILTER_SUBSCRIPTIONS))) {
  100. $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
  101. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  102. $sJoin .= ' ' . $aQueryParts['join'];
  103. }
  104. $sOder = " ORDER BY `{$this->_sTable}`.`cmt_time` ASC";
  105. if(isset($aOrder['by']) && isset($aOrder['way'])) {
  106. $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);
  107. switch($aOrder['by']) {
  108. case BX_CMT_ORDER_BY_DATE:
  109. $sOder = " ORDER BY `{$this->_sTable}`.`cmt_time` " . $aOrder['way'];
  110. break;
  111. case BX_CMT_ORDER_BY_POPULAR:
  112. $sOder = " ORDER BY `{$this->_sTable}`.`cmt_rate` " . $aOrder['way'];
  113. break;
  114. }
  115. }
  116. $sLimit = $iCount != -1 ? $this->prepareAsString(" LIMIT ?, ?", (int)$iStart, (int)$iCount) : '';
  117. $sQuery = "SELECT
  118. `{$this->_sTable}`.`cmt_id`,
  119. `{$this->_sTable}`.`cmt_parent_id`,
  120. `{$this->_sTable}`.`cmt_vparent_id`,
  121. `{$this->_sTable}`.`cmt_object_id`,
  122. `{$this->_sTable}`.`cmt_author_id`,
  123. `{$this->_sTable}`.`cmt_level`,
  124. `{$this->_sTable}`.`cmt_text`,
  125. `{$this->_sTable}`.`cmt_replies`,
  126. `{$this->_sTable}`.`cmt_time`
  127. $sFields
  128. FROM `{$this->_sTable}`
  129. $sJoin
  130. WHERE `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id" . $sWhereParent . $sOder . $sLimit;
  131. return $this->getAll($sQuery, $aBindings);
  132. }
  133. function getCommentsBy($aParams = array())
  134. {
  135. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  136. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  137. $sSelectClause = "
  138. `{$this->_sTable}`.`cmt_id`,
  139. `{$this->_sTable}`.`cmt_parent_id`,
  140. `{$this->_sTable}`.`cmt_vparent_id`,
  141. `{$this->_sTable}`.`cmt_object_id`,
  142. `{$this->_sTable}`.`cmt_author_id`,
  143. `{$this->_sTable}`.`cmt_level`,
  144. `{$this->_sTable}`.`cmt_text`,
  145. `{$this->_sTable}`.`cmt_replies`,
  146. `{$this->_sTable}`.`cmt_time`";
  147. if(isset($aParams['object_id'])) {
  148. $aMethod['params'][1]['cmt_object_id'] = (int)$aParams['object_id'];
  149. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id";
  150. }
  151. switch($aParams['type']) {
  152. case 'id':
  153. $aMethod['name'] = 'getRow';
  154. $aMethod['params'][1]['id'] = (int)$aParams['id'];
  155. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_id` = :id";
  156. break;
  157. case 'latest':
  158. if(!empty($aParams['author'])) {
  159. $aMethod['params'][1]['cmt_author_id'] = (int)$aParams['author'];
  160. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` " . (isset($aParams['others']) && (int)$aParams['others'] == 1 ? "<>" : "=") . " :cmt_author_id";
  161. }
  162. $sOrderClause = "`{$this->_sTable}`.`cmt_time` DESC";
  163. $sLimitClause = "";
  164. if(isset($aParams['per_page']))
  165. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  166. break;
  167. case 'object_id':
  168. $aMethod['params'][1]['cmt_object_id'] = (int)$aParams['object_id'];
  169. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id";
  170. $sOrderClause = "`{$this->_sTable}`.`cmt_time` ASC";
  171. $sLimitClause = "";
  172. if(isset($aParams['per_page']))
  173. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  174. break;
  175. case 'search_ids':
  176. $aMethod['name'] = 'getColumn';
  177. $sSelectClause = "`{$this->_sTable}`.`cmt_id`";
  178. if (!empty($aParams['start']) && !empty($aParams['per_page']))
  179. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  180. elseif (!empty($aParams['per_page']))
  181. $sLimitClause = $this->prepareAsString("?", $aParams['per_page']);
  182. $sWhereConditions = "1";
  183. foreach($aParams['search_params'] as $sSearchParam => $aSearchParam) {
  184. $sSearchValue = "";
  185. switch ($aSearchParam['operator']) {
  186. case 'like':
  187. $sSearchValue = " LIKE " . $this->escape("%" . $aSearchParam['value'] . "%");
  188. break;
  189. case 'in':
  190. $sSearchValue = " IN (" . $this->implode_escape($aSearchParam['value']) . ")";
  191. break;
  192. case 'and':
  193. $iResult = 0;
  194. if (is_array($aSearchParam['value']))
  195. foreach ($aSearchParam['value'] as $iValue)
  196. $iResult |= pow (2, $iValue - 1);
  197. else
  198. $iResult = (int)$aSearchParam['value'];
  199. $sSearchValue = " & " . $iResult . "";
  200. break;
  201. default:
  202. $sSearchValue = " " . $aSearchParam['operator'] . " :" . $sSearchParam;
  203. $aMethod['params'][1][$sSearchParam] = $aSearchParam['value'];
  204. }
  205. $sWhereConditions .= " AND `{$this->_sTable}`.`" . $sSearchParam . "`" . $sSearchValue;
  206. }
  207. $sWhereClause .= " AND (" . $sWhereConditions . ")";
  208. $sOrderClause .= "`{$this->_sTable}`.`cmt_time` ASC";
  209. break;
  210. case 'all_ids':
  211. $aMethod['name'] = 'getColumn';
  212. $sSelectClause = "`{$this->_sTable}`.`cmt_id`";
  213. $sOrderClause = "`{$this->_sTable}`.`cmt_time` ASC";
  214. break;
  215. }
  216. if(!empty($sOrderClause))
  217. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  218. if(!empty($sLimitClause))
  219. $sLimitClause = 'LIMIT ' . $sLimitClause;
  220. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `{$this->_sTable}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  221. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  222. }
  223. function getComment ($iId, $iCmtId)
  224. {
  225. $sFields = $sJoin = "";
  226. $oVote = $this->_oMain->getVoteObject($iCmtId);
  227. if($oVote !== false) {
  228. $aSql = $oVote->getSqlParts($this->_sTableIds, 'id');
  229. $sFields .= $aSql['fields'];
  230. $sJoin .= $aSql['join'];
  231. }
  232. $sQuery = $this->prepare("SELECT
  233. `{$this->_sTable}`.`cmt_id`,
  234. `{$this->_sTableIds}`.`id` AS `cmt_unique_id`,
  235. `{$this->_sTable}`.`cmt_parent_id`,
  236. `{$this->_sTable}`.`cmt_vparent_id`,
  237. `{$this->_sTable}`.`cmt_object_id`,
  238. `{$this->_sTable}`.`cmt_author_id`,
  239. `{$this->_sTable}`.`cmt_level`,
  240. `{$this->_sTable}`.`cmt_text`,
  241. `{$this->_sTable}`.`cmt_replies`,
  242. `{$this->_sTable}`.`cmt_time`
  243. $sFields
  244. FROM `{$this->_sTable}`
  245. LEFT JOIN `{$this->_sTableIds}` ON (`{$this->_sTable}`.`cmt_id` = `{$this->_sTableIds}`.`cmt_id` AND `{$this->_sTableIds}`.`system_id` = ?)
  246. $sJoin
  247. WHERE `{$this->_sTable}`.`cmt_object_id` = ? AND `{$this->_sTable}`.`cmt_id` = ?
  248. LIMIT 1", $this->_oMain->getSystemId(), $iId, $iCmtId);
  249. return $this->getRow($sQuery);
  250. }
  251. function getCommentSimple ($iId, $iCmtId)
  252. {
  253. $sQuery = $this->prepare("SELECT * FROM {$this->_sTable} AS `c` WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  254. return $this->getRow($sQuery);
  255. }
  256. function removeComment ($iId, $iCmtId, $iCmtParentId)
  257. {
  258. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  259. if (!$this->query($sQuery))
  260. return false;
  261. if($iCmtParentId)
  262. $this->updateRepliesCount($iCmtParentId, -1);
  263. return true;
  264. }
  265. function saveImages($iSystemId, $iCmtId, $iImageId)
  266. {
  267. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableFiles2Entries}` SET `system_id`=?, `cmt_id`=?, `image_id`=?", $iSystemId, $iCmtId, $iImageId);
  268. return (int)$this->query($sQuery) > 0;
  269. }
  270. function getFiles($iSystemId, $iCmtId, $iId = false)
  271. {
  272. $aBindings = array(
  273. 'system_id' => $iSystemId
  274. );
  275. $sJoin = "";
  276. $sWhere = " AND `tf2e`.`system_id` = :system_id ";
  277. if($iCmtId !== false) {
  278. $aBindings['cmt_id'] = $iCmtId;
  279. $sWhere .= " AND `tf2e`.`cmt_id` = :cmt_id ";
  280. }
  281. if($iId !== false) {
  282. $aBindings['cmt_object_id'] = $iId;
  283. $sWhere .= " AND `te`.`cmt_object_id` = :cmt_object_id";
  284. $sJoin .= " INNER JOIN `{$this->_sTable}` AS `te` ON (`tf2e`.`cmt_id` = `te`.`cmt_id`) ";
  285. }
  286. $sQuery = "SELECT
  287. `tf2e`.*,
  288. `tf`.`file_name` AS `file_name`,
  289. `tf`.`mime_type` AS `mime_type`,
  290. `tf`.`size` AS `size`
  291. FROM `{$this->_sTableFiles2Entries}` AS `tf2e`
  292. LEFT JOIN `{$this->_sTableFiles}` AS `tf` ON (`tf2e`.`image_id` = `tf`.`id`) " . $sJoin . "
  293. WHERE 1 " . $sWhere;
  294. return $this->getAll($sQuery, $aBindings);
  295. }
  296. function deleteImages($iSystemId, $iCmtId)
  297. {
  298. $aBindings = array(
  299. 'system_id' => $iSystemId
  300. );
  301. $sWhereAddon = "";
  302. if ($iCmtId !== false) {
  303. $aBindings['cmt_id'] = $iCmtId;
  304. $sWhereAddon = " AND `cmt_id` = :cmt_id ";
  305. }
  306. return $this->query("DELETE FROM `{$this->_sTableFiles2Entries}` WHERE `system_id` = :system_id" . $sWhereAddon, $aBindings);
  307. }
  308. function updateRepliesCount($iCmtId, $iCount)
  309. {
  310. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `cmt_replies`=`cmt_replies`+? WHERE `cmt_id`=? LIMIT 1", $iCount, $iCmtId);
  311. return $this->query($sQuery);
  312. }
  313. function deleteAuthorComments ($iAuthorId, &$aFiles = null, &$aCmtIds = null)
  314. {
  315. $aSystem = $this->_oMain->getSystemInfo();
  316. $isDelOccured = 0;
  317. $sQuery = $this->prepare("SELECT `cmt_id`, `cmt_parent_id` FROM {$this->_sTable} WHERE `cmt_author_id` = ? AND `cmt_replies` = 0", $iAuthorId);
  318. $a = $this->getAll ($sQuery);
  319. for ( reset($a) ; list (, $r) = each ($a) ; ) {
  320. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_id` = ?", $r['cmt_id']);
  321. $this->query ($sQuery);
  322. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = ?", $r['cmt_parent_id']);
  323. $this->query ($sQuery);
  324. $aFilesMore = $this->convertImagesArray($this->getFiles($aSystem['system_id'], $r['cmt_id']));
  325. $this->deleteImages($aSystem['system_id'], $r['cmt_id']);
  326. if ($aFilesMore && null !== $aFiles)
  327. $aFiles = array_merge($aFiles, $aFilesMore);
  328. if (null !== $aCmtIds)
  329. $aCmtIds[] = $r['cmt_id'];
  330. $isDelOccured = 1;
  331. }
  332. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_author_id` = 0 WHERE `cmt_author_id` = ? AND `cmt_replies` != 0", $iAuthorId);
  333. $this->query ($sQuery);
  334. if ($isDelOccured)
  335. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  336. }
  337. function deleteObjectComments ($iObjectId, &$aFilesReturn = null, &$aCmtIds = null)
  338. {
  339. $aSystem = $this->_oMain->getSystemInfo();
  340. $aFiles = $this->convertImagesArray($this->getFiles($aSystem['system_id'], false, $iObjectId));
  341. if ($aFiles) {
  342. $sQuery = $this->prepare("DELETE FROM {$this->_sTableFiles2Entries} WHERE `system_id` = ? AND `image_id` IN(" . $this->implode_escape($aFiles) . ")", $aSystem['system_id']);
  343. $this->query($sQuery);
  344. }
  345. if (null !== $aCmtIds) {
  346. $sQuery = $this->prepare("SELECT `cmt_id` FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  347. $aCmtIds = $this->getColumn ($sQuery);
  348. }
  349. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  350. $this->query ($sQuery);
  351. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  352. if (null !== $aFilesReturn)
  353. $aFilesReturn = $aFiles;
  354. }
  355. function deleteAll ($iSystemId, &$aFiles = null, &$aCmtIds = null)
  356. {
  357. // get files
  358. if (null !== $aFiles)
  359. $aFiles = $this->convertImagesArray($this->getFiles($iSystemId, false));
  360. // delete files
  361. $this->deleteImages($iSystemId, false);
  362. if (null !== $aCmtIds)
  363. $aCmtIds = $this->getColumn ("SELECT `cmt_id` FROM {$this->_sTable}");
  364. // delete comments
  365. $sQuery = $this->prepare("TRUNCATE TABLE {$this->_sTable}");
  366. $this->query ($sQuery);
  367. }
  368. function deleteCmtIds ($iSystemId, $iCmtId)
  369. {
  370. $sQuery = $this->prepare("DELETE FROM {$this->_sTableIds} WHERE `system_id` = ? AND `cmt_id` = ?", $iSystemId, $iCmtId);
  371. return $this->query ($sQuery);
  372. }
  373. function getObjectAuthorId($iId)
  374. {
  375. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldAuthor}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  376. return $this->getOne($sQuery);
  377. }
  378. function getObjectTitle($iId)
  379. {
  380. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldTitle}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  381. return $this->getOne($sQuery);
  382. }
  383. function updateTriggerTable($iId, $iCount)
  384. {
  385. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldComments}` = ? WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iCount, $iId);
  386. return $this->query($sQuery);
  387. }
  388. function getUniqId($iSystemId, $iCmtId)
  389. {
  390. $sQuery = $this->prepare("SELECT `id` FROM `{$this->_sTableIds}` WHERE `system_id` = ? AND `cmt_id` = ?", $iSystemId, $iCmtId);
  391. if ($iUniqId = $this->getOne($sQuery))
  392. return $iUniqId;
  393. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableIds}` SET `system_id` = ?, `cmt_id` = ?", $iSystemId, $iCmtId);
  394. if (!$this->query($sQuery))
  395. return false;
  396. return $this->lastId();
  397. }
  398. protected function convertImagesArray($a)
  399. {
  400. if (!$a || !is_array($a))
  401. return array();
  402. $aFiles = array ();
  403. foreach ($a as $aFile)
  404. $aFiles[] = $aFile['image_id'];
  405. return $aFiles;
  406. }
  407. }
  408. /** @} */