BxTimelineDb.php 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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($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 = array(
  257. 'profile_id' => $iUserId
  258. );
  259. $sWhereAddon = '';
  260. if(!empty($iLinkId)) {
  261. $aBindings['id'] = $iLinkId;
  262. $sWhereAddon = " AND `id`=:id";
  263. }
  264. return $this->query("DELETE FROM `" . $this->_sPrefix . "links` WHERE `profile_id`=:profile_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 = 24)
  353. {
  354. $aBindings = [
  355. 'interval' => $iInterval
  356. ];
  357. $sQuery = "SELECT
  358. `te`.`id` AS `event_id`,
  359. `te`.`date` AS `value`
  360. FROM `" . $this->_sTable . "` AS `te`
  361. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `te`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  362. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  363. }
  364. public function getHotTrackByCommentsDate($sModule, $sTableTrack, $iInterval = 24, $iThresholdAge = 0, $iThresholdCount = 0)
  365. {
  366. $aBindings = [
  367. 'module' => $sModule,
  368. 'interval' => $iInterval
  369. ];
  370. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`cmt_time` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  371. if($iThresholdAge != 0) {
  372. $aBindings['threshold_age'] = $iThresholdAge;
  373. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  374. }
  375. $sQueryGroup = "`te`.`id`";
  376. if($iThresholdCount != 0) {
  377. $aBindings['threshold_count'] = $iThresholdCount;
  378. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`cmt_author_id`) >= :threshold_count";
  379. }
  380. $sQuery = "SELECT
  381. `te`.`id` as `event_id`,
  382. MAX(`tt`.`cmt_time`) AS `value`
  383. FROM `" . $this->_sTable . "` AS `te`
  384. 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
  385. WHERE 1 " . $sQueryWhere . "
  386. GROUP BY " . $sQueryGroup;
  387. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  388. }
  389. public function getHotTrackByCommentsDateModule($sModule, $sTableTrack, $iInterval = 24, $iThresholdAge = 0, $iThresholdCount = 0)
  390. {
  391. $aBindings = [
  392. 'module' => $sModule,
  393. 'interval' => $iInterval
  394. ];
  395. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`cmt_time` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  396. if($iThresholdAge != 0) {
  397. $aBindings['threshold_age'] = $iThresholdAge;
  398. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  399. }
  400. $sQueryGroup = "`te`.`object_id`";
  401. if($iThresholdCount != 0) {
  402. $aBindings['threshold_count'] = $iThresholdCount;
  403. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`cmt_author_id`) >= :threshold_count";
  404. }
  405. $sQuery = "SELECT
  406. `te`.`id` as `event_id`,
  407. MAX(`tt`.`cmt_time`) AS `value`
  408. FROM `" . $this->_sTable . "` AS `te`
  409. 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
  410. WHERE 1 " . $sQueryWhere . "
  411. GROUP BY " . $sQueryGroup;
  412. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  413. }
  414. public function getHotTrackByVotesDate($sModule, $sTableTrack, $iInterval = 24, $iThresholdAge = 0, $iThresholdCount = 0)
  415. {
  416. $aBindings = [
  417. 'module' => $sModule,
  418. 'interval' => $iInterval
  419. ];
  420. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  421. if($iThresholdAge != 0) {
  422. $aBindings['threshold_age'] = $iThresholdAge;
  423. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  424. }
  425. $sQueryGroup = "`te`.`id`";
  426. if($iThresholdCount != 0) {
  427. $aBindings['threshold_count'] = $iThresholdCount;
  428. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`author_id`) >= :threshold_count";
  429. }
  430. $sQuery = "SELECT
  431. `te`.`id` as `event_id`,
  432. MAX(`tt`.`date`) AS `value`
  433. FROM `" . $this->_sTable . "` AS `te`
  434. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`id`=`tt`.`object_id` AND `te`.`object_owner_id`<>`tt`.`author_id` AND `te`.`type`=:module
  435. WHERE 1 " . $sQueryWhere . "
  436. GROUP BY " . $sQueryGroup;
  437. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  438. }
  439. public function getHotTrackByVotesDateModule($sModule, $sTableTrack, $iInterval = 24, $iThresholdAge = 0, $iThresholdCount = 0)
  440. {
  441. $aBindings = [
  442. 'module' => $sModule,
  443. 'interval' => $iInterval
  444. ];
  445. $sQueryWhere = " AND (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  446. if($iThresholdAge != 0) {
  447. $aBindings['threshold_age'] = $iThresholdAge;
  448. $sQueryWhere .= " AND (UNIX_TIMESTAMP() - `te`.`date`) / 86400 <= :threshold_age";
  449. }
  450. $sQueryGroup = "`te`.`object_id`";
  451. if($iThresholdCount != 0) {
  452. $aBindings['threshold_count'] = $iThresholdCount;
  453. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`author_id`) >= :threshold_count";
  454. }
  455. $sQuery = "SELECT
  456. `te`.`id` as `event_id`,
  457. MAX(`tt`.`date`) AS `value`
  458. FROM `" . $this->_sTable . "` AS `te`
  459. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`object_id`=`tt`.`object_id` AND `te`.`object_owner_id`<>`tt`.`author_id` AND `te`.`type`=:module
  460. WHERE 1 " . $sQueryWhere . "
  461. GROUP BY " . $sQueryGroup;
  462. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  463. }
  464. /**
  465. * Hot Track by Sum of Votes during specified Period is currently disabled.
  466. */
  467. public function getHotTrackByVotesSum($sModule, $sTableTrack, $iInterval = 24)
  468. {
  469. $sQuery = "SELECT
  470. `te`.`id` as `event_id`,
  471. SUM(`tt`.`value`) AS `value`
  472. FROM `" . $this->_sTable . "` AS `te`
  473. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`id`=`tt`.`object_id` AND `te`.`type`=:module
  474. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  475. GROUP BY `te`.`id`";
  476. return $this->getAll($sQuery, array('module' => $sModule, 'interval' => $iInterval));
  477. }
  478. /**
  479. * Hot Track by Sum of Votes during specified Period is currently disabled.
  480. */
  481. public function getHotTrackByVotesSumModule($sModule, $sTableTrack, $iInterval = 24)
  482. {
  483. $sQuery = "SELECT
  484. `te`.`id` as `event_id`,
  485. SUM(`tt`.`value`) AS `value`
  486. FROM `" . $this->_sTable . "` AS `te`
  487. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`object_id`=`tt`.`object_id` AND `te`.`type`=:module
  488. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  489. GROUP BY `te`.`object_id`";
  490. return $this->getAll($sQuery, array('module' => $sModule, 'interval' => $iInterval));
  491. }
  492. public function updateHotTrack($aTrack)
  493. {
  494. return (int)$this->query("REPLACE INTO `" . $this->_sTableHotTrack . "` SET " . $this->arrayToSQL($aTrack)) > 0;
  495. }
  496. public function rebuildSlice()
  497. {
  498. $this->query("TRUNCATE TABLE `" . $this->_sTableSlice . "`");
  499. $aWhereConditions = [];
  500. $iInterval = $this->_oConfig->getCacheTableInterval();
  501. $aCheckFields = $this->_oConfig->getCacheTableCheckFields();
  502. foreach($aCheckFields as $sField => $sType)
  503. switch($sType) {
  504. case 'flag':
  505. $aWhereConditions[] = "`" . $sField . "` <> 0";
  506. break;
  507. case 'date':
  508. $aWhereConditions[] = "`" . $sField . "` > (UNIX_TIMESTAMP() - " . $iInterval . ")";
  509. break;
  510. }
  511. return $this->query("INSERT INTO `" . $this->_sTableSlice . "` SELECT * FROM `" . $this->_sTable . "` WHERE " . implode(" OR ", $aWhereConditions));
  512. }
  513. public function getEvents($aParams)
  514. {
  515. bx_alert($this->_oConfig->getName(), 'get_events_before', 0, 0, [
  516. 'params' => &$aParams,
  517. ]);
  518. $sTable = isset($aParams['from_cache']) && $aParams['from_cache'] === true ? $this->_sTableSlice : $this->_sTable;
  519. $sTableAlias = $this->getTableAlias();
  520. list($sMethod, $sSelectClause, $mixedJoinClause, $mixedWhereClause, $sOrderClause, $sLimitClause) = $this->_getSqlPartsEvents($aParams);
  521. $bCount = isset($aParams['count']) && $aParams['count'] === true;
  522. if($bCount) {
  523. $sMethod = 'getOne';
  524. $sSelectClause = 'COUNT(*)';
  525. $sOrderClause = '';
  526. $sLimitClause = '';
  527. }
  528. $aAlertParams = $aParams;
  529. unset($aAlertParams['browse']);
  530. bx_alert($this->_oConfig->getName(), 'get_events', 0, 0, [
  531. 'browse' => $aParams['browse'],
  532. 'params' => $aAlertParams,
  533. 'table' => $sTable,
  534. 'table_alias' => $sTableAlias,
  535. 'method' => &$sMethod,
  536. 'select_clause' => &$sSelectClause,
  537. 'join_clause' => &$mixedJoinClause,
  538. 'where_clause' => &$mixedWhereClause,
  539. 'order_clause' => &$sOrderClause,
  540. 'limit_clause' => &$sLimitClause
  541. ]);
  542. $sSqlMask = "SELECT {select}
  543. FROM `{$sTable}` AS `{$sTableAlias}`
  544. LEFT JOIN `{$this->_sTableHandlers}` ON `{$sTableAlias}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$sTableAlias}`.`action`=`{$this->_sTableHandlers}`.`alert_action` {join}
  545. WHERE 1 {where} {order} {limit}";
  546. if(is_string($mixedWhereClause)) {
  547. $aSqlMarkers = array(
  548. 'select' => $sSelectClause,
  549. 'join' => $mixedJoinClause,
  550. 'where' => $mixedWhereClause,
  551. 'order' => $sOrderClause,
  552. 'limit' => $sLimitClause
  553. );
  554. return $this->$sMethod(bx_replace_markers($sSqlMask, $aSqlMarkers));
  555. }
  556. $bJoinAsArray = !empty($mixedJoinClause) && is_array($mixedJoinClause);
  557. $sOrderSubclause = $sLimitSubclause = '';
  558. if(!$bCount) {
  559. $sOrderSubclause = $sOrderClause;
  560. $sLimitSubclause = isset($aParams['per_page']) ? 'LIMIT 0, ' . ($aParams['start'] + $aParams['per_page']) : '';
  561. }
  562. $aSqlParts = array();
  563. foreach($mixedWhereClause as $sKey => $sValue) {
  564. $aSqlMarkers = array(
  565. 'select' => $sSelectClause,
  566. 'join' => $bJoinAsArray ? (isset($mixedJoinClause[$sKey]) ? $mixedJoinClause[$sKey] : '') : $mixedJoinClause,
  567. 'where' => $sValue,
  568. 'order' => $sOrderSubclause,
  569. 'limit' => $sLimitSubclause
  570. );
  571. $sSqlPart = bx_replace_markers($sSqlMask, $aSqlMarkers);
  572. $aSqlParts[] = !$bCount ? $sSqlPart : (int)$this->$sMethod($sSqlPart);
  573. }
  574. if($bCount)
  575. return array_sum($aSqlParts);
  576. $sSql = bx_replace_markers('(' . implode(') UNION (', $aSqlParts) . ') {order} {limit}', [
  577. 'order' => str_replace("`{$sTableAlias}`.", '', $sOrderClause),
  578. 'limit' => str_replace("`{$sTableAlias}`.", '', $sLimitClause),
  579. ]);
  580. //echoDbg($sSql); exit;
  581. return $this->$sMethod($sSql);
  582. }
  583. public function getEventFlagTypes()
  584. {
  585. return array_keys($this->_aTablesEventFlags);
  586. }
  587. public function updateEventFlagsByType($sType, $iEventId)
  588. {
  589. if(!isset($this->_aTablesEventFlags[$sType]))
  590. return false;
  591. return $this->query("INSERT IGNORE INTO `" . $this->_aTablesEventFlags[$sType] . "` (`event_id`) VALUES (:event_id)", [
  592. 'event_id' => $iEventId,
  593. ]) !== false;
  594. }
  595. public function deleteEventFlagsByType($sType, $iEventId)
  596. {
  597. if(!isset($this->_aTablesEventFlags[$sType]))
  598. return false;
  599. return $this->query("DELETE FROM `" . $this->_aTablesEventFlags[$sType] . "` WHERE `event_id`=:event_id", [
  600. 'event_id' => $iEventId
  601. ]) !== false;
  602. }
  603. public function deleteEventFlags($iEventId)
  604. {
  605. $aTypes = $this->getEventFlagTypes();
  606. foreach($aTypes as $sType)
  607. $this->deleteEventFlagsByType($sType, $iEventId);
  608. }
  609. protected function _getFilterAddon($iOwnerId, $sFilter)
  610. {
  611. $sTableAlias = $this->getTableAlias();
  612. switch($sFilter) {
  613. /**
  614. * Direct posts in Timeline made by a timeline owner ($iOwnerId)
  615. */
  616. case BX_TIMELINE_FILTER_OWNER:
  617. $sFilterAddon = $this->prepareAsString(" AND `{$sTableAlias}`.`action`='' AND `{$sTableAlias}`.`object_id`=? ", $iOwnerId);
  618. break;
  619. /**
  620. * Direct posts in Timeline made by users except a timeline owner ($iOwnerId)
  621. */
  622. case BX_TIMELINE_FILTER_OTHER:
  623. $sFilterAddon = $this->prepareAsString(" AND `{$sTableAlias}`.`action`='' AND `{$sTableAlias}`.`object_id`<>? ", $iOwnerId);
  624. break;
  625. /**
  626. * All (Direct and System) posts in Timeline (owned by $iOwnerId) made by users except the viewer
  627. */
  628. case BX_TIMELINE_FILTER_OTHER_VIEWER:
  629. $sFilterAddon = $this->prepareAsString(" AND (`{$sTableAlias}`.`action`<>'' OR (`{$sTableAlias}`.`action`='' AND `{$sTableAlias}`.`object_id`<>?)) ", bx_get_logged_profile_id());
  630. break;
  631. case BX_TIMELINE_FILTER_ALL:
  632. default:
  633. $sFilterAddon = "";
  634. }
  635. return $sFilterAddon;
  636. }
  637. protected function _getSqlPartsEvents($aParams)
  638. {
  639. $sMethod = 'getAll';
  640. $sTableAlias = $this->getTableAlias();
  641. $sSelectClause = "`{$sTableAlias}`.*";
  642. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  643. switch($aParams['browse']) {
  644. case 'owner_id':
  645. $sWhereClause = $this->prepareAsString("AND `{$sTableAlias}`.`owner_id`=? ", $aParams['value']);
  646. break;
  647. case 'source':
  648. $sWhereClause = $this->prepareAsString("AND `{$sTableAlias}`.`source`=? ", $aParams['value']);
  649. break;
  650. case 'common_by_object':
  651. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  652. $sWhereClause = $this->prepareAsString("AND `{$sTableAlias}`.`system`='0' AND `{$sTableAlias}`.`object_id`=? ", $aParams['value']);
  653. break;
  654. case 'descriptor':
  655. $sMethod = 'getRow';
  656. $sWhereClause = "";
  657. if(isset($aParams['type']))
  658. $sWhereClause .= $this->prepareAsString("AND `{$sTableAlias}`.`type`=? ", $aParams['type']);
  659. if(isset($aParams['action']))
  660. $sWhereClause .= $this->prepareAsString("AND `{$sTableAlias}`.`action`=? ", $aParams['action']);
  661. if(isset($aParams['object_id']))
  662. $sWhereClause .= $this->prepareAsString("AND `{$sTableAlias}`.`object_id`=? ", $aParams['object_id']);
  663. $sLimitClause = "LIMIT 1";
  664. break;
  665. case 'reposted_by_track':
  666. $sJoinClause = $this->prepareAsString("INNER JOIN `" . $this->_sTableRepostsTrack . "` AS `trt` ON `{$sTableAlias}`.`id`=`trt`.`event_id` AND `trt`.`reposted_id`=?", $aParams['value']);
  667. break;
  668. case 'reposted_by_descriptor':
  669. $sWhereClause = "";
  670. if(isset($aParams['type']))
  671. $sWhereClause .= "AND `{$sTableAlias}`.`content` LIKE " . $this->escape('%' . $aParams['type'] . '%');
  672. if(isset($aParams['action']))
  673. $sWhereClause .= "AND `{$sTableAlias}`.`content` LIKE " . $this->escape('%' . $aParams['action'] . '%');
  674. break;
  675. case 'last':
  676. $sMethod = 'getRow';
  677. $sSelectClause .= ", YEAR(FROM_UNIXTIME(`{$sTableAlias}`.`date`)) AS `year`";
  678. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  679. $sOrderClause = "ORDER BY `{$sTableAlias}`.`date` ASC, `{$sTableAlias}`.`id` ASC";
  680. $sLimitClause = "LIMIT 1";
  681. break;
  682. case 'ids':
  683. $sWhereClause = "AND `{$sTableAlias}`.`id` IN (" . $this->implode_escape($aParams['ids']) . ") ";
  684. break;
  685. default:
  686. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = parent::_getSqlPartsEvents($aParams);
  687. }
  688. if($this->_isList($aParams)) {
  689. $sOrderClause = "";
  690. switch($aParams['type']) {
  691. case BX_TIMELINE_TYPE_HOT:
  692. $sOrderClause = "`{$sTableAlias}`.`sticked` DESC, `{$this->_sTableHotTrack}`.`value` DESC, ";
  693. break;
  694. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  695. case BX_BASE_MOD_NTFS_TYPE_CONNECTIONS:
  696. case BX_TIMELINE_TYPE_CHANNELS:
  697. case BX_TIMELINE_TYPE_CONNECTED_CONTEXTS:
  698. case BX_TIMELINE_TYPE_FEED:
  699. case BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS:
  700. $sOrderClause = "`{$sTableAlias}`.`sticked` DESC, ";
  701. break;
  702. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  703. $sOrderClause = "`{$sTableAlias}`.`pinned` DESC, ";
  704. break;
  705. }
  706. if($this->_oConfig->isSortByUnread()) {
  707. $oProfileQuery = BxDolProfileQuery::getInstance();
  708. $iDate = 0;
  709. $iOwner = (int)$aParams['owner_id'];
  710. $aOwner = $oProfileQuery->getInfoById($iOwner);
  711. $iViewer = !empty($aParams['viewer_id']) ? (int)$aParams['viewer_id'] : bx_get_logged_profile_id();
  712. if(!empty($aOwner) && is_array($aOwner) && BxDolRequest::serviceExists($aOwner['type'], 'is_fan') && bx_srv($aOwner['type'], 'is_fan', [$iOwner, $iViewer])) {
  713. $oModule = BxDolModule::getInstance($aOwner['type']);
  714. $aConnection = BxDolConnection::getObjectInstance($oModule->_oConfig->CNF['OBJECT_CONNECTIONS'])->getConnection($iViewer, $iOwner);
  715. $iDate = (int)$aConnection['added'];
  716. }
  717. else {
  718. $aViewer = $oProfileQuery->getInfoById($iViewer);
  719. if(!empty($aViewer) && is_array($aViewer))
  720. $iDate = bx_srv($aViewer['type'], 'get_date_added', [$aViewer['content_id']]);
  721. }
  722. $sSelectClause .= ", IF(NOT ISNULL(`teu`.`id`), 1, 0) AS `read`";
  723. $sOrderClause .= $this->prepareAsString("IF(`{$sTableAlias}`.`date` > ?, `read`, 1) ASC, ", $iDate);
  724. }
  725. if($this->_oConfig->isSortByReaction())
  726. $sOrderClause .= "`{$sTableAlias}`.`reacted` DESC, ";
  727. else
  728. $sOrderClause .= "`{$sTableAlias}`.`date` DESC, ";
  729. $sOrderClause = "ORDER BY " . $sOrderClause . "`{$sTableAlias}`.`id` DESC";
  730. }
  731. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  732. }
  733. protected function _getSqlPartsEventsListStatusAdmin($aParams)
  734. {
  735. if(isset($aParams['moderator']) && $aParams['moderator'] === true)
  736. return '';
  737. $iViewerId = !empty($aParams['viewer_id']) ? $aParams['viewer_id'] : bx_get_logged_profile_id();
  738. $sTableAlias = $this->getTableAlias();
  739. //--- Check viewer as event author.
  740. $sWhereClause = $this->prepareAsString("`{$sTableAlias}`.`object_owner_id`=?", $iViewerId);
  741. //--- Check viewer as an administrator/moderator of event author.
  742. $aGroups = [];
  743. $aModules = bx_srv('system', 'get_modules_by_type', ['profile']);
  744. foreach($aModules as $aModule) {
  745. $oModule = BxDolModule::getInstance($aModule['name']);
  746. if(!$oModule || !($oModule instanceof BxBaseModGroupsModule))
  747. continue;
  748. $aGroups = array_merge($aGroups, $oModule->getGroupsByFan($iViewerId, [
  749. BX_BASE_MOD_GROUPS_ROLE_ADMINISTRATOR,
  750. BX_BASE_MOD_GROUPS_ROLE_MODERATOR
  751. ]));
  752. }
  753. if(!empty($aGroups))
  754. $sWhereClause .= " OR `{$sTableAlias}`.`object_owner_id` IN (" . $this->implode_escape($aGroups) . ")";
  755. 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);
  756. }
  757. protected function _getSqlPartsEventsList($aParams)
  758. {
  759. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  760. $sTableAlias = $this->getTableAlias();
  761. $mixedJoinClause = $mixedWhereClause = "";
  762. $mixedJoinClause = "INNER JOIN `sys_profiles` AS `tpoo` ON ABS(`{$sTableAlias}`.`object_owner_id`)=`tpoo`.`id` AND `tpoo`.`status`='active'";
  763. if($this->_oConfig->isSortByUnread())
  764. $mixedJoinClause .= $this->prepareAsString(" LEFT JOIN `{$this->_sTableEvent2User}` AS `teu` ON `{$sTableAlias}`.`id`=`teu`.`event_id` AND `teu`.`user_id`=? ", $aParams['viewer_id']);
  765. $sWhereClauseStatus = "AND `{$sTableAlias}`.`active`='1' ";
  766. $sWhereClauseStatus .= $this->prepareAsString("AND `{$sTableAlias}`.`status`=? ", isset($aParams['status']) ? $aParams['status'] : BX_TIMELINE_STATUS_ACTIVE);
  767. $sWhereClauseStatus .= $this->_getSqlPartsEventsListStatusAdmin($aParams);
  768. //--- Apply filter
  769. $sWhereClauseFilter = "";
  770. if(isset($aParams['filter']))
  771. $sWhereClauseFilter = $this->_getFilterAddon($aParams['owner_id'], $aParams['filter']);
  772. //--- Apply timeline
  773. $sWhereClauseTimeline = "";
  774. if(isset($aParams['timeline']) && !empty($aParams['timeline']) && strpos($aParams['timeline'], '-') !== false) {
  775. list($iY, $iM, $iD) = explode('-', $aParams['timeline']);
  776. $sWhereClauseTimeline = $this->prepareAsString("AND `date`<=? ", mktime(23, 59, 59, (int)$iM, (int)$iD, (int)$iY));
  777. }
  778. //--- Apply modules or handlers filter
  779. $sWhereClauseModules = "";
  780. if(!empty($aParams['modules']) && is_array($aParams['modules']))
  781. $sWhereClauseModules = "AND `" . $sTableAlias . "`.`type` IN (" . $this->implode_escape($aParams['modules']) . ") ";
  782. $sWhereClauseHidden = "";
  783. if(empty($sWhereClauseModules)) {
  784. $aHidden = $this->_oConfig->getHandlersHidden();
  785. $sWhereClauseHidden = !empty($aHidden) && is_array($aHidden) ? "AND `" . $this->_sTableHandlers . "`.`id` NOT IN (" . $this->implode_escape($aHidden) . ") " : "";
  786. }
  787. //--- Apply media filter
  788. $sWhereClauseMedias = "";
  789. if(!empty($aParams['media'])) {
  790. if(is_array($aParams['media'])) {
  791. $sWhereSubclauseMedias = "0";
  792. $aMediaTypes = $aParams['media'];
  793. foreach($aMediaTypes as $sMediaType) {
  794. if(!isset($this->_aTablesEventFlags[$sMediaType]))
  795. continue;
  796. $sTableAliasFlag = 't' . substr($sMediaType, 0, 2);
  797. $mixedJoinClause .= " LEFT JOIN `" . $this->_aTablesEventFlags[$sMediaType] . "` AS `{$sTableAliasFlag}` ON `{$sTableAlias}`.`id`=`{$sTableAliasFlag}`.`event_id`";
  798. $sWhereSubclauseMedias .= " OR NOT ISNULL(`{$sTableAliasFlag}`.`event_id`)";
  799. }
  800. $sWhereClauseMedias .= "AND ({$sWhereSubclauseMedias}) ";
  801. }
  802. else {
  803. $sMediaType = $aParams['media'];
  804. if(isset($this->_aTablesEventFlags[$sMediaType])) {
  805. $sTableAliasFlag = 't' . substr($sMediaType, 0, 2);
  806. $mixedJoinClause .= " INNER JOIN `" . $this->_aTablesEventFlags[$sMediaType] . "` AS `{$sTableAliasFlag}` ON `{$sTableAlias}`.`id`=`{$sTableAliasFlag}`.`event_id`";
  807. }
  808. }
  809. }
  810. //--- Apply mute filter
  811. $sWhereClauseMuted = "";
  812. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('connection_mute'));
  813. if($oConnection) {
  814. $aMuted = $oConnection->getConnectedContent(bx_get_logged_profile_id());
  815. if(!empty($aMuted) && is_array($aMuted)) {
  816. $sMuted = "NOT IN (". $this->implode_escape($aMuted) . ")";
  817. $sWhereClauseMuted = "AND `{$sTableAlias}`.`owner_id` $sMuted AND `{$sTableAlias}`.`object_owner_id` $sMuted ";
  818. }
  819. }
  820. //--- Apply unpublished (date in future) filter
  821. $sWhereClauseUnpublished = $this->prepareAsString("AND IF(`{$sTableAlias}`.`system`='0' AND `{$sTableAlias}`.`object_id` = ?, 1, `{$sTableAlias}`.`date` <= UNIX_TIMESTAMP()) ", bx_get_logged_profile_id());
  822. //--- Apply content filter
  823. $oCf = BxDolContentFilter::getInstance();
  824. $sWhereClauseCf = $oCf->isEnabled() ? $oCf->getSQLParts($sTableAlias, 'object_cf') . ' ' : '';
  825. //--- Check type
  826. $mixedJoinSubclause = $mixedWhereSubclause = "";
  827. switch($aParams['type']) {
  828. //--- Feed: Hot
  829. case BX_TIMELINE_TYPE_HOT:
  830. //--- Apply privacy filter
  831. $aPrivacyGroups = array(BX_DOL_PG_ALL);
  832. if(isLogged())
  833. $aPrivacyGroups[] = BX_DOL_PG_MEMBERS;
  834. $aQueryParts = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'))->getContentByGroupAsSQLPart($aPrivacyGroups);
  835. $mixedWhereClause .= $aQueryParts['where'] . " ";
  836. //--- Select Hot posts.
  837. $mixedJoinSubclause = "LEFT JOIN `{$this->_sTableHotTrack}` ON `{$sTableAlias}`.`id`=`{$this->_sTableHotTrack}`.`event_id`";
  838. $mixedWhereSubclause = "NOT ISNULL(`{$this->_sTableHotTrack}`.`value`)";
  839. //--- Select Promoted posts.
  840. $mixedWhereSubclause .= " OR `{$sTableAlias}`.`promoted` <> '0'";
  841. break;
  842. //--- Feed: Public
  843. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  844. //--- Apply privacy filter
  845. $aPrivacyGroups = array(BX_DOL_PG_ALL);
  846. if(isLogged())
  847. $aPrivacyGroups[] = BX_DOL_PG_MEMBERS;
  848. $aQueryParts = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'))->getContentByGroupAsSQLPart($aPrivacyGroups);
  849. $mixedWhereClause .= $aQueryParts['where'] . " ";
  850. if($this->_oConfig->isShowAll())
  851. break;
  852. //--- Select All System posts
  853. $mixedWhereSubclause = "`{$sTableAlias}`.`system`='1'";
  854. //--- Select Public (Direct) posts created on Home Page Timeline (Public Feed)
  855. $mixedWhereSubclause .= $this->prepareAsString(" OR `{$sTableAlias}`.`owner_id`=?", 0);
  856. //--- Select Promoted posts.
  857. $mixedWhereSubclause .= " OR `{$sTableAlias}`.`promoted` <> '0'";
  858. break;
  859. //--- Feed: Profile
  860. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  861. if(empty($aParams['owner_id']))
  862. break;
  863. //--- Select Own (System and Direct) posts from Profile's Timeline.
  864. $mixedWhereSubclause = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = ?)", $aParams['owner_id']);
  865. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  866. $mixedWhereSubclause .= $this->prepareAsString(" OR (`{$sTableAlias}`.`owner_id` = '0' AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` = ?, 1))", $aParams['owner_id']);
  867. break;
  868. //--- Feed: All Profile Connections
  869. case BX_BASE_MOD_NTFS_TYPE_CONNECTIONS:
  870. if(empty($aParams['owner_id']))
  871. break;
  872. $mixedJoinSubclause = [];
  873. $mixedWhereSubclause = [];
  874. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  875. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  876. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  877. $aJoin1 = $aQueryParts['join'];
  878. $mixedJoinSubclause['p1'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  879. $mixedWhereSubclause['p1'] = "1";
  880. //--- Exclude Own (Direct) posts on timelines of following members.
  881. //--- Note. Disabled for now.
  882. //$mixedWhereSubclause['p1'] .= $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  883. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'object_id', $aParams['owner_id']);
  884. $aJoin2 = $aQueryParts['join'];
  885. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'object_id', $aParams['owner_id']);
  886. $aJoin2 = $aQueryParts['join'];
  887. $aJoin2['table_alias'] = 'cc';
  888. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  889. $mixedJoinSubclause['p2'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system`='0' AND " . $aJoin2['condition'];
  890. $mixedWhereSubclause['p2'] = "1";
  891. //--- Select Promoted posts.
  892. $mixedJoinSubclause['p3'] = "";
  893. $mixedWhereSubclause['p3'] = "`{$sTableAlias}`.`promoted` <> '0'";
  894. break;
  895. //--- Feed: Profile Connections to contexts by type (groups, events, spaces, etc) only.
  896. case BX_TIMELINE_TYPE_CONNECTED_CONTEXTS:
  897. if(empty($aParams['owner_id']) || empty($aParams['context']))
  898. break;
  899. $mixedJoinSubclause = [];
  900. $mixedWhereSubclause = [];
  901. //--- Filter out unnecessary contexts.
  902. $aContexts = is_array($aParams['context']) ? $aParams['context'] : [$aParams['context']];
  903. $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' ";
  904. //--- Join System posts received by following contexts.
  905. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  906. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  907. $aJoin1 = $aQueryParts['join'];
  908. $mixedJoinSubclause['p1'] .= "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  909. $mixedWhereSubclause['p1'] = "1";
  910. /*
  911. * Disabled because promoted posts from different contexts
  912. * took a lot of space in the beginning of timeline.
  913. *
  914. //--- Select Promoted posts.
  915. $mixedJoinSubclause['p2'] = "";
  916. $mixedWhereSubclause['p2'] = "`{$sTableAlias}`.`promoted` <> '0'";
  917. */
  918. break;
  919. //--- Feed: Profile Connections to Channel contexts only
  920. case BX_TIMELINE_TYPE_CHANNELS:
  921. if(empty($aParams['owner_id']))
  922. break;
  923. $mixedJoinSubclause = [];
  924. $mixedWhereSubclause = [];
  925. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  926. //--- Join System posts received by following channels.
  927. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  928. $aJoin1 = $aQueryParts['join'];
  929. $mixedJoinSubclause['p1'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  930. $mixedWhereSubclause['p1'] = "`{$sTableAlias}`.`type`='bx_channels'";
  931. //--- Exclude Own (Direct) posts on timelines of following members.
  932. //--- Note. Disabled for now.
  933. //$mixedWhereSubclause['p1'] = $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  934. //--- Select Promoted posts.
  935. $mixedJoinSubclause['p2'] = "";
  936. $mixedWhereSubclause['p2'] = "`{$sTableAlias}`.`promoted` <> '0'";
  937. break;
  938. //--- Feed: Profile + Profile Connections to Non-Channel contexts
  939. case BX_TIMELINE_TYPE_FEED:
  940. case BX_TIMELINE_TYPE_FOR_YOU:
  941. if(empty($aParams['owner_id']))
  942. break;
  943. $bForYou = $aParams['type'] == BX_TIMELINE_TYPE_FOR_YOU;
  944. $aForYouSources = $bForYou ? $this->_oConfig->getForYouSources() : [];
  945. //var_dump($bForYou, $aForYouSources); exit;
  946. $mixedJoinSubclause = [];
  947. $mixedWhereSubclause = [];
  948. if($aParams['type'] == BX_TIMELINE_TYPE_FEED || ($bForYou && in_array(BX_TIMELINE_FYFS_FEED, $aForYouSources))) {
  949. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  950. //--- Select Own (System and Direct) posts from Profile's Timeline.
  951. $sWhereSubclauseOwnProfile = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = ?)", $aParams['owner_id']);
  952. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  953. $sWhereSubclauseOwnPublic = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = '0' AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` = ?, 1))", $aParams['owner_id']);
  954. $mixedJoinSubclause['p1'] = "";
  955. $mixedWhereSubclause['p1'] = "(" . $sWhereSubclauseOwnProfile . " OR " . $sWhereSubclauseOwnPublic . ")";
  956. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  957. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  958. $aJoin1 = $aQueryParts['join'];
  959. $mixedJoinSubclause['p2'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  960. $mixedWhereSubclause['p2'] = "`{$sTableAlias}`.`type`<>'bx_channels'";
  961. //--- Exclude Own (Direct) posts on timelines of following members.
  962. //--- Note. Disabled for now and next check is used instead.
  963. //$mixedWhereSubclause['p2'] = $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1))", $aParams['owner_id']);
  964. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'object_owner_id', $aParams['owner_id']);
  965. $aJoin2 = $aQueryParts['join'];
  966. $aJoin2['table_alias'] = 'cc';
  967. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  968. $mixedJoinSubclause['p3'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system` = 0 AND `" . $sTableAlias . "`.`object_privacy_view` > 0 AND " . $aJoin2['condition'];
  969. $mixedWhereSubclause['p3'] = "1";
  970. //--- Select Promoted posts.
  971. $mixedJoinSubclause['p4'] = "";
  972. $mixedWhereSubclause['p4'] = "`{$sTableAlias}`.`promoted` <> '0'";
  973. }
  974. //--- 'For You' feed only: Add Hot posts
  975. if($bForYou && in_array(BX_TIMELINE_FYFS_HOT, $aForYouSources)) {
  976. $mixedJoinSubclause['p5'] = "INNER JOIN `{$this->_sTableHotTrack}` ON `{$sTableAlias}`.`id`=`{$this->_sTableHotTrack}`.`event_id`";
  977. $mixedWhereSubclause['p5'] = "1";
  978. }
  979. //--- 'For You' feed only: Add posts from recommended friends
  980. if($bForYou && in_array(BX_TIMELINE_FYFS_RECOM_FRIENDS, $aForYouSources) && ($oRecommendation = BxDolRecommendation::getObjectInstance('sys_friends')) !== false) {
  981. $aList = $oRecommendation->getList($aParams['owner_id'], [
  982. 'threshold' => $this->_oConfig->getForYouThresholdRecomFrds()
  983. ]);
  984. if(!empty($aList) && is_array($aList)) {
  985. $mixedJoinSubclause['p6'] = "";
  986. $mixedWhereSubclause['p6'] = "`{$sTableAlias}`.`owner_id` IN (" . $this->implode_escape(array_keys($aList)) . ")";
  987. }
  988. }
  989. //--- 'For You' feed only: Add posts from recommended subscriptions
  990. if($bForYou && in_array(BX_TIMELINE_FYFS_RECOM_SUBSCRIPTIONS, $aForYouSources) && ($oRecommendation = BxDolRecommendation::getObjectInstance('sys_subscriptions')) !== false) {
  991. $aList = $oRecommendation->getList($aParams['owner_id'], [
  992. 'threshold' => $this->_oConfig->getForYouThresholdRecomSbns()
  993. ]);
  994. if(!empty($aList) && is_array($aList)) {
  995. $mixedJoinSubclause['p7'] = "";
  996. $mixedWhereSubclause['p7'] = "`{$sTableAlias}`.`owner_id` IN (" . $this->implode_escape(array_keys($aList)) . ")";
  997. }
  998. }
  999. break;
  1000. //--- Feed: Profile + All Profile Connections
  1001. case BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS:
  1002. if(empty($aParams['owner_id']))
  1003. break;
  1004. $mixedJoinSubclause = [];
  1005. $mixedWhereSubclause = [];
  1006. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  1007. //--- Select Own (System and Direct) posts from Profile's Timeline.
  1008. $sWhereSubclauseOwnProfile = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = ?)", $aParams['owner_id']);
  1009. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  1010. $sWhereSubclauseOwnPublic = $this->prepareAsString("(`{$sTableAlias}`.`owner_id` = '0' AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` = ?, 1))", $aParams['owner_id']);
  1011. $mixedJoinSubclause['p1'] = "";
  1012. $mixedWhereSubclause['p1'] = "(" . $sWhereSubclauseOwnProfile . " OR " . $sWhereSubclauseOwnPublic . ")";
  1013. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  1014. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'owner_id', $aParams['owner_id']);
  1015. $aJoin1 = $aQueryParts['join'];
  1016. $mixedJoinSubclause['p2'] = "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  1017. $mixedWhereSubclause['p2'] = "1";
  1018. //--- Exclude Own (Direct) posts on timelines of following members.
  1019. //--- Note. Disabled for now and next check is used instead.
  1020. //$mixedWhereSubclause['p2'] = $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1))", $aParams['owner_id']);
  1021. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($sTableAlias, 'object_id', $aParams['owner_id']);
  1022. $aJoin2 = $aQueryParts['join'];
  1023. $aJoin2['table_alias'] = 'cc';
  1024. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  1025. $mixedJoinSubclause['p3'] = "INNER JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $sTableAlias . "`.`system`='0' AND " . $aJoin2['condition'];
  1026. $mixedWhereSubclause['p3'] = "1";
  1027. //--- Select Promoted posts.
  1028. $mixedJoinSubclause['p4'] = "";
  1029. $mixedWhereSubclause['p4'] = "`{$sTableAlias}`.`promoted` <> '0'";
  1030. break;
  1031. //--- Feed: Profile Connections to contexts from Selected module
  1032. default:
  1033. if(empty($aParams['owner_id']))
  1034. break;
  1035. $mixedJoinSubclause = [];
  1036. $mixedWhereSubclause = [];
  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['p1'] = "INNER JOIN `sys_profiles` AS `p` ON `" . $sTableAlias ."`.`owner_id`=`p`.`id` AND `p`.`type`='" . $aParams['type'] . "' ";
  1042. $mixedJoinSubclause['p1'] .= "INNER JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  1043. $mixedWhereSubclause['p1'] = "1";
  1044. if(!empty($aParams['context']))
  1045. $mixedWhereSubclause['p1'] .= $this->prepareAsString(" AND `" . $aJoin1['table_alias'] . "`.`content`=?", $aParams['context']);
  1046. //--- Exclude Own (Direct) posts on timelines of following members.
  1047. //--- Note. Disabled for now.
  1048. //$mixedWhereSubclause['p1'] = $this->prepareAsString(" AND IF(`{$sTableAlias}`.`system`='0', `{$sTableAlias}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  1049. //--- Select Promoted posts.
  1050. $mixedJoinSubclause['p2'] = "";
  1051. $mixedWhereSubclause['p2'] = "`" . $sTableAlias . "`.`promoted` <> '0'";
  1052. break;
  1053. }
  1054. $aAlertParams = $aParams;
  1055. unset($aAlertParams['type'], $aAlertParams['owner_id']);
  1056. bx_alert($this->_oConfig->getName(), 'get_list_by_type', 0, 0, [
  1057. 'type' => $aParams['type'],
  1058. 'owner_id' => $aParams['owner_id'],
  1059. 'params' => $aAlertParams,
  1060. 'table' => isset($aParams['from_cache']) && $aParams['from_cache'] === true ? $this->_sTableSlice : $this->_sTable,
  1061. 'table_alias' => $sTableAlias,
  1062. 'join_clause' => &$mixedJoinClause,
  1063. 'join_subclause' => &$mixedJoinSubclause,
  1064. 'where_clause' => &$mixedWhereClause,
  1065. 'where_clause_status' => &$sWhereClauseStatus,
  1066. 'where_clause_filter' => &$sWhereClauseFilter,
  1067. 'where_clause_timeline' => &$sWhereClauseTimeline,
  1068. 'where_clause_modules' => &$sWhereClauseModules,
  1069. 'where_clause_hidden' => &$sWhereClauseHidden,
  1070. 'where_clause_medias' => &$sWhereClauseMedias,
  1071. 'where_clause_muted' => &$sWhereClauseMuted,
  1072. 'where_clause_unpublished' => &$sWhereClauseUnpublished,
  1073. 'where_clause_cf' => &$sWhereClauseCf,
  1074. 'where_subclause' => &$mixedWhereSubclause
  1075. ]);
  1076. $mixedWhereClause .= $sWhereClauseStatus;
  1077. $mixedWhereClause .= $sWhereClauseFilter;
  1078. $mixedWhereClause .= $sWhereClauseTimeline;
  1079. $mixedWhereClause .= $sWhereClauseModules;
  1080. $mixedWhereClause .= $sWhereClauseHidden;
  1081. $mixedWhereClause .= $sWhereClauseMedias;
  1082. $mixedWhereClause .= $sWhereClauseMuted;
  1083. $mixedWhereClause .= $sWhereClauseUnpublished;
  1084. $mixedWhereClause .= $sWhereClauseCf;
  1085. //--- Combine general Join with SubJoins.
  1086. if(!empty($mixedJoinSubclause)) {
  1087. if(is_array($mixedJoinSubclause)) {
  1088. $aJoinClause = [];
  1089. foreach($mixedJoinSubclause as $sKey => $sValue)
  1090. $aJoinClause[$sKey] = $mixedJoinClause . " " . $sValue . " ";
  1091. $mixedJoinClause = $aJoinClause;
  1092. }
  1093. else
  1094. $mixedJoinClause .= " " . $mixedJoinSubclause . " ";
  1095. }
  1096. //--- Combine general Where with SubWheres.
  1097. if(!empty($mixedWhereSubclause)) {
  1098. if(is_array($mixedWhereSubclause)) {
  1099. $aWhereClause = [];
  1100. foreach($mixedWhereSubclause as $sKey => $sValue)
  1101. $aWhereClause[$sKey] = $mixedWhereClause . "AND " . $sValue . " ";
  1102. $mixedWhereClause = $aWhereClause;
  1103. }
  1104. else
  1105. $mixedWhereClause .= "AND (" . $mixedWhereSubclause . ") ";
  1106. }
  1107. return [$mixedJoinClause, $mixedWhereClause];
  1108. }
  1109. protected function _isList($aParams)
  1110. {
  1111. return in_array($aParams['browse'], ['list', 'ids']) && (!isset($aParams['newest']) || $aParams['newest'] === false);
  1112. }
  1113. public function getMenuItemMaxOrder($sSetName)
  1114. {
  1115. return $this->getOne("SELECT IFNULL(MAX(`order`), 0) FROM `sys_menu_items` WHERE `set_name`=:set_name LIMIT 1", [
  1116. 'set_name' => $sSetName
  1117. ]);
  1118. }
  1119. public function getMenuItemId($sSetName, $sName)
  1120. {
  1121. return (int)$this->getOne("SELECT `id` FROM `sys_menu_items` WHERE `set_name`=:set_name AND `name`=:name LIMIT 1", [
  1122. 'set_name' => $sSetName,
  1123. 'name' => $sName
  1124. ]);
  1125. }
  1126. public function insertMenuItem($sSetName, $sModule, $sName, $sTitle, $iOrder)
  1127. {
  1128. return $this->query("INSERT INTO `sys_menu_items` SET " . $this->arrayToSQL([
  1129. 'set_name' => $sSetName,
  1130. 'module' => $sModule,
  1131. 'name' => $sName,
  1132. 'title_system' => $sTitle,
  1133. 'title' => $sTitle,
  1134. 'link' => 'javascript:void(0)',
  1135. 'onclick' => "javascript:{js_object_view}.changeFeed(this, '" . $sModule . "')",
  1136. 'target' => '_self',
  1137. 'collapsed' => 1,
  1138. 'order' => $iOrder
  1139. ]));
  1140. }
  1141. public function deleteMenuItem($sSetName, $sName)
  1142. {
  1143. return $this->query("DELETE FROM `sys_menu_items` WHERE `set_name`=:set_name AND `name`=:name LIMIT 1", [
  1144. 'set_name' => $sSetName,
  1145. 'name' => $sName
  1146. ]);
  1147. }
  1148. /**
  1149. * TEMPORARY: to find the source of Duplicates.
  1150. */
  1151. public function insertEvent($aParamsSet)
  1152. {
  1153. if(!empty($aParamsSet['date']) && $aParamsSet['date'] < (time() - 86400)) {
  1154. $sTracks = '';
  1155. $aTracks = debug_backtrace();
  1156. foreach($aTracks as $aTrack)
  1157. $sTracks .= "{$aTrack['file']} ({$aTrack['line']})\n";
  1158. bx_log('bx_timeline_custom', $sTracks);
  1159. }
  1160. return parent::insertEvent($aParamsSet);
  1161. }
  1162. }
  1163. /** @} */