BxDolCmtsQuery.php 37 KB

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