BxDolCmtsQuery.php 23 KB

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