BxDolCmtsQuery.php 34 KB

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