BxDolCmtsQuery.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. public static function getInfoByUniqId($iUniqId)
  40. {
  41. $oDb = BxDolDb::getInstance();
  42. $sQuery = "SELECT
  43. `ti`.`cmt_id` AS `cmt_id`,
  44. `to`.`Name` AS `system_name`,
  45. `to`.`Table` AS `table_name`
  46. FROM `" . BxDolCmts::$sTableIds . "` AS `ti`
  47. INNER JOIN `" . BxDolCmts::$sTableSystems . "` AS `to` ON `ti`.`system_id` = `to`.`ID`
  48. WHERE `ti`.`id` = :uniq_ig
  49. LIMIT 1";
  50. $aRow = $oDb->getRow($sQuery, array('uniq_ig' => $iUniqId));
  51. if(empty($aRow) || !is_array($aRow))
  52. return $aRow;
  53. $aRow['cmt_object_id'] = $oDb->getOne("SELECT `cmt_object_id` FROM `" . $aRow['table_name'] . "` WHERE `cmt_id` = :cmt_id LIMIT 1", array(
  54. 'cmt_id' => $aRow['cmt_id']
  55. ));
  56. return $aRow;
  57. }
  58. /**
  59. * @deprecated since version 12.0.1
  60. */
  61. public static function getCommentByUniq ($iUnicId)
  62. {
  63. return self::getInfoByUniqId($iUnicId);
  64. }
  65. public static function getCommentSimpleByUniqId($iUniqId)
  66. {
  67. $oDb = BxDolDb::getInstance();
  68. $sQuery = "SELECT
  69. `ti`.`cmt_id` AS `cmt_id`,
  70. `to`.`Table` AS `cmt_table`
  71. FROM `" . BxDolCmts::$sTableIds . "` as `ti`
  72. INNER JOIN `" . BxDolCmts::$sTableSystems . "` as `to` ON `ti`.`system_id` = `to`.`ID`
  73. WHERE `ti`.`id` = :uniq_ig
  74. LIMIT 1";
  75. $aData = $oDb->getRow($sQuery, array('uniq_ig' => $iUniqId));
  76. if(empty($aData) || !is_array($aData))
  77. return array();
  78. return $oDb->getRow("SELECT * FROM `" . $aData['cmt_table'] . "` WHERE `cmt_id` = :cmt_id LIMIT 1", array(
  79. 'cmt_id' => $aData['cmt_id']
  80. ));
  81. }
  82. public static function getCommentExtendedByUniqId($iUniqId)
  83. {
  84. $oDb = BxDolDb::getInstance();
  85. $sQuery = "SELECT
  86. `ti`.`cmt_id` AS `cmt_id`,
  87. `ti`.`system_id` AS `cmt_system_id`,
  88. `to`.`Table` AS `cmt_table`
  89. FROM `" . BxDolCmts::$sTableIds . "` AS `ti`
  90. INNER JOIN `" . BxDolCmts::$sTableSystems . "` AS `to` ON `ti`.`system_id` = `to`.`ID`
  91. WHERE `ti`.`id` = :uniq_ig
  92. LIMIT 1";
  93. $aData = $oDb->getRow($sQuery, array('uniq_ig' => $iUniqId));
  94. if(empty($aData) || !is_array($aData))
  95. return array();
  96. $sQuery = "SELECT
  97. `tc`.*,
  98. `ti`.`rate`, `ti`.`votes`,
  99. `ti`.`rrate`, `ti`.`rvotes`,
  100. `ti`.`score`, `ti`.`sc_up`, `ti`.`sc_down`,
  101. `ti`.`reports`
  102. FROM `" . $aData['cmt_table'] . "` AS `tc`
  103. LEFT JOIN `" . BxDolCmts::$sTableIds . "` AS `ti` ON `ti`.`system_id` = :cmt_system_id AND `tc`.`cmt_id` = `ti`.`cmt_id`
  104. WHERE `tc`.`cmt_id` = :cmt_id
  105. LIMIT 1";
  106. return $oDb->getRow($sQuery, array(
  107. 'cmt_id' => $aData['cmt_id'],
  108. 'cmt_system_id' => $aData['cmt_system_id']
  109. ));
  110. }
  111. function getTableName ()
  112. {
  113. return $this->_sTable;
  114. }
  115. /**
  116. * @deprecated since version 10.0.0-B3 and can be removed in later versions.
  117. */
  118. function setTableNameFiles($sTable)
  119. {
  120. $this->_sTableFiles = $sTable;
  121. }
  122. /**
  123. * @deprecated since version 10.0.0-B3 and can be removed in later versions.
  124. */
  125. function setTableNameFiles2Entries($sTable)
  126. {
  127. $this->_sTableFiles2Entries = $sTable;
  128. }
  129. function getCommentsCountAll ($iId)
  130. {
  131. if ($this->_sTriggerFieldComments) {
  132. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldComments}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ?", $iId);
  133. return (int)$this->getOne($sQuery);
  134. }
  135. else {
  136. return $this->getCommentsCount($iId);
  137. }
  138. }
  139. function getCommentsCount ($iId, $iCmtVParentId = -1, $iAuthorId = 0, $sFilter = '')
  140. {
  141. $aBindings = array(
  142. 'cmt_object_id' => $iId
  143. );
  144. $sWhereClause = '';
  145. if((int)$iCmtVParentId >= 0) {
  146. $aBindings['cmt_vparent_id'] = $iCmtVParentId;
  147. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_vparent_id` = :cmt_vparent_id";
  148. }
  149. $sJoinClause = '';
  150. switch($sFilter) {
  151. case BX_CMT_FILTER_FRIENDS:
  152. case BX_CMT_FILTER_SUBSCRIPTIONS:
  153. $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
  154. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  155. $sJoinClause .= ' ' . $aQueryParts['join'];
  156. break;
  157. case BX_CMT_FILTER_OTHERS:
  158. $aBindings['cmt_author_id'] = $iAuthorId;
  159. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` <> :cmt_author_id";
  160. break;
  161. }
  162. $sQuery = "SELECT COUNT(*) FROM `{$this->_sTable}` $sJoinClause WHERE `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id" . $sWhereClause;
  163. return (int)$this->getOne($sQuery, $aBindings);
  164. }
  165. function getStructure($iObjectId, $iAuthorId = 0, $iParentId = 0, $sFilter = '', $aOrder = array())
  166. {
  167. $sSelectClause = $sJoinClause = $sWhereClause = "";
  168. $aBindings = array(
  169. 'cmt_object_id' => $iObjectId,
  170. 'system_id' => $this->_oMain->getSystemId()
  171. );
  172. if((int)$iParentId >= 0) {
  173. $aBindings['cmt_parent_id'] = $iParentId;
  174. $sWhereClause .= " AND `tc`.`cmt_parent_id` = :cmt_parent_id";
  175. }
  176. if(in_array($sFilter, array(BX_CMT_FILTER_FRIENDS, BX_CMT_FILTER_SUBSCRIPTIONS))) {
  177. $sConnection = $this->_oMain->getConnectionObject($sFilter);
  178. $aQueryParts = BxDolConnection::getObjectInstance($sConnection)->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  179. $sJoinClause .= ' ' . $aQueryParts['join'];
  180. }
  181. if(isset($aOrder['by']) && isset($aOrder['way']))
  182. switch($aOrder['by']) {
  183. case BX_CMT_ORDER_BY_DATE:
  184. $sSelectClause .= ", `tc`.`cmt_time` AS `cmt_order`";
  185. break;
  186. case BX_CMT_ORDER_BY_POPULAR:
  187. $aOrderFields = array();
  188. if($this->_oMain->getVoteObject(0) !== false)
  189. $aOrderFields[] = "`ti`.`votes`";
  190. if($this->_oMain->getScoreObject(0) !== false)
  191. $aOrderFields[] = "`ti`.`score`";
  192. if(!empty($aOrderFields))
  193. $sSelectClause .= ", (" . implode(' + ', $aOrderFields) . ") AS `cmt_order`";
  194. else
  195. $sSelectClause .= ", `tc`.`id` AS `cmt_order`";
  196. break;
  197. }
  198. $sQuery = "SELECT
  199. `tc`.`cmt_id`,
  200. `tc`.`cmt_replies`,
  201. `tc`.`cmt_time` $sSelectClause
  202. FROM `{$this->_sTable}` AS `tc`
  203. LEFT JOIN `{$this->_sTableIds}` AS `ti` ON `tc`.`cmt_id` = `ti`.`cmt_id` AND `ti`.`system_id` = :system_id $sJoinClause
  204. WHERE `tc`.`cmt_object_id` = :cmt_object_id" . $sWhereClause;
  205. return $this->getAll($sQuery, $aBindings);
  206. }
  207. function getComments ($iId, $iCmtVParentId = 0, $iAuthorId = 0, $sFilter = '', $aOrder = array(), $iStart = 0, $iCount = -1)
  208. {
  209. $aBindings = array(
  210. 'cmt_object_id' => $iId,
  211. 'system_id' => $this->_oMain->getSystemId()
  212. );
  213. $sFields = $sJoin = "";
  214. $sWhereParent = '';
  215. if((int)$iCmtVParentId >= 0) {
  216. $aBindings['cmt_vparent_id'] = $iCmtVParentId;
  217. $sWhereParent = " AND `{$this->_sTable}`.`cmt_vparent_id` = :cmt_vparent_id";
  218. }
  219. if(in_array($sFilter, array(BX_CMT_FILTER_FRIENDS, BX_CMT_FILTER_SUBSCRIPTIONS))) {
  220. $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
  221. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  222. $sJoin .= ' ' . $aQueryParts['join'];
  223. }
  224. $sOrder = " ORDER BY `{$this->_sTable}`.`cmt_pinned` DESC, `{$this->_sTable}`.`cmt_time` ASC";
  225. if(isset($aOrder['by']) && isset($aOrder['way'])) {
  226. $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);
  227. switch($aOrder['by']) {
  228. case BX_CMT_ORDER_BY_DATE:
  229. $sOrder = " ORDER BY `{$this->_sTable}`.`cmt_pinned` DESC, `{$this->_sTable}`.`cmt_time` " . $aOrder['way'];
  230. break;
  231. case BX_CMT_ORDER_BY_POPULAR:
  232. $aSortFields = array();
  233. if($this->_oMain->getVoteObject(0) !== false)
  234. array_push($aSortFields, '`' . $this->_sTableIds . '`.`votes`');
  235. if($this->_oMain->getReactionObject(0) !== false)
  236. array_push($aSortFields, '`' . $this->_sTableIds . '`.`rvotes`');
  237. if($this->_oMain->getScoreObject(0) !== false)
  238. array_push($aSortFields, '`' . $this->_sTableIds . '`.`score`');
  239. if(count($aSortFields) == 0)
  240. array_push($aSortFields, '`' . $this->_sTable . '`.`id`');
  241. $sOrder = " ORDER BY `{$this->_sTable}`.`cmt_pinned` DESC, " . implode($aOrder['way'] . ', ', $aSortFields) . " " . $aOrder['way'];
  242. break;
  243. }
  244. }
  245. $sLimit = $iCount != -1 ? $this->prepareAsString(" LIMIT ?, ?", (int)$iStart, (int)$iCount) : '';
  246. $sQuery = "SELECT
  247. `{$this->_sTable}`.*,
  248. `{$this->_sTableIds}`.`id` AS `cmt_unique_id`
  249. $sFields
  250. FROM `{$this->_sTable}`
  251. LEFT JOIN `{$this->_sTableIds}` ON (`{$this->_sTable}`.`cmt_id` = `{$this->_sTableIds}`.`cmt_id` AND `{$this->_sTableIds}`.`system_id` = :system_id)
  252. $sJoin
  253. WHERE `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id" . $sWhereParent . $sOrder . $sLimit;
  254. return $this->getAll($sQuery, $aBindings);
  255. }
  256. function getCommentsBy($aParams = array())
  257. {
  258. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  259. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  260. $sSelectClause = "`{$this->_sTable}`.*";
  261. if(isset($aParams['object_id'])) {
  262. $aMethod['params'][1]['cmt_object_id'] = (int)$aParams['object_id'];
  263. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id";
  264. }
  265. switch($aParams['type']) {
  266. case 'id':
  267. $aMethod['name'] = 'getRow';
  268. $aMethod['params'][1]['id'] = (int)$aParams['id'];
  269. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_id` = :id";
  270. break;
  271. case 'latest':
  272. if(!empty($aParams['author'])) {
  273. $aMethod['params'][1]['cmt_author_id'] = (int)$aParams['author'];
  274. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` " . (isset($aParams['others']) && (int)$aParams['others'] == 1 ? "<>" : "=") . " :cmt_author_id";
  275. }
  276. $sOrderClause = "`{$this->_sTable}`.`cmt_time` DESC";
  277. $sLimitClause = "";
  278. if(isset($aParams['per_page']))
  279. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  280. break;
  281. case 'parent_id':
  282. $aMethod['params'][1]['cmt_parent_id'] = (int)$aParams['parent_id'];
  283. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_parent_id` = :cmt_parent_id";
  284. $sOrderClause = "`{$this->_sTable}`.`cmt_time` ASC";
  285. $sLimitClause = "";
  286. if(isset($aParams['per_page']))
  287. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  288. break;
  289. case 'object_id':
  290. $aMethod['params'][1]['cmt_object_id'] = (int)$aParams['object_id'];
  291. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id";
  292. $sOrderClause = "`{$this->_sTable}`.`cmt_time` " . (!empty($aParams['order_way']) ? strtoupper($aParams['order_way']) : "ASC");
  293. $sLimitClause = "";
  294. if(isset($aParams['per_page']))
  295. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  296. break;
  297. case 'author_id':
  298. $aMethod['params'][1]['cmt_author_id'] = (int)$aParams['author_id'];
  299. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` = :cmt_author_id";
  300. $sOrderClause = "`{$this->_sTable}`.`cmt_time` ASC";
  301. $sLimitClause = "";
  302. if(isset($aParams['per_page']))
  303. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  304. break;
  305. case 'search_ids':
  306. $aMethod['name'] = 'getColumn';
  307. $sSelectClause = "`{$this->_sTable}`.`cmt_id`";
  308. if (!empty($aParams['start']) && !empty($aParams['per_page']))
  309. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  310. elseif (!empty($aParams['per_page']))
  311. $sLimitClause = $this->prepareAsString("?", $aParams['per_page']);
  312. $sWhereConditions = "1";
  313. foreach($aParams['search_params'] as $sSearchParam => $aSearchParam) {
  314. $sSearchValue = "";
  315. switch ($aSearchParam['operator']) {
  316. case 'like':
  317. $sSearchValue = " LIKE " . $this->escape("%" . $aSearchParam['value'] . "%");
  318. break;
  319. case 'in':
  320. $sSearchValue = " IN (" . $this->implode_escape($aSearchParam['value']) . ")";
  321. break;
  322. case 'and':
  323. $iResult = 0;
  324. if (is_array($aSearchParam['value']))
  325. foreach ($aSearchParam['value'] as $iValue)
  326. $iResult |= pow (2, $iValue - 1);
  327. else
  328. $iResult = (int)$aSearchParam['value'];
  329. $sSearchValue = " & " . $iResult . "";
  330. break;
  331. default:
  332. $sSearchValue = " " . $aSearchParam['operator'] . " :" . $sSearchParam;
  333. $aMethod['params'][1][$sSearchParam] = $aSearchParam['value'];
  334. }
  335. $sWhereConditions .= " AND `{$this->_sTable}`.`" . $sSearchParam . "`" . $sSearchValue;
  336. }
  337. $sWhereClause .= " AND (" . $sWhereConditions . ")";
  338. $sOrderClause .= "`{$this->_sTable}`.`cmt_time` ASC";
  339. break;
  340. case 'all_ids':
  341. $aMethod['name'] = 'getColumn';
  342. $sSelectClause = "`{$this->_sTable}`.`cmt_id`";
  343. $sOrderClause = "`{$this->_sTable}`.`cmt_time` ASC";
  344. break;
  345. }
  346. if(!empty($sOrderClause))
  347. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  348. if(!empty($sLimitClause))
  349. $sLimitClause = 'LIMIT ' . $sLimitClause;
  350. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `{$this->_sTable}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  351. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  352. }
  353. function getComment ($iId, $iCmtId)
  354. {
  355. $sFields = $sJoin = "";
  356. $oVote = $this->_oMain->getVoteObject($iCmtId);
  357. if($oVote !== false) {
  358. $aSql = $oVote->getSqlParts($this->_sTableIds, 'id');
  359. $sFields .= $aSql['fields'];
  360. $sJoin .= $aSql['join'];
  361. }
  362. $sQuery = $this->prepare("SELECT
  363. `{$this->_sTable}`.*,
  364. `{$this->_sTableIds}`.`id` AS `cmt_unique_id`
  365. $sFields
  366. FROM `{$this->_sTable}`
  367. LEFT JOIN `{$this->_sTableIds}` ON (`{$this->_sTable}`.`cmt_id` = `{$this->_sTableIds}`.`cmt_id` AND `{$this->_sTableIds}`.`system_id` = ?)
  368. $sJoin
  369. WHERE `{$this->_sTable}`.`cmt_object_id` = ? AND `{$this->_sTable}`.`cmt_id` = ?
  370. LIMIT 1", $this->_oMain->getSystemId(), $iId, $iCmtId);
  371. return $this->getRow($sQuery);
  372. }
  373. function getCommentSimple ($iId, $iCmtId)
  374. {
  375. $sQuery = $this->prepare("SELECT * FROM {$this->_sTable} AS `c` WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  376. return $this->getRow($sQuery);
  377. }
  378. function removeComment ($iId, $iCmtId, $iCmtParentId)
  379. {
  380. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  381. if (!$this->query($sQuery))
  382. return false;
  383. if($iCmtParentId)
  384. $this->updateRepliesCount($iCmtParentId, -1);
  385. return true;
  386. }
  387. function saveImages($iSystemId, $iCmtId, $iImageId)
  388. {
  389. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableFiles2Entries}` SET `system_id`=?, `cmt_id`=?, `image_id`=?", $iSystemId, $iCmtId, $iImageId);
  390. return (int)$this->query($sQuery) > 0;
  391. }
  392. function getFiles($iSystemId, $iCmtId, $iId = false)
  393. {
  394. $aBindings = array(
  395. 'system_id' => $iSystemId
  396. );
  397. $sJoin = "";
  398. $sWhere = " AND `tf2e`.`system_id` = :system_id ";
  399. if($iCmtId !== false) {
  400. $aBindings['cmt_id'] = $iCmtId;
  401. $sWhere .= " AND `tf2e`.`cmt_id` = :cmt_id ";
  402. }
  403. if($iId !== false) {
  404. $aBindings['cmt_object_id'] = $iId;
  405. $sWhere .= " AND `te`.`cmt_object_id` = :cmt_object_id";
  406. $sJoin .= " INNER JOIN `{$this->_sTable}` AS `te` ON (`tf2e`.`cmt_id` = `te`.`cmt_id`) ";
  407. }
  408. $sQuery = "SELECT
  409. `tf2e`.*,
  410. `tf`.`file_name` AS `file_name`,
  411. `tf`.`mime_type` AS `mime_type`,
  412. `tf`.`size` AS `size`
  413. FROM `{$this->_sTableFiles2Entries}` AS `tf2e`
  414. LEFT JOIN `{$this->_sTableFiles}` AS `tf` ON (`tf2e`.`image_id` = `tf`.`id`) " . $sJoin . "
  415. WHERE 1 " . $sWhere;
  416. return $this->getAll($sQuery, $aBindings);
  417. }
  418. function deleteImages($iSystemId, $iCmtId, $iImageId = false)
  419. {
  420. $sWhereAddon = "";
  421. $aBindings = array();
  422. if ($iSystemId !== false) {
  423. $aBindings['system_id'] = $iSystemId;
  424. $sWhereAddon .= " AND `system_id` = :system_id ";
  425. }
  426. if ($iCmtId !== false) {
  427. $aBindings['cmt_id'] = $iCmtId;
  428. $sWhereAddon .= " AND `cmt_id` = :cmt_id ";
  429. }
  430. if ($iImageId !== false) {
  431. $aBindings['image_id'] = $iImageId;
  432. $sWhereAddon .= " AND `image_id` = :image_id ";
  433. }
  434. return $this->query("DELETE FROM `{$this->_sTableFiles2Entries}` WHERE 1" . $sWhereAddon, $aBindings);
  435. }
  436. function updateComments($aSetClause, $aWhereClause)
  437. {
  438. if(empty($aSetClause) || empty($aWhereClause))
  439. return;
  440. return (int)$this->query("UPDATE `{$this->_sTable}` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  441. }
  442. function updateRepliesCount($iCmtId, $iCount)
  443. {
  444. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `cmt_replies`=`cmt_replies`+? WHERE `cmt_id`=? LIMIT 1", $iCount, $iCmtId);
  445. return $this->query($sQuery);
  446. }
  447. function deleteAuthorComments ($iAuthorId, &$aFiles = null, &$aCmtIds = null)
  448. {
  449. $aSystem = $this->_oMain->getSystemInfo();
  450. $isDelOccured = 0;
  451. $sQuery = $this->prepare("SELECT `cmt_id`, `cmt_parent_id` FROM {$this->_sTable} WHERE `cmt_author_id` = ? AND `cmt_replies` = 0", $iAuthorId);
  452. $a = $this->getAll ($sQuery);
  453. foreach ($a as $r) {
  454. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_id` = ?", $r['cmt_id']);
  455. $this->query ($sQuery);
  456. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = ?", $r['cmt_parent_id']);
  457. $this->query ($sQuery);
  458. $aFilesMore = $this->convertImagesArray($this->getFiles($aSystem['system_id'], $r['cmt_id']));
  459. $this->deleteImages($aSystem['system_id'], $r['cmt_id']);
  460. if ($aFilesMore && null !== $aFiles)
  461. $aFiles = array_merge($aFiles, $aFilesMore);
  462. if (null !== $aCmtIds)
  463. $aCmtIds[] = $r['cmt_id'];
  464. $isDelOccured = 1;
  465. }
  466. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_author_id` = 0 WHERE `cmt_author_id` = ? AND `cmt_replies` != 0", $iAuthorId);
  467. $this->query ($sQuery);
  468. if ($isDelOccured)
  469. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  470. }
  471. function deleteObjectComments ($iObjectId, &$aFilesReturn = null, &$aCmtIds = null)
  472. {
  473. $aSystem = $this->_oMain->getSystemInfo();
  474. $aFiles = $this->convertImagesArray($this->getFiles($aSystem['system_id'], false, $iObjectId));
  475. if ($aFiles) {
  476. $sQuery = $this->prepare("DELETE FROM {$this->_sTableFiles2Entries} WHERE `system_id` = ? AND `image_id` IN(" . $this->implode_escape($aFiles) . ")", $aSystem['system_id']);
  477. $this->query($sQuery);
  478. }
  479. if (null !== $aCmtIds) {
  480. $sQuery = $this->prepare("SELECT `cmt_id` FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  481. $aCmtIds = $this->getColumn ($sQuery);
  482. }
  483. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  484. $this->query ($sQuery);
  485. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  486. if (null !== $aFilesReturn)
  487. $aFilesReturn = $aFiles;
  488. }
  489. function deleteAll ($iSystemId, &$aFiles = null, &$aCmtIds = null)
  490. {
  491. // get files
  492. if (null !== $aFiles)
  493. $aFiles = $this->convertImagesArray($this->getFiles($iSystemId, false));
  494. // delete files
  495. $this->deleteImages($iSystemId, false);
  496. if (null !== $aCmtIds)
  497. $aCmtIds = $this->getColumn ("SELECT `cmt_id` FROM {$this->_sTable}");
  498. // delete comments
  499. $sQuery = $this->prepare("TRUNCATE TABLE {$this->_sTable}");
  500. $this->query ($sQuery);
  501. }
  502. function deleteCmtIds ($iSystemId, $iCmtId)
  503. {
  504. $sQuery = $this->prepare("DELETE FROM {$this->_sTableIds} WHERE `system_id` = ? AND `cmt_id` = ?", $iSystemId, $iCmtId);
  505. return $this->query ($sQuery);
  506. }
  507. function getObjectAuthorId($iId)
  508. {
  509. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldAuthor}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  510. return $this->getOne($sQuery);
  511. }
  512. function getObjectTitle($iId)
  513. {
  514. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldTitle}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  515. return $this->getOne($sQuery);
  516. }
  517. function getObjectPrivacyView($iId, $sField = '')
  518. {
  519. if(empty($sField)) {
  520. $sField = 'allow_view_to';
  521. if(!$this->isFieldExists($this->_sTriggerTable, $sField))
  522. return false;
  523. }
  524. return $this->getOne("SELECT `{$sField}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = :id LIMIT 1", array(
  525. 'id' => $iId
  526. ));
  527. }
  528. function updateTriggerTable($iId, $iCount)
  529. {
  530. if (!$this->_sTriggerFieldComments)
  531. return true;
  532. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldComments}` = ? WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iCount, $iId);
  533. return $this->query($sQuery);
  534. }
  535. function getUniqId($iSystemId, $iCmtId, $iAuthorId = 0)
  536. {
  537. $sQuery = $this->prepare("SELECT `id` FROM `{$this->_sTableIds}` WHERE `system_id` = ? AND `cmt_id` = ?", $iSystemId, $iCmtId);
  538. if(($iUniqId = (int)$this->getOne($sQuery)) != 0)
  539. return $iUniqId;
  540. if(empty($iAuthorId))
  541. $iAuthorId = bx_get_logged_profile_id();
  542. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableIds}` SET `system_id` = ?, `cmt_id` = ?, `author_id` = ?", $iSystemId, $iCmtId, $iAuthorId);
  543. if(!$this->query($sQuery))
  544. return false;
  545. return $this->lastId();
  546. }
  547. protected function convertImagesArray($a)
  548. {
  549. if (!$a || !is_array($a))
  550. return array();
  551. $aFiles = array ();
  552. foreach ($a as $aFile)
  553. $aFiles[] = $aFile['image_id'];
  554. return $aFiles;
  555. }
  556. }
  557. /** @} */