BxDolCmtsQuery.php 33 KB

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