BxTimelineDb.php 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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. 'images' => $this->_sPrefix . 'ef_photos',
  32. 'videos' => $this->_sPrefix . 'ef_videos',
  33. 'files' => $this->_sPrefix . 'ef_files'
  34. ];
  35. $this->_sTableRepostsTrack = $this->_sPrefix . 'reposts_track';
  36. $this->_sTableHotTrack = $this->_sPrefix . 'hot_track';
  37. $this->_aTablesMedia = [
  38. $CNF['FIELD_PHOTO'] => $this->_sPrefix . 'photos',
  39. $CNF['FIELD_VIDEO'] => $this->_sPrefix . 'videos',
  40. $CNF['FIELD_FILE'] => $this->_sPrefix . 'files'
  41. ];
  42. $this->_aTablesMedia2Events = [
  43. $CNF['FIELD_PHOTO'] => $this->_sPrefix . 'photos2events',
  44. $CNF['FIELD_VIDEO'] => $this->_sPrefix . 'videos2events',
  45. $CNF['FIELD_FILE'] => $this->_sPrefix . 'files2events'
  46. ];
  47. }
  48. public function deleteModuleEvents($aData)
  49. {
  50. foreach($aData['handlers'] as $aHandler) {
  51. //Delete system events.
  52. $this->deleteEvent(array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  53. //Delete reposted events.
  54. $aEvents = $this->getEvents(array('browse' => 'reposted_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  55. foreach($aEvents as $aEvent) {
  56. $aContent = unserialize($aEvent['content']);
  57. if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
  58. $this->deleteEvent(array('id' => (int)$aEvent['id']));
  59. }
  60. }
  61. }
  62. public function updateStatusAdmin($iContentId, $isActive)
  63. {
  64. $CNF = $this->_oConfig->CNF;
  65. $sQuery = $this->prepare("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET `active` = ? WHERE `" . $CNF['FIELD_ID'] . "` = ?", $isActive ? '1' : '0', $iContentId);
  66. return $this->query($sQuery);
  67. }
  68. public function activateModuleEvents($aData, $bActivate = true)
  69. {
  70. $iActivate = $bActivate ? 1 : 0;
  71. foreach($aData['handlers'] as $aHandler) {
  72. //Activate (deactivate) system events.
  73. $this->updateEvent(array('active' => $iActivate), array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  74. //Activate (deactivate) reposted events.
  75. $aEvents = $this->getEvents(array('browse' => 'reposted_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  76. foreach($aEvents as $aEvent) {
  77. $aContent = unserialize($aEvent['content']);
  78. if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
  79. $this->updateEvent(array('active' => $iActivate), array('id' => (int)$aEvent['id']));
  80. }
  81. }
  82. }
  83. public function getMaxDuration($aParams)
  84. {
  85. $aParams['browse'] = 'last';
  86. if(isset($aParams['timeline']))
  87. unset($aParams['timeline']);
  88. $aEvent = $this->getEvents($aParams);
  89. if(empty($aEvent) || !is_array($aEvent))
  90. return 0;
  91. $iNowYear = date('Y', time());
  92. return (int)$aEvent['year'] < $iNowYear ? (int)$aEvent['year'] : 0;
  93. }
  94. public function markAsRead($iUserId, $iEventId)
  95. {
  96. return (int)$this->query("INSERT IGNORE INTO `{$this->_sTableEvent2User}` SET `user_id` = :user_id, `event_id` = :event_id", [
  97. 'user_id' => $iUserId,
  98. 'event_id' => $iEventId
  99. ]) !== false;
  100. }
  101. public function cleanRead($iLimit)
  102. {
  103. $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", [
  104. 'limit' => $iLimit
  105. ]);
  106. foreach($aUsers as $aUser)
  107. $this->query("DELETE FROM `{$this->_sTableEvent2User}` WHERE `user_id`=:user_id ORDER BY `event_id` LIMIT :limit", [
  108. 'user_id' => $aUser['id'],
  109. 'limit' => (int)$aUser['reads'] - $iLimit
  110. ]);
  111. }
  112. //--- Repost related methods ---//
  113. public function insertRepostTrack($iEventId, $iAuthorId, $sAuthorIp, $iRepostedId)
  114. {
  115. $iNow = time();
  116. $iAuthorNip = bx_get_ip_hash($sAuthorIp);
  117. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableRepostsTrack}` SET `event_id` = ?, `author_id` = ?, `author_nip` = ?, `reposted_id` = ?, `date` = ?", $iEventId, $iAuthorId, $iAuthorNip, $iRepostedId, $iNow);
  118. return (int)$this->query($sQuery) > 0;
  119. }
  120. public function updateRepostTrack($aParamsSet, $aParamsWhere)
  121. {
  122. if(empty($aParamsSet) || empty($aParamsWhere))
  123. return false;
  124. $sSql = "UPDATE `{$this->_sTableRepostsTrack}` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  125. return $this->query($sSql);
  126. }
  127. public function deleteRepostTrack($iEventId)
  128. {
  129. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableRepostsTrack}` WHERE `event_id` = ?", $iEventId);
  130. return (int)$this->query($sQuery) > 0;
  131. }
  132. public function updateRepostCounter($iId, $iCounter, $iIncrement = 1)
  133. {
  134. return (int)$this->updateEvent(array('reposts' => (int)$iCounter + $iIncrement), array('id' => $iId)) > 0;
  135. }
  136. public function getReposted($sType, $sAction, $iObjectId)
  137. {
  138. $bSystem = $this->_oConfig->isSystem($sType, $sAction);
  139. if($bSystem)
  140. $aParams = array('browse' => 'descriptor', 'type' => $sType, 'action' => $sAction, 'object_id' => $iObjectId);
  141. else
  142. $aParams = array('browse' => 'id', 'value' => $iObjectId);
  143. $aReposted = $this->getEvents($aParams);
  144. if($bSystem && (empty($aReposted) || !is_array($aReposted))) {
  145. $iOwnerId = 0;
  146. $iDate = 0;
  147. $sStatus = BX_TIMELINE_STATUS_DELETED;
  148. $mixedResult = $this->_oConfig->getSystemDataByDescriptor($sType, $sAction, $iObjectId);
  149. if(is_array($mixedResult)) {
  150. $iOwnerId = !empty($mixedResult['owner_id']) ? (int)$mixedResult['owner_id'] : 0;
  151. $iDate = !empty($mixedResult['date']) ? (int)$mixedResult['date'] : 0;
  152. if($this->_oConfig->isUnhideRestored() && !empty($iOwnerId) && !empty($iDate))
  153. $sStatus = BX_TIMELINE_STATUS_ACTIVE;
  154. }
  155. $iId = $this->insertEvent(array(
  156. 'owner_id' => $iOwnerId,
  157. 'type' => $sType,
  158. 'action' => $sAction,
  159. 'object_id' => $iObjectId,
  160. 'object_owner_id' => $iOwnerId,
  161. 'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault('object'),
  162. 'content' => '',
  163. 'title' => '',
  164. 'description' => '',
  165. 'date' => $iDate,
  166. 'reacted' => $iDate,
  167. 'status' => $sStatus
  168. ));
  169. $aReposted = $this->getEvents(array('browse' => 'id', 'value' => $iId));
  170. }
  171. return $aReposted;
  172. }
  173. function getRepostedBy($iRepostedId)
  174. {
  175. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableRepostsTrack}` WHERE `reposted_id`=?", $iRepostedId);
  176. return $this->getColumn($sQuery);
  177. }
  178. function getReposts($iRepostedId)
  179. {
  180. return $this->getAll("SELECT * FROM `{$this->_sTableRepostsTrack}` WHERE `reposted_id`=:reposted_id", array(
  181. 'reposted_id' => $iRepostedId
  182. ));
  183. }
  184. function isReposted($iRepostedId, $iOwnerId, $iAuthorId)
  185. {
  186. $sQuery = $this->prepare("SELECT
  187. `te`.`id`
  188. FROM `{$this->_sTableRepostsTrack}` AS `tst`
  189. LEFT JOIN `{$this->_sTable}` AS `te` ON `tst`.`event_id`=`te`.`id`
  190. WHERE `tst`.`author_id`=? AND `tst`.`reposted_id`=? AND `te`.`owner_id`=?", $iAuthorId, $iRepostedId, $iOwnerId);
  191. return (int)$this->getOne($sQuery) > 0;
  192. }
  193. //--- Photo uploader related methods ---//
  194. public function saveMedia($sType, $iEventId, $iItemId)
  195. {
  196. return (int)$this->query("INSERT INTO `" . $this->_aTablesMedia2Events[$sType] . "` SET `event_id`=:event_id, `media_id`=:media_id", array(
  197. 'event_id' => $iEventId,
  198. 'media_id' => $iItemId
  199. )) > 0;
  200. }
  201. public function deleteMedia($sType, $iEventId)
  202. {
  203. return (int)$this->query("DELETE FROM `" . $this->_aTablesMedia2Events[$sType] . "` WHERE `event_id`=:event_id", array(
  204. 'event_id' => $iEventId
  205. )) > 0;
  206. }
  207. public function hasMedia($iEventId)
  208. {
  209. $bResult = false;
  210. foreach(array_keys($this->_aTablesMedia) as $sType) {
  211. $aMedia = $this->getMedia($sType, $iEventId);
  212. if(!empty($aMedia) && is_array($aMedia)) {
  213. $bResult = true;
  214. break;
  215. }
  216. }
  217. return $bResult;
  218. }
  219. public function getMedia($sType, $iEventId, $iOffset = 0, $bFullInfo = false)
  220. {
  221. $sTableMedia = $this->_aTablesMedia[$sType];
  222. $sTableMedia2Events = $this->_aTablesMedia2Events[$sType];
  223. $sMethod = 'getColumn';
  224. $sSelectClause = "`tme`.`media_id` AS `id`";
  225. if($bFullInfo) {
  226. $sMethod = 'getAll';
  227. $sSelectClause = "`tm`.*, `tme`.`event_id` AS `event_id`";
  228. }
  229. $sLimitAddon = '';
  230. if($iOffset != 0)
  231. $sLimitAddon = $this->prepareAsString(" OFFSET ?", $iOffset);
  232. $sQuery = $this->prepare("SELECT " . $sSelectClause . "
  233. FROM `" . $sTableMedia2Events . "` AS `tme`
  234. LEFT JOIN `" . $sTableMedia . "` AS `tm` ON `tme`.`media_id`=`tm`.`id`
  235. WHERE `tme`.`event_id`=?" . $sLimitAddon, $iEventId);
  236. return $this->$sMethod($sQuery);
  237. }
  238. public function getMediaById($sType, $iMediaId)
  239. {
  240. $sTableMedia = $this->_aTablesMedia[$sType];
  241. $sTableMedia2Events = $this->_aTablesMedia2Events[$sType];
  242. 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(
  243. 'media_id' => $iMediaId
  244. ));
  245. }
  246. //--- Link attach related methods ---//
  247. public function getUnusedLinks($iUserId)
  248. {
  249. return $this->getLinksBy(array(
  250. 'type' => 'unused',
  251. 'profile_id' => $iUserId
  252. ));
  253. }
  254. public function deleteUnusedLinks($iUserId, $iLinkId = 0)
  255. {
  256. $aBindings = [
  257. 'profile_id' => $iUserId
  258. ];
  259. $sWhereAddon = '';
  260. if(!empty($iLinkId)) {
  261. $aBindings['id'] = $iLinkId;
  262. $sWhereAddon = " AND `tl`.`id`=:id";
  263. }
  264. 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);
  265. }
  266. public function saveLink($iEventId, $iLinkId)
  267. {
  268. $aBindings = array(
  269. 'event_id' => $iEventId,
  270. 'link_id' => $iLinkId
  271. );
  272. $iId = $this->getOne("SELECT `id` FROM `" . $this->_sPrefix . "links2events` WHERE `event_id`=:event_id AND `link_id`=:link_id LIMIT 1", $aBindings);
  273. if(!empty($iId))
  274. return true;
  275. return (int)$this->query("INSERT INTO `" . $this->_sPrefix . "links2events` SET `event_id`=:event_id, `link_id`=:link_id", $aBindings) > 0;
  276. }
  277. public function deleteLink($iId)
  278. {
  279. return (int)$this->query("DELETE FROM `tl`, `tle` USING `" . $this->_sPrefix . "links` AS `tl` LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id` WHERE `tl`.`id` = :id", array(
  280. 'id' => $iId
  281. )) > 0;
  282. }
  283. public function deleteLinks($iEventId)
  284. {
  285. 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(
  286. 'event_id' => $iEventId
  287. )) > 0;
  288. }
  289. public function getLinks($iEventId)
  290. {
  291. return $this->getLinksBy(array('type' => 'event_id', 'event_id' => $iEventId));
  292. }
  293. public function getLinksBy($aParams = array())
  294. {
  295. $CNF = &$this->_oConfig->CNF;
  296. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  297. $sSelectClause = "`tl`.*";
  298. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  299. switch($aParams['type']) {
  300. case 'id':
  301. $aMethod['name'] = 'getRow';
  302. $aMethod['params'][1] = array(
  303. 'id' => $aParams['id']
  304. );
  305. $sWhereClause = " AND `tl`.`id`=:id";
  306. if(!empty($aParams['profile_id'])) {
  307. $aMethod['params'][1]['profile_id'] = $aParams['profile_id'];
  308. $sWhereClause .= " AND `tl`.`profile_id`=:profile_id";
  309. }
  310. break;
  311. case 'event_id':
  312. $aMethod['params'][1] = array(
  313. 'event_id' => $aParams['event_id']
  314. );
  315. $sJoinClause = "LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`";
  316. $sWhereClause = " AND `tle`.`event_id`=:event_id";
  317. break;
  318. case 'unused':
  319. $aBindings = array(
  320. 'profile_id' => $aParams['profile_id']
  321. );
  322. if(isset($aParams['short']) && $aParams['short'] === true) {
  323. $aMethod['name'] = 'getPairs';
  324. $aMethod['params'][1] = 'url';
  325. $aMethod['params'][2] = 'id';
  326. $aMethod['params'][3] = $aBindings;
  327. }
  328. else
  329. $aMethod['params'][1] = $aBindings;
  330. $sJoinClause = "LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`";
  331. $sWhereClause = " AND `tl`.`profile_id`=:profile_id AND ISNULL(`tle`.`event_id`)";
  332. $sOrderClause = "`tl`.`added` DESC";
  333. break;
  334. }
  335. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  336. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  337. $aMethod['params'][0] = "SELECT
  338. " . $sSelectClause . "
  339. FROM `" . $this->_sPrefix . "links` AS `tl` " . $sJoinClause . "
  340. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  341. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  342. }
  343. public function getHot()
  344. {
  345. return $this->fromCache($this->_oConfig->getCacheHotKey(), 'getColumn', "SELECT `event_id` FROM `" . $this->_sTableHotTrack . "`");
  346. }
  347. public function clearHot()
  348. {
  349. $this->cleanCache($this->_oConfig->getCacheHotKey());
  350. return $this->query("TRUNCATE TABLE `" . $this->_sTableHotTrack . "`");
  351. }
  352. public function getHotTrackByDate($iInterval = 0)
  353. {
  354. $aBindings = [
  355. 'mux' => $this->_oConfig->getHotContentAgeMux()
  356. ];
  357. $sQueryWhere = "";
  358. if($iInterval != 0) {
  359. $aBindings['interval'] = $iInterval;
  360. $sQueryWhere .= " AND `te`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  361. }
  362. $sQuery = "SELECT
  363. `te`.`id` AS `event_id`,
  364. `te`.`date` * (1 - :mux * (UNIX_TIMESTAMP() - `te`.`date`) / UNIX_TIMESTAMP()) AS `value`
  365. FROM `" . $this->_sTable . "` AS `te`
  366. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0)" . $sQueryWhere;
  367. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  368. }
  369. public function getHotTrackByCommentsDate($sModule, $sTableTrack, $iInterval = 0, $iThresholdAge = 0, $iThresholdCount = 0)
  370. {
  371. $aBindings = [
  372. 'mux' => $this->_oConfig->getHotContentAgeMux(),
  373. 'module' => $sModule,
  374. ];
  375. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0)";
  376. if($iInterval != 0) {
  377. $aBindings['interval'] = $iInterval;
  378. $sQueryWhere .= " AND `tt`.`cmt_time` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  379. }
  380. if($iThresholdAge != 0) {
  381. $aBindings['threshold_age'] = $iThresholdAge;
  382. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  383. }
  384. $sQueryGroup = "`te`.`id`";
  385. if($iThresholdCount != 0) {
  386. $aBindings['threshold_count'] = $iThresholdCount;
  387. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`cmt_author_id`) >= :threshold_count";
  388. }
  389. $sQuery = "SELECT
  390. `te`.`id` as `event_id`,
  391. MAX(`tt`.`cmt_time`) * (1 - :mux * (UNIX_TIMESTAMP() - `te`.`date`) / UNIX_TIMESTAMP()) AS `value`
  392. FROM `" . $this->_sTable . "` AS `te`
  393. 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
  394. WHERE 1 " . $sQueryWhere . "
  395. GROUP BY " . $sQueryGroup;
  396. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  397. }
  398. public function getHotTrackByCommentsDateModule($sModule, $sTableTrack, $iInterval = 0, $iThresholdAge = 0, $iThresholdCount = 0)
  399. {
  400. $aBindings = [
  401. 'mux' => $this->_oConfig->getHotContentAgeMux(),
  402. 'module' => $sModule,
  403. ];
  404. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0)";
  405. if($iInterval != 0) {
  406. $aBindings['interval'] = $iInterval;
  407. $sQueryWhere .= " AND `tt`.`cmt_time` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  408. }
  409. if($iThresholdAge != 0) {
  410. $aBindings['threshold_age'] = $iThresholdAge;
  411. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  412. }
  413. $sQueryGroup = "`te`.`object_id`";
  414. if($iThresholdCount != 0) {
  415. $aBindings['threshold_count'] = $iThresholdCount;
  416. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`cmt_author_id`) >= :threshold_count";
  417. }
  418. $sQuery = "SELECT
  419. `te`.`id` as `event_id`,
  420. MAX(`tt`.`cmt_time`) * (1 - :mux * (UNIX_TIMESTAMP() - `te`.`date`) / UNIX_TIMESTAMP()) AS `value`
  421. FROM `" . $this->_sTable . "` AS `te`
  422. 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
  423. WHERE 1 " . $sQueryWhere . "
  424. GROUP BY " . $sQueryGroup;
  425. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  426. }
  427. public function getHotTrackByVotesDate($sModule, $sTableTrack, $iInterval = 0, $iThresholdAge = 0, $iThresholdCount = 0)
  428. {
  429. $aBindings = [
  430. 'mux' => $this->_oConfig->getHotContentAgeMux(),
  431. 'module' => $sModule,
  432. ];
  433. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0)";
  434. if($iInterval != 0) {
  435. $aBindings['interval'] = $iInterval;
  436. $sQueryWhere .= " AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  437. }
  438. if($iThresholdAge != 0) {
  439. $aBindings['threshold_age'] = $iThresholdAge;
  440. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  441. }
  442. $sQueryGroup = "`te`.`id`";
  443. if($iThresholdCount != 0) {
  444. $aBindings['threshold_count'] = $iThresholdCount;
  445. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`author_id`) >= :threshold_count";
  446. }
  447. $sQuery = "SELECT
  448. `te`.`id` as `event_id`,
  449. MAX(`tt`.`date`) * (1 - :mux * (UNIX_TIMESTAMP() - `te`.`date`) / UNIX_TIMESTAMP()) AS `value`
  450. FROM `" . $this->_sTable . "` AS `te`
  451. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`id`=`tt`.`object_id` AND `te`.`object_owner_id`<>`tt`.`author_id` AND `te`.`type`=:module
  452. WHERE 1 " . $sQueryWhere . "
  453. GROUP BY " . $sQueryGroup;
  454. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  455. }
  456. public function getHotTrackByVotesDateModule($sModule, $sTableTrack, $iInterval = 0, $iThresholdAge = 0, $iThresholdCount = 0)
  457. {
  458. $aBindings = [
  459. 'mux' => $this->_oConfig->getHotContentAgeMux(),
  460. 'module' => $sModule,
  461. ];
  462. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0)";
  463. if($iInterval != 0) {
  464. $aBindings['interval'] = $iInterval;
  465. $sQueryWhere .= " AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  466. }
  467. if($iThresholdAge != 0) {
  468. $aBindings['threshold_age'] = $iThresholdAge;
  469. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  470. }
  471. $sQueryGroup = "`te`.`object_id`";
  472. if($iThresholdCount != 0) {
  473. $aBindings['threshold_count'] = $iThresholdCount;
  474. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`author_id`) >= :threshold_count";
  475. }
  476. $sQuery = "SELECT
  477. `te`.`id` as `event_id`,
  478. MAX(`tt`.`date`) * (1 - :mux * (UNIX_TIMESTAMP() - `te`.`date`) / UNIX_TIMESTAMP()) AS `value`
  479. FROM `" . $this->_sTable . "` AS `te`
  480. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`object_id`=`tt`.`object_id` AND `te`.`object_owner_id`<>`tt`.`author_id` AND `te`.`type`=:module
  481. WHERE 1 " . $sQueryWhere . "
  482. GROUP BY " . $sQueryGroup;
  483. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  484. }
  485. /**
  486. * Hot Track by Sum of Votes during specified Period is currently disabled.
  487. */
  488. public function getHotTrackByVotesSum($sModule, $sTableTrack, $iInterval = 24)
  489. {
  490. $sQuery = "SELECT
  491. `te`.`id` as `event_id`,
  492. SUM(`tt`.`value`) AS `value`
  493. FROM `" . $this->_sTable . "` AS `te`
  494. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`id`=`tt`.`object_id` AND `te`.`type`=:module
  495. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  496. GROUP BY `te`.`id`";
  497. return $this->getAll($sQuery, array('module' => $sModule, 'interval' => $iInterval));
  498. }
  499. /**
  500. * Hot Track by Sum of Votes during specified Period is currently disabled.
  501. */
  502. public function getHotTrackByVotesSumModule($sModule, $sTableTrack, $iInterval = 24)
  503. {
  504. $sQuery = "SELECT
  505. `te`.`id` as `event_id`,
  506. SUM(`tt`.`value`) AS `value`
  507. FROM `" . $this->_sTable . "` AS `te`
  508. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`object_id`=`tt`.`object_id` AND `te`.`type`=:module
  509. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  510. GROUP BY `te`.`object_id`";
  511. return $this->getAll($sQuery, array('module' => $sModule, 'interval' => $iInterval));
  512. }
  513. public function updateHotTrack($iEventId, $iValue)
  514. {
  515. return (int)$this->query("INSERT INTO `" . $this->_sTableHotTrack . "` (`event_id`, `value`) VALUES (:event_id, :value) ON DUPLICATE KEY UPDATE `value` = :value", [
  516. 'event_id' => $iEventId,
  517. 'value' => $iValue
  518. ]) > 0;
  519. }
  520. public function rebuildSlice()
  521. {
  522. $this->query("TRUNCATE TABLE `" . $this->_sTableSlice . "`");
  523. $aWhereConditions = [];
  524. $iInterval = $this->_oConfig->getCacheTableInterval();
  525. $aCheckFields = $this->_oConfig->getCacheTableCheckFields();
  526. foreach($aCheckFields as $sField => $sType)
  527. switch($sType) {
  528. case 'flag':
  529. $aWhereConditions[] = "`" . $sField . "` <> 0";
  530. break;
  531. case 'date':
  532. $aWhereConditions[] = "`" . $sField . "` > (UNIX_TIMESTAMP() - " . $iInterval . ")";
  533. break;
  534. }
  535. return $this->query("INSERT INTO `" . $this->_sTableSlice . "` SELECT * FROM `" . $this->_sTable . "` WHERE " . implode(" OR ", $aWhereConditions));
  536. }
  537. public function getEvents($aParams)
  538. {
  539. /**
  540. * @hooks
  541. * @hookdef hook-bx_timeline-get_events_before 'bx_timeline', 'get_events_before' - hook to override params which are used to get events
  542. * - $unit_name - equals `bx_timeline`
  543. * - $action - equals `get_events_before`
  544. * - $object_id - not used
  545. * - $sender_id - not used
  546. * - $extra_params - array of additional params with the following array keys:
  547. * - `params` - [array] by ref, params array as key&value pairs, can be overridden in hook processing
  548. * @hook @ref hook-bx_timeline-get_events_before
  549. */
  550. bx_alert($this->_oConfig->getName(), 'get_events_before', 0, 0, [
  551. 'params' => &$aParams,
  552. ]);
  553. $bValidate = !empty($aParams['validate']) && is_array($aParams['validate']);
  554. $sTable = isset($aParams['from_cache']) && $aParams['from_cache'] === true ? $this->_sTableSlice : $this->_sTable;
  555. $sTableAlias = $this->getTableAlias();
  556. list($sMethod, $sSelectClause, $mixedJoinClause, $mixedWhereClause, $sOrderClause, $sLimitClause) = $this->_getSqlPartsEvents($aParams);
  557. $bCount = isset($aParams['count']) && $aParams['count'] === true;
  558. if($bCount) {
  559. $sMethod = 'getOne';
  560. $sSelectClause = 'COUNT(*)';
  561. $sOrderClause = '';
  562. $sLimitClause = '';
  563. }
  564. $aAlertParams = $aParams;
  565. unset($aAlertParams['browse']);
  566. /**
  567. * @hooks
  568. * @hookdef hook-bx_timeline-get_events 'bx_timeline', 'get_events' - hook to override events list which will be received from database
  569. * - $unit_name - equals `bx_timeline`
  570. * - $action - equals `get_events`
  571. * - $object_id - not used
  572. * - $sender_id - not used
  573. * - $extra_params - array of additional params with the following array keys:
  574. * - `browse` - [string] browsing type
  575. * - `params` - [array] browse params array as key&value pairs
  576. * - `table` - [string] datatbase table name
  577. * - `table_alias` - [string] datatbase table alias
  578. * - `method` - [string] by ref, database class method name, @see BxDolDb, can be overridden in hook processing
  579. * - `select_clause` - [string] by ref, 'select' part of SQL query, can be overridden in hook processing
  580. * - `join_clause` - [string] by ref, 'join' part of SQL query, can be overridden in hook processing
  581. * - `where_clause` - [string] by ref, 'where' part of SQL query, can be overridden in hook processing
  582. * - `order_clause` - [string] by ref, 'order' part of SQL query, can be overridden in hook processing
  583. * - `limit_clause` - [string] by ref, 'limit' part of SQL query, can be overridden in hook processing
  584. * @hook @ref hook-bx_timeline-get_events
  585. */
  586. bx_alert($this->_oConfig->getName(), 'get_events', 0, 0, [
  587. 'browse' => $aParams['browse'],
  588. 'params' => $aAlertParams,
  589. 'table' => $sTable,
  590. 'table_alias' => $sTableAlias,
  591. 'method' => &$sMethod,
  592. 'select_clause' => &$sSelectClause,
  593. 'join_clause' => &$mixedJoinClause,
  594. 'where_clause' => &$mixedWhereClause,
  595. 'order_clause' => &$sOrderClause,
  596. 'limit_clause' => &$sLimitClause
  597. ]);
  598. $sSqlMask = "SELECT {select}
  599. FROM `{$sTable}` AS `{$sTableAlias}`
  600. LEFT JOIN `{$this->_sTableHandlers}` ON `{$sTableAlias}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$sTableAlias}`.`action`=`{$this->_sTableHandlers}`.`alert_action` {join}
  601. WHERE 1 {where} {order} {limit}";
  602. if(is_string($mixedWhereClause)) {
  603. $aSqlMarkers = array(
  604. 'select' => $sSelectClause,
  605. 'join' => $mixedJoinClause,
  606. 'where' => $mixedWhereClause,
  607. 'order' => $sOrderClause,
  608. 'limit' => $sLimitClause
  609. );
  610. return $this->$sMethod(bx_replace_markers($sSqlMask, $aSqlMarkers));
  611. }
  612. $bJoinAsArray = !empty($mixedJoinClause) && is_array($mixedJoinClause);
  613. $sOrderSubclause = $sLimitSubclause = '';
  614. if(!$bCount) {
  615. $sOrderSubclause = $sOrderClause;
  616. $sLimitSubclause = isset($aParams['per_page']) ? 'LIMIT 0, ' . ($aParams['start'] + $aParams['per_page']) : '';
  617. }
  618. $aSqlParts = array();
  619. foreach($mixedWhereClause as $sKey => $sValue) {
  620. $aSqlMarkers = array(
  621. 'select' => $sSelectClause,
  622. 'join' => $bJoinAsArray ? (isset($mixedJoinClause[$sKey]) ? $mixedJoinClause[$sKey] : '') : $mixedJoinClause,
  623. 'where' => $sValue,
  624. 'order' => $sOrderSubclause,
  625. 'limit' => $sLimitSubclause
  626. );
  627. $sSqlPart = bx_replace_markers($sSqlMask, $aSqlMarkers);
  628. $aSqlParts[] = !$bCount ? $sSqlPart : (int)$this->$sMethod($sSqlPart);
  629. }
  630. if($bCount)
  631. return array_sum($aSqlParts);
  632. $sSqlMaskUnion = '(' . implode(') UNION (', $aSqlParts) . ')';
  633. if($bValidate)
  634. $sSqlMaskUnion = 'SELECT MIN(`tu`.`id`) AS `min_id`, MAX(`tu`.`id`) AS `max_id` FROM (' . $sSqlMaskUnion . ') AS `tu` GROUP BY `tu`.`source`';
  635. else
  636. $sSqlMaskUnion = 'SELECT *, GROUP_CONCAT(`owner_id`) AS `owner_id_grouped` FROM (' . $sSqlMaskUnion . ') AS `tu` GROUP BY `tu`.`source`';
  637. $sSqlMaskUnion .= ' {order} {limit}';
  638. $sSql = bx_replace_markers($sSqlMaskUnion, [
  639. 'order' => str_replace("`{$sTableAlias}`.", $bValidate ? '`tu`.' : '', $sOrderClause),
  640. 'limit' => str_replace("`{$sTableAlias}`.", $bValidate ? '`tu`.' : '', $sLimitClause),
  641. ]);
  642. //echoDbg($sSql); exit;
  643. return $this->$sMethod($sSql);
  644. }
  645. public function getEventFlagTypes()
  646. {
  647. return array_keys($this->_aTablesEventFlags);
  648. }
  649. public function updateEventFlagsByType($sType, $iEventId)
  650. {
  651. if(!isset($this->_aTablesEventFlags[$sType]))
  652. return false;
  653. return $this->query("INSERT IGNORE INTO `" . $this->_aTablesEventFlags[$sType] . "` (`event_id`) VALUES (:event_id)", [
  654. 'event_id' => $iEventId,
  655. ]) !== false;
  656. }
  657. public function deleteEventFlagsByType($sType, $iEventId)
  658. {
  659. if(!isset($this->_aTablesEventFlags[$sType]))
  660. return false;
  661. return $this->query("DELETE FROM `" . $this->_aTablesEventFlags[$sType] . "` WHERE `event_id`=:event_id", [
  662. 'event_id' => $iEventId
  663. ]) !== false;
  664. }
  665. public function deleteEventFlags($iEventId)
  666. {
  667. $aTypes = $this->getEventFlagTypes();
  668. foreach($aTypes as $sType)
  669. $this->deleteEventFlagsByType($sType, $iEventId);
  670. }
  671. protected function _getFilterAddon($iOwnerId, $sFilter)
  672. {
  673. $sTableAlias = $this->getTableAlias();
  674. switch($sFilter) {
  675. /**
  676. * Direct posts in Timeline made by a timeline owner ($iOwnerId)
  677. */
  678. case BX_TIMELINE_FILTER_OWNER:
  679. $sFilterAddon = $this->prepareAsString(" AND `{$sTableAlias}`.`action`='' AND `{$sTableAlias}`.`object_id`=? ", $iOwnerId);
  680. break;
  681. /**
  682. * Direct posts in Timeline made by users except a timeline owner ($iOwnerId)
  683. */
  684. case BX_TIMELINE_FILTER_OTHER:
  685. $sFilterAddon = $this->prepareAsString(" AND `{$sTableAlias}`.`action`='' AND `{$sTableAlias}`.`object_id`<>? ", $iOwnerId);
  686. break;
  687. /**
  688. * All (Direct and System) posts in Timeline (owned by $iOwnerId) made by users except the viewer
  689. */
  690. case BX_TIMELINE_FILTER_OTHER_VIEWER:
  691. $sFilterAddon = $this->prepareAsString(" AND (`{$sTableAlias}`.`action`<>'' OR (`{$sTableAlias}`.`action`='' AND `{$sTableAlias}`.`object_id`<>?)) ", bx_get_logged_profile_id());
  692. break;
  693. case BX_TIMELINE_FILTER_ALL:
  694. default:
  695. $sFilterAddon = "";
  696. }
  697. return $sFilterAddon;
  698. }
  699. protected function _getSqlPartsEvents($aParams)
  700. {
  701. $sMethod = 'getAll';
  702. $sTableAlias = $this->getTableAlias();
  703. $sSelectClause = "`{$sTableAlias}`.*";
  704. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  705. switch($aParams['browse']) {
  706. case 'owner_id':
  707. $sWhereClause = $this->prepareAsString("AND `{$sTableAlias}`.`owner_id`=? ", $aParams['value']);
  708. break;
  709. case 'source':
  710. $sWhereClause = $this->prepareAsString("AND `{$sTableAlias}`.`source`=? ", $aParams['value']);
  711. break;
  712. case 'common_by_object':
  713. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  714. $sWhereClause = $this->prepareAsString("AND `{$sTableAlias}`.`system`='0' AND `{$sTableAlias}`.`object_id`=? ", $aParams['value']);
  715. break;
  716. case 'descriptor':
  717. $sMethod = 'getRow';
  718. $sWhereClause = "";
  719. if(isset($aParams['type']))
  720. $sWhereClause .= $this->prepareAsString("AND `{$sTableAlias}`.`type`=? ", $aParams['type']);
  721. if(isset($aParams['action']))
  722. $sWhereClause .= $this->prepareAsString("AND `{$sTableAlias}`.`action`=? ", $aParams['action']);
  723. if(isset($aParams['object_id']))
  724. $sWhereClause .= $this->prepareAsString("AND `{$sTableAlias}`.`object_id`=? ", $aParams['object_id']);
  725. $sLimitClause = "LIMIT 1";
  726. break;
  727. case 'reposted_by_track':
  728. $sJoinClause = $this->prepareAsString("INNER JOIN `" . $this->_sTableRepostsTrack . "` AS `trt` ON `{$sTableAlias}`.`id`=`trt`.`event_id` AND `trt`.`reposted_id`=?", $aParams['value']);
  729. break;
  730. case 'reposted_by_descriptor':
  731. $sWhereClause = "";
  732. if(isset($aParams['type']))
  733. $sWhereClause .= "AND `{$sTableAlias}`.`content` LIKE " . $this->escape('%' . $aParams['type'] . '%');
  734. if(isset($aParams['action']))
  735. $sWhereClause .= "AND `{$sTableAlias}`.`content` LIKE " . $this->escape('%' . $aParams['action'] . '%');
  736. break;
  737. case 'last':
  738. $sMethod = 'getRow';
  739. $sSelectClause .= ", YEAR(FROM_UNIXTIME(`{$sTableAlias}`.`date`)) AS `year`";
  740. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  741. $sOrderClause = "ORDER BY `{$sTableAlias}`.`date` ASC, `{$sTableAlias}`.`id` ASC";
  742. $sLimitClause = "LIMIT 1";
  743. break;
  744. case 'ids':
  745. $sWhereClause = "AND `{$sTableAlias}`.`id` IN (" . $this->implode_escape($aParams['ids']) . ") ";
  746. break;
  747. default:
  748. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = parent::_getSqlPartsEvents($aParams);
  749. }
  750. if($this->_isList($aParams)) {
  751. $sOrderClause = "";
  752. switch($aParams['type']) {
  753. case BX_TIMELINE_TYPE_HOT:
  754. $sOrderClause = "`{$sTableAlias}`.`sticked` DESC, `{$this->_sTableHotTrack}`.`value` DESC, ";
  755. break;
  756. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  757. case BX_BASE_MOD_NTFS_TYPE_CONNECTIONS:
  758. case BX_TIMELINE_TYPE_CHANNELS:
  759. case BX_TIMELINE_TYPE_CONNECTED_CONTEXTS:
  760. case BX_TIMELINE_TYPE_FEED:
  761. case BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS:
  762. $sOrderClause = "`{$sTableAlias}`.`sticked` DESC, ";
  763. break;
  764. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  765. $sOrderClause = "`{$sTableAlias}`.`pinned` DESC, ";
  766. break;
  767. }
  768. if($this->_oConfig->isSortByUnread()) {
  769. $oProfileQuery = BxDolProfileQuery::getInstance();
  770. $iDate = 0;
  771. $iOwner = (int)$aParams['owner_id'];
  772. $aOwner = $oProfileQuery->getInfoById($iOwner);
  773. $iViewer = !empty($aParams['viewer_id']) ? (int)$aParams['viewer_id'] : bx_get_logged_profile_id();
  774. if(!empty($aOwner) && is_array($aOwner) && BxDolRequest::serviceExists($aOwner['type'], 'is_fan') && bx_srv($aOwner['type'], 'is_fan', [$iOwner, $iViewer])) {
  775. $oModule = BxDolModule::getInstance($aOwner['type']);
  776. $aConnection = BxDolConnection::getObjectInstance($oModule->_oConfig->CNF['OBJECT_CONNECTIONS'])->getConnection($iViewer, $iOwner);
  777. $iDate = (int)$aConnection['added'];
  778. }
  779. else {
  780. $aViewer = $oProfileQuery->getInfoById($iViewer);
  781. if(!empty($aViewer) && is_array($aViewer))
  782. $iDate = bx_srv($aViewer['type'], 'get_date_added', [$aViewer['content_id']]);
  783. }
  784. $sSelectClause .= ", IF(NOT ISNULL(`teu`.`id`), 1, 0) AS `read`";
  785. $sOrderClause .= $this->prepareAsString("IF(`{$sTableAlias}`.`date` > ?, `read`, 1) ASC, ", $iDate);
  786. }
  787. if($this->_oConfig->isSortByReaction())
  788. $sOrderClause .= "`{$sTableAlias}`.`reacted` DESC, ";
  789. else
  790. $sOrderClause .= "`{$sTableAlias}`.`date` DESC, ";
  791. $sOrderClause = "ORDER BY " . $sOrderClause . "`{$sTableAlias}`.`id` DESC";
  792. }
  793. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  794. }
  795. protected function _getSqlPartsEventsListStatusAdmin($aParams)
  796. {
  797. if(isset($aParams['moderator']) && $aParams['moderator'] === true)
  798. return '';
  799. $iViewerId = !empty($aParams['viewer_id']) ? $aParams['viewer_id'] : bx_get_logged_profile_id();
  800. $sTableAlias = $this->getTableAlias();
  801. //--- Check viewer as event author.
  802. $sWhereClause = $this->prepareAsString("`{$sTableAlias}`.`object_owner_id`=?", $iViewerId);
  803. //--- Check viewer as an administrator/moderator of event author.
  804. $aGroups = [];
  805. $aModules = bx_srv('system', 'get_modules_by_type', ['profile']);
  806. foreach($aModules as $aModule) {
  807. $oModule = BxDolModule::getInstance($aModule['name']);
  808. if(!$oModule || !($oModule instanceof BxBaseModGroupsModule))
  809. continue;
  810. $aGroups = array_merge($aGroups, $oModule->getGroupsByFan($iViewerId, [
  811. BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR,
  812. BX_BASE_MOD_GROUPS_ROLE_MODERATOR
  813. ]));
  814. }
  815. if(!empty($aGroups))
  816. $sWhereClause .= " OR `{$sTableAlias}`.`object_owner_id` IN (" . $this->implode_escape($aGroups) . ")";
  817. 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);
  818. }
  819. protected function _getSqlPartsEventsList($aParams)
  820. {
  821. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  822. $sTableAlias = $this->getTableAlias();
  823. $mixedJoinClause = $mixedWhereClause = "";
  824. $mixedJoinClause = "INNER JOIN `sys_profiles` AS `tpoo` ON ABS(`{$sTableAlias}`.`object_owner_id`)=`tpoo`.`id` AND `tpoo`.`status`='active'";
  825. if($this->_oConfig->isSortByUnread())
  826. $mixedJoinClause .= $this->prepareAsString(" LEFT JOIN `{$this->_sTableEvent2User}` AS `teu` ON `{$sTableAlias}`.`id`=`teu`.`event_id` AND `teu`.`user_id`=? ", $aParams['viewer_id']);
  827. $sWhereClauseStatus = "AND `{$sTableAlias}`.`active`='1' ";
  828. $sWhereClauseStatus .= $this->prepareAsString("AND `{$sTableAlias}`.`status`=? ", isset($aParams['status']) ? $aParams['status'] : BX_TIMELINE_STATUS_ACTIVE);
  829. $sWhereClauseStatus .= $this->_getSqlPartsEventsListStatusAdmin($aParams);
  830. //--- Apply filter
  831. $sWhereClauseFilter = "";
  832. if(isset($aParams['filter']))
  833. $sWhereClauseFilter = $this->_getFilterAddon($aParams['owner_id'], $aParams['filter']);
  834. //--- Apply timeline
  835. $sWhereClauseTimeline = "";
  836. if(isset($aParams['timeline']) && !empty($aParams['timeline']) && strpos($aParams['timeline'], '-') !== false) {
  837. list($iY, $iM, $iD) = explode('-', $aParams['timeline']);
  838. $sWhereClauseTimeline = $this->prepareAsString("AND `date`<=? ", mktime(23, 59, 59, (int)$iM, (int)$iD, (int)$iY));
  839. }
  840. //--- Apply modules or handlers filter
  841. $sWhereClauseModules = "";
  842. if(!empty($aParams['modules']) && is_array($aParams['modules']))
  843. $sWhereClauseModules = "AND `" . $sTableAlias . "`.`type` IN (" . $this->implode_escape($aParams['modules']) . ") ";
  844. $sWhereClauseHidden = "";
  845. if(empty($sWhereClauseModules)) {
  846. $aHidden = $this->_oConfig->getHandlersHidden();
  847. $sWhereClauseHidden = !empty($aHidden) && is_array($aHidden) ? "AND `" . $this->_sTableHandlers . "`.`id` NOT IN (" . $this->implode_escape($aHidden) . ") " : "";
  848. }
  849. //--- Apply media filter
  850. $sWhereClauseMedias = "";
  851. if(!empty($aParams['media'])) {
  852. if(is_array($aParams['media'])) {
  853. $sWhereSubclauseMedias = "0";
  854. $aMediaTypes = $aParams['media'];
  855. foreach($aMediaTypes as $sMediaType) {
  856. if(!isset($this->_aTablesEventFlags[$sMediaType]))
  857. continue;
  858. $sTableAliasFlag = 't' . substr($sMediaType, 0, 2);
  859. $mixedJoinClause .= " LEFT JOIN `" . $this->_aTablesEventFlags[$sMediaType] . "` AS `{$sTableAliasFlag}` ON `{$sTableAlias}`.`id`=`{$sTableAliasFlag}`.`event_id`";
  860. $sWhereSubclauseMedias .= " OR NOT ISNULL(`{$sTableAliasFlag}`.`event_id`)";
  861. }
  862. $sWhereClauseMedias .= "AND ({$sWhereSubclauseMedias}) ";
  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 IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` = ?, 1))", $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 IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` = ?, 1))", $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 IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` = ?, 1))", $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. /** @} */