BxDolCmtsQuery.php 37 KB

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