BxTimelineDb.php 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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. public 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 = [
  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 = [
  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. if(!$this->isHandler($sType, $sAction))
  133. return [];
  134. $bSystem = $this->_oConfig->isSystem($sType, $sAction);
  135. $aReposted = $this->getEvents($bSystem ? [
  136. 'browse' => 'descriptor', 'type' => $sType, 'action' => $sAction, 'object_id' => $iObjectId
  137. ] : [
  138. 'browse' => 'id', 'value' => $iObjectId
  139. ]);
  140. if($bSystem && (empty($aReposted) || !is_array($aReposted))) {
  141. $iOwnerId = 0;
  142. $iDate = 0;
  143. $sStatus = BX_TIMELINE_STATUS_DELETED;
  144. $mixedResult = $this->_oConfig->getSystemDataByDescriptor($sType, $sAction, $iObjectId);
  145. if(is_array($mixedResult)) {
  146. $iOwnerId = !empty($mixedResult['owner_id']) ? (int)$mixedResult['owner_id'] : 0;
  147. $iDate = !empty($mixedResult['date']) ? (int)$mixedResult['date'] : 0;
  148. if($this->_oConfig->isUnhideRestored() && !empty($iOwnerId) && !empty($iDate))
  149. $sStatus = BX_TIMELINE_STATUS_ACTIVE;
  150. }
  151. $iId = $this->insertEvent([
  152. 'owner_id' => $iOwnerId,
  153. 'type' => $sType,
  154. 'action' => $sAction,
  155. 'object_id' => $iObjectId,
  156. 'object_owner_id' => $iOwnerId,
  157. 'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault('object'),
  158. 'content' => '',
  159. 'title' => '',
  160. 'description' => '',
  161. 'date' => $iDate,
  162. 'reacted' => $iDate,
  163. 'status' => $sStatus
  164. ]);
  165. $aReposted = $this->getEvents(['browse' => 'id', 'value' => $iId]);
  166. }
  167. return $aReposted;
  168. }
  169. function getRepostedBy($iRepostedId)
  170. {
  171. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableRepostsTrack}` WHERE `reposted_id`=?", $iRepostedId);
  172. return $this->getColumn($sQuery);
  173. }
  174. function getReposts($iRepostedId)
  175. {
  176. return $this->getAll("SELECT * FROM `{$this->_sTableRepostsTrack}` WHERE `reposted_id`=:reposted_id", array(
  177. 'reposted_id' => $iRepostedId
  178. ));
  179. }
  180. function isReposted($iRepostedId, $iOwnerId, $iAuthorId)
  181. {
  182. $sQuery = $this->prepare("SELECT
  183. `te`.`id`
  184. FROM `{$this->_sTableRepostsTrack}` AS `tst`
  185. LEFT JOIN `{$this->_sTable}` AS `te` ON `tst`.`event_id`=`te`.`id`
  186. WHERE `tst`.`author_id`=? AND `tst`.`reposted_id`=? AND `te`.`owner_id`=?", $iAuthorId, $iRepostedId, $iOwnerId);
  187. return (int)$this->getOne($sQuery) > 0;
  188. }
  189. //--- Photo uploader related methods ---//
  190. public function saveMedia($sType, $iEventId, $iItemId)
  191. {
  192. return (int)$this->query("INSERT INTO `" . $this->_aTablesMedia2Events[$sType] . "` SET `event_id`=:event_id, `media_id`=:media_id", array(
  193. 'event_id' => $iEventId,
  194. 'media_id' => $iItemId
  195. )) > 0;
  196. }
  197. public function deleteMedia($sType, $iEventId)
  198. {
  199. return (int)$this->query("DELETE FROM `" . $this->_aTablesMedia2Events[$sType] . "` WHERE `event_id`=:event_id", array(
  200. 'event_id' => $iEventId
  201. )) > 0;
  202. }
  203. public function hasMedia($iEventId)
  204. {
  205. $bResult = false;
  206. foreach(array_keys($this->_aTablesMedia) as $sType) {
  207. $aMedia = $this->getMedia($sType, $iEventId);
  208. if(!empty($aMedia) && is_array($aMedia)) {
  209. $bResult = true;
  210. break;
  211. }
  212. }
  213. return $bResult;
  214. }
  215. public function getMedia($sType, $iEventId, $iOffset = 0, $bFullInfo = false)
  216. {
  217. $sTableMedia = $this->_aTablesMedia[$sType];
  218. $sTableMedia2Events = $this->_aTablesMedia2Events[$sType];
  219. $sMethod = 'getColumn';
  220. $sSelectClause = "`tme`.`media_id` AS `id`";
  221. if($bFullInfo) {
  222. $sMethod = 'getAll';
  223. $sSelectClause = "`tm`.*, `tme`.`event_id` AS `event_id`";
  224. }
  225. $sLimitAddon = '';
  226. if($iOffset != 0)
  227. $sLimitAddon = $this->prepareAsString(" OFFSET ?", $iOffset);
  228. $sQuery = $this->prepare("SELECT " . $sSelectClause . "
  229. FROM `" . $sTableMedia2Events . "` AS `tme`
  230. LEFT JOIN `" . $sTableMedia . "` AS `tm` ON `tme`.`media_id`=`tm`.`id`
  231. WHERE `tme`.`event_id`=?" . $sLimitAddon, $iEventId);
  232. return $this->$sMethod($sQuery);
  233. }
  234. public function getMediaById($sType, $iMediaId)
  235. {
  236. $sTableMedia = $this->_aTablesMedia[$sType];
  237. $sTableMedia2Events = $this->_aTablesMedia2Events[$sType];
  238. 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(
  239. 'media_id' => $iMediaId
  240. ));
  241. }
  242. //--- Link attach related methods ---//
  243. public function getUnusedLinks($iUserId)
  244. {
  245. return $this->getLinksBy(array(
  246. 'type' => 'unused',
  247. 'profile_id' => $iUserId
  248. ));
  249. }
  250. public function deleteUnusedLinks($iUserId, $iLinkId = 0)
  251. {
  252. $aBindings = [
  253. 'profile_id' => $iUserId
  254. ];
  255. $sWhereAddon = '';
  256. if(!empty($iLinkId)) {
  257. $aBindings['id'] = $iLinkId;
  258. $sWhereAddon = " AND `tl`.`id`=:id";
  259. }
  260. return $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`.`profile_id`=:profile_id AND ISNULL(`tle`.`event_id`)" . $sWhereAddon, $aBindings);
  261. }
  262. public function saveLink($iEventId, $iLinkId)
  263. {
  264. $aBindings = array(
  265. 'event_id' => $iEventId,
  266. 'link_id' => $iLinkId
  267. );
  268. $iId = $this->getOne("SELECT `id` FROM `" . $this->_sPrefix . "links2events` WHERE `event_id`=:event_id AND `link_id`=:link_id LIMIT 1", $aBindings);
  269. if(!empty($iId))
  270. return true;
  271. return (int)$this->query("INSERT INTO `" . $this->_sPrefix . "links2events` SET `event_id`=:event_id, `link_id`=:link_id", $aBindings) > 0;
  272. }
  273. public function deleteLink($iId)
  274. {
  275. 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(
  276. 'id' => $iId
  277. )) > 0;
  278. }
  279. public function deleteLinks($iEventId)
  280. {
  281. 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(
  282. 'event_id' => $iEventId
  283. )) > 0;
  284. }
  285. public function getLinks($iEventId)
  286. {
  287. return $this->getLinksBy(array('type' => 'event_id', 'event_id' => $iEventId));
  288. }
  289. public function getLinksBy($aParams = array())
  290. {
  291. $CNF = &$this->_oConfig->CNF;
  292. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  293. $sSelectClause = "`tl`.*";
  294. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  295. switch($aParams['type']) {
  296. case 'id':
  297. $aMethod['name'] = 'getRow';
  298. $aMethod['params'][1] = array(
  299. 'id' => $aParams['id']
  300. );
  301. $sWhereClause = " AND `tl`.`id`=:id";
  302. if(!empty($aParams['profile_id'])) {
  303. $aMethod['params'][1]['profile_id'] = $aParams['profile_id'];
  304. $sWhereClause .= " AND `tl`.`profile_id`=:profile_id";
  305. }
  306. break;
  307. case 'event_id':
  308. $aMethod['params'][1] = array(
  309. 'event_id' => $aParams['event_id']
  310. );
  311. $sJoinClause = "LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`";
  312. $sWhereClause = " AND `tle`.`event_id`=:event_id";
  313. break;
  314. case 'unused':
  315. $aBindings = array(
  316. 'profile_id' => $aParams['profile_id']
  317. );
  318. if(isset($aParams['short']) && $aParams['short'] === true) {
  319. $aMethod['name'] = 'getPairs';
  320. $aMethod['params'][1] = 'url';
  321. $aMethod['params'][2] = 'id';
  322. $aMethod['params'][3] = $aBindings;
  323. }
  324. else
  325. $aMethod['params'][1] = $aBindings;
  326. $sJoinClause = "LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`";
  327. $sWhereClause = " AND `tl`.`profile_id`=:profile_id AND ISNULL(`tle`.`event_id`)";
  328. $sOrderClause = "`tl`.`added` DESC";
  329. break;
  330. }
  331. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  332. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  333. $aMethod['params'][0] = "SELECT
  334. " . $sSelectClause . "
  335. FROM `" . $this->_sPrefix . "links` AS `tl` " . $sJoinClause . "
  336. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  337. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  338. }
  339. public function getHot()
  340. {
  341. return $this->fromCache($this->_oConfig->getCacheHotKey(), 'getColumn', "SELECT `event_id` FROM `" . $this->_sTableHotTrack . "`");
  342. }
  343. public function clearHot()
  344. {
  345. $this->cleanCache($this->_oConfig->getCacheHotKey());
  346. return $this->query("TRUNCATE TABLE `" . $this->_sTableHotTrack . "`");
  347. }
  348. public function getHotTrackByDate($iInterval = 0)
  349. {
  350. $aBindings = [
  351. 'mux' => $this->_oConfig->getHotContentAgeMux()
  352. ];
  353. $sQueryWhere = "";
  354. if($iInterval != 0) {
  355. $aBindings['interval'] = $iInterval;
  356. $sQueryWhere .= " AND `te`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  357. }
  358. $sQuery = "SELECT
  359. `te`.`id` AS `event_id`,
  360. `te`.`date` * (1 - :mux * (UNIX_TIMESTAMP() - `te`.`date`) / UNIX_TIMESTAMP()) AS `value`
  361. FROM `" . $this->_sTable . "` AS `te`
  362. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0)" . $sQueryWhere;
  363. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  364. }
  365. public function getHotTrackByCommentsDate($sModule, $sTableTrack, $iInterval = 0, $iThresholdAge = 0, $iThresholdCount = 0)
  366. {
  367. $aBindings = [
  368. 'mux' => $this->_oConfig->getHotContentAgeMux(),
  369. 'module' => $sModule,
  370. ];
  371. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0)";
  372. if($iInterval != 0) {
  373. $aBindings['interval'] = $iInterval;
  374. $sQueryWhere .= " AND `tt`.`cmt_time` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  375. }
  376. if($iThresholdAge != 0) {
  377. $aBindings['threshold_age'] = $iThresholdAge;
  378. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  379. }
  380. $sQueryGroup = "`te`.`id`";
  381. if($iThresholdCount != 0) {
  382. $aBindings['threshold_count'] = $iThresholdCount;
  383. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`cmt_author_id`) >= :threshold_count";
  384. }
  385. $sQuery = "SELECT
  386. `te`.`id` as `event_id`,
  387. MAX(`tt`.`cmt_time`) * (1 - :mux * (UNIX_TIMESTAMP() - `te`.`date`) / UNIX_TIMESTAMP()) AS `value`
  388. FROM `" . $this->_sTable . "` AS `te`
  389. 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
  390. WHERE 1 " . $sQueryWhere . "
  391. GROUP BY " . $sQueryGroup;
  392. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  393. }
  394. public function getHotTrackByCommentsDateModule($sModule, $sTableTrack, $iInterval = 0, $iThresholdAge = 0, $iThresholdCount = 0)
  395. {
  396. $aBindings = [
  397. 'mux' => $this->_oConfig->getHotContentAgeMux(),
  398. 'module' => $sModule,
  399. ];
  400. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0)";
  401. if($iInterval != 0) {
  402. $aBindings['interval'] = $iInterval;
  403. $sQueryWhere .= " AND `tt`.`cmt_time` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  404. }
  405. if($iThresholdAge != 0) {
  406. $aBindings['threshold_age'] = $iThresholdAge;
  407. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  408. }
  409. $sQueryGroup = "`te`.`object_id`";
  410. if($iThresholdCount != 0) {
  411. $aBindings['threshold_count'] = $iThresholdCount;
  412. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`cmt_author_id`) >= :threshold_count";
  413. }
  414. $sQuery = "SELECT
  415. `te`.`id` as `event_id`,
  416. MAX(`tt`.`cmt_time`) * (1 - :mux * (UNIX_TIMESTAMP() - `te`.`date`) / UNIX_TIMESTAMP()) AS `value`
  417. FROM `" . $this->_sTable . "` AS `te`
  418. 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
  419. WHERE 1 " . $sQueryWhere . "
  420. GROUP BY " . $sQueryGroup;
  421. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  422. }
  423. public function getHotTrackByVotesDate($sModule, $sTableTrack, $iInterval = 0, $iThresholdAge = 0, $iThresholdCount = 0)
  424. {
  425. $aBindings = [
  426. 'mux' => $this->_oConfig->getHotContentAgeMux(),
  427. 'module' => $sModule,
  428. ];
  429. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0)";
  430. if($iInterval != 0) {
  431. $aBindings['interval'] = $iInterval;
  432. $sQueryWhere .= " AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  433. }
  434. if($iThresholdAge != 0) {
  435. $aBindings['threshold_age'] = $iThresholdAge;
  436. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  437. }
  438. $sQueryGroup = "`te`.`id`";
  439. if($iThresholdCount != 0) {
  440. $aBindings['threshold_count'] = $iThresholdCount;
  441. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`author_id`) >= :threshold_count";
  442. }
  443. $sQuery = "SELECT
  444. `te`.`id` as `event_id`,
  445. MAX(`tt`.`date`) * (1 - :mux * (UNIX_TIMESTAMP() - `te`.`date`) / UNIX_TIMESTAMP()) AS `value`
  446. FROM `" . $this->_sTable . "` AS `te`
  447. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`id`=`tt`.`object_id` AND `te`.`object_owner_id`<>`tt`.`author_id` AND `te`.`type`=:module
  448. WHERE 1 " . $sQueryWhere . "
  449. GROUP BY " . $sQueryGroup;
  450. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  451. }
  452. public function getHotTrackByVotesDateModule($sModule, $sTableTrack, $iInterval = 0, $iThresholdAge = 0, $iThresholdCount = 0)
  453. {
  454. $aBindings = [
  455. 'mux' => $this->_oConfig->getHotContentAgeMux(),
  456. 'module' => $sModule,
  457. ];
  458. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0)";
  459. if($iInterval != 0) {
  460. $aBindings['interval'] = $iInterval;
  461. $sQueryWhere .= " AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  462. }
  463. if($iThresholdAge != 0) {
  464. $aBindings['threshold_age'] = $iThresholdAge;
  465. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  466. }
  467. $sQueryGroup = "`te`.`object_id`";
  468. if($iThresholdCount != 0) {
  469. $aBindings['threshold_count'] = $iThresholdCount;
  470. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`author_id`) >= :threshold_count";
  471. }
  472. $sQuery = "SELECT
  473. `te`.`id` as `event_id`,
  474. MAX(`tt`.`date`) * (1 - :mux * (UNIX_TIMESTAMP() - `te`.`date`) / UNIX_TIMESTAMP()) AS `value`
  475. FROM `" . $this->_sTable . "` AS `te`
  476. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`object_id`=`tt`.`object_id` AND `te`.`object_owner_id`<>`tt`.`author_id` AND `te`.`type`=:module
  477. WHERE 1 " . $sQueryWhere . "
  478. GROUP BY " . $sQueryGroup;
  479. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  480. }
  481. /**
  482. * Hot Track by Sum of Votes during specified Period is currently disabled.
  483. */
  484. public function getHotTrackByVotesSum($sModule, $sTableTrack, $iInterval = 24)
  485. {
  486. $sQuery = "SELECT
  487. `te`.`id` as `event_id`,
  488. SUM(`tt`.`value`) AS `value`
  489. FROM `" . $this->_sTable . "` AS `te`
  490. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`id`=`tt`.`object_id` AND `te`.`type`=:module
  491. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  492. GROUP BY `te`.`id`";
  493. return $this->getAll($sQuery, array('module' => $sModule, 'interval' => $iInterval));
  494. }
  495. /**
  496. * Hot Track by Sum of Votes during specified Period is currently disabled.
  497. */
  498. public function getHotTrackByVotesSumModule($sModule, $sTableTrack, $iInterval = 24)
  499. {
  500. $sQuery = "SELECT
  501. `te`.`id` as `event_id`,
  502. SUM(`tt`.`value`) AS `value`
  503. FROM `" . $this->_sTable . "` AS `te`
  504. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`object_id`=`tt`.`object_id` AND `te`.`type`=:module
  505. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  506. GROUP BY `te`.`object_id`";
  507. return $this->getAll($sQuery, array('module' => $sModule, 'interval' => $iInterval));
  508. }
  509. public function updateHotTrack($iEventId, $iValue)
  510. {
  511. return (int)$this->query("INSERT INTO `" . $this->_sTableHotTrack . "` (`event_id`, `value`) VALUES (:event_id, :value) ON DUPLICATE KEY UPDATE `value` = :value", [
  512. 'event_id' => $iEventId,
  513. 'value' => $iValue
  514. ]) > 0;
  515. }
  516. public function rebuildSlice()
  517. {
  518. $this->query("TRUNCATE TABLE `" . $this->_sTableSlice . "`");
  519. $aWhereConditions = [];
  520. $iInterval = $this->_oConfig->getCacheTableInterval();
  521. $aCheckFields = $this->_oConfig->getCacheTableCheckFields();
  522. foreach($aCheckFields as $sField => $sType)
  523. switch($sType) {
  524. case 'flag':
  525. $aWhereConditions[] = "`" . $sField . "` <> 0";
  526. break;
  527. case 'date':
  528. $aWhereConditions[] = "`" . $sField . "` > (UNIX_TIMESTAMP() - " . $iInterval . ")";
  529. break;
  530. }
  531. return $this->query("INSERT INTO `" . $this->_sTableSlice . "` SELECT * FROM `" . $this->_sTable . "` WHERE " . implode(" OR ", $aWhereConditions));
  532. }
  533. public function getEvents($aParams)
  534. {
  535. /**
  536. * @hooks
  537. * @hookdef hook-bx_timeline-get_events_before 'bx_timeline', 'get_events_before' - hook to override params which are used to get events
  538. * - $unit_name - equals `bx_timeline`
  539. * - $action - equals `get_events_before`
  540. * - $object_id - not used
  541. * - $sender_id - not used
  542. * - $extra_params - array of additional params with the following array keys:
  543. * - `params` - [array] by ref, params array as key&value pairs, can be overridden in hook processing
  544. * @hook @ref hook-bx_timeline-get_events_before
  545. */
  546. bx_alert($this->_oConfig->getName(), 'get_events_before', 0, 0, [
  547. 'params' => &$aParams,
  548. 'params_ref' => &$aParams,
  549. ]);
  550. $sTable = isset($aParams['from_cache']) && $aParams['from_cache'] === true ? $this->_sTableSlice : $this->_sTable;
  551. $sTableAlias = $this->getTableAlias();
  552. list($sMethod, $sSelectClause, $mixedJoinClause, $mixedWhereClause, $sOrderClause, $sLimitClause) = $this->_getSqlPartsEvents($aParams);
  553. $bCount = isset($aParams['count']) && $aParams['count'] === true;
  554. if($bCount) {
  555. $sMethod = 'getOne';
  556. $sSelectClause = 'COUNT(*)';
  557. $sOrderClause = '';
  558. $sLimitClause = '';
  559. }
  560. $aAlertParams = $aParams;
  561. unset($aAlertParams['browse']);
  562. /**
  563. * @hooks
  564. * @hookdef hook-bx_timeline-get_events 'bx_timeline', 'get_events' - hook to override events list which will be received from database
  565. * - $unit_name - equals `bx_timeline`
  566. * - $action - equals `get_events`
  567. * - $object_id - not used
  568. * - $sender_id - not used
  569. * - $extra_params - array of additional params with the following array keys:
  570. * - `browse` - [string] browsing type
  571. * - `params` - [array] browse params array as key&value pairs
  572. * - `table` - [string] datatbase table name
  573. * - `table_alias` - [string] datatbase table alias
  574. * - `method` - [string] by ref, database class method name, @see BxDolDb, can be overridden in hook processing
  575. * - `select_clause` - [string] by ref, 'select' part of SQL query, can be overridden in hook processing
  576. * - `join_clause` - [string] by ref, 'join' part of SQL query, can be overridden in hook processing
  577. * - `where_clause` - [string] by ref, 'where' part of SQL query, can be overridden in hook processing
  578. * - `order_clause` - [string] by ref, 'order' part of SQL query, can be overridden in hook processing
  579. * - `limit_clause` - [string] by ref, 'limit' part of SQL query, can be overridden in hook processing
  580. * @hook @ref hook-bx_timeline-get_events
  581. */
  582. bx_alert($this->_oConfig->getName(), 'get_events', 0, 0, [
  583. 'browse' => $aParams['browse'],
  584. 'params' => $aAlertParams,
  585. 'table' => $sTable,
  586. 'table_alias' => $sTableAlias,
  587. 'method' => &$sMethod,
  588. 'select_clause' => &$sSelectClause,
  589. 'join_clause' => &$mixedJoinClause,
  590. 'where_clause' => &$mixedWhereClause,
  591. 'order_clause' => &$sOrderClause,
  592. 'limit_clause' => &$sLimitClause,
  593. 'method_ref' => &$sMethod,
  594. 'select_clause_ref' => &$sSelectClause,
  595. 'join_clause_ref' => &$mixedJoinClause,
  596. 'where_clause_ref' => &$mixedWhereClause,
  597. 'order_clause_ref' => &$sOrderClause,
  598. 'limit_clause_ref' => &$sLimitClause,
  599. ]);
  600. $sSqlMask = "SELECT {select}
  601. FROM `{$sTable}` AS `{$sTableAlias}`
  602. LEFT JOIN `{$this->_sTableHandlers}` ON `{$sTableAlias}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$sTableAlias}`.`action`=`{$this->_sTableHandlers}`.`alert_action` {join}
  603. WHERE 1 {where} {order} {limit}";
  604. if(is_string($mixedWhereClause)) {
  605. $aSqlMarkers = array(
  606. 'select' => $sSelectClause,
  607. 'join' => $mixedJoinClause,
  608. 'where' => $mixedWhereClause,
  609. 'order' => $sOrderClause,
  610. 'limit' => $sLimitClause
  611. );
  612. return $this->$sMethod(bx_replace_markers($sSqlMask, $aSqlMarkers));
  613. }
  614. $bJoinAsArray = !empty($mixedJoinClause) && is_array($mixedJoinClause);
  615. $sOrderSubclause = $sLimitSubclause = '';
  616. if(!$bCount) {
  617. $sOrderSubclause = $sOrderClause;
  618. $sLimitSubclause = isset($aParams['per_page']) ? 'LIMIT 0, ' . ($aParams['start'] + $aParams['per_page']) : '';
  619. }
  620. $aSqlParts = array();
  621. foreach($mixedWhereClause as $sKey => $sValue) {
  622. $aSqlMarkers = array(
  623. 'select' => $sSelectClause,
  624. 'join' => $bJoinAsArray ? (isset($mixedJoinClause[$sKey]) ? $mixedJoinClause[$sKey] : '') : $mixedJoinClause,
  625. 'where' => $sValue,
  626. 'order' => $sOrderSubclause,
  627. 'limit' => $sLimitSubclause
  628. );
  629. $sSqlPart = bx_replace_markers($sSqlMask, $aSqlMarkers);
  630. $aSqlParts[] = !$bCount ? $sSqlPart : (int)$this->$sMethod($sSqlPart);
  631. }
  632. if($bCount)
  633. return array_sum($aSqlParts);
  634. $sSqlMaskUnion = 'SELECT *, GROUP_CONCAT(`owner_id`) AS `owner_id_grouped` FROM ((' . implode(') UNION (', $aSqlParts) . ')) AS `tu` GROUP BY `tu`.`source` {order} {limit}';
  635. $sSql = bx_replace_markers($sSqlMaskUnion, [
  636. 'order' => str_replace("`{$sTableAlias}`.", '', $sOrderClause),
  637. 'limit' => str_replace("`{$sTableAlias}`.", '', $sLimitClause),
  638. ]);
  639. //echoDbg($sSql); exit;
  640. return $this->$sMethod($sSql);
  641. }
  642. public function updateEventFlagsByType($sType, $iEventId)
  643. {
  644. $aTablesEventFlags = $this->_oConfig->getFiltersMedia(false);
  645. if(empty($aTablesEventFlags[$sType]))
  646. return false;
  647. return $this->query("INSERT IGNORE INTO `" . $aTablesEventFlags[$sType] . "` (`event_id`) VALUES (:event_id)", [
  648. 'event_id' => $iEventId,
  649. ]) !== false;
  650. }
  651. public function deleteEventFlagsByType($sType, $iEventId)
  652. {
  653. $aTablesEventFlags = $this->_oConfig->getFiltersMedia(false);
  654. if(empty($aTablesEventFlags[$sType]))
  655. return false;
  656. return $this->query("DELETE FROM `" . $aTablesEventFlags[$sType] . "` WHERE `event_id`=:event_id", [
  657. 'event_id' => $iEventId
  658. ]) !== false;
  659. }
  660. public function deleteEventFlags($iEventId)
  661. {
  662. $aTypes = $this->_oConfig->getFiltersMedia();
  663. foreach($aTypes as $sType)
  664. $this->deleteEventFlagsByType($sType, $iEventId);
  665. }
  666. protected function _getFilterAddon($iOwnerId, $sFilter)
  667. {
  668. $sTableAlias = $this->getTableAlias();
  669. switch($sFilter) {
  670. /**
  671. * Direct posts in Timeline made by a timeline owner ($iOwnerId)
  672. */
  673. case BX_TIMELINE_FILTER_OWNER:
  674. $sFilterAddon = $this->prepareAsString(" AND `{$sTableAlias}`.`action`='' AND `{$sTableAlias}`.`object_id`=? ", $iOwnerId);
  675. break;
  676. /**
  677. * Direct posts in Timeline made by users except a timeline owner ($iOwnerId)
  678. */
  679. case BX_TIMELINE_FILTER_OTHER:
  680. $sFilterAddon = $this->prepareAsString(" AND `{$sTableAlias}`.`action`='' AND `{$sTableAlias}`.`object_id`<>? ", $iOwnerId);
  681. break;
  682. /**
  683. * All (Direct and System) posts in Timeline (owned by $iOwnerId) made by users except the viewer
  684. */
  685. case BX_TIMELINE_FILTER_OTHER_VIEWER:
  686. $sFilterAddon = $this->prepareAsString(" AND (`{$sTableAlias}`.`action`<>'' OR (`{$sTableAlias}`.`action`='' AND `{$sTableAlias}`.`object_id`<>?)) ", bx_get_logged_profile_id());
  687. break;
  688. case BX_TIMELINE_FILTER_ALL:
  689. default:
  690. $sFilterAddon = "";
  691. }
  692. return $sFilterAddon;
  693. }
  694. protected function _getSqlPartsEvents($aParams)
  695. {
  696. $sMethod = 'getAll';
  697. $sTableAlias = $this->getTableAlias();
  698. $sSelectClause = "`{$sTableAlias}`.*";
  699. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  700. switch($aParams['browse']) {
  701. case 'owner_id':
  702. $sWhereClause = $this->prepareAsString("AND `{$sTableAlias}`.`owner_id`=? ", $aParams['value']);
  703. break;
  704. case 'source':
  705. $sWhereClause = $this->prepareAsString("AND `{$sTableAlias}`.`source`=? ", $aParams['value']);
  706. break;
  707. case 'common_by_object':
  708. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  709. $sWhereClause = $this->prepareAsString("AND `{$sTableAlias}`.`system`='0' AND `{$sTableAlias}`.`object_id`=? ", $aParams['value']);
  710. break;
  711. case 'descriptor':
  712. $sMethod = 'getRow';
  713. $sWhereClause = "";
  714. if(isset($aParams['type']))
  715. $sWhereClause .= $this->prepareAsString("AND `{$sTableAlias}`.`type`=? ", $aParams['type']);
  716. if(isset($aParams['action']))
  717. $sWhereClause .= $this->prepareAsString("AND `{$sTableAlias}`.`action`=? ", $aParams['action']);
  718. if(isset($aParams['object_id']))
  719. $sWhereClause .= $this->prepareAsString("AND `{$sTableAlias}`.`object_id`=? ", $aParams['object_id']);
  720. $sLimitClause = "LIMIT 1";
  721. break;
  722. case 'reposted_by_track':
  723. $sJoinClause = $this->prepareAsString("INNER JOIN `" . $this->_sTableRepostsTrack . "` AS `trt` ON `{$sTableAlias}`.`id`=`trt`.`event_id` AND `trt`.`reposted_id`=?", $aParams['value']);
  724. break;
  725. case 'reposted_by_descriptor':
  726. $sWhereClause = "";
  727. if(isset($aParams['type']))
  728. $sWhereClause .= "AND `{$sTableAlias}`.`content` LIKE " . $this->escape('%' . $aParams['type'] . '%');
  729. if(isset($aParams['action']))
  730. $sWhereClause .= "AND `{$sTableAlias}`.`content` LIKE " . $this->escape('%' . $aParams['action'] . '%');
  731. break;
  732. case 'last':
  733. $sMethod = 'getRow';
  734. $sSelectClause .= ", YEAR(FROM_UNIXTIME(`{$sTableAlias}`.`date`)) AS `year`";
  735. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  736. $sOrderClause = "ORDER BY `{$sTableAlias}`.`date` ASC, `{$sTableAlias}`.`id` ASC";
  737. $sLimitClause = "LIMIT 1";
  738. break;
  739. case 'ids':
  740. $sWhereClause = "AND `{$sTableAlias}`.`id` IN (" . $this->implode_escape($aParams['ids']) . ") ";
  741. break;
  742. default:
  743. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = parent::_getSqlPartsEvents($aParams);
  744. }
  745. if($this->_isList($aParams)) {
  746. $sOrderClause = "";
  747. switch($aParams['type']) {
  748. case BX_TIMELINE_TYPE_HOT:
  749. $sOrderClause = "`{$sTableAlias}`.`sticked` DESC, `{$this->_sTableHotTrack}`.`value` DESC, ";
  750. break;
  751. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  752. case BX_BASE_MOD_NTFS_TYPE_CONNECTIONS:
  753. case BX_TIMELINE_TYPE_CHANNELS:
  754. case BX_TIMELINE_TYPE_CONNECTED_CONTEXTS:
  755. case BX_TIMELINE_TYPE_FEED:
  756. case BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS:
  757. $sOrderClause = "`{$sTableAlias}`.`sticked` DESC, ";
  758. break;
  759. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  760. $sOrderClause = "`{$sTableAlias}`.`pinned` DESC, ";
  761. break;
  762. }
  763. if($this->_oConfig->isSortByUnread()) {
  764. $oProfileQuery = BxDolProfileQuery::getInstance();
  765. $iDate = 0;
  766. $iOwner = (int)$aParams['owner_id'];
  767. $aOwner = $oProfileQuery->getInfoById($iOwner);
  768. $iViewer = !empty($aParams['viewer_id']) ? (int)$aParams['viewer_id'] : bx_get_logged_profile_id();
  769. if(!empty($aOwner) && is_array($aOwner) && BxDolRequest::serviceExists($aOwner['type'], 'is_fan') && bx_srv($aOwner['type'], 'is_fan', [$iOwner, $iViewer])) {
  770. $oModule = BxDolModule::getInstance($aOwner['type']);
  771. $aConnection = BxDolConnection::getObjectInstance($oModule->_oConfig->CNF['OBJECT_CONNECTIONS'])->getConnection($iViewer, $iOwner);
  772. $iDate = (int)$aConnection['added'];
  773. }
  774. else {
  775. $aViewer = $oProfileQuery->getInfoById($iViewer);
  776. if(!empty($aViewer) && is_array($aViewer))
  777. $iDate = bx_srv($aViewer['type'], 'get_date_added', [$aViewer['content_id']]);
  778. }
  779. $sSelectClause .= ", IF(NOT ISNULL(`teu`.`id`), 1, 0) AS `read`";
  780. $sOrderClause .= $this->prepareAsString("IF(`{$sTableAlias}`.`date` > ?, `read`, 1) ASC, ", $iDate);
  781. }
  782. if($this->_oConfig->isSortByReaction())
  783. $sOrderClause .= "`{$sTableAlias}`.`reacted` DESC, ";
  784. else
  785. $sOrderClause .= "`{$sTableAlias}`.`date` DESC, ";
  786. $sOrderClause = "ORDER BY " . $sOrderClause . "`{$sTableAlias}`.`id` DESC";
  787. }
  788. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  789. }
  790. protected function _getSqlPartsEventsListStatusAdmin($aParams)
  791. {
  792. if(isset($aParams['moderator']) && $aParams['moderator'] === true)
  793. return '';
  794. $iViewerId = !empty($aParams['viewer_id']) ? $aParams['viewer_id'] : bx_get_logged_profile_id();
  795. $sTableAlias = $this->getTableAlias();
  796. //--- Check viewer as event author.
  797. $sWhereClause = $this->prepareAsString("`{$sTableAlias}`.`object_owner_id`=?", $iViewerId);
  798. //--- Check viewer as an administrator/moderator of event author.
  799. $aGroups = [];
  800. $aModules = bx_srv('system', 'get_modules_by_type', ['profile']);
  801. foreach($aModules as $aModule) {
  802. $oModule = BxDolModule::getInstance($aModule['name']);
  803. if(!$oModule || !($oModule instanceof BxBaseModGroupsModule))
  804. continue;
  805. $aGroups = array_merge($aGroups, $oModule->getGroupsByFan($iViewerId, [
  806. BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR,
  807. BX_BASE_MOD_GROUPS_ROLE_MODERATOR
  808. ]));
  809. }
  810. if(!empty($aGroups))
  811. $sWhereClause .= " OR `{$sTableAlias}`.`object_owner_id` IN (" . $this->implode_escape($aGroups) . ")";
  812. 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);
  813. }
  814. protected function _getSqlPartsEventsList($aParams)
  815. {
  816. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  817. $sTableAlias = $this->getTableAlias();
  818. $mixedJoinClause = $mixedWhereClause = "";
  819. $mixedJoinClause = "INNER JOIN `sys_profiles` AS `tpoo` ON ABS(`{$sTableAlias}`.`object_owner_id`)=`tpoo`.`id` AND `tpoo`.`status`='active'";
  820. if($this->_oConfig->isSortByUnread())
  821. $mixedJoinClause .= $this->prepareAsString(" LEFT JOIN `{$this->_sTableEvent2User}` AS `teu` ON `{$sTableAlias}`.`id`=`teu`.`event_id` AND `teu`.`user_id`=? ", $aParams['viewer_id']);
  822. $sWhereClauseStatus = "AND `{$sTableAlias}`.`active`='1' ";
  823. $sWhereClauseStatus .= $this->prepareAsString("AND `{$sTableAlias}`.`status`=? ", isset($aParams['status']) ? $aParams['status'] : BX_TIMELINE_STATUS_ACTIVE);
  824. $sWhereClauseStatus .= $this->_getSqlPartsEventsListStatusAdmin($aParams);
  825. //--- Apply filter
  826. $sWhereClauseFilter = "";
  827. if(isset($aParams['filter']))
  828. $sWhereClauseFilter = $this->_getFilterAddon($aParams['owner_id'], $aParams['filter']);
  829. //--- Apply timeline
  830. $sWhereClauseTimeline = "";
  831. if(isset($aParams['timeline']) && !empty($aParams['timeline']) && strpos($aParams['timeline'], '-') !== false) {
  832. list($iY, $iM, $iD) = explode('-', $aParams['timeline']);
  833. $sWhereClauseTimeline = $this->prepareAsString("AND `date`<=? ", mktime(23, 59, 59, (int)$iM, (int)$iD, (int)$iY));
  834. }
  835. //--- Apply modules or handlers filter
  836. $sWhereClauseModules = "";
  837. if(!empty($aParams['modules']) && is_array($aParams['modules']))
  838. $sWhereClauseModules = "AND `" . $sTableAlias . "`.`type` IN (" . $this->implode_escape($aParams['modules']) . ") ";
  839. $sWhereClauseHidden = "";
  840. if(empty($sWhereClauseModules)) {
  841. $aHidden = $this->_oConfig->getHandlersHidden();
  842. $sWhereClauseHidden = !empty($aHidden) && is_array($aHidden) ? "AND `" . $this->_sTableHandlers . "`.`id` NOT IN (" . $this->implode_escape($aHidden) . ") " : "";
  843. }
  844. //--- Apply media filter
  845. $sWhereClauseMedias = "";
  846. if(($sKm = 'media') && !empty($aParams[$sKm])) {
  847. $aTablesEventFlags = $this->_oConfig->getFiltersMedia(false);
  848. if(is_array($aParams[$sKm]) && ($aMts = $aParams[$sKm]) && !(count($aMts) == 1 && empty($aTablesEventFlags[reset($aMts)])) && $aMts !== array_keys($aTablesEventFlags)) {
  849. $sWhereSubclauseMedias = "0";
  850. foreach($aMts as $sMt) {
  851. if(empty($aTablesEventFlags[$sMt]))
  852. continue;
  853. $sTableAliasFlag = 't' . substr($sMt, 0, 2);
  854. $mixedJoinClause .= " LEFT JOIN `" . $aTablesEventFlags[$sMt] . "` AS `{$sTableAliasFlag}` ON `{$sTableAlias}`.`id`=`{$sTableAliasFlag}`.`event_id`";
  855. $sWhereSubclauseMedias .= " OR NOT ISNULL(`{$sTableAliasFlag}`.`event_id`)";
  856. }
  857. $sWhereClauseMedias .= "AND ({$sWhereSubclauseMedias}) ";
  858. }
  859. else if(is_string($aParams[$sKm]) && ($sMt = $aParams[$sKm]) && !empty($aTablesEventFlags[$sMt])) {
  860. $sTableAliasFlag = 't' . substr($sMt, 0, 2);
  861. $mixedJoinClause .= " INNER JOIN `" . $aTablesEventFlags[$sMt] . "` AS `{$sTableAliasFlag}` ON `{$sTableAlias}`.`id`=`{$sTableAliasFlag}`.`event_id`";
  862. }
  863. }
  864. //--- Apply mute filter
  865. $sWhereClauseMuted = "";
  866. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('connection_mute'));
  867. if($oConnection) {
  868. $aMuted = $oConnection->getConnectedContent(bx_get_logged_profile_id());
  869. if(!empty($aMuted) && is_array($aMuted)) {
  870. $sMuted = "NOT IN (". $this->implode_escape($aMuted) . ")";
  871. $sWhereClauseMuted = "AND `{$sTableAlias}`.`owner_id` $sMuted AND `{$sTableAlias}`.`object_owner_id` $sMuted ";
  872. }
  873. }
  874. //--- Apply unpublished (date in future) filter
  875. $sWhereClauseUnpublished = $this->prepareAsString("AND IF(`{$sTableAlias}`.`system`='0' AND `{$sTableAlias}`.`object_id` = ?, 1, `{$sTableAlias}`.`date` <= UNIX_TIMESTAMP()) ", bx_get_logged_profile_id());
  876. //--- Apply content filter
  877. $oCf = BxDolContentFilter::getInstance();
  878. $sWhereClauseCf = $oCf->isEnabled() ? $oCf->getSQLParts($sTableAlias, 'object_cf') . ' ' : '';
  879. //--- Check type
  880. $mixedJoinSubclause = $mixedWhereSubclause = "";
  881. switch($aParams['type']) {
  882. //--- Feed: Hot
  883. case BX_TIMELINE_TYPE_HOT:
  884. //--- Apply privacy filter
  885. $aPrivacyGroups = array(BX_DOL_PG_ALL);
  886. if(isLogged())
  887. $aPrivacyGroups[] = BX_DOL_PG_MEMBERS;
  888. $aQueryParts = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'))->getContentByGroupAsSQLPart($aPrivacyGroups);
  889. $mixedWhereClause .= $aQueryParts['where'] . " ";
  890. //--- Select Hot posts.
  891. $mixedJoinSubclause = "LEFT JOIN `{$this->_sTableHotTrack}` ON `{$sTableAlias}`.`id`=`{$this->_sTableHotTrack}`.`event_id`";
  892. $mixedWhereSubclause = "NOT ISNULL(`{$this->_sTableHotTrack}`.`value`)";
  893. //--- Select Promoted posts.
  894. $mixedWhereSubclause .= " OR `{$sTableAlias}`.`promoted` <> '0'";
  895. break;
  896. //--- Feed: Public
  897. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  898. //--- Apply privacy filter
  899. $aPrivacyGroups = array(BX_DOL_PG_ALL);
  900. if(isLogged())
  901. $aPrivacyGroups[] = BX_DOL_PG_MEMBERS;
  902. $aQueryParts = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'))->getContentByGroupAsSQLPart($aPrivacyGroups);
  903. $mixedWhereClause .= $aQueryParts['where'] . " ";
  904. if($this->_oConfig->isShowAll())
  905. break;
  906. //--- Select All System posts
  907. $mixedWhereSubclause = "`{$sTableAlias}`.`system`='1'";
  908. //--- Select Public (created on Home Page Timeline (Public Feed)) updates
  909. $mixedWhereSubclause .= $this->prepareAsString(" OR `{$sTableAlias}`.`owner_id`=?", 0);
  910. //--- Select Promoted posts.
  911. $mixedWhereSubclause .= " OR `{$sTableAlias}`.`promoted` <> '0'";
  912. break;
  913. //--- Feed: Profile
  914. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  915. if(empty($aParams['owner_id']))
  916. break;
  917. //--- Select Own (System and Direct) posts from Profile's Timeline.
  918. $mixedWhereSubclause = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = ?)", $aParams['owner_id']);
  919. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  920. $mixedWhereSubclause .= $this->prepareAsString(" OR (`{$sTableAlias}`.`owner_id` = '0' AND `{$sTableAlias}`.`system`='0' AND `{$sTableAlias}`.`object_id` = ?)", $aParams['owner_id']);
  921. break;
  922. //--- Feed: All Profile Connections
  923. case BX_BASE_MOD_NTFS_TYPE_CONNECTIONS:
  924. if(empty($aParams['owner_id']))
  925. break;
  926. $mixedJoinSubclause = [];
  927. $mixedWhereSubclause = [];
  928. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  929. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  930. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  931. $aJoin1 = $aQueryParts['join'];
  932. $mixedJoinSubclause['p1'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  933. $mixedWhereSubclause['p1'] = "1";
  934. //--- Exclude Own (Direct) posts on timelines of following members.
  935. //--- Note. Disabled for now.
  936. //$mixedWhereSubclause['p1'] .= $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  937. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'object_id', $aParams['owner_id']);
  938. $aJoin2 = $aQueryParts['join'];
  939. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'object_id', $aParams['owner_id']);
  940. $aJoin2 = $aQueryParts['join'];
  941. $aJoin2['table_alias'] = 'cc';
  942. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  943. $mixedJoinSubclause['p2'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system`='0' AND " . $aJoin2['condition'];
  944. $mixedWhereSubclause['p2'] = "1";
  945. //--- Select Promoted posts.
  946. $mixedJoinSubclause['p3'] = "";
  947. $mixedWhereSubclause['p3'] = "`{$sTableAlias}`.`promoted` <> '0'";
  948. break;
  949. //--- Feed: Profile Connections to contexts by type (groups, events, spaces, etc) only.
  950. case BX_TIMELINE_TYPE_CONNECTED_CONTEXTS:
  951. if(empty($aParams['owner_id']) || empty($aParams['context']))
  952. break;
  953. $mixedJoinSubclause = [];
  954. $mixedWhereSubclause = [];
  955. //--- Filter out unnecessary contexts.
  956. $aContexts = is_array($aParams['context']) ? $aParams['context'] : [$aParams['context']];
  957. $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' ";
  958. //--- Join System posts received by following contexts.
  959. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  960. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  961. $aJoin1 = $aQueryParts['join'];
  962. $mixedJoinSubclause['p1'] .= "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  963. $mixedWhereSubclause['p1'] = "1";
  964. /*
  965. * Disabled because promoted posts from different contexts
  966. * took a lot of space in the beginning of timeline.
  967. *
  968. //--- Select Promoted posts.
  969. $mixedJoinSubclause['p2'] = "";
  970. $mixedWhereSubclause['p2'] = "`{$sTableAlias}`.`promoted` <> '0'";
  971. */
  972. break;
  973. //--- Feed: Profile Connections to Channel contexts only
  974. case BX_TIMELINE_TYPE_CHANNELS:
  975. if(empty($aParams['owner_id']))
  976. break;
  977. $mixedJoinSubclause = [];
  978. $mixedWhereSubclause = [];
  979. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  980. //--- Join System posts received by following channels.
  981. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  982. $aJoin1 = $aQueryParts['join'];
  983. $mixedJoinSubclause['p1'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  984. $mixedWhereSubclause['p1'] = "`{$sTableAlias}`.`type`='bx_channels'";
  985. //--- Exclude Own (Direct) posts on timelines of following members.
  986. //--- Note. Disabled for now.
  987. //$mixedWhereSubclause['p1'] = $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  988. //--- Select Promoted posts.
  989. $mixedJoinSubclause['p2'] = "";
  990. $mixedWhereSubclause['p2'] = "`{$sTableAlias}`.`promoted` <> '0'";
  991. break;
  992. //--- Feed: Profile + Profile Connections to Non-Channel contexts
  993. case BX_TIMELINE_TYPE_FEED:
  994. case BX_TIMELINE_TYPE_FOR_YOU:
  995. if(empty($aParams['owner_id']))
  996. break;
  997. $bForYou = $aParams['type'] == BX_TIMELINE_TYPE_FOR_YOU;
  998. $aForYouSources = $bForYou ? $this->_oConfig->getForYouSources() : [];
  999. $mixedJoinSubclause = [];
  1000. $mixedWhereSubclause = [];
  1001. if($aParams['type'] == BX_TIMELINE_TYPE_FEED || ($bForYou && in_array(BX_TIMELINE_FYFS_FEED, $aForYouSources))) {
  1002. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  1003. //--- Select Own (System and Direct) posts from Profile's Timeline.
  1004. $sWhereSubclauseOwnProfile = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = ?)", $aParams['owner_id']);
  1005. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  1006. $sWhereSubclauseOwnPublic = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = '0' AND `{$sTableAlias}`.`system`='0' AND `{$sTableAlias}`.`object_id` = ?)", $aParams['owner_id']);
  1007. $mixedJoinSubclause['p1'] = "";
  1008. $mixedWhereSubclause['p1'] = "(" . $sWhereSubclauseOwnProfile . " OR " . $sWhereSubclauseOwnPublic . ")";
  1009. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  1010. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  1011. $aJoin1 = $aQueryParts['join'];
  1012. $mixedJoinSubclause['p2'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  1013. $mixedWhereSubclause['p2'] = "`{$sTableAlias}`.`type`<>'bx_channels'";
  1014. //--- Exclude Own (Direct) posts on timelines of following members.
  1015. //--- Note. Disabled for now and next check is used instead.
  1016. //$mixedWhereSubclause['p2'] = $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1))", $aParams['owner_id']);
  1017. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'object_owner_id', $aParams['owner_id']);
  1018. $aJoin2 = $aQueryParts['join'];
  1019. $aJoin2['table_alias'] = 'cc';
  1020. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  1021. $mixedJoinSubclause['p3'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system` = 0 AND `" . $sTableAlias . "`.`object_privacy_view` > 0 AND " . $aJoin2['condition'];
  1022. $mixedWhereSubclause['p3'] = "1";
  1023. //--- Select Promoted posts.
  1024. $mixedJoinSubclause['p4'] = "";
  1025. $mixedWhereSubclause['p4'] = "`{$sTableAlias}`.`promoted` <> '0'";
  1026. }
  1027. //--- 'For You' feed only: Channels
  1028. if($bForYou && in_array(BX_TIMELINE_FYFS_CHANNELS, $aForYouSources)) {
  1029. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  1030. //--- Join System posts received by following channels.
  1031. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  1032. $aJoin1 = $aQueryParts['join'];
  1033. $mixedJoinSubclause['p5'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  1034. $mixedWhereSubclause['p5'] = "`{$sTableAlias}`.`type`='bx_channels'";
  1035. }
  1036. //--- 'For You' feed only: Add Hot posts
  1037. if($bForYou && in_array(BX_TIMELINE_FYFS_HOT, $aForYouSources)) {
  1038. $mixedJoinSubclause['p6'] = "INNER JOIN `{$this->_sTableHotTrack}` ON `{$sTableAlias}`.`id`=`{$this->_sTableHotTrack}`.`event_id`";
  1039. $mixedWhereSubclause['p6'] = "1";
  1040. }
  1041. //--- 'For You' feed only: Add posts from recommended friends
  1042. if($bForYou && in_array(BX_TIMELINE_FYFS_RECOM_FRIENDS, $aForYouSources) && ($oRecommendation = BxDolRecommendation::getObjectInstance('sys_friends')) !== false) {
  1043. $aList = $oRecommendation->getList($aParams['owner_id'], [
  1044. 'threshold' => $this->_oConfig->getForYouThresholdRecomFrds()
  1045. ]);
  1046. if(!empty($aList) && is_array($aList)) {
  1047. $mixedJoinSubclause['p7'] = "";
  1048. $mixedWhereSubclause['p7'] = "`{$sTableAlias}`.`owner_id` IN (" . $this->implode_escape(array_keys($aList)) . ")";
  1049. }
  1050. }
  1051. //--- 'For You' feed only: Add posts from recommended subscriptions
  1052. if($bForYou && in_array(BX_TIMELINE_FYFS_RECOM_SUBSCRIPTIONS, $aForYouSources) && ($oRecommendation = BxDolRecommendation::getObjectInstance('sys_subscriptions')) !== false) {
  1053. $aList = $oRecommendation->getList($aParams['owner_id'], [
  1054. 'threshold' => $this->_oConfig->getForYouThresholdRecomSbns()
  1055. ]);
  1056. if(!empty($aList) && is_array($aList)) {
  1057. $mixedJoinSubclause['p8'] = "";
  1058. $mixedWhereSubclause['p8'] = "`{$sTableAlias}`.`owner_id` IN (" . $this->implode_escape(array_keys($aList)) . ")";
  1059. }
  1060. }
  1061. //--- 'For You' feed only: Public
  1062. if($bForYou && in_array(BX_TIMELINE_FYFS_PUBLIC, $aForYouSources)) {
  1063. //--- Select All System posts
  1064. $sWhereSubclause = "`{$sTableAlias}`.`system`='1'";
  1065. //--- Select Public (created on Home Page Timeline (Public Feed)) updates or all if 'showAll' setting is enabled
  1066. if(!$this->_oConfig->isShowAll())
  1067. $sWhereSubclause .= $this->prepareAsString(" OR `{$sTableAlias}`.`owner_id`=?", 0);
  1068. else
  1069. $sWhereSubclause .= $this->prepareAsString(" OR `{$sTableAlias}`.`owner_id`<>?", $aParams['owner_id']);
  1070. $mixedJoinSubclause['p9'] = "";
  1071. $mixedWhereSubclause['p9'] = "(" . $sWhereSubclause . ")";
  1072. }
  1073. break;
  1074. //--- Feed: Profile + All Profile Connections
  1075. case BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS:
  1076. if(empty($aParams['owner_id']))
  1077. break;
  1078. $mixedJoinSubclause = [];
  1079. $mixedWhereSubclause = [];
  1080. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  1081. //--- Select Own (System and Direct) posts from Profile's Timeline.
  1082. $sWhereSubclauseOwnProfile = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = ?)", $aParams['owner_id']);
  1083. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  1084. $sWhereSubclauseOwnPublic = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = '0' AND `{$sTableAlias}`.`system`='0' AND `{$sTableAlias}`.`object_id` = ?)", $aParams['owner_id']);
  1085. $mixedJoinSubclause['p1'] = "";
  1086. $mixedWhereSubclause['p1'] = "(" . $sWhereSubclauseOwnProfile . " OR " . $sWhereSubclauseOwnPublic . ")";
  1087. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  1088. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  1089. $aJoin1 = $aQueryParts['join'];
  1090. $mixedJoinSubclause['p2'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  1091. $mixedWhereSubclause['p2'] = "1";
  1092. //--- Exclude Own (Direct) posts on timelines of following members.
  1093. //--- Note. Disabled for now and next check is used instead.
  1094. //$mixedWhereSubclause['p2'] = $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1))", $aParams['owner_id']);
  1095. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'object_id', $aParams['owner_id']);
  1096. $aJoin2 = $aQueryParts['join'];
  1097. $aJoin2['table_alias'] = 'cc';
  1098. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  1099. $mixedJoinSubclause['p3'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system`='0' AND " . $aJoin2['condition'];
  1100. $mixedWhereSubclause['p3'] = "1";
  1101. //--- Select Promoted posts.
  1102. $mixedJoinSubclause['p4'] = "";
  1103. $mixedWhereSubclause['p4'] = "`{$sTableAlias}`.`promoted` <> '0'";
  1104. break;
  1105. //--- Feed: Profile Connections to contexts from Selected module
  1106. default:
  1107. if(empty($aParams['owner_id']))
  1108. break;
  1109. $mixedJoinSubclause = [];
  1110. $mixedWhereSubclause = [];
  1111. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  1112. //--- Join System posts received by following channels.
  1113. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  1114. $aJoin1 = $aQueryParts['join'];
  1115. $mixedJoinSubclause['p1'] = "INNER JOIN `sys_profiles` AS `p` ON `" . $sTableAlias ."`.`owner_id`=`p`.`id` AND `p`.`type`='" . $aParams['type'] . "' ";
  1116. $mixedJoinSubclause['p1'] .= "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  1117. $mixedWhereSubclause['p1'] = "1";
  1118. if(!empty($aParams['context']))
  1119. $mixedWhereSubclause['p1'] .= $this->prepareAsString(" AND `" . $aJoin1['table_alias'] . "`.`content`=?", $aParams['context']);
  1120. //--- Exclude Own (Direct) posts on timelines of following members.
  1121. //--- Note. Disabled for now.
  1122. //$mixedWhereSubclause['p1'] = $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  1123. //--- Select Promoted posts.
  1124. $mixedJoinSubclause['p2'] = "";
  1125. $mixedWhereSubclause['p2'] = "`" . $sTableAlias . "`.`promoted` <> '0'";
  1126. break;
  1127. }
  1128. $aAlertParams = $aParams;
  1129. unset($aAlertParams['type'], $aAlertParams['owner_id']);
  1130. /**
  1131. * @hooks
  1132. * @hookdef hook-bx_timeline-get_list_by_type 'bx_timeline', 'get_list_by_type' - hook to override SQL query parts which are used to get events list
  1133. * - $unit_name - equals `bx_timeline`
  1134. * - $action - equals `get_list_by_type`
  1135. * - $object_id - not used
  1136. * - $sender_id - not used
  1137. * - $extra_params - array of additional params with the following array keys:
  1138. * - `type` - [string] a type of events list
  1139. * - `owner_id` - [int] owner profile id
  1140. * - `params` - [array] browse params array as key&value pairs
  1141. * - `table` - [string] datatbase table name
  1142. * - `table_alias` - [string] datatbase table alias
  1143. * - `join_clause` - [string] by ref, 'join' part of SQL query, can be overridden in hook processing
  1144. * - `join_subclause` - [string] or [array] by ref, 'join subclause' part of SQL query, string is attached to 'join' part, array is used to create query with UNIONs, can be overridden in hook processing
  1145. * - `where_clause` - [string] by ref, 'where' part of SQL query, can be overridden in hook processing
  1146. * - `where_clause_status` - [string] by ref, 'status' condition in 'where' part of SQL query, can be overridden in hook processing
  1147. * - `where_clause_filter` - [string] by ref, 'filter' condition in 'where' part of SQL query, can be overridden in hook processing
  1148. * - `where_clause_timeline` - [string] by ref, 'timeline' condition in 'where' part of SQL query, can be overridden in hook processing
  1149. * - `where_clause_modules` - [string] by ref, 'modules' condition in 'where' part of SQL query, can be overridden in hook processing
  1150. * - `where_clause_hidden` - [string] by ref, 'hidden' condition in 'where' part of SQL query, can be overridden in hook processing
  1151. * - `where_clause_medias` - [string] by ref, 'medias' condition in 'where' part of SQL query, can be overridden in hook processing
  1152. * - `where_clause_muted` - [string] by ref, 'muted' conditions in 'where' part of SQL query, can be overridden in hook processing
  1153. * - `where_clause_unpublished` - [string] by ref, 'unpublished' condition in 'where' part of SQL query, can be overridden in hook processing
  1154. * - `where_clause_cf` - [string] by ref, 'cf' condition in 'where' part of SQL query, can be overridden in hook processing
  1155. * - `where_subclause` - [string] or [array] by ref, 'where subclause' part of SQL query, string is attached to 'where' part, array is used to create query with UNIONs, can be overridden in hook processing
  1156. * @hook @ref hook-bx_timeline-get_list_by_type
  1157. */
  1158. bx_alert($this->_oConfig->getName(), 'get_list_by_type', 0, 0, [
  1159. 'type' => $aParams['type'],
  1160. 'owner_id' => $aParams['owner_id'],
  1161. 'params' => $aAlertParams,
  1162. 'table' => isset($aParams['from_cache']) && $aParams['from_cache'] === true ? $this->_sTableSlice : $this->_sTable,
  1163. 'table_alias' => $sTableAlias,
  1164. 'join_clause' => &$mixedJoinClause,
  1165. 'join_subclause' => &$mixedJoinSubclause,
  1166. 'where_clause' => &$mixedWhereClause,
  1167. 'where_clause_status' => &$sWhereClauseStatus,
  1168. 'where_clause_filter' => &$sWhereClauseFilter,
  1169. 'where_clause_timeline' => &$sWhereClauseTimeline,
  1170. 'where_clause_modules' => &$sWhereClauseModules,
  1171. 'where_clause_hidden' => &$sWhereClauseHidden,
  1172. 'where_clause_medias' => &$sWhereClauseMedias,
  1173. 'where_clause_muted' => &$sWhereClauseMuted,
  1174. 'where_clause_unpublished' => &$sWhereClauseUnpublished,
  1175. 'where_clause_cf' => &$sWhereClauseCf,
  1176. 'where_subclause' => &$mixedWhereSubclause,
  1177. 'join_clause_ref' => &$mixedJoinClause,
  1178. 'join_subclause_ref' => &$mixedJoinSubclause,
  1179. 'where_clause_ref' => &$mixedWhereClause,
  1180. 'where_clause_status_ref' => &$sWhereClauseStatus,
  1181. 'where_clause_filter_ref' => &$sWhereClauseFilter,
  1182. 'where_clause_timeline_ref' => &$sWhereClauseTimeline,
  1183. 'where_clause_modules_ref' => &$sWhereClauseModules,
  1184. 'where_clause_hidden_ref' => &$sWhereClauseHidden,
  1185. 'where_clause_medias_ref' => &$sWhereClauseMedias,
  1186. 'where_clause_muted_ref' => &$sWhereClauseMuted,
  1187. 'where_clause_unpublished_ref' => &$sWhereClauseUnpublished,
  1188. 'where_clause_cf_ref' => &$sWhereClauseCf,
  1189. 'where_subclause_ref' => &$mixedWhereSubclause,
  1190. ]);
  1191. $mixedWhereClause .= $sWhereClauseStatus;
  1192. $mixedWhereClause .= $sWhereClauseFilter;
  1193. $mixedWhereClause .= $sWhereClauseTimeline;
  1194. $mixedWhereClause .= $sWhereClauseModules;
  1195. $mixedWhereClause .= $sWhereClauseHidden;
  1196. $mixedWhereClause .= $sWhereClauseMedias;
  1197. $mixedWhereClause .= $sWhereClauseMuted;
  1198. $mixedWhereClause .= $sWhereClauseUnpublished;
  1199. $mixedWhereClause .= $sWhereClauseCf;
  1200. //--- Combine general Join with SubJoins.
  1201. if(!empty($mixedJoinSubclause)) {
  1202. if(is_array($mixedJoinSubclause)) {
  1203. $aJoinClause = [];
  1204. foreach($mixedJoinSubclause as $sKey => $sValue)
  1205. $aJoinClause[$sKey] = $mixedJoinClause . " " . $sValue . " ";
  1206. $mixedJoinClause = $aJoinClause;
  1207. }
  1208. else
  1209. $mixedJoinClause .= " " . $mixedJoinSubclause . " ";
  1210. }
  1211. //--- Combine general Where with SubWheres.
  1212. if(!empty($mixedWhereSubclause)) {
  1213. if(is_array($mixedWhereSubclause)) {
  1214. $aWhereClause = [];
  1215. foreach($mixedWhereSubclause as $sKey => $sValue)
  1216. $aWhereClause[$sKey] = $mixedWhereClause . "AND " . $sValue . " ";
  1217. $mixedWhereClause = $aWhereClause;
  1218. }
  1219. else
  1220. $mixedWhereClause .= "AND (" . $mixedWhereSubclause . ") ";
  1221. }
  1222. return [$mixedJoinClause, $mixedWhereClause];
  1223. }
  1224. protected function _isList($aParams)
  1225. {
  1226. return $this->_oConfig->isBrowseList($aParams) && (!isset($aParams['newest']) || $aParams['newest'] === false);
  1227. }
  1228. public function getMenuItemMaxOrder($sSetName)
  1229. {
  1230. return $this->getOne("SELECT IFNULL(MAX(`order`), 0) FROM `sys_menu_items` WHERE `set_name`=:set_name LIMIT 1", [
  1231. 'set_name' => $sSetName
  1232. ]);
  1233. }
  1234. public function getMenuItemId($sSetName, $sName)
  1235. {
  1236. return (int)$this->getOne("SELECT `id` FROM `sys_menu_items` WHERE `set_name`=:set_name AND `name`=:name LIMIT 1", [
  1237. 'set_name' => $sSetName,
  1238. 'name' => $sName
  1239. ]);
  1240. }
  1241. public function insertMenuItem($sSetName, $sModule, $sName, $sTitle, $iOrder)
  1242. {
  1243. return $this->query("INSERT INTO `sys_menu_items` SET " . $this->arrayToSQL([
  1244. 'set_name' => $sSetName,
  1245. 'module' => $sModule,
  1246. 'name' => $sName,
  1247. 'title_system' => $sTitle,
  1248. 'title' => $sTitle,
  1249. 'link' => 'javascript:void(0)',
  1250. 'onclick' => "javascript:{js_object_view}.changeFeed(this, '" . $sModule . "')",
  1251. 'target' => '_self',
  1252. 'collapsed' => 1,
  1253. 'order' => $iOrder
  1254. ]));
  1255. }
  1256. public function deleteMenuItem($sSetName, $sName)
  1257. {
  1258. return $this->query("DELETE FROM `sys_menu_items` WHERE `set_name`=:set_name AND `name`=:name LIMIT 1", [
  1259. 'set_name' => $sSetName,
  1260. 'name' => $sName
  1261. ]);
  1262. }
  1263. /**
  1264. * TEMPORARY: to find the source of Duplicates.
  1265. */
  1266. public function insertEvent($aParamsSet)
  1267. {
  1268. if(!empty($aParamsSet['date']) && $aParamsSet['date'] < (time() - 86400)) {
  1269. $sTracks = '';
  1270. $aTracks = debug_backtrace();
  1271. foreach($aTracks as $aTrack)
  1272. $sTracks .= "{$aTrack['file']} ({$aTrack['line']})\n";
  1273. bx_log('bx_timeline_custom', $sTracks, BX_LOG_DEBUG);
  1274. }
  1275. return parent::insertEvent($aParamsSet);
  1276. }
  1277. }
  1278. /** @} */