BxDolCmtsQuery.php 29 KB

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