BxTimelineDb.php 71 KB

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