BxDolCmtsQuery.php 37 KB

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