BxDolCmtsQuery.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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 $_sTableSystems;
  24. protected $_sTableIds;
  25. public function __construct(&$oMain)
  26. {
  27. $this->_sTableSystems = BxDolCmts::$sTableSystems;
  28. $this->_sTableIds = BxDolCmts::$sTableIds;
  29. $this->_oMain = $oMain;
  30. $this->_sTableFiles = $this->_oMain->getTableNameImages();
  31. $this->_sTableFiles2Entries = $this->_oMain->getTableNameImages2Entries();
  32. $aSystem = $this->_oMain->getSystemInfo();
  33. $this->_sTable = $aSystem['table'];
  34. $this->_sTriggerTable = $aSystem['trigger_table'];
  35. $this->_sTriggerFieldId = $aSystem['trigger_field_id'];
  36. $this->_sTriggerFieldAuthor = $aSystem['trigger_field_author'];
  37. $this->_sTriggerFieldTitle = $aSystem['trigger_field_title'];
  38. $this->_sTriggerFieldComments = $aSystem['trigger_field_comments'];
  39. parent::__construct();
  40. }
  41. public static function getSystemBy($aParams)
  42. {
  43. $oDb = BxDolDb::getInstance();
  44. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query', 1 => []]];
  45. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  46. $sSelectClause = "`ts`.*";
  47. switch($aParams['type']) {
  48. case 'name':
  49. $aMethod['name'] = 'getRow';
  50. $aMethod['params'][1]['name'] = $aParams['name'];
  51. $sWhereClause .= " AND `ts`.`Name` = :name";
  52. break;
  53. case 'all':
  54. break;
  55. }
  56. if(!empty($sOrderClause))
  57. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  58. if(!empty($sLimitClause))
  59. $sLimitClause = 'LIMIT ' . $sLimitClause;
  60. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . BxDolCmts::$sTableSystems . "` AS `ts` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  61. return call_user_func_array(array($oDb, $aMethod['name']), $aMethod['params']);
  62. }
  63. public static function getInfoBy($aParams)
  64. {
  65. $oDb = BxDolDb::getInstance();
  66. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  67. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  68. $sSelectClause = "`ti`.*";
  69. switch($aParams['type']) {
  70. case 'uniq_id':
  71. $aMethod['name'] = 'getRow';
  72. $aMethod['params'][1]['uniq_id'] = (int)$aParams['uniq_id'];
  73. $sWhereClause .= " AND `ti`.`id` = :uniq_id";
  74. break;
  75. case 'all':
  76. if(isset($aParams['count']) && $aParams['count'] === true) {
  77. $aMethod['name'] = 'getOne';
  78. $sSelectClause = "COUNT(`ti`.`id`)";
  79. }
  80. break;
  81. }
  82. if(!empty($sOrderClause))
  83. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  84. if(!empty($sLimitClause))
  85. $sLimitClause = 'LIMIT ' . $sLimitClause;
  86. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . BxDolCmts::$sTableIds . "` AS `ti` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  87. return call_user_func_array(array($oDb, $aMethod['name']), $aMethod['params']);
  88. }
  89. public static function getInfoByUniqId($iUniqId)
  90. {
  91. $oDb = BxDolDb::getInstance();
  92. $sQuery = "SELECT
  93. `ti`.`cmt_id` AS `cmt_id`,
  94. `to`.`Name` AS `system_name`,
  95. `to`.`Table` AS `table_name`
  96. FROM `" . BxDolCmts::$sTableIds . "` AS `ti`
  97. INNER JOIN `" . BxDolCmts::$sTableSystems . "` AS `to` ON `ti`.`system_id` = `to`.`ID`
  98. WHERE `ti`.`id` = :uniq_ig
  99. LIMIT 1";
  100. $aRow = $oDb->getRow($sQuery, array('uniq_ig' => $iUniqId));
  101. if(empty($aRow) || !is_array($aRow))
  102. return $aRow;
  103. $aRow['cmt_object_id'] = $oDb->getOne("SELECT `cmt_object_id` FROM `" . $aRow['table_name'] . "` WHERE `cmt_id` = :cmt_id LIMIT 1", array(
  104. 'cmt_id' => $aRow['cmt_id']
  105. ));
  106. return $aRow;
  107. }
  108. /**
  109. * @deprecated since version 12.0.1
  110. */
  111. public static function getCommentByUniq ($iUnicId)
  112. {
  113. return self::getInfoByUniqId($iUnicId);
  114. }
  115. public static function getCommentSimpleByUniqId($iUniqId)
  116. {
  117. $oDb = BxDolDb::getInstance();
  118. $sQuery = "SELECT
  119. `ti`.`cmt_id` AS `cmt_id`,
  120. `to`.`Table` AS `cmt_table`
  121. FROM `" . BxDolCmts::$sTableIds . "` as `ti`
  122. INNER JOIN `" . BxDolCmts::$sTableSystems . "` as `to` ON `ti`.`system_id` = `to`.`ID`
  123. WHERE `ti`.`id` = :uniq_ig
  124. LIMIT 1";
  125. $aData = $oDb->getRow($sQuery, array('uniq_ig' => $iUniqId));
  126. if(empty($aData) || !is_array($aData))
  127. return array();
  128. return $oDb->getRow("SELECT * FROM `" . $aData['cmt_table'] . "` WHERE `cmt_id` = :cmt_id LIMIT 1", array(
  129. 'cmt_id' => $aData['cmt_id']
  130. ));
  131. }
  132. public static function getCommentExtendedByUniqId($iUniqId)
  133. {
  134. $oDb = BxDolDb::getInstance();
  135. $sQuery = "SELECT
  136. `ti`.`cmt_id` AS `cmt_id`,
  137. `ti`.`system_id` AS `cmt_system_id`,
  138. `to`.`Name` AS `system_name`,
  139. `to`.`Table` AS `cmt_table`
  140. FROM `" . BxDolCmts::$sTableIds . "` AS `ti`
  141. INNER JOIN `" . BxDolCmts::$sTableSystems . "` AS `to` ON `ti`.`system_id` = `to`.`ID`
  142. WHERE `ti`.`id` = :uniq_ig
  143. LIMIT 1";
  144. $aData = $oDb->getRow($sQuery, array('uniq_ig' => $iUniqId));
  145. if(empty($aData) || !is_array($aData))
  146. return array();
  147. $sQuery = "SELECT
  148. `tc`.*,
  149. `ti`.`rate`, `ti`.`votes`,
  150. `ti`.`rrate`, `ti`.`rvotes`,
  151. `ti`.`score`, `ti`.`sc_up`, `ti`.`sc_down`,
  152. `ti`.`reports`,
  153. :system_name AS `system_name`
  154. FROM `" . $aData['cmt_table'] . "` AS `tc`
  155. LEFT JOIN `" . BxDolCmts::$sTableIds . "` AS `ti` ON `ti`.`system_id` = :cmt_system_id AND `tc`.`cmt_id` = `ti`.`cmt_id`
  156. WHERE `tc`.`cmt_id` = :cmt_id
  157. LIMIT 1";
  158. return $oDb->getRow($sQuery, array(
  159. 'cmt_id' => $aData['cmt_id'],
  160. 'cmt_system_id' => $aData['cmt_system_id'],
  161. 'system_name' => $aData['system_name']
  162. ));
  163. }
  164. function getTableName ()
  165. {
  166. return $this->_sTable;
  167. }
  168. /**
  169. * @deprecated since version 10.0.0-B3 and can be removed in later versions.
  170. */
  171. function setTableNameFiles($sTable)
  172. {
  173. $this->_sTableFiles = $sTable;
  174. }
  175. /**
  176. * @deprecated since version 10.0.0-B3 and can be removed in later versions.
  177. */
  178. function setTableNameFiles2Entries($sTable)
  179. {
  180. $this->_sTableFiles2Entries = $sTable;
  181. }
  182. function getCommentsCountAll ($iId, $iAuthorId = 0, $bForceCalculate = false)
  183. {
  184. $iCount = false;
  185. /**
  186. * @hooks
  187. * @hookdef hook-comment-get_comments_count 'comment', 'get_comments_count' - hook to override number of comments for commented object
  188. * - $unit_name - equals `comment`
  189. * - $action - equals `get_comments_count`
  190. * - $object_id - not used
  191. * - $sender_id - profile id
  192. * - $extra_params - array of additional params with the following array keys:
  193. * - `system` - [string] comment object name
  194. * - `object_id` - [int] commented object id
  195. * - `result` - [int] by ref, number of comments, can be overridden in hook processing
  196. * @hook @ref hook-comment-get_comments_count
  197. */
  198. bx_alert('comment', 'get_comments_count', 0, $iAuthorId, [
  199. 'system' => $this->_oMain->getSystemInfo(),
  200. 'object_id' => $iId,
  201. 'result' => &$iCount
  202. ]);
  203. if ($iCount !== false)
  204. return $iCount;
  205. if ($this->_sTriggerFieldComments && !$bForceCalculate)
  206. return (int)$this->getOne("SELECT `{$this->_sTriggerFieldComments}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = :id", [
  207. 'id' => $iId
  208. ]);
  209. else
  210. return $this->getCommentsCount($iId, -1, $iAuthorId);
  211. }
  212. function getCommentsCount ($iId, $iCmtVParentId = -1, $iAuthorId = 0, $sFilter = '')
  213. {
  214. $aBindings = array(
  215. 'cmt_object_id' => $iId,
  216. 'system_id' => $this->_oMain->getSystemId()
  217. );
  218. $sWhereClause = $this->getCommentsCheckStatus($iAuthorId);
  219. if((int)$iCmtVParentId >= 0) {
  220. $aBindings['cmt_vparent_id'] = $iCmtVParentId;
  221. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_vparent_id` = :cmt_vparent_id";
  222. }
  223. $sJoinClause = '';
  224. switch($sFilter) {
  225. case BX_CMT_FILTER_FRIENDS:
  226. case BX_CMT_FILTER_SUBSCRIPTIONS:
  227. $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
  228. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  229. $sJoinClause .= ' ' . $aQueryParts['join'];
  230. break;
  231. case BX_CMT_FILTER_OTHERS:
  232. $aBindings['cmt_author_id'] = $iAuthorId;
  233. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` <> :cmt_author_id";
  234. break;
  235. }
  236. if(($oCf = $this->_oMain->getObjectContentFilter()) !== false)
  237. $sWhereClause .= $oCf->getSQLParts($this->_sTable, 'cmt_cf');
  238. /**
  239. * @hooks
  240. * @hookdef hook-comment-get_comments 'comment', 'get_comments' - hook to override comments list. Is used during comments count retrieving.
  241. * - $unit_name - equals `comment`
  242. * - $action - equals `get_comments`
  243. * - $object_id - not used
  244. * - $sender_id - currently logged in profile id
  245. * - $extra_params - array of additional params with the following array keys:
  246. * - `system` - [string] comment object name
  247. * - `join_clause` - [string] by ref, 'join' part of SQL query, can be overridden in hook processing
  248. * - `where_clause` - [string] by ref, 'where' part of SQL query, can be overridden in hook processing
  249. * - `params` - [array] by ref, SQL query bindings array as key&value pairs, can be overridden in hook processing
  250. * @hook @ref hook-comment-get_comments
  251. */
  252. bx_alert('comment', 'get_comments', 0, bx_get_logged_profile_id(), [
  253. 'system' => $this->_oMain->getSystemInfo(),
  254. 'join_clause' => &$sJoinClause,
  255. 'where_clause' => &$sWhereClause,
  256. 'params' => &$aBindings,
  257. 'join_clause_ref' => &$sJoinClause,
  258. 'where_clause_ref' => &$sWhereClause,
  259. 'params_ref' => &$aBindings
  260. ]);
  261. $sQuery = "SELECT
  262. COUNT(*)
  263. FROM `{$this->_sTable}`
  264. LEFT JOIN `{$this->_sTableIds}` ON (`{$this->_sTable}`.`cmt_id` = `{$this->_sTableIds}`.`cmt_id` AND `{$this->_sTableIds}`.`system_id` = :system_id) $sJoinClause
  265. WHERE `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id" . $sWhereClause;
  266. return (int)$this->getOne($sQuery, $aBindings);
  267. }
  268. function getStructure($iObjectId, $iAuthorId = 0, $iParentId = 0, $sFilter = '', $aOrder = array())
  269. {
  270. $sSelectClause = $sJoinClause = $sWhereClause = "";
  271. $aBindings = array(
  272. 'cmt_object_id' => $iObjectId,
  273. 'system_id' => $this->_oMain->getSystemId()
  274. );
  275. if((int)$iParentId >= 0) {
  276. $aBindings['cmt_parent_id'] = $iParentId;
  277. $sWhereClause .= " AND `tc`.`cmt_parent_id` = :cmt_parent_id";
  278. }
  279. if(in_array($sFilter, array(BX_CMT_FILTER_FRIENDS, BX_CMT_FILTER_SUBSCRIPTIONS))) {
  280. $sConnection = $this->_oMain->getConnectionObject($sFilter);
  281. $aQueryParts = BxDolConnection::getObjectInstance($sConnection)->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  282. $sJoinClause .= ' ' . $aQueryParts['join'];
  283. }
  284. if(isset($aOrder['by']) && isset($aOrder['way']))
  285. switch($aOrder['by']) {
  286. case BX_CMT_ORDER_BY_DATE:
  287. $sSelectClause .= ", `tc`.`cmt_time` AS `cmt_order`";
  288. break;
  289. case BX_CMT_ORDER_BY_POPULAR:
  290. $aOrderFields = array();
  291. if($this->_oMain->getVoteObject(0) !== false)
  292. $aOrderFields[] = "`ti`.`votes`";
  293. if($this->_oMain->getScoreObject(0) !== false)
  294. $aOrderFields[] = "`ti`.`score`";
  295. if(!empty($aOrderFields))
  296. $sSelectClause .= ", (" . implode(' + ', $aOrderFields) . ") AS `cmt_order`";
  297. else
  298. $sSelectClause .= ", `tc`.`id` AS `cmt_order`";
  299. break;
  300. }
  301. bx_alert('comment', 'get_comments', 0, $iAuthorId, [
  302. 'data_type' => 'structure',
  303. 'system' => $this->_oMain->getSystemInfo(),
  304. 'select_clause' => &$sSelectClause,
  305. 'join_clause' => &$sJoinClause,
  306. 'where_clause' => &$sWhereClause,
  307. 'params' => &$aBindings,
  308. 'select_clause_ref' => &$sSelectClause,
  309. 'join_clause_ref' => &$sJoinClause,
  310. 'where_clause_ref' => &$sWhereClause,
  311. 'params_ref' => &$aBindings
  312. ]);
  313. $sQuery = "SELECT
  314. `tc`.`cmt_id`,
  315. `tc`.`cmt_replies`,
  316. `tc`.`cmt_time` $sSelectClause
  317. FROM `{$this->_sTable}` AS `tc`
  318. LEFT JOIN `{$this->_sTableIds}` AS `ti` ON `tc`.`cmt_id` = `ti`.`cmt_id` AND `ti`.`system_id` = :system_id $sJoinClause
  319. WHERE `tc`.`cmt_object_id` = :cmt_object_id" . $sWhereClause;
  320. return $this->getAll($sQuery, $aBindings);
  321. }
  322. function getComments ($iId, $iCmtVParentId = 0, $iAuthorId = 0, $sFilter = '', $aOrder = array(), $iStart = 0, $iCount = -1)
  323. {
  324. $aBindings = array(
  325. 'cmt_object_id' => $iId,
  326. 'system_id' => $this->_oMain->getSystemId()
  327. );
  328. $sFields = $sJoin = "";
  329. $sWhereStatus = $this->getCommentsCheckStatus($iAuthorId);
  330. $sWhereParent = '';
  331. if((int)$iCmtVParentId >= 0) {
  332. $aBindings['cmt_vparent_id'] = $iCmtVParentId;
  333. $sWhereParent = " AND `{$this->_sTable}`.`cmt_vparent_id` = :cmt_vparent_id";
  334. }
  335. $sWhereFilter = '';
  336. switch($sFilter) {
  337. case BX_CMT_FILTER_PINNED:
  338. $sWhereFilter .= " AND `{$this->_sTable}`.`cmt_pinned` <> 0";
  339. break;
  340. case BX_CMT_FILTER_FRIENDS:
  341. case BX_CMT_FILTER_SUBSCRIPTIONS:
  342. $oConnection = BxDolConnection::getObjectInstance($this->_oMain->getConnectionObject($sFilter));
  343. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sTable, 'cmt_author_id', $iAuthorId);
  344. $sJoin .= ' ' . $aQueryParts['join'];
  345. break;
  346. }
  347. $sWhereCf = '';
  348. if(($oCf = $this->_oMain->getObjectContentFilter()) !== false)
  349. $sWhereCf = $oCf->getSQLParts($this->_sTable, 'cmt_cf');
  350. $sOrder = " ORDER BY `{$this->_sTable}`.`cmt_pinned` DESC, `{$this->_sTable}`.`cmt_time` ASC";
  351. if(isset($aOrder['by']) && isset($aOrder['way'])) {
  352. $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);
  353. switch($aOrder['by']) {
  354. case BX_CMT_ORDER_BY_DATE:
  355. $sOrder = " ORDER BY `{$this->_sTable}`.`cmt_time` " . $aOrder['way'];
  356. break;
  357. case BX_CMT_ORDER_BY_POPULAR:
  358. $aSortFields = array();
  359. if($this->_oMain->getVoteObject(0) !== false)
  360. array_push($aSortFields, '`' . $this->_sTableIds . '`.`votes`');
  361. if($this->_oMain->getReactionObject(0) !== false)
  362. array_push($aSortFields, '`' . $this->_sTableIds . '`.`rvotes`');
  363. if($this->_oMain->getScoreObject(0) !== false)
  364. array_push($aSortFields, '`' . $this->_sTableIds . '`.`score`');
  365. if(count($aSortFields) == 0)
  366. array_push($aSortFields, '`' . $this->_sTable . '`.`id`');
  367. $sOrder = " ORDER BY " . implode($aOrder['way'] . ', ', $aSortFields) . " " . $aOrder['way'];
  368. break;
  369. }
  370. }
  371. $sLimit = $iCount != -1 ? $this->prepareAsString(" LIMIT ?, ?", (int)$iStart, (int)$iCount) : '';
  372. $sWhereClause = $sWhereStatus . $sWhereParent . $sWhereFilter . $sWhereCf;
  373. $sQuery = "SELECT
  374. `{$this->_sTable}`.*,
  375. `{$this->_sTableIds}`.`id` AS `cmt_unique_id`,
  376. `{$this->_sTableIds}`.`status_admin` AS `cmt_status_admin`
  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` = :system_id)
  380. LEFT JOIN `sys_profiles` AS `p` ON `p`.`id` = `{$this->_sTable}`.`cmt_author_id`";
  381. /**
  382. * @hooks
  383. * @hookdef hook-comment-get_comments 'comment', 'get_comments' - hook to override comments list.
  384. * - $unit_name - equals `comment`
  385. * - $action - equals `get_comments`
  386. * - $object_id - not used
  387. * - $sender_id - profile id
  388. * - $extra_params - array of additional params with the following array keys:
  389. * - `system` - [string] comment object name
  390. * - `select_clause` - [string] by ref, 'select' part of SQL query, can be overridden in hook processing
  391. * - `join_clause` - [string] by ref, 'join' part of SQL query, can be overridden in hook processing
  392. * - `where_clause` - [string] by ref, 'where' part of SQL query, can be overridden in hook processing
  393. * - `order_clause` - [string] by ref, 'order' part of SQL query, can be overridden in hook processing
  394. * - `limit_clause` - [string] by ref, 'limit' part of SQL query, can be overridden in hook processing
  395. * - `params` - [array] by ref, SQL query bindings array as key&value pairs, can be overridden in hook processing
  396. * @hook @ref hook-comment-get_comments
  397. */
  398. bx_alert('comment', 'get_comments', 0, $iAuthorId, [
  399. 'system' => $this->_oMain->getSystemInfo(),
  400. 'select_clause' => &$sQuery,
  401. 'join_clause' => &$sJoin,
  402. 'where_clause' => &$sWhereClause,
  403. 'order_clause' => &$sOrder,
  404. 'limit_clause' => &$sLimit,
  405. 'params' => &$aBindings,
  406. 'select_clause_ref' => &$sQuery,
  407. 'join_clause_ref' => &$sJoin,
  408. 'where_clause_ref' => &$sWhereClause,
  409. 'order_clause_ref' => &$sOrder,
  410. 'limit_clause_ref' => &$sLimit,
  411. 'params_ref' => &$aBindings
  412. ]);
  413. $sQuery = $sQuery . $sJoin . " WHERE `{$this->_sTable}`.`cmt_object_id`=:cmt_object_id AND (ISNULL(`p`.`status`) OR `p`.`status`='active' OR `{$this->_sTable}`.`cmt_replies`!=0)" . $sWhereClause . $sOrder . $sLimit;
  414. return $this->getAll($sQuery, $aBindings);
  415. }
  416. protected function getCommentsCheckStatus($iAuthorId, $sStatus = BX_CMT_STATUS_ACTIVE)
  417. {
  418. if($this->_oMain->isModerator())
  419. return '';
  420. //--- Check viewer as comment author.
  421. $sWhereClause = $this->prepareAsString("`{$this->_sTable}`.`cmt_author_id`=?", $iAuthorId);
  422. //--- Check viewer as an administrator/moderator of comment author.
  423. $aGroups = [];
  424. $aModules = bx_srv('system', 'get_modules_by_type', ['profile']);
  425. foreach($aModules as $aModule) {
  426. $oModule = BxDolModule::getInstance($aModule['name']);
  427. if(!$oModule || !($oModule instanceof BxBaseModGroupsModule))
  428. continue;
  429. $aGroups = array_merge($aGroups, $oModule->getGroupsByFan($iAuthorId, [
  430. BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR,
  431. BX_BASE_MOD_GROUPS_ROLE_MODERATOR
  432. ]));
  433. }
  434. if(!empty($aGroups))
  435. $sWhereClause .= " OR `{$this->_sTable}`.`cmt_author_id` IN (" . $this->implode_escape($aGroups) . ")";
  436. return $this->prepareAsString(" AND IF(" . $sWhereClause . ", 1, `{$this->_sTableIds}`.`status_admin`=?) ", $sStatus);
  437. }
  438. function getCommentsBy($aParams = array())
  439. {
  440. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  441. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  442. $sSelectClause = "`{$this->_sTable}`.*";
  443. if(isset($aParams['object_id'])) {
  444. $aMethod['params'][1]['cmt_object_id'] = (int)$aParams['object_id'];
  445. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id";
  446. }
  447. switch($aParams['type']) {
  448. case 'id':
  449. $aMethod['name'] = 'getRow';
  450. $aMethod['params'][1]['id'] = (int)$aParams['id'];
  451. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_id` = :id";
  452. break;
  453. case 'uniq_id':
  454. $aMethod['name'] = 'getRow';
  455. $aMethod['params'][1]['system_id'] = $this->_oMain->getSystemId();
  456. $aMethod['params'][1]['uniq_id'] = (int)$aParams['uniq_id'];
  457. $sJoinClause = "LEFT JOIN `{$this->_sTableIds}` ON `{$this->_sTable}`.`cmt_id` = `{$this->_sTableIds}`.`cmt_id` AND `{$this->_sTableIds}`.`system_id` = :system_id";
  458. $sWhereClause .= " AND `{$this->_sTableIds}`.`id` = :uniq_id";
  459. break;
  460. case 'latest':
  461. if(!empty($aParams['object_id'])) {
  462. $aMethod['params'][1]['cmt_object_id'] = (int)$aParams['object_id'];
  463. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id";
  464. }
  465. if(!empty($aParams['author'])) {
  466. $aMethod['params'][1]['cmt_author_id'] = (int)$aParams['author'];
  467. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` " . (isset($aParams['others']) && (int)$aParams['others'] == 1 ? "<>" : "=") . " :cmt_author_id";
  468. }
  469. $sOrderClause = "`{$this->_sTable}`.`cmt_time` DESC";
  470. $sLimitClause = "";
  471. if(isset($aParams['per_page']))
  472. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  473. break;
  474. case 'parent_id':
  475. $aMethod['params'][1]['cmt_parent_id'] = (int)$aParams['parent_id'];
  476. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_parent_id` = :cmt_parent_id";
  477. $sOrderClause = "`{$this->_sTable}`.`cmt_time` ASC";
  478. $sLimitClause = "";
  479. if(isset($aParams['per_page']))
  480. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  481. break;
  482. case 'object_id':
  483. $aMethod['params'][1]['cmt_object_id'] = (int)$aParams['object_id'];
  484. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_object_id` = :cmt_object_id";
  485. $sOrderClause = "`{$this->_sTable}`.`cmt_time` " . (!empty($aParams['order_way']) ? strtoupper($aParams['order_way']) : "ASC");
  486. $sLimitClause = "";
  487. if(isset($aParams['per_page']))
  488. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  489. break;
  490. case 'author_id':
  491. $aMethod['params'][1]['cmt_author_id'] = (int)$aParams['author_id'];
  492. $sWhereClause .= " AND `{$this->_sTable}`.`cmt_author_id` = :cmt_author_id";
  493. $sOrderClause = "`{$this->_sTable}`.`cmt_time` ASC";
  494. $sLimitClause = "";
  495. if(isset($aParams['per_page']))
  496. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  497. break;
  498. case 'search_ids':
  499. $aMethod['name'] = 'getColumn';
  500. $sSelectClause = "`{$this->_sTable}`.`cmt_id`";
  501. if (!empty($aParams['start']) && !empty($aParams['per_page']))
  502. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['per_page']);
  503. elseif (!empty($aParams['per_page']))
  504. $sLimitClause = $this->prepareAsString("?", $aParams['per_page']);
  505. BxDolSearchExtended::processParams($aParams['search_params'], [
  506. 'table' => $this->_sTable,
  507. 'bindings' => &$aMethod['params'][1],
  508. 'where_clause' => &$sWhereClause,
  509. 'bindings_ref' => &$aMethod['params'][1],
  510. 'where_clause_ref' => &$sWhereClause
  511. ]);
  512. if(($oCf = $this->_oMain->getObjectContentFilter()) !== false)
  513. $sWhereClause .= $oCf->getSQLParts($this->_sTable, 'cmt_cf');
  514. $sOrderClause .= "`{$this->_sTable}`.`cmt_time` ASC";
  515. break;
  516. case 'all_ids':
  517. $aMethod['name'] = 'getColumn';
  518. $sSelectClause = "`{$this->_sTable}`.`cmt_id`";
  519. $sOrderClause = "`{$this->_sTable}`.`cmt_time` ASC";
  520. break;
  521. }
  522. if(!empty($sOrderClause))
  523. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  524. if(!empty($sLimitClause))
  525. $sLimitClause = 'LIMIT ' . $sLimitClause;
  526. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `{$this->_sTable}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  527. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  528. }
  529. function getComment ($iId, $iCmtId)
  530. {
  531. $sFields = $sJoin = "";
  532. $oVote = $this->_oMain->getVoteObject($iCmtId);
  533. if($oVote !== false) {
  534. $aSql = $oVote->getSqlParts($this->_sTableIds, 'id');
  535. $sFields .= $aSql['fields'];
  536. $sJoin .= $aSql['join'];
  537. }
  538. $sQuery = $this->prepare("SELECT
  539. `{$this->_sTable}`.*,
  540. `{$this->_sTableIds}`.`id` AS `cmt_unique_id`,
  541. `{$this->_sTableIds}`.`status_admin` AS `cmt_status_admin`
  542. $sFields
  543. FROM `{$this->_sTable}`
  544. LEFT JOIN `{$this->_sTableIds}` ON (`{$this->_sTable}`.`cmt_id` = `{$this->_sTableIds}`.`cmt_id` AND `{$this->_sTableIds}`.`system_id` = ?)
  545. $sJoin
  546. WHERE `{$this->_sTable}`.`cmt_object_id` = ? AND `{$this->_sTable}`.`cmt_id` = ?
  547. LIMIT 1", $this->_oMain->getSystemId(), $iId, $iCmtId);
  548. return $this->getRow($sQuery);
  549. }
  550. function getCommentSimple ($iId, $iCmtId)
  551. {
  552. $sQuery = $this->prepare("SELECT * FROM {$this->_sTable} AS `c` WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  553. return $this->getRow($sQuery);
  554. }
  555. function removeComment ($iId, $iCmtId, $iCmtParentId)
  556. {
  557. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ? AND `cmt_id` = ? LIMIT 1", $iId, $iCmtId);
  558. if (!$this->query($sQuery))
  559. return false;
  560. if($iCmtParentId)
  561. $this->updateRepliesCount($iCmtParentId, -1);
  562. return true;
  563. }
  564. function saveImages($iSystemId, $iCmtId, $iImageId)
  565. {
  566. $sQuery = $this->prepare("INSERT IGNORE INTO `{$this->_sTableFiles2Entries}` SET `system_id`=?, `cmt_id`=?, `image_id`=?", $iSystemId, $iCmtId, $iImageId);
  567. return (int)$this->query($sQuery) > 0;
  568. }
  569. function getFiles($iSystemId, $iCmtId, $iId = false)
  570. {
  571. $aBindings = array(
  572. 'system_id' => $iSystemId
  573. );
  574. $sJoin = "";
  575. $sWhere = " AND `tf2e`.`system_id` = :system_id ";
  576. if($iCmtId !== false) {
  577. $aBindings['cmt_id'] = $iCmtId;
  578. $sWhere .= " AND `tf2e`.`cmt_id` = :cmt_id ";
  579. }
  580. if($iId !== false) {
  581. $aBindings['cmt_object_id'] = $iId;
  582. $sWhere .= " AND `te`.`cmt_object_id` = :cmt_object_id";
  583. $sJoin .= " INNER JOIN `{$this->_sTable}` AS `te` ON (`tf2e`.`cmt_id` = `te`.`cmt_id`) ";
  584. }
  585. $sQuery = "SELECT
  586. `tf2e`.*,
  587. `tf`.`file_name` AS `file_name`,
  588. `tf`.`mime_type` AS `mime_type`,
  589. `tf`.`size` AS `size`,
  590. `tf`.`dimensions` AS `dimensions`
  591. FROM `{$this->_sTableFiles2Entries}` AS `tf2e`
  592. LEFT JOIN `{$this->_sTableFiles}` AS `tf` ON (`tf2e`.`image_id` = `tf`.`id`) " . $sJoin . "
  593. WHERE 1 " . $sWhere;
  594. return $this->getAll($sQuery, $aBindings);
  595. }
  596. public function getFileInfoById($iFileId)
  597. {
  598. $sQuery = "SELECT
  599. `tf2e`.*,
  600. `tf`.`file_name` AS `file_name`,
  601. `tf`.`mime_type` AS `mime_type`,
  602. `tf`.`size` AS `size`
  603. FROM `{$this->_sTableFiles2Entries}` AS `tf2e`
  604. LEFT JOIN `{$this->_sTableFiles}` AS `tf` ON (`tf2e`.`image_id` = `tf`.`id`)
  605. WHERE `tf2e`.`id`=:id ";
  606. return $this->getRow($sQuery, array(
  607. 'id' => $iFileId
  608. ));
  609. }
  610. function deleteImages($iSystemId, $iCmtId, $iImageId = false)
  611. {
  612. $sWhereAddon = "";
  613. $aBindings = array();
  614. if ($iSystemId !== false) {
  615. $aBindings['system_id'] = $iSystemId;
  616. $sWhereAddon .= " AND `system_id` = :system_id ";
  617. }
  618. if ($iCmtId !== false) {
  619. $aBindings['cmt_id'] = $iCmtId;
  620. $sWhereAddon .= " AND `cmt_id` = :cmt_id ";
  621. }
  622. if ($iImageId !== false) {
  623. $aBindings['image_id'] = $iImageId;
  624. $sWhereAddon .= " AND `image_id` = :image_id ";
  625. }
  626. return $this->query("DELETE FROM `{$this->_sTableFiles2Entries}` WHERE 1" . $sWhereAddon, $aBindings);
  627. }
  628. function updateComments($aSetClause, $aWhereClause)
  629. {
  630. if(empty($aSetClause) || empty($aWhereClause))
  631. return;
  632. return (int)$this->query("UPDATE `{$this->_sTable}` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  633. }
  634. function updateRepliesCount($iCmtId, $iCount)
  635. {
  636. $sQuery = $this->prepare("UPDATE `{$this->_sTable}` SET `cmt_replies`=`cmt_replies`+? WHERE `cmt_id`=? LIMIT 1", $iCount, $iCmtId);
  637. return $this->query($sQuery);
  638. }
  639. function deleteAuthorComments ($iAuthorId, &$aFiles = null, &$aCmtIds = null)
  640. {
  641. $aSystem = $this->_oMain->getSystemInfo();
  642. $isDelOccured = 0;
  643. $sQuery = $this->prepare("SELECT `cmt_id`, `cmt_parent_id` FROM {$this->_sTable} WHERE `cmt_author_id` = ? AND `cmt_replies` = 0", $iAuthorId);
  644. $a = $this->getAll ($sQuery);
  645. foreach ($a as $r) {
  646. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_id` = ?", $r['cmt_id']);
  647. $this->query ($sQuery);
  648. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_replies` = `cmt_replies` - 1 WHERE `cmt_id` = ?", $r['cmt_parent_id']);
  649. $this->query ($sQuery);
  650. $aFilesMore = $this->convertImagesArray($this->getFiles($aSystem['system_id'], $r['cmt_id']));
  651. $this->deleteImages($aSystem['system_id'], $r['cmt_id']);
  652. if ($aFilesMore && null !== $aFiles)
  653. $aFiles = array_merge($aFiles, $aFilesMore);
  654. if (null !== $aCmtIds)
  655. $aCmtIds[] = $r['cmt_id'];
  656. $isDelOccured = 1;
  657. }
  658. $sQuery = $this->prepare("UPDATE {$this->_sTable} SET `cmt_author_id` = 0 WHERE `cmt_author_id` = ? AND `cmt_replies` != 0", $iAuthorId);
  659. $this->query ($sQuery);
  660. if ($isDelOccured)
  661. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  662. }
  663. function deleteObjectComments ($iObjectId, &$aFilesReturn = null, &$aCmtIds = null)
  664. {
  665. $aSystem = $this->_oMain->getSystemInfo();
  666. $aFiles = $this->convertImagesArray($this->getFiles($aSystem['system_id'], false, $iObjectId));
  667. if ($aFiles) {
  668. $sQuery = $this->prepare("DELETE FROM {$this->_sTableFiles2Entries} WHERE `system_id` = ? AND `image_id` IN(" . $this->implode_escape($aFiles) . ")", $aSystem['system_id']);
  669. $this->query($sQuery);
  670. }
  671. if (null !== $aCmtIds) {
  672. $sQuery = $this->prepare("SELECT `cmt_id` FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  673. $aCmtIds = $this->getColumn ($sQuery);
  674. }
  675. $sQuery = $this->prepare("DELETE FROM {$this->_sTable} WHERE `cmt_object_id` = ?", $iObjectId);
  676. $this->query ($sQuery);
  677. $this->query ("OPTIMIZE TABLE {$this->_sTable}");
  678. if (null !== $aFilesReturn)
  679. $aFilesReturn = $aFiles;
  680. }
  681. function deleteAll ($iSystemId, &$aFiles = null, &$aCmtIds = null)
  682. {
  683. // get files
  684. if (null !== $aFiles)
  685. $aFiles = $this->convertImagesArray($this->getFiles($iSystemId, false));
  686. // delete files
  687. $this->deleteImages($iSystemId, false);
  688. if (null !== $aCmtIds)
  689. $aCmtIds = $this->getColumn ("SELECT `cmt_id` FROM {$this->_sTable}");
  690. // delete comments
  691. $sQuery = $this->prepare("TRUNCATE TABLE {$this->_sTable}");
  692. $this->query ($sQuery);
  693. }
  694. function deleteCmtIds ($iSystemId, $iCmtId)
  695. {
  696. $sQuery = $this->prepare("DELETE FROM {$this->_sTableIds} WHERE `system_id` = ? AND `cmt_id` = ?", $iSystemId, $iCmtId);
  697. return $this->query ($sQuery);
  698. }
  699. function getObjectAuthorId($iId)
  700. {
  701. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldAuthor}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  702. return $this->getOne($sQuery);
  703. }
  704. function getObjectTitle($iId)
  705. {
  706. $sQuery = $this->prepare("SELECT `{$this->_sTriggerFieldTitle}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iId);
  707. return $this->getOne($sQuery);
  708. }
  709. function getObjectPrivacyView($iId, $sField = '')
  710. {
  711. if(empty($sField)) {
  712. $sField = 'allow_view_to';
  713. if(!$this->isFieldExists($this->_sTriggerTable, $sField))
  714. return false;
  715. }
  716. return $this->getOne("SELECT `{$sField}` FROM `{$this->_sTriggerTable}` WHERE `{$this->_sTriggerFieldId}` = :id LIMIT 1", array(
  717. 'id' => $iId
  718. ));
  719. }
  720. public function updateTriggerTable($iId, $iCount)
  721. {
  722. if (!$this->_sTriggerFieldComments)
  723. return true;
  724. $sQuery = $this->prepare("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldComments}` = ? WHERE `{$this->_sTriggerFieldId}` = ? LIMIT 1", $iCount, $iId);
  725. return $this->query($sQuery);
  726. }
  727. public function getUniqId($iSystemId, $iCmtId, $aData = [])
  728. {
  729. $sQuery = $this->prepare("SELECT `id` FROM `{$this->_sTableIds}` WHERE `system_id` = ? AND `cmt_id` = ?", $iSystemId, $iCmtId);
  730. if(($iUniqId = (int)$this->getOne($sQuery)) != 0)
  731. return $iUniqId;
  732. $aDataDefault = [
  733. 'system_id' => $iSystemId,
  734. 'cmt_id' => $iCmtId,
  735. 'author_id' => bx_get_logged_profile_id()
  736. ];
  737. if(!$this->query("INSERT INTO `{$this->_sTableIds}` SET " . $this->arrayToSQL(array_merge($aDataDefault, $aData))))
  738. return false;
  739. return $this->lastId();
  740. }
  741. public function updateUniqId($aSetClause, $aWhereClause)
  742. {
  743. if(empty($aSetClause) || empty($aWhereClause))
  744. return;
  745. return (int)$this->query("UPDATE `{$this->_sTableIds}` SET " . $this->arrayToSQL($aSetClause) . " WHERE " . $this->arrayToSQL($aWhereClause)) > 0;
  746. }
  747. protected function convertImagesArray($a)
  748. {
  749. if (!$a || !is_array($a))
  750. return array();
  751. $aFiles = array ();
  752. foreach ($a as $aFile)
  753. $aFiles[] = $aFile['image_id'];
  754. return $aFiles;
  755. }
  756. }
  757. /** @} */