BxTimelineDb.php 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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 Timeline Timeline
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxTimelineDb extends BxBaseModNotificationsDb
  12. {
  13. protected $_sTableSlice;
  14. protected $_sTableEvent2User;
  15. protected $_sTableRepostsTrack;
  16. protected $_sTableHotTrack;
  17. protected $_aTablesMedia;
  18. protected $_aTablesMedia2Events;
  19. /*
  20. * Constructor.
  21. */
  22. function __construct(&$oConfig)
  23. {
  24. parent::__construct($oConfig);
  25. $CNF = &$this->_oConfig->CNF;
  26. $this->_sTableAlias = 'te';
  27. $this->_sTableSlice = $this->_sPrefix . 'events_slice';
  28. $this->_sTableEvent2User = $this->_sPrefix . 'events2users';
  29. $this->_sTableRepostsTrack = $this->_sPrefix . 'reposts_track';
  30. $this->_sTableHotTrack = $this->_sPrefix . 'hot_track';
  31. $this->_aTablesMedia = array(
  32. $CNF['FIELD_PHOTO'] => $this->_sPrefix . 'photos',
  33. $CNF['FIELD_VIDEO'] => $this->_sPrefix . 'videos',
  34. $CNF['FIELD_FILE'] => $this->_sPrefix . 'files'
  35. );
  36. $this->_aTablesMedia2Events = array(
  37. $CNF['FIELD_PHOTO'] => $this->_sPrefix . 'photos2events',
  38. $CNF['FIELD_VIDEO'] => $this->_sPrefix . 'videos2events',
  39. $CNF['FIELD_FILE'] => $this->_sPrefix . 'files2events'
  40. );
  41. }
  42. public function deleteModuleEvents($aData)
  43. {
  44. foreach($aData['handlers'] as $aHandler) {
  45. //Delete system events.
  46. $this->deleteEvent(array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  47. //Delete reposted events.
  48. $aEvents = $this->getEvents(array('browse' => 'reposted_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  49. foreach($aEvents as $aEvent) {
  50. $aContent = unserialize($aEvent['content']);
  51. if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
  52. $this->deleteEvent(array('id' => (int)$aEvent['id']));
  53. }
  54. }
  55. }
  56. public function updateStatusAdmin($iContentId, $isActive)
  57. {
  58. $CNF = $this->_oConfig->CNF;
  59. $sQuery = $this->prepare("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET `active` = ? WHERE `" . $CNF['FIELD_ID'] . "` = ?", $isActive ? '1' : '0', $iContentId);
  60. return $this->query($sQuery);
  61. }
  62. public function activateModuleEvents($aData, $bActivate = true)
  63. {
  64. $iActivate = $bActivate ? 1 : 0;
  65. foreach($aData['handlers'] as $aHandler) {
  66. //Activate (deactivate) system events.
  67. $this->updateEvent(array('active' => $iActivate), array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  68. //Activate (deactivate) reposted events.
  69. $aEvents = $this->getEvents(array('browse' => 'reposted_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  70. foreach($aEvents as $aEvent) {
  71. $aContent = unserialize($aEvent['content']);
  72. if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
  73. $this->updateEvent(array('active' => $iActivate), array('id' => (int)$aEvent['id']));
  74. }
  75. }
  76. }
  77. public function getMaxDuration($aParams)
  78. {
  79. $aParams['browse'] = 'last';
  80. if(isset($aParams['timeline']))
  81. unset($aParams['timeline']);
  82. $aEvent = $this->getEvents($aParams);
  83. if(empty($aEvent) || !is_array($aEvent))
  84. return 0;
  85. $iNowYear = date('Y', time());
  86. return (int)$aEvent['year'] < $iNowYear ? (int)$aEvent['year'] : 0;
  87. }
  88. public function markAsRead($iUserId, $iEventId)
  89. {
  90. return (int)$this->query("INSERT IGNORE INTO `{$this->_sTableEvent2User}` SET `user_id` = :user_id, `event_id` = :event_id", [
  91. 'user_id' => $iUserId,
  92. 'event_id' => $iEventId
  93. ]) !== false;
  94. }
  95. public function cleanRead($iLimit)
  96. {
  97. $aUsers = $this->getAll("SELECT `user_id` AS `id`, COUNT(`event_id`) AS `reads` FROM `{$this->_sTableEvent2User}` WHERE 1 GROUP BY `user_id` HAVING `reads`>:limit", [
  98. 'limit' => $iLimit
  99. ]);
  100. foreach($aUsers as $aUser)
  101. $this->query("DELETE FROM `{$this->_sTableEvent2User}` WHERE `user_id`=:user_id ORDER BY `event_id` LIMIT :limit", [
  102. 'user_id' => $aUser['id'],
  103. 'limit' => (int)$aUser['reads'] - $iLimit
  104. ]);
  105. }
  106. //--- Repost related methods ---//
  107. public function insertRepostTrack($iEventId, $iAuthorId, $sAuthorIp, $iRepostedId)
  108. {
  109. $iNow = time();
  110. $iAuthorNip = bx_get_ip_hash($sAuthorIp);
  111. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableRepostsTrack}` SET `event_id` = ?, `author_id` = ?, `author_nip` = ?, `reposted_id` = ?, `date` = ?", $iEventId, $iAuthorId, $iAuthorNip, $iRepostedId, $iNow);
  112. return (int)$this->query($sQuery) > 0;
  113. }
  114. public function updateRepostTrack($aParamsSet, $aParamsWhere)
  115. {
  116. if(empty($aParamsSet) || empty($aParamsWhere))
  117. return false;
  118. $sSql = "UPDATE `{$this->_sTableRepostsTrack}` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  119. return $this->query($sSql);
  120. }
  121. public function deleteRepostTrack($iEventId)
  122. {
  123. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableRepostsTrack}` WHERE `event_id` = ?", $iEventId);
  124. return (int)$this->query($sQuery) > 0;
  125. }
  126. public function updateRepostCounter($iId, $iCounter, $iIncrement = 1)
  127. {
  128. return (int)$this->updateEvent(array('reposts' => (int)$iCounter + $iIncrement), array('id' => $iId)) > 0;
  129. }
  130. public function getReposted($sType, $sAction, $iObjectId)
  131. {
  132. $bSystem = $this->_oConfig->isSystem($sType, $sAction);
  133. if($bSystem)
  134. $aParams = array('browse' => 'descriptor', 'type' => $sType, 'action' => $sAction, 'object_id' => $iObjectId);
  135. else
  136. $aParams = array('browse' => 'id', 'value' => $iObjectId);
  137. $aReposted = $this->getEvents($aParams);
  138. if($bSystem && (empty($aReposted) || !is_array($aReposted))) {
  139. $iOwnerId = 0;
  140. $iDate = 0;
  141. $sStatus = BX_TIMELINE_STATUS_DELETED;
  142. $mixedResult = $this->_oConfig->getSystemDataByDescriptor($sType, $sAction, $iObjectId);
  143. if(is_array($mixedResult)) {
  144. $iOwnerId = !empty($mixedResult['owner_id']) ? (int)$mixedResult['owner_id'] : 0;
  145. $iDate = !empty($mixedResult['date']) ? (int)$mixedResult['date'] : 0;
  146. if($this->_oConfig->isUnhideRestored() && !empty($iOwnerId) && !empty($iDate))
  147. $sStatus = BX_TIMELINE_STATUS_ACTIVE;
  148. }
  149. $iId = $this->insertEvent(array(
  150. 'owner_id' => $iOwnerId,
  151. 'type' => $sType,
  152. 'action' => $sAction,
  153. 'object_id' => $iObjectId,
  154. 'object_owner_id' => $iOwnerId,
  155. 'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault('object'),
  156. 'content' => '',
  157. 'title' => '',
  158. 'description' => '',
  159. 'date' => $iDate,
  160. 'reacted' => $iDate,
  161. 'status' => $sStatus
  162. ));
  163. $aReposted = $this->getEvents(array('browse' => 'id', 'value' => $iId));
  164. }
  165. return $aReposted;
  166. }
  167. function getRepostedBy($iRepostedId)
  168. {
  169. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableRepostsTrack}` WHERE `reposted_id`=?", $iRepostedId);
  170. return $this->getColumn($sQuery);
  171. }
  172. function getReposts($iRepostedId)
  173. {
  174. return $this->getAll("SELECT * FROM `{$this->_sTableRepostsTrack}` WHERE `reposted_id`=:reposted_id", array(
  175. 'reposted_id' => $iRepostedId
  176. ));
  177. }
  178. function isReposted($iRepostedId, $iOwnerId, $iAuthorId)
  179. {
  180. $sQuery = $this->prepare("SELECT
  181. `te`.`id`
  182. FROM `{$this->_sTableRepostsTrack}` AS `tst`
  183. LEFT JOIN `{$this->_sTable}` AS `te` ON `tst`.`event_id`=`te`.`id`
  184. WHERE `tst`.`author_id`=? AND `tst`.`reposted_id`=? AND `te`.`owner_id`=?", $iAuthorId, $iRepostedId, $iOwnerId);
  185. return (int)$this->getOne($sQuery) > 0;
  186. }
  187. //--- Photo uploader related methods ---//
  188. public function saveMedia($sType, $iEventId, $iItemId)
  189. {
  190. return (int)$this->query("INSERT INTO `" . $this->_aTablesMedia2Events[$sType] . "` SET `event_id`=:event_id, `media_id`=:media_id", array(
  191. 'event_id' => $iEventId,
  192. 'media_id' => $iItemId
  193. )) > 0;
  194. }
  195. public function deleteMedia($sType, $iEventId)
  196. {
  197. return (int)$this->query("DELETE FROM `" . $this->_aTablesMedia2Events[$sType] . "` WHERE `event_id`=:event_id", array(
  198. 'event_id' => $iEventId
  199. )) > 0;
  200. }
  201. public function hasMedia($iEventId)
  202. {
  203. $bResult = false;
  204. foreach($this->_aTablesMedia as $sType) {
  205. $aMedia = $this->getMedia($sType, $iEventId);
  206. if(!empty($aMedia) && is_array($aMedia)) {
  207. $bResult = true;
  208. break;
  209. }
  210. }
  211. return $bResult;
  212. }
  213. public function getMedia($sType, $iEventId, $iOffset = 0, $bFullInfo = false)
  214. {
  215. $sTableMedia = $this->_aTablesMedia[$sType];
  216. $sTableMedia2Events = $this->_aTablesMedia2Events[$sType];
  217. $sMethod = 'getColumn';
  218. $sSelectClause = "`tme`.`media_id` AS `id`";
  219. if($bFullInfo) {
  220. $sMethod = 'getAll';
  221. $sSelectClause = "`tm`.*, `tme`.`event_id` AS `event_id`";
  222. }
  223. $sLimitAddon = '';
  224. if($iOffset != 0)
  225. $sLimitAddon = $this->prepareAsString(" OFFSET ?", $iOffset);
  226. $sQuery = $this->prepare("SELECT " . $sSelectClause . "
  227. FROM `" . $sTableMedia2Events . "` AS `tme`
  228. LEFT JOIN `" . $sTableMedia . "` AS `tm` ON `tme`.`media_id`=`tm`.`id`
  229. WHERE `tme`.`event_id`=?" . $sLimitAddon, $iEventId);
  230. return $this->$sMethod($sQuery);
  231. }
  232. public function getMediaById($sType, $iMediaId)
  233. {
  234. $sTableMedia = $this->_aTablesMedia[$sType];
  235. $sTableMedia2Events = $this->_aTablesMedia2Events[$sType];
  236. return $this->getRow("SELECT `tm`.*, `tme`.`event_id` AS `event_id` FROM `" . $sTableMedia2Events . "` AS `tme` LEFT JOIN `" . $sTableMedia . "` AS `tm` ON `tme`.`media_id`=`tm`.`id` WHERE `tme`.`media_id`=:media_id LIMIT 1", array(
  237. 'media_id' => $iMediaId
  238. ));
  239. }
  240. //--- Link attach related methods ---//
  241. public function getUnusedLinks($iUserId)
  242. {
  243. return $this->getLinksBy(array(
  244. 'type' => 'unused',
  245. 'profile_id' => $iUserId
  246. ));
  247. }
  248. public function deleteUnusedLinks($iUserId, $iLinkId = 0)
  249. {
  250. $aBindings = array(
  251. 'profile_id' => $iUserId
  252. );
  253. $sWhereAddon = '';
  254. if(!empty($iLinkId)) {
  255. $aBindings['id'] = $iLinkId;
  256. $sWhereAddon = " AND `id`=:id";
  257. }
  258. return $this->query("DELETE FROM `" . $this->_sPrefix . "links` WHERE `profile_id`=:profile_id" . $sWhereAddon, $aBindings);
  259. }
  260. public function saveLink($iEventId, $iLinkId)
  261. {
  262. $aBindings = array(
  263. 'event_id' => $iEventId,
  264. 'link_id' => $iLinkId
  265. );
  266. $iId = $this->getOne("SELECT `id` FROM `" . $this->_sPrefix . "links2events` WHERE `event_id`=:event_id AND `link_id`=:link_id LIMIT 1", $aBindings);
  267. if(!empty($iId))
  268. return true;
  269. return (int)$this->query("INSERT INTO `" . $this->_sPrefix . "links2events` SET `event_id`=:event_id, `link_id`=:link_id", $aBindings) > 0;
  270. }
  271. public function deleteLink($iId)
  272. {
  273. return (int)$this->query("DELETE FROM `tl`, `tle` USING `" . $this->_sPrefix . "links` AS `tl` LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id` WHERE `tl`.`id` = :id", array(
  274. 'id' => $iId
  275. )) > 0;
  276. }
  277. public function deleteLinks($iEventId)
  278. {
  279. return (int)$this->query("DELETE FROM `tl`, `tle` USING `" . $this->_sPrefix . "links` AS `tl` LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id` WHERE `tle`.`event_id` = :event_id", array(
  280. 'event_id' => $iEventId
  281. )) > 0;
  282. }
  283. public function getLinks($iEventId)
  284. {
  285. return $this->getLinksBy(array('type' => 'event_id', 'event_id' => $iEventId));
  286. }
  287. public function getLinksBy($aParams = array())
  288. {
  289. $CNF = &$this->_oConfig->CNF;
  290. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  291. $sSelectClause = "`tl`.*";
  292. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  293. switch($aParams['type']) {
  294. case 'id':
  295. $aMethod['name'] = 'getRow';
  296. $aMethod['params'][1] = array(
  297. 'id' => $aParams['id']
  298. );
  299. $sWhereClause = " AND `tl`.`id`=:id";
  300. if(!empty($aParams['profile_id'])) {
  301. $aMethod['params'][1]['profile_id'] = $aParams['profile_id'];
  302. $sWhereClause .= " AND `tl`.`profile_id`=:profile_id";
  303. }
  304. break;
  305. case 'event_id':
  306. $aMethod['params'][1] = array(
  307. 'event_id' => $aParams['event_id']
  308. );
  309. $sJoinClause = "LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`";
  310. $sWhereClause = " AND `tle`.`event_id`=:event_id";
  311. break;
  312. case 'unused':
  313. $aBindings = array(
  314. 'profile_id' => $aParams['profile_id']
  315. );
  316. if(isset($aParams['short']) && $aParams['short'] === true) {
  317. $aMethod['name'] = 'getPairs';
  318. $aMethod['params'][1] = 'url';
  319. $aMethod['params'][2] = 'id';
  320. $aMethod['params'][3] = $aBindings;
  321. }
  322. else
  323. $aMethod['params'][1] = $aBindings;
  324. $sJoinClause = "LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`";
  325. $sWhereClause = " AND `tl`.`profile_id`=:profile_id AND ISNULL(`tle`.`event_id`)";
  326. $sOrderClause = "`tl`.`added` DESC";
  327. break;
  328. }
  329. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  330. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  331. $aMethod['params'][0] = "SELECT
  332. " . $sSelectClause . "
  333. FROM `" . $this->_sPrefix . "links` AS `tl` " . $sJoinClause . "
  334. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  335. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  336. }
  337. public function getHot()
  338. {
  339. return $this->fromCache($this->_oConfig->getCacheHotKey(), 'getColumn', "SELECT `event_id` FROM `" . $this->_sTableHotTrack . "`");
  340. }
  341. public function clearHot()
  342. {
  343. $this->cleanCache($this->_oConfig->getCacheHotKey());
  344. return $this->query("TRUNCATE TABLE `" . $this->_sTableHotTrack . "`");
  345. }
  346. public function getHotTrackByDate($iInterval = 24)
  347. {
  348. $aBindings = [
  349. 'interval' => $iInterval
  350. ];
  351. $sQuery = "SELECT
  352. `te`.`id` AS `event_id`,
  353. `te`.`date` AS `value`
  354. FROM `" . $this->_sTable . "` AS `te`
  355. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `te`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  356. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  357. }
  358. public function getHotTrackByCommentsDate($sModule, $sTableTrack, $iInterval = 24, $iThresholdAge = 0, $iThresholdCount = 0)
  359. {
  360. $aBindings = [
  361. 'module' => $sModule,
  362. 'interval' => $iInterval
  363. ];
  364. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`cmt_time` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  365. if($iThresholdAge != 0) {
  366. $aBindings['threshold_age'] = $iThresholdAge;
  367. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  368. }
  369. $sQueryGroup = "`te`.`id`";
  370. if($iThresholdCount != 0) {
  371. $aBindings['threshold_count'] = $iThresholdCount;
  372. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`cmt_author_id`) >= :threshold_count";
  373. }
  374. $sQuery = "SELECT
  375. `te`.`id` as `event_id`,
  376. MAX(`tt`.`cmt_time`) AS `value`
  377. FROM `" . $this->_sTable . "` AS `te`
  378. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`id`=`tt`.`cmt_object_id` AND `te`.`object_owner_id`<>`tt`.`cmt_author_id` AND `te`.`type`=:module
  379. WHERE 1 " . $sQueryWhere . "
  380. GROUP BY " . $sQueryGroup;
  381. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  382. }
  383. public function getHotTrackByCommentsDateModule($sModule, $sTableTrack, $iInterval = 24, $iThresholdAge = 0, $iThresholdCount = 0)
  384. {
  385. $aBindings = [
  386. 'module' => $sModule,
  387. 'interval' => $iInterval
  388. ];
  389. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`cmt_time` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  390. if($iThresholdAge != 0) {
  391. $aBindings['threshold_age'] = $iThresholdAge;
  392. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  393. }
  394. $sQueryGroup = "`te`.`object_id`";
  395. if($iThresholdCount != 0) {
  396. $aBindings['threshold_count'] = $iThresholdCount;
  397. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`cmt_author_id`) >= :threshold_count";
  398. }
  399. $sQuery = "SELECT
  400. `te`.`id` as `event_id`,
  401. MAX(`tt`.`cmt_time`) AS `value`
  402. FROM `" . $this->_sTable . "` AS `te`
  403. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`object_id`=`tt`.`cmt_object_id` AND `te`.`object_owner_id`<>`tt`.`cmt_author_id` AND `te`.`type`=:module
  404. WHERE 1 " . $sQueryWhere . "
  405. GROUP BY " . $sQueryGroup;
  406. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  407. }
  408. public function getHotTrackByVotesDate($sModule, $sTableTrack, $iInterval = 24, $iThresholdAge = 0, $iThresholdCount = 0)
  409. {
  410. $aBindings = [
  411. 'module' => $sModule,
  412. 'interval' => $iInterval
  413. ];
  414. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  415. if($iThresholdAge != 0) {
  416. $aBindings['threshold_age'] = $iThresholdAge;
  417. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  418. }
  419. $sQueryGroup = "`te`.`id`";
  420. if($iThresholdCount != 0) {
  421. $aBindings['threshold_count'] = $iThresholdCount;
  422. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`author_id`) >= :threshold_count";
  423. }
  424. $sQuery = "SELECT
  425. `te`.`id` as `event_id`,
  426. MAX(`tt`.`date`) AS `value`
  427. FROM `" . $this->_sTable . "` AS `te`
  428. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`id`=`tt`.`object_id` AND `te`.`object_owner_id`<>`tt`.`author_id` AND `te`.`type`=:module
  429. WHERE 1 " . $sQueryWhere . "
  430. GROUP BY " . $sQueryGroup;
  431. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  432. }
  433. public function getHotTrackByVotesDateModule($sModule, $sTableTrack, $iInterval = 24, $iThresholdAge = 0, $iThresholdCount = 0)
  434. {
  435. $aBindings = [
  436. 'module' => $sModule,
  437. 'interval' => $iInterval
  438. ];
  439. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  440. if($iThresholdAge != 0) {
  441. $aBindings['threshold_age'] = $iThresholdAge;
  442. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  443. }
  444. $sQueryGroup = "`te`.`object_id`";
  445. if($iThresholdCount != 0) {
  446. $aBindings['threshold_count'] = $iThresholdCount;
  447. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`author_id`) >= :threshold_count";
  448. }
  449. $sQuery = "SELECT
  450. `te`.`id` as `event_id`,
  451. MAX(`tt`.`date`) AS `value`
  452. FROM `" . $this->_sTable . "` AS `te`
  453. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`object_id`=`tt`.`object_id` AND `te`.`object_owner_id`<>`tt`.`author_id` AND `te`.`type`=:module
  454. WHERE 1 " . $sQueryWhere . "
  455. GROUP BY " . $sQueryGroup;
  456. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  457. }
  458. /**
  459. * Hot Track by Sum of Votes during specified Period is currently disabled.
  460. */
  461. public function getHotTrackByVotesSum($sModule, $sTableTrack, $iInterval = 24)
  462. {
  463. $sQuery = "SELECT
  464. `te`.`id` as `event_id`,
  465. SUM(`tt`.`value`) AS `value`
  466. FROM `" . $this->_sTable . "` AS `te`
  467. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`id`=`tt`.`object_id` AND `te`.`type`=:module
  468. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  469. GROUP BY `te`.`id`";
  470. return $this->getAll($sQuery, array('module' => $sModule, 'interval' => $iInterval));
  471. }
  472. /**
  473. * Hot Track by Sum of Votes during specified Period is currently disabled.
  474. */
  475. public function getHotTrackByVotesSumModule($sModule, $sTableTrack, $iInterval = 24)
  476. {
  477. $sQuery = "SELECT
  478. `te`.`id` as `event_id`,
  479. SUM(`tt`.`value`) AS `value`
  480. FROM `" . $this->_sTable . "` AS `te`
  481. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`object_id`=`tt`.`object_id` AND `te`.`type`=:module
  482. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  483. GROUP BY `te`.`object_id`";
  484. return $this->getAll($sQuery, array('module' => $sModule, 'interval' => $iInterval));
  485. }
  486. public function updateHotTrack($aTrack)
  487. {
  488. return (int)$this->query("REPLACE INTO `" . $this->_sTableHotTrack . "` SET " . $this->arrayToSQL($aTrack)) > 0;
  489. }
  490. public function rebuildSlice()
  491. {
  492. $this->query("TRUNCATE TABLE `" . $this->_sTableSlice . "`");
  493. $aWhereConditions = [];
  494. $iInterval = $this->_oConfig->getCacheTableInterval();
  495. $aCheckFields = $this->_oConfig->getCacheTableCheckFields();
  496. foreach($aCheckFields as $sField => $sType)
  497. switch($sType) {
  498. case 'flag':
  499. $aWhereConditions[] = "`" . $sField . "` <> 0";
  500. break;
  501. case 'date':
  502. $aWhereConditions[] = "`" . $sField . "` > (UNIX_TIMESTAMP() - " . $iInterval . ")";
  503. break;
  504. }
  505. return $this->query("INSERT INTO `" . $this->_sTableSlice . "` SELECT * FROM `" . $this->_sTable . "` WHERE " . implode(" OR ", $aWhereConditions));
  506. }
  507. public function getEvents($aParams)
  508. {
  509. bx_alert($this->_oConfig->getName(), 'get_events_before', 0, 0, array(
  510. 'params' => &$aParams,
  511. ));
  512. $sTable = isset($aParams['from_cache']) && $aParams['from_cache'] === true ? $this->_sTableSlice : $this->_sTable;
  513. $sTableAlias = $this->getTableAlias();
  514. list($sMethod, $sSelectClause, $mixedJoinClause, $mixedWhereClause, $sOrderClause, $sLimitClause) = $this->_getSqlPartsEvents($aParams);
  515. $bCount = isset($aParams['count']) && $aParams['count'] === true;
  516. if($bCount) {
  517. $sMethod = 'getOne';
  518. $sSelectClause = 'COUNT(*)';
  519. $sOrderClause = '';
  520. $sLimitClause = '';
  521. }
  522. $aAlertParams = $aParams;
  523. unset($aAlertParams['browse']);
  524. bx_alert($this->_oConfig->getName(), 'get_events', 0, 0, array(
  525. 'browse' => $aParams['browse'],
  526. 'params' => $aAlertParams,
  527. 'table' => $sTable,
  528. 'method' => &$sMethod,
  529. 'select_clause' => &$sSelectClause,
  530. 'join_clause' => &$mixedJoinClause,
  531. 'where_clause' => &$mixedWhereClause,
  532. 'order_clause' => &$sOrderClause,
  533. 'limit_clause' => &$sLimitClause
  534. ));
  535. $sSqlMask = "SELECT {select}
  536. FROM `{$sTable}` AS `{$sTableAlias}`
  537. LEFT JOIN `{$this->_sTableHandlers}` ON `{$sTableAlias}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$sTableAlias}`.`action`=`{$this->_sTableHandlers}`.`alert_action` {join}
  538. WHERE 1 {where} {order} {limit}";
  539. if(is_string($mixedWhereClause)) {
  540. $aSqlMarkers = array(
  541. 'select' => $sSelectClause,
  542. 'join' => $mixedJoinClause,
  543. 'where' => $mixedWhereClause,
  544. 'order' => $sOrderClause,
  545. 'limit' => $sLimitClause
  546. );
  547. return $this->$sMethod(bx_replace_markers($sSqlMask, $aSqlMarkers));
  548. }
  549. $bJoinAsArray = !empty($mixedJoinClause) && is_array($mixedJoinClause);
  550. $sOrderSubclause = $sLimitSubclause = '';
  551. if(!$bCount) {
  552. $sOrderSubclause = $sOrderClause;
  553. $sLimitSubclause = isset($aParams['per_page']) ? 'LIMIT 0, ' . ($aParams['start'] + $aParams['per_page']) : '';
  554. }
  555. $aSqlParts = array();
  556. foreach($mixedWhereClause as $sKey => $sValue) {
  557. $aSqlMarkers = array(
  558. 'select' => $sSelectClause,
  559. 'join' => $bJoinAsArray ? (isset($mixedJoinClause[$sKey]) ? $mixedJoinClause[$sKey] : '') : $mixedJoinClause,
  560. 'where' => $sValue,
  561. 'order' => $sOrderSubclause,
  562. 'limit' => $sLimitSubclause
  563. );
  564. $sSqlPart = bx_replace_markers($sSqlMask, $aSqlMarkers);
  565. $aSqlParts[] = !$bCount ? $sSqlPart : (int)$this->$sMethod($sSqlPart);
  566. }
  567. if($bCount)
  568. return array_sum($aSqlParts);
  569. $sSql = bx_replace_markers('(' . implode(') UNION (', $aSqlParts) . ') {order} {limit}', [
  570. 'order' => str_replace("`{$sTableAlias}`.", '', $sOrderClause),
  571. 'limit' => str_replace("`{$sTableAlias}`.", '', $sLimitClause),
  572. ]);
  573. //echoDbg($sSql); exit;
  574. return $this->$sMethod($sSql);
  575. }
  576. protected function _getFilterAddon($iOwnerId, $sFilter)
  577. {
  578. $sTableAlias = $this->getTableAlias();
  579. switch($sFilter) {
  580. /**
  581. * Direct posts in Timeline made by a timeline owner ($iOwnerId)
  582. */
  583. case BX_TIMELINE_FILTER_OWNER:
  584. $sFilterAddon = $this->prepareAsString(" AND `{$sTableAlias}`.`action`='' AND `{$sTableAlias}`.`object_id`=? ", $iOwnerId);
  585. break;
  586. /**
  587. * Direct posts in Timeline made by users except a timeline owner ($iOwnerId)
  588. */
  589. case BX_TIMELINE_FILTER_OTHER:
  590. $sFilterAddon = $this->prepareAsString(" AND `{$sTableAlias}`.`action`='' AND `{$sTableAlias}`.`object_id`<>? ", $iOwnerId);
  591. break;
  592. /**
  593. * All (Direct and System) posts in Timeline (owned by $iOwnerId) made by users except the viewer
  594. */
  595. case BX_TIMELINE_FILTER_OTHER_VIEWER:
  596. $sFilterAddon = $this->prepareAsString(" AND (`{$sTableAlias}`.`action`<>'' OR (`{$sTableAlias}`.`action`='' AND `{$sTableAlias}`.`object_id`<>?)) ", bx_get_logged_profile_id());
  597. break;
  598. case BX_TIMELINE_FILTER_ALL:
  599. default:
  600. $sFilterAddon = "";
  601. }
  602. return $sFilterAddon;
  603. }
  604. protected function _getSqlPartsEvents($aParams)
  605. {
  606. $sMethod = 'getAll';
  607. $sTableAlias = $this->getTableAlias();
  608. $sSelectClause = "`{$sTableAlias}`.*";
  609. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  610. switch($aParams['browse']) {
  611. case 'owner_id':
  612. $sWhereClause = $this->prepareAsString("AND `{$sTableAlias}`.`owner_id`=? ", $aParams['value']);
  613. break;
  614. case 'common_by_object':
  615. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  616. $sWhereClause = $this->prepareAsString("AND `{$sTableAlias}`.`system`='0' AND `{$sTableAlias}`.`object_id`=? ", $aParams['value']);
  617. break;
  618. case 'descriptor':
  619. $sMethod = 'getRow';
  620. $sWhereClause = "";
  621. if(isset($aParams['type']))
  622. $sWhereClause .= $this->prepareAsString("AND `{$sTableAlias}`.`type`=? ", $aParams['type']);
  623. if(isset($aParams['action']))
  624. $sWhereClause .= $this->prepareAsString("AND `{$sTableAlias}`.`action`=? ", $aParams['action']);
  625. if(isset($aParams['object_id']))
  626. $sWhereClause .= $this->prepareAsString("AND `{$sTableAlias}`.`object_id`=? ", $aParams['object_id']);
  627. $sLimitClause = "LIMIT 1";
  628. break;
  629. case 'reposted_by_track':
  630. $sJoinClause = $this->prepareAsString("INNER JOIN `" . $this->_sTableRepostsTrack . "` AS `trt` ON `{$sTableAlias}`.`id`=`trt`.`event_id` AND `trt`.`reposted_id`=?", $aParams['value']);
  631. break;
  632. case 'reposted_by_descriptor':
  633. $sWhereClause = "";
  634. if(isset($aParams['type']))
  635. $sWhereClause .= "AND `{$sTableAlias}`.`content` LIKE " . $this->escape('%' . $aParams['type'] . '%');
  636. if(isset($aParams['action']))
  637. $sWhereClause .= "AND `{$sTableAlias}`.`content` LIKE " . $this->escape('%' . $aParams['action'] . '%');
  638. break;
  639. case 'last':
  640. $sMethod = 'getRow';
  641. $sSelectClause .= ", YEAR(FROM_UNIXTIME(`{$sTableAlias}`.`date`)) AS `year`";
  642. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  643. $sOrderClause = "ORDER BY `{$sTableAlias}`.`date` ASC, `{$sTableAlias}`.`id` ASC";
  644. $sLimitClause = "LIMIT 1";
  645. break;
  646. case 'ids':
  647. $sWhereClause = "AND `{$sTableAlias}`.`id` IN (" . $this->implode_escape($aParams['ids']) . ") ";
  648. break;
  649. default:
  650. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = parent::_getSqlPartsEvents($aParams);
  651. }
  652. if($this->_isList($aParams)) {
  653. $sOrderClause = "";
  654. switch($aParams['type']) {
  655. case BX_TIMELINE_TYPE_HOT:
  656. $sOrderClause = "`{$sTableAlias}`.`sticked` DESC, `{$this->_sTableHotTrack}`.`value` DESC, ";
  657. break;
  658. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  659. case BX_BASE_MOD_NTFS_TYPE_CONNECTIONS:
  660. case BX_TIMELINE_TYPE_CHANNELS:
  661. case BX_TIMELINE_TYPE_CONNECTED_CONTEXTS:
  662. case BX_TIMELINE_TYPE_FEED:
  663. case BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS:
  664. $sOrderClause = "`{$sTableAlias}`.`sticked` DESC, ";
  665. break;
  666. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  667. $sOrderClause = "`{$sTableAlias}`.`pinned` DESC, ";
  668. break;
  669. }
  670. if($this->_oConfig->isSortByUnread()) {
  671. $oProfileQuery = BxDolProfileQuery::getInstance();
  672. $iDate = 0;
  673. $iOwner = (int)$aParams['owner_id'];
  674. $aOwner = $oProfileQuery->getInfoById($iOwner);
  675. $iViewer = !empty($aParams['viewer_id']) ? (int)$aParams['viewer_id'] : bx_get_logged_profile_id();
  676. if(!empty($aOwner) && is_array($aOwner) && BxDolRequest::serviceExists($aOwner['type'], 'is_fan') && bx_srv($aOwner['type'], 'is_fan', [$iOwner, $iViewer])) {
  677. $oModule = BxDolModule::getInstance($aOwner['type']);
  678. $aConnection = BxDolConnection::getObjectInstance($oModule->_oConfig->CNF['OBJECT_CONNECTIONS'])->getConnection($iViewer, $iOwner);
  679. $iDate = (int)$aConnection['added'];
  680. }
  681. else {
  682. $aViewer = $oProfileQuery->getInfoById($iViewer);
  683. if(!empty($aViewer) && is_array($aViewer))
  684. $iDate = bx_srv($aViewer['type'], 'get_date_added', [$aViewer['content_id']]);
  685. }
  686. $sSelectClause .= ", IF(NOT ISNULL(`teu`.`id`), 1, 0) AS `read`";
  687. $sOrderClause .= $this->prepareAsString("IF(`{$sTableAlias}`.`date` > ?, `read`, 1) ASC, ", $iDate);
  688. }
  689. if($this->_oConfig->isSortByReaction())
  690. $sOrderClause .= "`{$sTableAlias}`.`reacted` DESC, ";
  691. else
  692. $sOrderClause .= "`{$sTableAlias}`.`date` DESC, ";
  693. $sOrderClause = "ORDER BY " . $sOrderClause . "`{$sTableAlias}`.`id` DESC";
  694. }
  695. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  696. }
  697. protected function _getSqlPartsEventsListStatusAdmin($aParams)
  698. {
  699. if(isset($aParams['moderator']) && $aParams['moderator'] === true)
  700. return '';
  701. $iViewerId = !empty($aParams['viewer_id']) ? $aParams['viewer_id'] : bx_get_logged_profile_id();
  702. $sTableAlias = $this->getTableAlias();
  703. //--- Check viewer as event author.
  704. $sWhereClause = $this->prepareAsString("`{$sTableAlias}`.`object_owner_id`=?", $iViewerId);
  705. //--- Check viewer as an administrator/moderator of event author.
  706. $aGroups = [];
  707. $aModules = bx_srv('system', 'get_modules_by_type', ['profile']);
  708. foreach($aModules as $aModule) {
  709. $oModule = BxDolModule::getInstance($aModule['name']);
  710. if(!$oModule || !($oModule instanceof BxBaseModGroupsModule))
  711. continue;
  712. $aGroups = array_merge($aGroups, $oModule->getGroupsByFan($iViewerId, [
  713. BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR,
  714. BX_BASE_MOD_GROUPS_ROLE_MODERATOR
  715. ]));
  716. }
  717. if(!empty($aGroups))
  718. $sWhereClause .= " OR `{$sTableAlias}`.`object_owner_id` IN (" . $this->implode_escape($aGroups) . ")";
  719. return $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0' AND (" . $sWhereClause . "), 1, `{$sTableAlias}`.`status_admin`=?) ", isset($aParams['status_admin']) ? $aParams['status_admin'] : BX_TIMELINE_STATUS_ACTIVE);
  720. }
  721. protected function _getSqlPartsEventsList($aParams)
  722. {
  723. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  724. $sTableAlias = $this->getTableAlias();
  725. $mixedJoinClause = $mixedWhereClause = "";
  726. $mixedJoinClause = "INNER JOIN `sys_profiles` AS `tpoo` ON ABS(`{$sTableAlias}`.`object_owner_id`)=`tpoo`.`id` AND `tpoo`.`status`='active'";
  727. if($this->_oConfig->isSortByUnread())
  728. $mixedJoinClause .= $this->prepareAsString(" LEFT JOIN `{$this->_sTableEvent2User}` AS `teu` ON `{$sTableAlias}`.`id`=`teu`.`event_id` AND `teu`.`user_id`=? ", $aParams['viewer_id']);
  729. $sWhereClauseStatus = "AND `{$sTableAlias}`.`active`='1' ";
  730. $sWhereClauseStatus .= $this->prepareAsString("AND `{$sTableAlias}`.`status`=? ", isset($aParams['status']) ? $aParams['status'] : BX_TIMELINE_STATUS_ACTIVE);
  731. $sWhereClauseStatus .= $this->_getSqlPartsEventsListStatusAdmin($aParams);
  732. //--- Apply filter
  733. $sWhereClauseFilter = "";
  734. if(isset($aParams['filter']))
  735. $sWhereClauseFilter = $this->_getFilterAddon($aParams['owner_id'], $aParams['filter']);
  736. //--- Apply timeline
  737. $sWhereClauseTimeline = "";
  738. if(isset($aParams['timeline']) && !empty($aParams['timeline']) && strpos($aParams['timeline'], '-') !== false) {
  739. list($iY, $iM, $iD) = explode('-', $aParams['timeline']);
  740. $sWhereClauseTimeline = $this->prepareAsString("AND `date`<=? ", mktime(23, 59, 59, (int)$iM, (int)$iD, (int)$iY));
  741. }
  742. //--- Apply modules or handlers filter
  743. $sWhereClauseModules = "";
  744. if(!empty($aParams['modules']) && is_array($aParams['modules']))
  745. $sWhereClauseModules = "AND `" . $sTableAlias . "`.`type` IN (" . $this->implode_escape($aParams['modules']) . ") ";
  746. $sWhereClauseHidden = "";
  747. if(empty($sWhereClauseModules)) {
  748. $aHidden = $this->_oConfig->getHandlersHidden();
  749. $sWhereClauseHidden = !empty($aHidden) && is_array($aHidden) ? "AND `" . $this->_sTableHandlers . "`.`id` NOT IN (" . $this->implode_escape($aHidden) . ") " : "";
  750. }
  751. //--- Apply mute filter
  752. $sWhereClauseMuted = "";
  753. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('connection_mute'));
  754. if($oConnection) {
  755. $aMuted = $oConnection->getConnectedContent(bx_get_logged_profile_id());
  756. if(!empty($aMuted) && is_array($aMuted)) {
  757. $sMuted = "NOT IN (". $this->implode_escape($aMuted) . ")";
  758. $sWhereClauseMuted = "AND `{$sTableAlias}`.`owner_id` $sMuted AND `{$sTableAlias}`.`object_owner_id` $sMuted ";
  759. }
  760. }
  761. //--- Apply unpublished (date in future) filter
  762. $sWhereClauseUnpublished = $this->prepareAsString("AND IF(`{$sTableAlias}`.`system`='0' AND `{$sTableAlias}`.`object_id` = ?, 1, `{$sTableAlias}`.`date` <= UNIX_TIMESTAMP()) ", bx_get_logged_profile_id());
  763. //--- Apply content filter
  764. $oCf = BxDolContentFilter::getInstance();
  765. $sWhereClauseCf = $oCf->isEnabled() ? $oCf->getSQLParts($sTableAlias, 'object_cf') . ' ' : '';
  766. //--- Check type
  767. $mixedJoinSubclause = $mixedWhereSubclause = "";
  768. switch($aParams['type']) {
  769. //--- Feed: Hot
  770. case BX_TIMELINE_TYPE_HOT:
  771. //--- Apply privacy filter
  772. $aPrivacyGroups = array(BX_DOL_PG_ALL);
  773. if(isLogged())
  774. $aPrivacyGroups[] = BX_DOL_PG_MEMBERS;
  775. $aQueryParts = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'))->getContentByGroupAsSQLPart($aPrivacyGroups);
  776. $mixedWhereClause .= $aQueryParts['where'] . " ";
  777. //--- Select Hot posts.
  778. $mixedJoinSubclause = "LEFT JOIN `{$this->_sTableHotTrack}` ON `{$sTableAlias}`.`id`=`{$this->_sTableHotTrack}`.`event_id`";
  779. $mixedWhereSubclause = "NOT ISNULL(`{$this->_sTableHotTrack}`.`value`)";
  780. //--- Select Promoted posts.
  781. $mixedWhereSubclause .= " OR `{$sTableAlias}`.`promoted` <> '0'";
  782. break;
  783. //--- Feed: Public
  784. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  785. //--- Apply privacy filter
  786. $aPrivacyGroups = array(BX_DOL_PG_ALL);
  787. if(isLogged())
  788. $aPrivacyGroups[] = BX_DOL_PG_MEMBERS;
  789. $aQueryParts = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'))->getContentByGroupAsSQLPart($aPrivacyGroups);
  790. $mixedWhereClause .= $aQueryParts['where'] . " ";
  791. if($this->_oConfig->isShowAll())
  792. break;
  793. //--- Select All System posts
  794. $mixedWhereSubclause = "`{$sTableAlias}`.`system`='1'";
  795. //--- Select Public (Direct) posts created on Home Page Timeline (Public Feed)
  796. $mixedWhereSubclause .= $this->prepareAsString(" OR `{$sTableAlias}`.`owner_id`=?", 0);
  797. //--- Select Promoted posts.
  798. $mixedWhereSubclause .= " OR `{$sTableAlias}`.`promoted` <> '0'";
  799. break;
  800. //--- Feed: Profile
  801. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  802. if(empty($aParams['owner_id']))
  803. break;
  804. //--- Select Own (System and Direct) posts from Profile's Timeline.
  805. $mixedWhereSubclause = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = ?)", $aParams['owner_id']);
  806. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  807. $mixedWhereSubclause .= $this->prepareAsString(" OR (`{$sTableAlias}`.`owner_id` = '0' AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` = ?, 1))", $aParams['owner_id']);
  808. break;
  809. //--- Feed: All Profile Connections
  810. case BX_BASE_MOD_NTFS_TYPE_CONNECTIONS:
  811. if(empty($aParams['owner_id']))
  812. break;
  813. $mixedJoinSubclause = array();
  814. $mixedWhereSubclause = array();
  815. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  816. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  817. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  818. $aJoin1 = $aQueryParts['join'];
  819. $mixedJoinSubclause['p1'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  820. $mixedWhereSubclause['p1'] = "1";
  821. //--- Exclude Own (Direct) posts on timelines of following members.
  822. //--- Note. Disabled for now.
  823. //$mixedWhereSubclause['p1'] .= $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  824. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'object_id', $aParams['owner_id']);
  825. $aJoin2 = $aQueryParts['join'];
  826. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'object_id', $aParams['owner_id']);
  827. $aJoin2 = $aQueryParts['join'];
  828. $aJoin2['table_alias'] = 'cc';
  829. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  830. $mixedJoinSubclause['p2'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system`='0' AND " . $aJoin2['condition'];
  831. $mixedWhereSubclause['p2'] = "1";
  832. //--- Select Promoted posts.
  833. $mixedWhereSubclause['p3'] = "`{$sTableAlias}`.`promoted` <> '0'";
  834. break;
  835. //--- Feed: Profile Connections to contexts by type (groups, events, spaces, etc) only.
  836. case BX_TIMELINE_TYPE_CONNECTED_CONTEXTS:
  837. if(empty($aParams['owner_id']) || empty($aParams['context']))
  838. break;
  839. $mixedJoinSubclause = [];
  840. $mixedWhereSubclause = [];
  841. //--- Filter out unnecessary contexts.
  842. $aContexts = is_array($aParams['context']) ? $aParams['context'] : [$aParams['context']];
  843. $mixedJoinSubclause['p1'] = "INNER JOIN `sys_profiles` AS `tpo` ON ABS(`{$sTableAlias}`.`owner_id`)=`tpo`.`id` AND `tpo`.`type` IN (" . $this->implode_escape($aContexts) . ") AND `tpo`.`status`='active' ";
  844. //--- Join System posts received by following contexts.
  845. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  846. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  847. $aJoin1 = $aQueryParts['join'];
  848. $mixedJoinSubclause['p1'] .= "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  849. $mixedWhereSubclause['p1'] = "1";
  850. //--- Select Promoted posts.
  851. $mixedWhereSubclause['p2'] = "`{$sTableAlias}`.`promoted` <> '0'";
  852. break;
  853. //--- Feed: Profile Connections to Channel contexts only
  854. case BX_TIMELINE_TYPE_CHANNELS:
  855. if(empty($aParams['owner_id']))
  856. break;
  857. $mixedJoinSubclause = [];
  858. $mixedWhereSubclause = [];
  859. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  860. //--- Join System posts received by following channels.
  861. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  862. $aJoin1 = $aQueryParts['join'];
  863. $mixedJoinSubclause['p1'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  864. $mixedWhereSubclause['p1'] = "`{$sTableAlias}`.`type`='bx_channels'";
  865. //--- Exclude Own (Direct) posts on timelines of following members.
  866. //--- Note. Disabled for now.
  867. //$mixedWhereSubclause['p1'] = $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  868. //--- Select Promoted posts.
  869. $mixedWhereSubclause['p2'] = "`{$sTableAlias}`.`promoted` <> '0'";
  870. break;
  871. //--- Feed: Profile + Profile Connections to Non-Channel contexts
  872. case BX_TIMELINE_TYPE_FEED:
  873. case BX_TIMELINE_TYPE_FEED_AND_HOT:
  874. if(empty($aParams['owner_id']))
  875. break;
  876. $mixedJoinSubclause = array();
  877. $mixedWhereSubclause = array();
  878. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  879. //--- Select Own (System and Direct) posts from Profile's Timeline.
  880. $sWhereSubclauseOwnProfile = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = ?)", $aParams['owner_id']);
  881. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  882. $sWhereSubclauseOwnPublic = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = '0' AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` = ?, 1))", $aParams['owner_id']);
  883. $mixedWhereSubclause['p1'] = "(" . $sWhereSubclauseOwnProfile . " OR " . $sWhereSubclauseOwnPublic . ")";
  884. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  885. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  886. $aJoin1 = $aQueryParts['join'];
  887. $mixedJoinSubclause['p2'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  888. $mixedWhereSubclause['p2'] = "`{$sTableAlias}`.`type`<>'bx_channels'";
  889. //--- Exclude Own (Direct) posts on timelines of following members.
  890. //--- Note. Disabled for now and next check is used instead.
  891. //$mixedWhereSubclause['p2'] = $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1))", $aParams['owner_id']);
  892. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'object_owner_id', $aParams['owner_id']);
  893. $aJoin2 = $aQueryParts['join'];
  894. $aJoin2['table_alias'] = 'cc';
  895. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  896. $mixedJoinSubclause['p3'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system` = 0 AND `" . $sTableAlias . "`.`object_privacy_view` > 0 AND " . $aJoin2['condition'];
  897. $mixedWhereSubclause['p3'] = "1";
  898. //--- Select Promoted posts.
  899. $mixedWhereSubclause['p4'] = "`{$sTableAlias}`.`promoted` <> '0'";
  900. //--- Select Hot posts.
  901. if($aParams['type'] == BX_TIMELINE_TYPE_FEED_AND_HOT) {
  902. $mixedJoinSubclause['p5'] = "INNER JOIN `{$this->_sTableHotTrack}` ON `{$sTableAlias}`.`id`=`{$this->_sTableHotTrack}`.`event_id`";
  903. $mixedWhereSubclause['p5'] = "1";
  904. }
  905. break;
  906. //--- Feed: Profile + All Profile Connections
  907. case BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS:
  908. if(empty($aParams['owner_id']))
  909. break;
  910. $mixedJoinSubclause = array();
  911. $mixedWhereSubclause = array();
  912. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  913. //--- Select Own (System and Direct) posts from Profile's Timeline.
  914. $sWhereSubclauseOwnProfile = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = ?)", $aParams['owner_id']);
  915. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  916. $sWhereSubclauseOwnPublic = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = '0' AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` = ?, 1))", $aParams['owner_id']);
  917. $mixedWhereSubclause['p1'] = "(" . $sWhereSubclauseOwnProfile . " OR " . $sWhereSubclauseOwnPublic . ")";
  918. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  919. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  920. $aJoin1 = $aQueryParts['join'];
  921. $mixedJoinSubclause['p2'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  922. $mixedWhereSubclause['p2'] = "1";
  923. //--- Exclude Own (Direct) posts on timelines of following members.
  924. //--- Note. Disabled for now and next check is used instead.
  925. //$mixedWhereSubclause['p2'] = $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1))", $aParams['owner_id']);
  926. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'object_id', $aParams['owner_id']);
  927. $aJoin2 = $aQueryParts['join'];
  928. $aJoin2['table_alias'] = 'cc';
  929. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  930. $mixedJoinSubclause['p3'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system`='0' AND " . $aJoin2['condition'];
  931. $mixedWhereSubclause['p3'] = "1";
  932. //--- Select Promoted posts.
  933. $mixedWhereSubclause['p4'] = "`{$sTableAlias}`.`promoted` <> '0'";
  934. break;
  935. //--- Feed: Profile Connections to contexts from Selected module
  936. default:
  937. if(empty($aParams['owner_id']))
  938. break;
  939. $mixedJoinSubclause = [];
  940. $mixedWhereSubclause = [];
  941. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  942. //--- Join System posts received by following channels.
  943. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  944. $aJoin1 = $aQueryParts['join'];
  945. $mixedJoinSubclause['p1'] = "INNER JOIN `sys_profiles` AS `p` ON `" . $sTableAlias ."`.`owner_id`=`p`.`id` AND `p`.`type`='" . $aParams['type'] . "' ";
  946. $mixedJoinSubclause['p1'] .= "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  947. $mixedWhereSubclause['p1'] = "1";
  948. if(!empty($aParams['context']))
  949. $mixedWhereSubclause['p1'] .= $this->prepareAsString(" AND `" . $aJoin1['table_alias'] . "`.`content`=?", $aParams['context']);
  950. //--- Exclude Own (Direct) posts on timelines of following members.
  951. //--- Note. Disabled for now.
  952. //$mixedWhereSubclause['p1'] = $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  953. //--- Select Promoted posts.
  954. $mixedWhereSubclause['p2'] = "`" . $sTableAlias . "`.`promoted` <> '0'";
  955. break;
  956. }
  957. $aAlertParams = $aParams;
  958. unset($aAlertParams['type'], $aAlertParams['owner_id']);
  959. bx_alert($this->_oConfig->getName(), 'get_list_by_type', 0, 0, array(
  960. 'type' => $aParams['type'],
  961. 'owner_id' => $aParams['owner_id'],
  962. 'params' => $aAlertParams,
  963. 'table' => isset($aParams['from_cache']) && $aParams['from_cache'] === true ? $this->_sTableSlice : $this->_sTable,
  964. 'join_clause' => &$mixedJoinClause,
  965. 'join_subclause' => &$mixedJoinSubclause,
  966. 'where_clause' => &$mixedWhereClause,
  967. 'where_clause_status' => &$sWhereClauseStatus,
  968. 'where_clause_filter' => &$sWhereClauseFilter,
  969. 'where_clause_timeline' => &$sWhereClauseTimeline,
  970. 'where_clause_modules' => &$sWhereClauseModules,
  971. 'where_clause_hidden' => &$sWhereClauseHidden,
  972. 'where_clause_muted' => &$sWhereClauseMuted,
  973. 'where_clause_unpublished' => &$sWhereClauseUnpublished,
  974. 'where_clause_cf' => &$sWhereClauseCf,
  975. 'where_subclause' => &$mixedWhereSubclause
  976. ));
  977. $mixedWhereClause .= $sWhereClauseStatus;
  978. $mixedWhereClause .= $sWhereClauseFilter;
  979. $mixedWhereClause .= $sWhereClauseTimeline;
  980. $mixedWhereClause .= $sWhereClauseModules;
  981. $mixedWhereClause .= $sWhereClauseHidden;
  982. $mixedWhereClause .= $sWhereClauseMuted;
  983. $mixedWhereClause .= $sWhereClauseUnpublished;
  984. $mixedWhereClause .= $sWhereClauseCf;
  985. //--- Combine general Join with SubJoins.
  986. if(!empty($mixedJoinSubclause)) {
  987. if(is_array($mixedJoinSubclause)) {
  988. $aJoinClause = [];
  989. foreach($mixedJoinSubclause as $sKey => $sValue)
  990. $aJoinClause[$sKey] = $mixedJoinClause . " " . $sValue . " ";
  991. $mixedJoinClause = $aJoinClause;
  992. }
  993. else
  994. $mixedJoinClause .= " " . $mixedJoinSubclause . " ";
  995. }
  996. //--- Combine general Where with SubWheres.
  997. if(!empty($mixedWhereSubclause)) {
  998. if(is_array($mixedWhereSubclause)) {
  999. $aWhereClause = [];
  1000. foreach($mixedWhereSubclause as $sKey => $sValue)
  1001. $aWhereClause[$sKey] = $mixedWhereClause . "AND " . $sValue . " ";
  1002. $mixedWhereClause = $aWhereClause;
  1003. }
  1004. else
  1005. $mixedWhereClause .= "AND (" . $mixedWhereSubclause . ") ";
  1006. }
  1007. return [$mixedJoinClause, $mixedWhereClause];
  1008. }
  1009. protected function _isList($aParams)
  1010. {
  1011. return in_array($aParams['browse'], ['list', 'ids']) && (!isset($aParams['newest']) || $aParams['newest'] === false);
  1012. }
  1013. public function getMenuItemMaxOrder($sSetName)
  1014. {
  1015. return $this->getOne("SELECT IFNULL(MAX(`order`), 0) FROM `sys_menu_items` WHERE `set_name`=:set_name LIMIT 1", [
  1016. 'set_name' => $sSetName
  1017. ]);
  1018. }
  1019. public function getMenuItemId($sSetName, $sName)
  1020. {
  1021. return (int)$this->getOne("SELECT `id` FROM `sys_menu_items` WHERE `set_name`=:set_name AND `name`=:name LIMIT 1", [
  1022. 'set_name' => $sSetName,
  1023. 'name' => $sName
  1024. ]);
  1025. }
  1026. public function insertMenuItem($sSetName, $sModule, $sName, $sTitle, $iOrder)
  1027. {
  1028. return $this->query("INSERT INTO `sys_menu_items` SET " . $this->arrayToSQL([
  1029. 'set_name' => $sSetName,
  1030. 'module' => $sModule,
  1031. 'name' => $sName,
  1032. 'title_system' => $sTitle,
  1033. 'title' => $sTitle,
  1034. 'link' => 'javascript:void(0)',
  1035. 'onclick' => "javascript:{js_object_view}.changeFeed(this, '" . $sModule . "')",
  1036. 'target' => '_self',
  1037. 'collapsed' => 1,
  1038. 'order' => $iOrder
  1039. ]));
  1040. }
  1041. public function deleteMenuItem($sSetName, $sName)
  1042. {
  1043. return $this->query("DELETE FROM `sys_menu_items` WHERE `set_name`=:set_name AND `name`=:name LIMIT 1", [
  1044. 'set_name' => $sSetName,
  1045. 'name' => $sName
  1046. ]);
  1047. }
  1048. }
  1049. /** @} */