BxDolCmtsQuery.php 19 KB

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