BxDolCmtsQuery.php 22 KB

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