BxTimelineDb.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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 $_sTableCache;
  14. protected $_sTableEvent2User;
  15. protected $_sTablesRepostTrack;
  16. protected $_sTableHotTrack;
  17. protected $_aTablesMedia;
  18. protected $_aTablesMedia2Events;
  19. /*
  20. * Constructor.
  21. */
  22. function __construct(&$oConfig)
  23. {
  24. parent::__construct($oConfig);
  25. $CNF = &$this->_oConfig->CNF;
  26. $this->_sTableCache = $this->_sPrefix . 'cache';
  27. $this->_sTableEvent2User = $this->_sPrefix . 'events2users';
  28. $this->_sTableRepostsTrack = $this->_sPrefix . 'reposts_track';
  29. $this->_sTableHotTrack = $this->_sPrefix . 'hot_track';
  30. $this->_aTablesMedia = array(
  31. $CNF['FIELD_PHOTO'] => $this->_sPrefix . 'photos',
  32. $CNF['FIELD_VIDEO'] => $this->_sPrefix . 'videos'
  33. );
  34. $this->_aTablesMedia2Events = array(
  35. $CNF['FIELD_PHOTO'] => $this->_sPrefix . 'photos2events',
  36. $CNF['FIELD_VIDEO'] => $this->_sPrefix . 'videos2events'
  37. );
  38. }
  39. public function deleteModuleEvents($aData)
  40. {
  41. foreach($aData['handlers'] as $aHandler) {
  42. //Delete system events.
  43. $this->deleteEvent(array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  44. //Delete reposted events.
  45. $aEvents = $this->getEvents(array('browse' => 'reposted_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  46. foreach($aEvents as $aEvent) {
  47. $aContent = unserialize($aEvent['content']);
  48. if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
  49. $this->deleteEvent(array('id' => (int)$aEvent['id']));
  50. }
  51. }
  52. }
  53. public function updateStatusAdmin($iContentId, $isActive)
  54. {
  55. $CNF = $this->_oConfig->CNF;
  56. $sQuery = $this->prepare("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET `active` = ? WHERE `" . $CNF['FIELD_ID'] . "` = ?", $isActive ? '1' : '0', $iContentId);
  57. return $this->query($sQuery);
  58. }
  59. public function activateModuleEvents($aData, $bActivate = true)
  60. {
  61. $iActivate = $bActivate ? 1 : 0;
  62. foreach($aData['handlers'] as $aHandler) {
  63. //Activate (deactivate) system events.
  64. $this->updateEvent(array('active' => $iActivate), array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  65. //Activate (deactivate) reposted events.
  66. $aEvents = $this->getEvents(array('browse' => 'reposted_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  67. foreach($aEvents as $aEvent) {
  68. $aContent = unserialize($aEvent['content']);
  69. if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
  70. $this->updateEvent(array('active' => $iActivate), array('id' => (int)$aEvent['id']));
  71. }
  72. }
  73. }
  74. public function getMaxDuration($aParams)
  75. {
  76. $aParams['browse'] = 'last';
  77. if(isset($aParams['timeline']))
  78. unset($aParams['timeline']);
  79. $aEvent = $this->getEvents($aParams);
  80. if(empty($aEvent) || !is_array($aEvent))
  81. return 0;
  82. $iNowYear = date('Y', time());
  83. return (int)$aEvent['year'] < $iNowYear ? (int)$aEvent['year'] : 0;
  84. }
  85. //--- Repost related methods ---//
  86. public function insertRepostTrack($iEventId, $iAuthorId, $sAuthorIp, $iRepostedId)
  87. {
  88. $iNow = time();
  89. $iAuthorNip = ip2long($sAuthorIp);
  90. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableRepostsTrack}` SET `event_id` = ?, `author_id` = ?, `author_nip` = ?, `reposted_id` = ?, `date` = ?", $iEventId, $iAuthorId, $iAuthorNip, $iRepostedId, $iNow);
  91. return (int)$this->query($sQuery) > 0;
  92. }
  93. public function deleteRepostTrack($iEventId)
  94. {
  95. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableRepostsTrack}` WHERE `event_id` = ?", $iEventId);
  96. return (int)$this->query($sQuery) > 0;
  97. }
  98. public function updateRepostCounter($iId, $iCounter, $iIncrement = 1)
  99. {
  100. return (int)$this->updateEvent(array('reposts' => (int)$iCounter + $iIncrement), array('id' => $iId)) > 0;
  101. }
  102. public function getReposted($sType, $sAction, $iObjectId)
  103. {
  104. $bSystem = $this->_oConfig->isSystem($sType, $sAction);
  105. if($bSystem)
  106. $aParams = array('browse' => 'descriptor', 'type' => $sType, 'action' => $sAction, 'object_id' => $iObjectId);
  107. else
  108. $aParams = array('browse' => 'id', 'value' => $iObjectId);
  109. $aReposted = $this->getEvents($aParams);
  110. if($bSystem && (empty($aReposted) || !is_array($aReposted))) {
  111. $iOwnerId = 0;
  112. $iDate = 0;
  113. $sStatus = BX_TIMELINE_STATUS_DELETED;
  114. $mixedResult = $this->_oConfig->getSystemDataByDescriptor($sType, $sAction, $iObjectId);
  115. if(is_array($mixedResult)) {
  116. $iOwnerId = !empty($mixedResult['owner_id']) ? (int)$mixedResult['owner_id'] : 0;
  117. $iDate = !empty($mixedResult['date']) ? (int)$mixedResult['date'] : 0;
  118. if($this->_oConfig->isUnhideRestored() && !empty($iOwnerId) && !empty($iDate))
  119. $sStatus = BX_TIMELINE_STATUS_ACTIVE;
  120. }
  121. $iId = $this->insertEvent(array(
  122. 'owner_id' => $iOwnerId,
  123. 'type' => $sType,
  124. 'action' => $sAction,
  125. 'object_id' => $iObjectId,
  126. 'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault('object'),
  127. 'content' => '',
  128. 'title' => '',
  129. 'description' => '',
  130. 'date' => $iDate,
  131. 'status' => $sStatus
  132. ));
  133. $aReposted = $this->getEvents(array('browse' => 'id', 'value' => $iId));
  134. }
  135. return $aReposted;
  136. }
  137. function getRepostedBy($iRepostedId)
  138. {
  139. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableRepostsTrack}` WHERE `reposted_id`=?", $iRepostedId);
  140. return $this->getColumn($sQuery);
  141. }
  142. function getReposts($iRepostedId)
  143. {
  144. return $this->getAll("SELECT * FROM `{$this->_sTableRepostsTrack}` WHERE `reposted_id`=:reposted_id", array(
  145. 'reposted_id' => $iRepostedId
  146. ));
  147. }
  148. function isReposted($iRepostedId, $iOwnerId, $iAuthorId)
  149. {
  150. $sQuery = $this->prepare("SELECT
  151. `te`.`id`
  152. FROM `{$this->_sTableRepostsTrack}` AS `tst`
  153. LEFT JOIN `{$this->_sTable}` AS `te` ON `tst`.`event_id`=`te`.`id`
  154. WHERE `tst`.`author_id`=? AND `tst`.`reposted_id`=? AND `te`.`owner_id`=?", $iAuthorId, $iRepostedId, $iOwnerId);
  155. return (int)$this->getOne($sQuery) > 0;
  156. }
  157. //--- Photo uploader related methods ---//
  158. public function saveMedia($sType, $iEventId, $iItemId)
  159. {
  160. return (int)$this->query("INSERT INTO `" . $this->_aTablesMedia2Events[$sType] . "` SET `event_id`=:event_id, `media_id`=:media_id", array(
  161. 'event_id' => $iEventId,
  162. 'media_id' => $iItemId
  163. )) > 0;
  164. }
  165. public function deleteMedia($sType, $iEventId)
  166. {
  167. return (int)$this->query("DELETE FROM `" . $this->_aTablesMedia2Events[$sType] . "` WHERE `event_id`=:event_id", array(
  168. 'event_id' => $iEventId
  169. )) > 0;
  170. }
  171. public function getMedia($sType, $iEventId, $iOffset = 0)
  172. {
  173. $sTableMedia = $this->_aTablesMedia[$sType];
  174. $sTableMedia2Events = $this->_aTablesMedia2Events[$sType];
  175. $sLimitAddon = '';
  176. if($iOffset != 0)
  177. $sLimitAddon = $this->prepareAsString(" OFFSET ?", $iOffset);
  178. $sQuery = $this->prepare("SELECT
  179. `tme`.`media_id` AS `id`
  180. FROM `" . $sTableMedia2Events . "` AS `tme`
  181. LEFT JOIN `" . $sTableMedia . "` AS `tm` ON `tme`.`media_id`=`tm`.`id`
  182. WHERE `tme`.`event_id`=?" . $sLimitAddon, $iEventId);
  183. return $this->getColumn($sQuery);
  184. }
  185. public function getMediaById($sType, $iMediaId)
  186. {
  187. $sTableMedia = $this->_aTablesMedia[$sType];
  188. $sTableMedia2Events = $this->_aTablesMedia2Events[$sType];
  189. 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(
  190. 'media_id' => $iMediaId
  191. ));
  192. }
  193. //--- Link attach related methods ---//
  194. public function getUnusedLinks($iUserId)
  195. {
  196. return $this->getLinksBy(array(
  197. 'type' => 'unused',
  198. 'profile_id' => $iUserId
  199. ));
  200. }
  201. public function deleteUnusedLinks($iUserId, $iLinkId = 0)
  202. {
  203. $aBindings = array(
  204. 'profile_id' => $iUserId
  205. );
  206. $sWhereAddon = '';
  207. if(!empty($iLinkId)) {
  208. $aBindings['id'] = $iLinkId;
  209. $sWhereAddon = " AND `id`=:id";
  210. }
  211. return $this->query("DELETE FROM `" . $this->_sPrefix . "links` WHERE `profile_id`=:profile_id" . $sWhereAddon, $aBindings);
  212. }
  213. public function saveLink($iEventId, $iLinkId)
  214. {
  215. $aBindings = array(
  216. 'event_id' => $iEventId,
  217. 'link_id' => $iLinkId
  218. );
  219. $iId = $this->getOne("SELECT `id` FROM `" . $this->_sPrefix . "links2events` WHERE `event_id`=:event_id AND `link_id`=:link_id LIMIT 1", $aBindings);
  220. if(!empty($iId))
  221. return true;
  222. return (int)$this->query("INSERT INTO `" . $this->_sPrefix . "links2events` SET `event_id`=:event_id, `link_id`=:link_id", $aBindings) > 0;
  223. }
  224. public function deleteLink($iId)
  225. {
  226. 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(
  227. 'id' => $iId
  228. )) > 0;
  229. }
  230. public function deleteLinks($iEventId)
  231. {
  232. 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(
  233. 'event_id' => $iEventId
  234. )) > 0;
  235. }
  236. public function getLinks($iEventId)
  237. {
  238. return $this->getLinksBy(array('type' => 'event_id', 'event_id' => $iEventId));
  239. }
  240. public function getLinksBy($aParams = array())
  241. {
  242. $CNF = &$this->_oConfig->CNF;
  243. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  244. $sSelectClause = "`tl`.*";
  245. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  246. switch($aParams['type']) {
  247. case 'id':
  248. $aMethod['name'] = 'getRow';
  249. $aMethod['params'][1] = array(
  250. 'id' => $aParams['id']
  251. );
  252. $sWhereClause = " AND `tl`.`id`=:id";
  253. if(!empty($aParams['profile_id'])) {
  254. $aMethod['params'][1]['profile_id'] = $aParams['profile_id'];
  255. $sWhereClause .= " AND `tl`.`profile_id`=:profile_id";
  256. }
  257. break;
  258. case 'event_id':
  259. $aMethod['params'][1] = array(
  260. 'event_id' => $aParams['event_id']
  261. );
  262. $sJoinClause = "LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`";
  263. $sWhereClause = " AND `tle`.`event_id`=:event_id";
  264. break;
  265. case 'unused':
  266. $aMethod['params'][1] = array(
  267. 'profile_id' => $aParams['profile_id']
  268. );
  269. $sJoinClause = "LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`";
  270. $sWhereClause = " AND `tl`.`profile_id`=:profile_id AND ISNULL(`tle`.`event_id`)";
  271. $sOrderClause = "`tl`.`added` DESC";
  272. break;
  273. }
  274. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  275. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  276. $aMethod['params'][0] = "SELECT
  277. " . $sSelectClause . "
  278. FROM `" . $this->_sPrefix . "links` AS `tl` " . $sJoinClause . "
  279. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  280. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  281. }
  282. public function getHot()
  283. {
  284. return $this->fromCache($this->_oConfig->getCacheHotKey(), 'getColumn', "SELECT `event_id` FROM `" . $this->_sTableHotTrack . "`");
  285. }
  286. public function clearHot()
  287. {
  288. $this->cleanCache($this->_oConfig->getCacheHotKey());
  289. return $this->query("TRUNCATE TABLE `" . $this->_sTableHotTrack . "`");
  290. }
  291. public function getHotTrackByDate($iInterval = 24)
  292. {
  293. $sQuery = "SELECT
  294. `te`.`id` AS `event_id`,
  295. `te`.`date` AS `value`
  296. FROM `" . $this->_sTable . "` AS `te`
  297. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `te`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  298. return $this->getPairs($sQuery, 'event_id', 'value', array('interval' => $iInterval));
  299. }
  300. public function getHotTrackByCommentsDate($sModule, $sTableTrack, $iInterval = 24)
  301. {
  302. $sQuery = "SELECT
  303. `te`.`id` as `event_id`,
  304. MAX(`tt`.`cmt_time`) AS `value`
  305. FROM `" . $this->_sTable . "` AS `te`
  306. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`id`=`tt`.`cmt_object_id` AND `te`.`type`=:module
  307. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`cmt_time` > (UNIX_TIMESTAMP() - 3600 * :interval)
  308. GROUP BY `te`.`id`";
  309. return $this->getPairs($sQuery, 'event_id', 'value', array('module' => $sModule, 'interval' => $iInterval));
  310. }
  311. public function getHotTrackByCommentsDateModule($sModule, $sTableTrack, $iInterval = 24)
  312. {
  313. $sQuery = "SELECT
  314. `te`.`id` as `event_id`,
  315. MAX(`tt`.`cmt_time`) AS `value`
  316. FROM `" . $this->_sTable . "` AS `te`
  317. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`object_id`=`tt`.`cmt_object_id` AND `te`.`type`=:module
  318. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`cmt_time` > (UNIX_TIMESTAMP() - 3600 * :interval)
  319. GROUP BY `te`.`object_id`";
  320. return $this->getPairs($sQuery, 'event_id', 'value', array('module' => $sModule, 'interval' => $iInterval));
  321. }
  322. public function getHotTrackByVotesDate($sModule, $sTableTrack, $iInterval = 24)
  323. {
  324. $sQuery = "SELECT
  325. `te`.`id` as `event_id`,
  326. MAX(`tt`.`date`) AS `value`
  327. FROM `" . $this->_sTable . "` AS `te`
  328. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`id`=`tt`.`object_id` AND `te`.`type`=:module
  329. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  330. GROUP BY `te`.`id`";
  331. return $this->getPairs($sQuery, 'event_id', 'value', array('module' => $sModule, 'interval' => $iInterval));
  332. }
  333. public function getHotTrackByVotesDateModule($sModule, $sTableTrack, $iInterval = 24)
  334. {
  335. $sQuery = "SELECT
  336. `te`.`id` as `event_id`,
  337. MAX(`tt`.`date`) AS `value`
  338. FROM `" . $this->_sTable . "` AS `te`
  339. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`object_id`=`tt`.`object_id` AND `te`.`type`=:module
  340. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  341. GROUP BY `te`.`object_id`";
  342. return $this->getPairs($sQuery, 'event_id', 'value', array('module' => $sModule, 'interval' => $iInterval));
  343. }
  344. /**
  345. * Hot Track by Sum of Votes during specified Period is currently disabled.
  346. */
  347. public function getHotTrackByVotesSum($sModule, $sTableTrack, $iInterval = 24)
  348. {
  349. $sQuery = "SELECT
  350. `te`.`id` as `event_id`,
  351. SUM(`tt`.`value`) AS `value`
  352. FROM `" . $this->_sTable . "` AS `te`
  353. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`id`=`tt`.`object_id` AND `te`.`type`=:module
  354. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  355. GROUP BY `te`.`id`";
  356. return $this->getAll($sQuery, array('module' => $sModule, 'interval' => $iInterval));
  357. }
  358. /**
  359. * Hot Track by Sum of Votes during specified Period is currently disabled.
  360. */
  361. public function getHotTrackByVotesSumModule($sModule, $sTableTrack, $iInterval = 24)
  362. {
  363. $sQuery = "SELECT
  364. `te`.`id` as `event_id`,
  365. SUM(`tt`.`value`) AS `value`
  366. FROM `" . $this->_sTable . "` AS `te`
  367. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`object_id`=`tt`.`object_id` AND `te`.`type`=:module
  368. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  369. GROUP BY `te`.`object_id`";
  370. return $this->getAll($sQuery, array('module' => $sModule, 'interval' => $iInterval));
  371. }
  372. public function updateHotTrack($aTrack)
  373. {
  374. return (int)$this->query("REPLACE INTO `" . $this->_sTableHotTrack . "` SET " . $this->arrayToSQL($aTrack)) > 0;
  375. }
  376. public function insertCache($aParamsSet)
  377. {
  378. if(empty($aParamsSet))
  379. return false;
  380. $iRet = (int)$this->query("REPLACE INTO `{$this->_sTableCache}` SET " . $this->arrayToSQL($aParamsSet)) > 0;
  381. $this->setReadOnlyMode(true);
  382. return $iRet;
  383. }
  384. public function deleteCache($aParamsWhere)
  385. {
  386. if(empty($aParamsWhere))
  387. return false;
  388. return (int)$this->query("DELETE FROM `{$this->_sTableCache}` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) > 0;
  389. }
  390. public function clearCache()
  391. {
  392. return (int)$this->query("DELETE FROM `{$this->_sTableCache}` WHERE 1") > 0;
  393. }
  394. public function getCache($aParams)
  395. {
  396. $CNF = &$this->_oConfig->CNF;
  397. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  398. $sFieldsClause = '*';
  399. $sWhereClause = $sLimitClause = '';
  400. if(isset($aParams['start']) && !empty($aParams['per_page']))
  401. $sLimitClause = $aParams['start'] . ", " . $aParams['per_page'];
  402. switch($aParams['browse']) {
  403. case 'list': // by type + owner profile (context) + viewer profile
  404. $aMethod['name'] = 'getAllWithKey';
  405. $aMethod['params'][1] = 'event_id';
  406. $aMethod['params'][2] = array(
  407. 'type' => $aParams['type'],
  408. 'context_id' => $aParams['context_id'],
  409. 'profile_id' => $aParams['profile_id']
  410. );
  411. //--- Apply custom check depending on Type
  412. switch($aParams['type']) {
  413. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  414. $aMethod['params'][2]['context_id'] = 0;
  415. break;
  416. case BX_TIMELINE_TYPE_CHANNELS:
  417. case BX_TIMELINE_TYPE_FEED:
  418. case BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS:
  419. $aMethod['params'][2]['context_id'] = $aParams['profile_id'];
  420. break;
  421. }
  422. $sWhereClause .= " AND `type`=:type AND `context_id`=:context_id AND `profile_id`=:profile_id";
  423. break;
  424. }
  425. $sLimitClause = $sLimitClause ? "LIMIT " . $sLimitClause : "";
  426. $aMethod['params'][0] = "SELECT " . $sFieldsClause . "
  427. FROM `{$this->_sTableCache}`
  428. WHERE 1" . $sWhereClause . " ORDER BY `important` DESC, `date` DESC " . $sLimitClause;
  429. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  430. }
  431. public function getEvents($aParams)
  432. {
  433. bx_alert($this->_oConfig->getName(), 'get_events_before', 0, 0, array(
  434. 'params' => &$aParams,
  435. ));
  436. list($sMethod, $sSelectClause, $mixedJoinClause, $mixedWhereClause, $sOrderClause, $sLimitClause) = $this->_getSqlPartsEvents($aParams);
  437. $bCount = isset($aParams['count']) && $aParams['count'] === true;
  438. if($bCount) {
  439. $sMethod = 'getOne';
  440. $sSelectClause = 'COUNT(*)';
  441. $sOrderClause = '';
  442. $sLimitClause = '';
  443. }
  444. $aAlertParams = $aParams;
  445. unset($aAlertParams['browse']);
  446. bx_alert($this->_oConfig->getName(), 'get_events', 0, 0, array(
  447. 'browse' => $aParams['browse'],
  448. 'params' => $aAlertParams,
  449. 'table' => $this->_sTable,
  450. 'method' => &$sMethod,
  451. 'select_clause' => &$sSelectClause,
  452. 'join_clause' => &$mixedJoinClause,
  453. 'where_clause' => &$mixedWhereClause,
  454. 'order_clause' => &$sOrderClause,
  455. 'limit_clause' => &$sLimitClause
  456. ));
  457. $sSqlMask = "SELECT {select}
  458. FROM `{$this->_sTable}`
  459. LEFT JOIN `{$this->_sTableHandlers}` ON `{$this->_sTable}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$this->_sTable}`.`action`=`{$this->_sTableHandlers}`.`alert_action` {join}
  460. WHERE 1 {where} {order} {limit}";
  461. if(is_string($mixedWhereClause)) {
  462. $aSqlMarkers = array(
  463. 'select' => $sSelectClause,
  464. 'join' => $mixedJoinClause,
  465. 'where' => $mixedWhereClause,
  466. 'order' => $sOrderClause,
  467. 'limit' => $sLimitClause
  468. );
  469. return $this->$sMethod(bx_replace_markers($sSqlMask, $aSqlMarkers));
  470. }
  471. $bJoinAsArray = !empty($mixedJoinClause) && is_array($mixedJoinClause);
  472. $sOrderSubclause = $sLimitSubclause = '';
  473. if(!$bCount) {
  474. $sOrderSubclause = $sOrderClause;
  475. $sLimitSubclause = isset($aParams['per_page']) ? 'LIMIT 0, ' . ($aParams['start'] + $aParams['per_page']) : '';
  476. }
  477. $aSqlParts = array();
  478. foreach($mixedWhereClause as $sKey => $sValue) {
  479. $aSqlMarkers = array(
  480. 'select' => $sSelectClause,
  481. 'join' => $bJoinAsArray ? (isset($mixedJoinClause[$sKey]) ? $mixedJoinClause[$sKey] : '') : $mixedJoinClause,
  482. 'where' => $sValue,
  483. 'order' => $sOrderSubclause,
  484. 'limit' => $sLimitSubclause
  485. );
  486. $sSqlPart = bx_replace_markers($sSqlMask, $aSqlMarkers);
  487. $aSqlParts[] = !$bCount ? $sSqlPart : (int)$this->$sMethod($sSqlPart);
  488. }
  489. return $bCount ? array_sum($aSqlParts) : $this->$sMethod(bx_replace_markers('(' . implode(') UNION (', $aSqlParts) . ') {order} {limit}', array(
  490. 'order' => str_replace('`' . $this->_sTable . '`.', '', $sOrderClause),
  491. 'limit' => str_replace('`' . $this->_sTable . '`.', '', $sLimitClause),
  492. )));
  493. }
  494. protected function _getFilterAddon($iOwnerId, $sFilter)
  495. {
  496. switch($sFilter) {
  497. /**
  498. * Direct posts in Timeline made by a timeline owner ($iOwnerId)
  499. */
  500. case BX_TIMELINE_FILTER_OWNER:
  501. $sFilterAddon = $this->prepareAsString(" AND `{$this->_sTable}`.`action`='' AND `{$this->_sTable}`.`object_id`=? ", $iOwnerId);
  502. break;
  503. /**
  504. * Direct posts in Timeline made by users except a timeline owner ($iOwnerId)
  505. */
  506. case BX_TIMELINE_FILTER_OTHER:
  507. $sFilterAddon = $this->prepareAsString(" AND `{$this->_sTable}`.`action`='' AND `{$this->_sTable}`.`object_id`<>? ", $iOwnerId);
  508. break;
  509. /**
  510. * All (Direct and System) posts in Timeline (owned by $iOwnerId) made by users except the viewer
  511. */
  512. case BX_TIMELINE_FILTER_OTHER_VIEWER:
  513. $sFilterAddon = $this->prepareAsString(" AND (`{$this->_sTable}`.`action`<>'' OR (`{$this->_sTable}`.`action`='' AND `{$this->_sTable}`.`object_id`<>?)) ", bx_get_logged_profile_id());
  514. break;
  515. case BX_TIMELINE_FILTER_ALL:
  516. default:
  517. $sFilterAddon = "";
  518. }
  519. return $sFilterAddon;
  520. }
  521. protected function _getSqlPartsEvents($aParams)
  522. {
  523. $sMethod = 'getAll';
  524. $sSelectClause = "`{$this->_sTable}`.*";
  525. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  526. switch($aParams['browse']) {
  527. case 'owner_id':
  528. $sWhereClause = $this->prepareAsString("AND `{$this->_sTable}`.`owner_id`=? ", $aParams['value']);
  529. break;
  530. case 'common_by_object':
  531. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  532. $sWhereClause = $this->prepareAsString("AND `{$this->_sTable}`.`system`='0' AND `{$this->_sTable}`.`object_id`=? ", $aParams['value']);
  533. break;
  534. case 'descriptor':
  535. $sMethod = 'getRow';
  536. $sWhereClause = "";
  537. if(isset($aParams['type']))
  538. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`type`=? ", $aParams['type']);
  539. if(isset($aParams['action']))
  540. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`action`=? ", $aParams['action']);
  541. if(isset($aParams['object_id']))
  542. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`object_id`=? ", $aParams['object_id']);
  543. $sLimitClause = "LIMIT 1";
  544. break;
  545. case 'reposted_by_descriptor':
  546. $sWhereClause = "";
  547. if(isset($aParams['type']))
  548. $sWhereClause .= "AND `{$this->_sTable}`.`content` LIKE " . $this->escape('%' . $aParams['type'] . '%');
  549. if(isset($aParams['action']))
  550. $sWhereClause .= "AND `{$this->_sTable}`.`content` LIKE " . $this->escape('%' . $aParams['action'] . '%');
  551. break;
  552. case 'list':
  553. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = parent::_getSqlPartsEvents($aParams);
  554. if(in_array($aParams['type'], array(BX_TIMELINE_TYPE_CHANNELS, BX_TIMELINE_TYPE_FEED, BX_BASE_MOD_NTFS_TYPE_CONNECTIONS, BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS)))
  555. $sSelectClause = "DISTINCT " . $sSelectClause;
  556. break;
  557. case 'ids':
  558. $sWhereClause = "AND `{$this->_sTable}`.`id` IN (" . $this->implode_escape($aParams['ids']) . ") ";
  559. break;
  560. default:
  561. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = parent::_getSqlPartsEvents($aParams);
  562. }
  563. $sSelectClause .= ", DAYOFYEAR(FROM_UNIXTIME(`{$this->_sTable}`.`date`)) AS `days`, DAYOFYEAR(NOW()) AS `today`, ROUND((UNIX_TIMESTAMP() - `{$this->_sTable}`.`date`)/86400) AS `ago_days`, YEAR(FROM_UNIXTIME(`{$this->_sTable}`.`date`)) AS `year`";
  564. if(in_array($aParams['browse'], array('list', 'ids')) && (!isset($aParams['newest']) || $aParams['newest'] === false)) {
  565. $sOrderClause = "";
  566. switch($aParams['type']) {
  567. case BX_TIMELINE_TYPE_HOT:
  568. $sOrderClause = "`{$this->_sTableHotTrack}`.`value` DESC, ";
  569. break;
  570. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  571. case BX_BASE_MOD_NTFS_TYPE_CONNECTIONS:
  572. case BX_TIMELINE_TYPE_CHANNELS:
  573. case BX_TIMELINE_TYPE_FEED:
  574. case BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS:
  575. $sOrderClause = "`{$this->_sTable}`.`sticked` DESC, ";
  576. break;
  577. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  578. $sOrderClause = "`{$this->_sTable}`.`pinned` DESC, ";
  579. break;
  580. }
  581. $sOrderClause = "ORDER BY " . $sOrderClause . "`{$this->_sTable}`.`date` DESC, `{$this->_sTable}`.`id` DESC";
  582. }
  583. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  584. }
  585. protected function _getSqlPartsEventsList($aParams)
  586. {
  587. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  588. $mixedJoinClause = $mixedWhereClause = "";
  589. $sWhereClauseStatus = "AND `{$this->_sTable}`.`active`='1' ";
  590. $sWhereClauseStatus .= $this->prepareAsString("AND `{$this->_sTable}`.`status`=? ", isset($aParams['status']) ? $aParams['status'] : BX_TIMELINE_STATUS_ACTIVE);
  591. //--- Apply filter
  592. $sWhereClauseFilter = "";
  593. if(isset($aParams['filter']))
  594. $sWhereClauseFilter = $this->_getFilterAddon($aParams['owner_id'], $aParams['filter']);
  595. //--- Apply timeline
  596. $sWhereClauseTimeline = "";
  597. if(isset($aParams['timeline']) && !empty($aParams['timeline']) && strpos($aParams['timeline'], '-') !== false) {
  598. list($iY, $iM, $iD) = explode('-', $aParams['timeline']);
  599. $sWhereClauseTimeline = $this->prepareAsString("AND `date`<=? ", mktime(23, 59, 59, (int)$iM, (int)$iD, (int)$iY));
  600. }
  601. //--- Apply modules or handlers filter
  602. $sWhereClauseModules = "";
  603. if(!empty($aParams['modules']) && is_array($aParams['modules']))
  604. $sWhereClauseModules = "AND `" . $this->_sTable . "`.`type` IN (" . $this->implode_escape($aParams['modules']) . ") ";
  605. $sWhereClauseHidden = "";
  606. if(empty($sWhereClauseModules)) {
  607. $aHidden = $this->_oConfig->getHandlersHidden();
  608. $sWhereClauseHidden = !empty($aHidden) && is_array($aHidden) ? "AND `" . $this->_sTableHandlers . "`.`id` NOT IN (" . $this->implode_escape($aHidden) . ") " : "";
  609. }
  610. //--- Apply mute filter
  611. $sWhereClauseMuted = "";
  612. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('connection_mute'));
  613. if($oConnection) {
  614. $aMuted = $oConnection->getConnectedContent(bx_get_logged_profile_id());
  615. if(!empty($aMuted) && is_array($aMuted)) {
  616. $sMuted = "NOT IN (". $this->implode_escape($aMuted) . ")";
  617. $sWhereClauseMuted = "AND `{$this->_sTable}`.`owner_id` $sMuted AND `{$this->_sTable}`.`object_owner_id` $sMuted ";
  618. }
  619. }
  620. //--- Apply unpublished (date in future) filter
  621. $sWhereClauseUnpublished = $this->prepareAsString("AND IF(`{$this->_sTable}`.`system`='0' AND `{$this->_sTable}`.`object_id` = ?, 1, `{$this->_sTable}`.`date` <= UNIX_TIMESTAMP()) ", bx_get_logged_profile_id());
  622. //--- Check type
  623. $mixedWhereSubclause = "";
  624. switch($aParams['type']) {
  625. //--- Feed: Hot (Public)
  626. case BX_TIMELINE_TYPE_HOT:
  627. $mixedJoinClause .= " INNER JOIN `{$this->_sTableHotTrack}` ON `{$this->_sTable}`.`id`=`{$this->_sTableHotTrack}`.`event_id`";
  628. //--- Feed: Public
  629. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  630. //--- Apply privacy filter
  631. $aPrivacyGroups = array(BX_DOL_PG_ALL);
  632. if(isLogged())
  633. $aPrivacyGroups[] = BX_DOL_PG_MEMBERS;
  634. $aQueryParts = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'))->getContentByGroupAsSQLPart($aPrivacyGroups);
  635. $mixedWhereClause .= $aQueryParts['where'] . " ";
  636. if($this->_oConfig->isShowAll())
  637. break;
  638. //--- Select All System posts
  639. $mixedWhereSubclause = "`{$this->_sTable}`.`system`='1'";
  640. //--- Select Public (Direct) posts created on Home Page Timeline (Public Feed)
  641. $mixedWhereSubclause .= $this->prepareAsString(" OR `{$this->_sTable}`.`owner_id`=?", 0);
  642. //--- Select Promoted posts.
  643. $mixedWhereSubclause .= " OR `{$this->_sTable}`.`promoted` <> '0'";
  644. break;
  645. //--- Feed: Profile
  646. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  647. if(empty($aParams['owner_id']))
  648. break;
  649. //--- Select Own (System and Direct) posts from Profile's Timeline.
  650. $mixedWhereSubclause = $this->prepareAsString("(`{$this->_sTable}`.`owner_id` = ?)", $aParams['owner_id']);
  651. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  652. $mixedWhereSubclause .= $this->prepareAsString(" OR (`{$this->_sTable}`.`owner_id` = '0' AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` = ?, 1))", $aParams['owner_id']);
  653. break;
  654. //--- Feed: All Profile Connections
  655. case BX_BASE_MOD_NTFS_TYPE_CONNECTIONS:
  656. if(empty($aParams['owner_id']))
  657. break;
  658. $mixedJoinClause = array();
  659. $mixedWhereSubclause = array();
  660. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  661. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  662. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'owner_id', $aParams['owner_id']);
  663. $aJoin1 = $aQueryParts['join'];
  664. $mixedJoinClause['p1'] = "LEFT JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  665. $mixedWhereSubclause['p1'] = "NOT ISNULL(`" . $aJoin1['table_alias'] . "`.`content`)";
  666. //--- Exclude Own (Direct) posts on timelines of following members.
  667. //--- Note. Disabled for now.
  668. //$mixedWhereSubclause['p1'] .= $this->prepareAsString(" AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  669. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'object_id', $aParams['owner_id']);
  670. $aJoin2 = $aQueryParts['join'];
  671. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'object_id', $aParams['owner_id']);
  672. $aJoin2 = $aQueryParts['join'];
  673. $aJoin2['table_alias'] = 'cc';
  674. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  675. $mixedJoinClause['p2'] = "LEFT JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $this->_sTable . "`.`system`='0' AND " . $aJoin2['condition'];
  676. $mixedWhereSubclause['p2'] = "NOT ISNULL(`" . $aJoin2['table_alias'] . "`.`content`)";
  677. //--- Select Promoted posts.
  678. $mixedWhereSubclause['p3'] = "`{$this->_sTable}`.`promoted` <> '0'";
  679. break;
  680. //--- Feed: Profile Connections to Channel contexts only
  681. case BX_TIMELINE_TYPE_CHANNELS:
  682. if(empty($aParams['owner_id']))
  683. break;
  684. $mixedJoinClause = array();
  685. $mixedWhereSubclause = array();
  686. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  687. //--- Join System posts received by following channels.
  688. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'owner_id', $aParams['owner_id']);
  689. $aJoin1 = $aQueryParts['join'];
  690. $mixedJoinClause['p1'] = "LEFT JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  691. $mixedWhereSubclause['p1'] = "`{$this->_sTable}`.`type`='bx_channels' AND NOT ISNULL(`" . $aJoin1['table_alias'] . "`.`content`)";
  692. //--- Exclude Own (Direct) posts on timelines of following members.
  693. //--- Note. Disabled for now.
  694. //$mixedWhereSubclause['p1'] = $this->prepareAsString(" AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  695. //--- Select Promoted posts.
  696. $mixedWhereSubclause['p2'] = "`{$this->_sTable}`.`promoted` <> '0'";
  697. break;
  698. //--- Feed: Profile + Profile Connections to Non-Channel contexts
  699. case BX_TIMELINE_TYPE_FEED:
  700. if(empty($aParams['owner_id']))
  701. break;
  702. $mixedJoinClause = array();
  703. $mixedWhereSubclause = array();
  704. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  705. //--- Select Own (System and Direct) posts from Profile's Timeline.
  706. $sWhereSubclauseOwnProfile = $this->prepareAsString("(`{$this->_sTable}`.`owner_id` = ?)", $aParams['owner_id']);
  707. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  708. $sWhereSubclauseOwnPublic = $this->prepareAsString("(`{$this->_sTable}`.`owner_id` = '0' AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` = ?, 1))", $aParams['owner_id']);
  709. $mixedWhereSubclause['p1'] = "(" . $sWhereSubclauseOwnProfile . " OR " . $sWhereSubclauseOwnPublic . ")";
  710. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  711. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'owner_id', $aParams['owner_id']);
  712. $aJoin1 = $aQueryParts['join'];
  713. $mixedJoinClause['p2'] = "LEFT JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  714. $mixedWhereSubclause['p2'] = "`{$this->_sTable}`.`type`<>'bx_channels' AND NOT ISNULL(`" . $aJoin1['table_alias'] . "`.`content`)";
  715. //--- Exclude Own (Direct) posts on timelines of following members.
  716. //--- Note. Disabled for now and next check is used instead.
  717. //$mixedWhereSubclause['p2'] = $this->prepareAsString(" AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` <> ?, 1))", $aParams['owner_id']);
  718. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'object_id', $aParams['owner_id']);
  719. $aJoin2 = $aQueryParts['join'];
  720. $aJoin2['table_alias'] = 'cc';
  721. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  722. $mixedJoinClause['p3'] = "LEFT JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $this->_sTable . "`.`system`='0' AND " . $aJoin2['condition'];
  723. $mixedWhereSubclause['p3'] = "NOT ISNULL(`" . $aJoin2['table_alias'] . "`.`content`)";
  724. //--- Select Promoted posts.
  725. $mixedWhereSubclause['p4'] = "`{$this->_sTable}`.`promoted` <> '0'";
  726. break;
  727. //--- Feed: Profile + All Profile Connections
  728. case BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS:
  729. if(empty($aParams['owner_id']))
  730. break;
  731. $mixedJoinClause = array();
  732. $mixedWhereSubclause = array();
  733. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  734. //--- Select Own (System and Direct) posts from Profile's Timeline.
  735. $sWhereSubclauseOwnProfile = $this->prepareAsString("(`{$this->_sTable}`.`owner_id` = ?)", $aParams['owner_id']);
  736. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  737. $sWhereSubclauseOwnPublic = $this->prepareAsString("(`{$this->_sTable}`.`owner_id` = '0' AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` = ?, 1))", $aParams['owner_id']);
  738. $mixedWhereSubclause['p1'] = "(" . $sWhereSubclauseOwnProfile . " OR " . $sWhereSubclauseOwnPublic . ")";
  739. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  740. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'owner_id', $aParams['owner_id']);
  741. $aJoin1 = $aQueryParts['join'];
  742. $mixedJoinClause['p2'] = "LEFT JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  743. $mixedWhereSubclause['p2'] = "NOT ISNULL(`" . $aJoin1['table_alias'] . "`.`content`)";
  744. //--- Exclude Own (Direct) posts on timelines of following members.
  745. //--- Note. Disabled for now and next check is used instead.
  746. //$mixedWhereSubclause['p2'] = $this->prepareAsString(" AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` <> ?, 1))", $aParams['owner_id']);
  747. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'object_id', $aParams['owner_id']);
  748. $aJoin2 = $aQueryParts['join'];
  749. $aJoin2['table_alias'] = 'cc';
  750. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  751. $mixedJoinClause['p3'] = "LEFT JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $this->_sTable . "`.`system`='0' AND " . $aJoin2['condition'];
  752. $mixedWhereSubclause['p3'] = "NOT ISNULL(`" . $aJoin2['table_alias'] . "`.`content`)";
  753. //--- Select Promoted posts.
  754. $mixedWhereSubclause['p4'] = "`{$this->_sTable}`.`promoted` <> '0'";
  755. break;
  756. }
  757. $aAlertParams = $aParams;
  758. unset($aAlertParams['type'], $aAlertParams['owner_id']);
  759. bx_alert($this->_oConfig->getName(), 'get_list_by_type', 0, 0, array(
  760. 'type' => $aParams['type'],
  761. 'owner_id' => $aParams['owner_id'],
  762. 'params' => $aAlertParams,
  763. 'table' => $this->_sTable,
  764. 'join_clause' => &$mixedJoinClause,
  765. 'where_clause' => &$mixedWhereClause,
  766. 'where_clause_status' => &$sWhereClauseStatus,
  767. 'where_clause_filter' => &$sWhereClauseFilter,
  768. 'where_clause_timeline' => &$sWhereClauseTimeline,
  769. 'where_clause_modules' => &$sWhereClauseModules,
  770. 'where_clause_hidden' => &$sWhereClauseHidden,
  771. 'where_clause_muted' => &$sWhereClauseMuted,
  772. 'where_clause_unpublished' => &$sWhereClauseUnpublished,
  773. 'where_subclause' => &$mixedWhereSubclause
  774. ));
  775. $mixedWhereClause .= $sWhereClauseStatus;
  776. $mixedWhereClause .= $sWhereClauseFilter;
  777. $mixedWhereClause .= $sWhereClauseTimeline;
  778. $mixedWhereClause .= $sWhereClauseModules;
  779. $mixedWhereClause .= $sWhereClauseHidden;
  780. $mixedWhereClause .= $sWhereClauseMuted;
  781. $mixedWhereClause .= $sWhereClauseUnpublished;
  782. if(!empty($mixedWhereSubclause)) {
  783. if(is_array($mixedWhereSubclause)) {
  784. $aWhereClause = array();
  785. foreach($mixedWhereSubclause as $sKey => $sValue)
  786. $aWhereClause[$sKey] = $mixedWhereClause . "AND " . $sValue . " ";
  787. $mixedWhereClause = $aWhereClause;
  788. }
  789. else
  790. $mixedWhereClause .= "AND (" . $mixedWhereSubclause . ") ";
  791. }
  792. return array($mixedJoinClause, $mixedWhereClause);
  793. }
  794. }
  795. /** @} */