BxDolCmtsQuery.php 27 KB

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