BxTimelineDb.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 $_sTableEvent2User;
  14. protected $_sTablesRepostTrack;
  15. protected $_aTablesMedia;
  16. protected $_aTablesMedia2Events;
  17. /*
  18. * Constructor.
  19. */
  20. function __construct(&$oConfig)
  21. {
  22. parent::__construct($oConfig);
  23. $this->_sTableEvent2User = $this->_sPrefix . 'events2users';
  24. $this->_sTableRepostsTrack = $this->_sPrefix . 'reposts_track';
  25. $this->_aTablesMedia = array(
  26. BX_TIMELINE_MEDIA_PHOTO => $this->_sPrefix . 'photos',
  27. BX_TIMELINE_MEDIA_VIDEO => $this->_sPrefix . 'videos'
  28. );
  29. $this->_aTablesMedia2Events = array(
  30. BX_TIMELINE_MEDIA_PHOTO => $this->_sPrefix . 'photos2events',
  31. BX_TIMELINE_MEDIA_VIDEO => $this->_sPrefix . 'videos2events'
  32. );
  33. }
  34. public function deleteModuleEvents($aData)
  35. {
  36. foreach($aData['handlers'] as $aHandler) {
  37. //Delete system events.
  38. $this->deleteEvent(array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  39. //Delete reposted events.
  40. $aEvents = $this->getEvents(array('browse' => 'reposted_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  41. foreach($aEvents as $aEvent) {
  42. $aContent = unserialize($aEvent['content']);
  43. if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
  44. $this->deleteEvent(array('id' => (int)$aEvent['id']));
  45. }
  46. }
  47. }
  48. public function activateModuleEvents($aData, $bActivate = true)
  49. {
  50. $iActivate = $bActivate ? 1 : 0;
  51. foreach($aData['handlers'] as $aHandler) {
  52. //Activate (deactivate) system events.
  53. $this->updateEvent(array('active' => $iActivate), array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  54. //Activate (deactivate) reposted events.
  55. $aEvents = $this->getEvents(array('browse' => 'reposted_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  56. foreach($aEvents as $aEvent) {
  57. $aContent = unserialize($aEvent['content']);
  58. if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
  59. $this->updateEvent(array('active' => $iActivate), array('id' => (int)$aEvent['id']));
  60. }
  61. }
  62. }
  63. public function getMaxDuration($aParams)
  64. {
  65. $aParams['browse'] = 'last';
  66. if(isset($aParams['timeline']))
  67. unset($aParams['timeline']);
  68. $aEvent = $this->getEvents($aParams);
  69. if(empty($aEvent) || !is_array($aEvent))
  70. return 0;
  71. $iNowYear = date('Y', time());
  72. return (int)$aEvent['year'] < $iNowYear ? (int)$aEvent['year'] : 0;
  73. }
  74. //--- Repost related methods ---//
  75. public function insertRepostTrack($iEventId, $iAuthorId, $sAuthorIp, $iRepostedId)
  76. {
  77. $iNow = time();
  78. $iAuthorNip = ip2long($sAuthorIp);
  79. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableRepostsTrack}` SET `event_id` = ?, `author_id` = ?, `author_nip` = ?, `reposted_id` = ?, `date` = ?", $iEventId, $iAuthorId, $iAuthorNip, $iRepostedId, $iNow);
  80. return (int)$this->query($sQuery) > 0;
  81. }
  82. public function deleteRepostTrack($iEventId)
  83. {
  84. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableRepostsTrack}` WHERE `event_id` = ?", $iEventId);
  85. return (int)$this->query($sQuery) > 0;
  86. }
  87. public function updateRepostCounter($iId, $iCounter, $iIncrement = 1)
  88. {
  89. return (int)$this->updateEvent(array('reposts' => (int)$iCounter + $iIncrement), array('id' => $iId)) > 0;
  90. }
  91. public function getReposted($sType, $sAction, $iObjectId)
  92. {
  93. $bSystem = $this->_oConfig->isSystem($sType, $sAction);
  94. if($bSystem)
  95. $aParams = array('browse' => 'descriptor', 'type' => $sType, 'action' => $sAction, 'object_id' => $iObjectId);
  96. else
  97. $aParams = array('browse' => 'id', 'value' => $iObjectId);
  98. $aReposted = $this->getEvents($aParams);
  99. if($bSystem && (empty($aReposted) || !is_array($aReposted))) {
  100. $iOwnerId = 0;
  101. $iDate = 0;
  102. $iHidden = 1;
  103. $mixedResult = $this->_oConfig->getSystemDataByDescriptor($sType, $sAction, $iObjectId);
  104. if(is_array($mixedResult)) {
  105. $iOwnerId = !empty($mixedResult['owner_id']) ? (int)$mixedResult['owner_id'] : 0;
  106. $iDate = !empty($mixedResult['date']) ? (int)$mixedResult['date'] : 0;
  107. if(!empty($iOwnerId) && !empty($iDate))
  108. $iHidden = 0;
  109. }
  110. $iId = $this->insertEvent(array(
  111. 'owner_id' => $iOwnerId,
  112. 'type' => $sType,
  113. 'action' => $sAction,
  114. 'object_id' => $iObjectId,
  115. 'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault('object'),
  116. 'content' => '',
  117. 'title' => '',
  118. 'description' => '',
  119. 'date' => $iDate,
  120. 'hidden' => $iHidden
  121. ));
  122. $aReposted = $this->getEvents(array('browse' => 'id', 'value' => $iId));
  123. }
  124. return $aReposted;
  125. }
  126. function getRepostedBy($iRepostedId)
  127. {
  128. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableRepostsTrack}` WHERE `reposted_id`=?", $iRepostedId);
  129. return $this->getColumn($sQuery);
  130. }
  131. function isReposted($iRepostedId, $iOwnerId, $iAuthorId)
  132. {
  133. $sQuery = $this->prepare("SELECT
  134. `te`.`id`
  135. FROM `{$this->_sTableRepostsTrack}` AS `tst`
  136. LEFT JOIN `{$this->_sTable}` AS `te` ON `tst`.`event_id`=`te`.`id`
  137. WHERE `tst`.`author_id`=? AND `tst`.`reposted_id`=? AND `te`.`owner_id`=?", $iAuthorId, $iRepostedId, $iOwnerId);
  138. return (int)$this->getOne($sQuery) > 0;
  139. }
  140. //--- Photo uploader related methods ---//
  141. public function saveMedia($sType, $iEventId, $iItemId)
  142. {
  143. $sTable = $this->_aTablesMedia2Events[$sType];
  144. $sQuery = $this->prepare("INSERT INTO `" . $sTable . "` SET `event_id`=?, `media_id`=?", $iEventId, $iItemId);
  145. return (int)$this->query($sQuery) > 0;
  146. }
  147. public function deleteMedia($sType, $iEventId)
  148. {
  149. $sTable = $this->_aTablesMedia2Events[$sType];
  150. $sQuery = $this->prepare("DELETE FROM `" . $sTable . "` WHERE `event_id` = ?", $iEventId);
  151. return (int)$this->query($sQuery) > 0;
  152. }
  153. public function getMedia($sType, $iEventId, $iOffset = 0)
  154. {
  155. $sTableMedia = $this->_aTablesMedia[$sType];
  156. $sTableMedia2Events = $this->_aTablesMedia2Events[$sType];
  157. $sLimitAddon = '';
  158. if($iOffset != 0)
  159. $sLimitAddon = $this->prepareAsString(" OFFSET ?", $iOffset);
  160. $sQuery = $this->prepare("SELECT
  161. `tme`.`media_id` AS `id`
  162. FROM `" . $sTableMedia2Events . "` AS `tme`
  163. LEFT JOIN `" . $sTableMedia . "` AS `tm` ON `tme`.`media_id`=`tm`.`id`
  164. WHERE `tme`.`event_id`=?" . $sLimitAddon, $iEventId);
  165. return $this->getColumn($sQuery);
  166. }
  167. //--- Link attach related methods ---//
  168. public function getUnusedLinks($iUserId, $iLinkId = 0)
  169. {
  170. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  171. $aMethod['params'][1] = array(
  172. 'profile_id' => $iUserId
  173. );
  174. $sWhereAddon = '';
  175. if(!empty($iLinkId)) {
  176. $aMethod['name'] = 'getRow';
  177. $aMethod['params'][1]['id'] = $iLinkId;
  178. $sWhereAddon = " AND `tl`.`id`=:id";
  179. }
  180. $aMethod['params'][0] = "SELECT
  181. `tl`.`id` AS `id`,
  182. `tl`.`profile_id` AS `profile_id`,
  183. `tl`.`media_id` AS `media_id`,
  184. `tl`.`url` AS `url`,
  185. `tl`.`title` AS `title`,
  186. `tl`.`text` AS `text`,
  187. `tl`.`added` AS `added`
  188. FROM `" . $this->_sPrefix . "links` AS `tl`
  189. LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`
  190. WHERE `tl`.`profile_id`=:profile_id AND ISNULL(`tle`.`event_id`)" . $sWhereAddon . "
  191. ORDER BY `tl`.`added` DESC";
  192. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);;
  193. }
  194. public function deleteUnusedLinks($iUserId, $iLinkId = 0)
  195. {
  196. $aBindings = array(
  197. 'profile_id' => $iUserId
  198. );
  199. $sWhereAddon = '';
  200. if(!empty($iLinkId)) {
  201. $aBindings['id'] = $iLinkId;
  202. $sWhereAddon = " AND `id`=:id";
  203. }
  204. return $this->query("DELETE FROM `" . $this->_sPrefix . "links` WHERE `profile_id`=:profile_id" . $sWhereAddon, $aBindings);
  205. }
  206. public function saveLink($iEventId, $iLinkId)
  207. {
  208. $sQuery = $this->prepare("INSERT INTO `" . $this->_sPrefix . "links2events` SET `event_id`=?, `link_id`=?", $iEventId, $iLinkId);
  209. return (int)$this->query($sQuery) > 0;
  210. }
  211. public function deleteLinks($iEventId)
  212. {
  213. $sQuery = $this->prepare("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` = ?", $iEventId);
  214. return (int)$this->query($sQuery) > 0;
  215. }
  216. public function getLinks($iEventId)
  217. {
  218. $sQuery = $this->prepare("SELECT
  219. `tl`.`id` AS `id`,
  220. `tl`.`profile_id` AS `profile_id`,
  221. `tl`.`media_id` AS `media_id`,
  222. `tl`.`url` AS `url`,
  223. `tl`.`title` AS `title`,
  224. `tl`.`text` AS `text`,
  225. `tl`.`added` AS `added`
  226. FROM `" . $this->_sPrefix . "links` AS `tl`
  227. LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`
  228. WHERE `tle`.`event_id`=?", $iEventId);
  229. return $this->getAll($sQuery);
  230. }
  231. protected function _getFilterAddon($iOwnerId, $sFilter)
  232. {
  233. switch($sFilter) {
  234. /**
  235. * Direct posts in Timeline made by a timeline owner ($iOwnerId)
  236. */
  237. case BX_TIMELINE_FILTER_OWNER:
  238. $sFilterAddon = $this->prepareAsString(" AND `{$this->_sTable}`.`action`='' AND `{$this->_sTable}`.`object_id`=? ", $iOwnerId);
  239. break;
  240. /**
  241. * Direct posts in Timeline made by users except a timeline owner ($iOwnerId)
  242. */
  243. case BX_TIMELINE_FILTER_OTHER:
  244. $sFilterAddon = $this->prepareAsString(" AND `{$this->_sTable}`.`action`='' AND `{$this->_sTable}`.`object_id`<>? ", $iOwnerId);
  245. break;
  246. /**
  247. * All (Direct and System) posts in Timeline (owned by $iOwnerId) made by users except the viewer
  248. */
  249. case BX_TIMELINE_FILTER_OTHER_VIEWER:
  250. $sFilterAddon = $this->prepareAsString(" AND (`{$this->_sTable}`.`action`<>'' OR (`{$this->_sTable}`.`action`='' AND `{$this->_sTable}`.`object_id`<>?)) ", bx_get_logged_profile_id());
  251. break;
  252. case BX_TIMELINE_FILTER_ALL:
  253. default:
  254. $sFilterAddon = "";
  255. }
  256. return $sFilterAddon;
  257. }
  258. protected function _getSqlPartsEvents($aParams)
  259. {
  260. $sMethod = 'getAll';
  261. $sSelectClause = "`{$this->_sTable}`.*";
  262. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  263. switch($aParams['browse']) {
  264. case 'owner_id':
  265. $sWhereClause = $this->prepareAsString("AND `{$this->_sTable}`.`owner_id`=? ", $aParams['value']);
  266. break;
  267. case 'common_by_object':
  268. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  269. $sWhereClause = $this->prepareAsString("AND SUBSTRING(`{$this->_sTable}`.`type`, 1, " . strlen($sCommonPostPrefix) . ")='" . $sCommonPostPrefix . "' AND `{$this->_sTable}`.`object_id`=? ", $aParams['value']);
  270. break;
  271. case 'descriptor':
  272. $sMethod = 'getRow';
  273. $sWhereClause = "";
  274. if(isset($aParams['type']))
  275. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`type`=? ", $aParams['type']);
  276. if(isset($aParams['action']))
  277. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`action`=? ", $aParams['action']);
  278. if(isset($aParams['object_id']))
  279. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`object_id`=? ", $aParams['object_id']);
  280. $sLimitClause = "LIMIT 1";
  281. break;
  282. case 'reposted_by_descriptor':
  283. $sWhereClause = "";
  284. if(isset($aParams['type']))
  285. $sWhereClause .= "AND `{$this->_sTable}`.`content` LIKE " . $this->escape('%' . $aParams['type'] . '%');
  286. if(isset($aParams['action']))
  287. $sWhereClause .= "AND `{$this->_sTable}`.`content` LIKE " . $this->escape('%' . $aParams['action'] . '%');
  288. break;
  289. default:
  290. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = parent::_getSqlPartsEvents($aParams);
  291. }
  292. $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`";
  293. if($aParams['browse'] == 'list')
  294. $sOrderClause = "ORDER BY `{$this->_sTable}`.`pinned` DESC, `{$this->_sTable}`.`date` DESC";
  295. if(isset($aParams['count']) && $aParams['count'] === true) {
  296. $sMethod = 'getOne';
  297. $sSelectClause = "COUNT(`{$this->_sTable}`.`id`)";
  298. }
  299. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  300. }
  301. protected function _getSqlPartsEventsList($aParams)
  302. {
  303. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  304. $sJoinClause = "";
  305. $sWhereClause = "AND `{$this->_sTable}`.`hidden`='0' ";
  306. if(isset($aParams['active']))
  307. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`active`=? ", (int)$aParams['active']);
  308. //--- Apply filter
  309. if(isset($aParams['filter']))
  310. $sWhereClause .= $this->_getFilterAddon($aParams['owner_id'], $aParams['filter']);
  311. //--- Apply timeline
  312. if(isset($aParams['timeline']) && !empty($aParams['timeline'])) {
  313. $iYear = (int)$aParams['timeline'];
  314. $sWhereClause .= $this->prepareAsString("AND `date`<=? ", mktime(23, 59, 59, 12, 31, $iYear));
  315. }
  316. //--- Apply modules or handlers filter
  317. $sWhereModuleFilter = '';
  318. if(!empty($aParams['modules']) && is_array($aParams['modules']))
  319. $sWhereModuleFilter = "AND `" . $this->_sTable . "`.`type` IN (" . $this->implode_escape($aParams['modules']) . ") ";
  320. if($sWhereModuleFilter == '') {
  321. $aHidden = $this->_oConfig->getHandlersHidden();
  322. $sWhereModuleFilter = !empty($aHidden) && is_array($aHidden) ? "AND `" . $this->_sTableHandlers . "`.`id` NOT IN (" . $this->implode_escape($aHidden) . ") " : "";
  323. }
  324. if($sWhereModuleFilter != '')
  325. $sWhereClause .= $sWhereModuleFilter;
  326. //--- Apply unpublished (date in future) filter
  327. $sWhereClause .= $this->prepareAsString("AND IF(SUBSTRING(`{$this->_sTable}`.`type`, 1, " . strlen($sCommonPostPrefix) . ") = '" . $sCommonPostPrefix . "' AND `{$this->_sTable}`.`object_id` = ?, 1, `{$this->_sTable}`.`date` <= UNIX_TIMESTAMP()) ", bx_get_logged_profile_id());
  328. //--- Check type
  329. $sWhereSubclause = "";
  330. switch($aParams['type']) {
  331. //--- Site (Public) Feed.
  332. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  333. //--- Apply privacy filter
  334. $aPrivacyGroups = array(BX_DOL_PG_ALL);
  335. if(isLogged())
  336. $aPrivacyGroups[] = BX_DOL_PG_MEMBERS;
  337. $aQueryParts = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'))->getContentByGroupAsSQLPart($aPrivacyGroups);
  338. $sWhereClause .= $aQueryParts['where'] . " ";
  339. if($this->_oConfig->isShowAll())
  340. break;
  341. //--- Select All System posts
  342. $sWhereSubclause = "SUBSTRING(`{$this->_sTable}`.`type`, 1, " . strlen($sCommonPostPrefix) . ") <> '" . $sCommonPostPrefix . "'";
  343. //--- Select Public (Direct) posts created on Home Page Timeline (Public Feed)
  344. $sWhereSubclause .= $this->prepareAsString(" OR `{$this->_sTable}`.`owner_id`=?", 0);
  345. //--- Select Promoted posts.
  346. $sWhereSubclause .= " OR `{$this->_sTable}`.`promoted` <> '0'";
  347. break;
  348. //--- Profile Feed
  349. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  350. if(empty($aParams['owner_id']))
  351. break;
  352. //--- Select Own (System and Direct) posts from Profile's Timeline.
  353. $sWhereSubclause = $this->prepareAsString("(`{$this->_sTable}`.`owner_id` = ?)", $aParams['owner_id']);
  354. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  355. $sWhereSubclause .= $this->prepareAsString(" OR (`{$this->_sTable}`.`owner_id` = '0' AND IF(SUBSTRING(`{$this->_sTable}`.`type`, 1, " . strlen($sCommonPostPrefix) . ") = '" . $sCommonPostPrefix . "', `{$this->_sTable}`.`object_id` = ?, 1))", $aParams['owner_id']);
  356. break;
  357. //--- Profile Connections Feed
  358. case BX_BASE_MOD_NTFS_TYPE_CONNECTIONS:
  359. if(empty($aParams['owner_id']))
  360. break;
  361. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  362. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'owner_id', $aParams['owner_id']);
  363. $aJoin1 = $aQueryParts['join'];
  364. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'object_id', $aParams['owner_id']);
  365. $aJoin2 = $aQueryParts['join'];
  366. //--- Join System and Direct posts made by following members.
  367. $sJoinClause .= " " . $aJoin1['type'] . " JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON ((" . $aJoin1['condition'] . ") OR (SUBSTRING(`" . $this->_sTable . "`.`type`, 1, " . strlen($sCommonPostPrefix) . ") = '" . $sCommonPostPrefix . "' AND " . $aJoin2['condition'] . "))";
  368. //--- Exclude Own (Direct) posts on timelines of following members.
  369. //--- Note. Disabled for now.
  370. //$sWhereSubclause = $this->prepareAsString("IF(SUBSTRING(`{$this->_sTable}`.`type`, 1, " . strlen($sCommonPostPrefix) . ") = '" . $sCommonPostPrefix . "', `{$this->_sTable}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  371. //--- Select Promoted posts.
  372. $sWhereSubclause .= " OR `{$this->_sTable}`.`promoted` <> '0'";
  373. break;
  374. //--- Profile + Profile Connections Feed
  375. case BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS:
  376. if(empty($aParams['owner_id']))
  377. break;
  378. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  379. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'owner_id', $aParams['owner_id']);
  380. $aJoin1 = $aQueryParts['join'];
  381. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'object_id', $aParams['owner_id']);
  382. $aJoin2 = $aQueryParts['join'];
  383. //--- Join System and Direct posts made by following members. 'LEFT' join is essential to apply different conditions.
  384. $sJoinClause .= " LEFT JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON ((" . $aJoin1['condition'] . ") OR (SUBSTRING(`" . $this->_sTable . "`.`type`, 1, " . strlen($sCommonPostPrefix) . ") = '" . $sCommonPostPrefix . "' AND " . $aJoin2['condition'] . "))";
  385. //--- Select Own (System and Direct) posts from Profile's Timeline.
  386. $sWhereSubclause = $this->prepareAsString("(`{$this->_sTable}`.`owner_id` = ?)", $aParams['owner_id']);
  387. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  388. $sWhereSubclause .= $this->prepareAsString(" OR (`{$this->_sTable}`.`owner_id` = '0' AND IF(SUBSTRING(`{$this->_sTable}`.`type`, 1, " . strlen($sCommonPostPrefix) . ") = '" . $sCommonPostPrefix . "', `{$this->_sTable}`.`object_id` = ?, 1))", $aParams['owner_id']);
  389. //--- Exclude Own (Direct) posts on timelines of following members.
  390. //--- Note. Disabled for now and next check is used instead.
  391. //$sWhereSubclause .= $this->prepareAsString(" OR (NOT ISNULL(`c`.`content`) AND IF(SUBSTRING(`{$this->_sTable}`.`type`, 1, " . strlen($sCommonPostPrefix) . ") = '" . $sCommonPostPrefix . "', `{$this->_sTable}`.`object_id` <> ?, 1))", $aParams['owner_id']);
  392. //--- All posts on timelines of following members.
  393. $sWhereSubclause .= " OR NOT ISNULL(`c`.`content`)";
  394. //--- Select Promoted posts.
  395. $sWhereSubclause .= " OR `{$this->_sTable}`.`promoted` <> '0'";
  396. break;
  397. }
  398. if(!empty($sWhereSubclause))
  399. $sWhereClause .= "AND (" . $sWhereSubclause . ") ";
  400. return array($sJoinClause, $sWhereClause);
  401. }
  402. function updateSimilarObject($iId, &$oAlert, $sDuration = 'day')
  403. {
  404. $sType = $oAlert->sUnit;
  405. $sAction = $oAlert->sAction;
  406. //Check handler
  407. $aHandler = $this->_oConfig->getHandlers($sType . '_' . $sAction);
  408. if(empty($aHandler) || !is_array($aHandler) || (int)$aHandler['groupable'] != 1)
  409. return false;
  410. //Check content's extra values
  411. if(isset($aHandler['group_by']) && !empty($aHandler['group_by']) && (!isset($oAlert->aExtras[$aHandler['group_by']]) || empty($oAlert->aExtras[$aHandler['group_by']])))
  412. return false;
  413. $aBindings = array(
  414. 'object_id' => $oAlert->iObject,
  415. 'id' => $iId,
  416. 'owner_id' => $oAlert->iSender,
  417. 'type' => $sType,
  418. 'action' => $sAction
  419. );
  420. $sWhereClause = "";
  421. switch($sDuration) {
  422. case 'day':
  423. $aBindings['day_start'] = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  424. $aBindings['day_end'] = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
  425. $sWhereClause .= "AND `date`>:day_start AND `date`<:day_end ";
  426. break;
  427. }
  428. if(isset($aHandler['group_by'])) {
  429. $aBindings['content'] = '%' . $oAlert->aExtras[$aHandler['group_by']] . '%';
  430. $sWhereClause .= "AND `content` LIKE :content ";
  431. }
  432. $sSql = "UPDATE `{$this->_sTable}`
  433. SET
  434. `object_id`=CONCAT(`object_id`, ',:object_id'),
  435. `title`='',
  436. `description`='',
  437. `date`=UNIX_TIMESTAMP()
  438. WHERE
  439. `id`<>:id AND
  440. `owner_id`=:owner_id AND
  441. `type`=:type AND
  442. `action`=:action " . $sWhereClause;
  443. $mixedResult = $this->query($sSql, $aBindings);
  444. if((int)$mixedResult > 0)
  445. $this->deleteEvent(array('id' => $iId));
  446. return $mixedResult;
  447. }
  448. }
  449. /** @} */