BxTimelineDb.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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 $_sTableRepostsTrack;
  15. protected $_sTableHotTrack;
  16. protected $_aTablesMedia;
  17. protected $_aTablesMedia2Events;
  18. /*
  19. * Constructor.
  20. */
  21. function __construct(&$oConfig)
  22. {
  23. parent::__construct($oConfig);
  24. $CNF = &$this->_oConfig->CNF;
  25. $this->_sTableEvent2User = $this->_sPrefix . 'events2users';
  26. $this->_sTableRepostsTrack = $this->_sPrefix . 'reposts_track';
  27. $this->_sTableHotTrack = $this->_sPrefix . 'hot_track';
  28. $this->_aTablesMedia = array(
  29. $CNF['FIELD_PHOTO'] => $this->_sPrefix . 'photos',
  30. $CNF['FIELD_VIDEO'] => $this->_sPrefix . 'videos',
  31. $CNF['FIELD_FILE'] => $this->_sPrefix . 'files'
  32. );
  33. $this->_aTablesMedia2Events = array(
  34. $CNF['FIELD_PHOTO'] => $this->_sPrefix . 'photos2events',
  35. $CNF['FIELD_VIDEO'] => $this->_sPrefix . 'videos2events',
  36. $CNF['FIELD_FILE'] => $this->_sPrefix . 'files2events'
  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. $aBindings = array(
  267. 'profile_id' => $aParams['profile_id']
  268. );
  269. if(isset($aParams['short']) && $aParams['short'] === true) {
  270. $aMethod['name'] = 'getPairs';
  271. $aMethod['params'][1] = 'url';
  272. $aMethod['params'][2] = 'id';
  273. $aMethod['params'][3] = $aBindings;
  274. }
  275. else
  276. $aMethod['params'][1] = $aBindings;
  277. $sJoinClause = "LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`";
  278. $sWhereClause = " AND `tl`.`profile_id`=:profile_id AND ISNULL(`tle`.`event_id`)";
  279. $sOrderClause = "`tl`.`added` DESC";
  280. break;
  281. }
  282. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  283. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  284. $aMethod['params'][0] = "SELECT
  285. " . $sSelectClause . "
  286. FROM `" . $this->_sPrefix . "links` AS `tl` " . $sJoinClause . "
  287. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  288. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  289. }
  290. public function getHot()
  291. {
  292. return $this->fromCache($this->_oConfig->getCacheHotKey(), 'getColumn', "SELECT `event_id` FROM `" . $this->_sTableHotTrack . "`");
  293. }
  294. public function clearHot()
  295. {
  296. $this->cleanCache($this->_oConfig->getCacheHotKey());
  297. return $this->query("TRUNCATE TABLE `" . $this->_sTableHotTrack . "`");
  298. }
  299. public function getHotTrackByDate($iInterval = 24)
  300. {
  301. $aBindings = [
  302. 'interval' => $iInterval
  303. ];
  304. $sQuery = "SELECT
  305. `te`.`id` AS `event_id`,
  306. `te`.`date` AS `value`
  307. FROM `" . $this->_sTable . "` AS `te`
  308. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `te`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)";
  309. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  310. }
  311. public function getHotTrackByCommentsDate($sModule, $sTableTrack, $iInterval = 24, $iThreshold = 0)
  312. {
  313. $aBindings = [
  314. 'module' => $sModule,
  315. 'interval' => $iInterval
  316. ];
  317. $sQueryGroup = "`te`.`id`";
  318. if($iThreshold != 0) {
  319. $aBindings['threshold'] = $iThreshold;
  320. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`cmt_author_id`) >= :threshold";
  321. }
  322. $sQuery = "SELECT
  323. `te`.`id` as `event_id`,
  324. MAX(`tt`.`cmt_time`) AS `value`
  325. FROM `" . $this->_sTable . "` AS `te`
  326. 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
  327. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`cmt_time` > (UNIX_TIMESTAMP() - 3600 * :interval)
  328. GROUP BY " . $sQueryGroup;
  329. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  330. }
  331. public function getHotTrackByCommentsDateModule($sModule, $sTableTrack, $iInterval = 24, $iThreshold = 0)
  332. {
  333. $aBindings = [
  334. 'module' => $sModule,
  335. 'interval' => $iInterval
  336. ];
  337. $sQueryGroup = "`te`.`object_id`";
  338. if($iThreshold != 0) {
  339. $aBindings['threshold'] = $iThreshold;
  340. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`cmt_author_id`) >= :threshold";
  341. }
  342. $sQuery = "SELECT
  343. `te`.`id` as `event_id`,
  344. MAX(`tt`.`cmt_time`) AS `value`
  345. FROM `" . $this->_sTable . "` AS `te`
  346. 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
  347. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`cmt_time` > (UNIX_TIMESTAMP() - 3600 * :interval)
  348. GROUP BY " . $sQueryGroup;
  349. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  350. }
  351. public function getHotTrackByVotesDate($sModule, $sTableTrack, $iInterval = 24, $iThreshold = 0)
  352. {
  353. $aBindings = [
  354. 'module' => $sModule,
  355. 'interval' => $iInterval
  356. ];
  357. $sQueryGroup = "`te`.`id`";
  358. if($iThreshold != 0) {
  359. $aBindings['threshold'] = $iThreshold;
  360. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`author_id`) >= :threshold";
  361. }
  362. $sQuery = "SELECT
  363. `te`.`id` as `event_id`,
  364. MAX(`tt`.`date`) AS `value`
  365. FROM `" . $this->_sTable . "` AS `te`
  366. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`id`=`tt`.`object_id` AND `te`.`object_owner_id`<>`tt`.`author_id` AND `te`.`type`=:module
  367. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  368. GROUP BY " . $sQueryGroup;
  369. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  370. }
  371. public function getHotTrackByVotesDateModule($sModule, $sTableTrack, $iInterval = 24, $iThreshold = 0)
  372. {
  373. $aBindings = [
  374. 'module' => $sModule,
  375. 'interval' => $iInterval
  376. ];
  377. $sQueryGroup = "`te`.`object_id`";
  378. if($iThreshold != 0) {
  379. $aBindings['threshold'] = $iThreshold;
  380. $sQueryGroup .= " HAVING COUNT(DISTINCT `tt`.`author_id`) >= :threshold";
  381. }
  382. $sQuery = "SELECT
  383. `te`.`id` as `event_id`,
  384. MAX(`tt`.`date`) AS `value`
  385. FROM `" . $this->_sTable . "` AS `te`
  386. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`object_id`=`tt`.`object_id` AND `te`.`object_owner_id`<>`tt`.`author_id` AND `te`.`type`=:module
  387. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  388. GROUP BY " . $sQueryGroup;
  389. return $this->getPairs($sQuery, 'event_id', 'value', $aBindings);
  390. }
  391. /**
  392. * Hot Track by Sum of Votes during specified Period is currently disabled.
  393. */
  394. public function getHotTrackByVotesSum($sModule, $sTableTrack, $iInterval = 24)
  395. {
  396. $sQuery = "SELECT
  397. `te`.`id` as `event_id`,
  398. SUM(`tt`.`value`) AS `value`
  399. FROM `" . $this->_sTable . "` AS `te`
  400. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`id`=`tt`.`object_id` AND `te`.`type`=:module
  401. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  402. GROUP BY `te`.`id`";
  403. return $this->getAll($sQuery, array('module' => $sModule, 'interval' => $iInterval));
  404. }
  405. /**
  406. * Hot Track by Sum of Votes during specified Period is currently disabled.
  407. */
  408. public function getHotTrackByVotesSumModule($sModule, $sTableTrack, $iInterval = 24)
  409. {
  410. $sQuery = "SELECT
  411. `te`.`id` as `event_id`,
  412. SUM(`tt`.`value`) AS `value`
  413. FROM `" . $this->_sTable . "` AS `te`
  414. INNER JOIN `" . $sTableTrack . "` AS `tt` ON `te`.`object_id`=`tt`.`object_id` AND `te`.`type`=:module
  415. WHERE (`te`.`system` <> 0 OR `te`.`owner_id` = 0) AND `tt`.`date` > (UNIX_TIMESTAMP() - 3600 * :interval)
  416. GROUP BY `te`.`object_id`";
  417. return $this->getAll($sQuery, array('module' => $sModule, 'interval' => $iInterval));
  418. }
  419. public function updateHotTrack($aTrack)
  420. {
  421. return (int)$this->query("REPLACE INTO `" . $this->_sTableHotTrack . "` SET " . $this->arrayToSQL($aTrack)) > 0;
  422. }
  423. public function getEvents($aParams)
  424. {
  425. bx_alert($this->_oConfig->getName(), 'get_events_before', 0, 0, array(
  426. 'params' => &$aParams,
  427. ));
  428. list($sMethod, $sSelectClause, $mixedJoinClause, $mixedWhereClause, $sOrderClause, $sLimitClause) = $this->_getSqlPartsEvents($aParams);
  429. $bCount = isset($aParams['count']) && $aParams['count'] === true;
  430. if($bCount) {
  431. $sMethod = 'getOne';
  432. $sSelectClause = 'COUNT(*)';
  433. $sOrderClause = '';
  434. $sLimitClause = '';
  435. }
  436. $aAlertParams = $aParams;
  437. unset($aAlertParams['browse']);
  438. bx_alert($this->_oConfig->getName(), 'get_events', 0, 0, array(
  439. 'browse' => $aParams['browse'],
  440. 'params' => $aAlertParams,
  441. 'table' => $this->_sTable,
  442. 'method' => &$sMethod,
  443. 'select_clause' => &$sSelectClause,
  444. 'join_clause' => &$mixedJoinClause,
  445. 'where_clause' => &$mixedWhereClause,
  446. 'order_clause' => &$sOrderClause,
  447. 'limit_clause' => &$sLimitClause
  448. ));
  449. $sSqlMask = "SELECT {select}
  450. FROM `{$this->_sTable}`
  451. INNER JOIN `sys_profiles` ON `{$this->_sTable}`.`object_owner_id`=`sys_profiles`.`id` AND `sys_profiles`.`status`='active' LEFT JOIN `{$this->_sTableHandlers}` ON `{$this->_sTable}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$this->_sTable}`.`action`=`{$this->_sTableHandlers}`.`alert_action` {join}
  452. WHERE 1 {where} {order} {limit}";
  453. if(is_string($mixedWhereClause)) {
  454. $aSqlMarkers = array(
  455. 'select' => $sSelectClause,
  456. 'join' => $mixedJoinClause,
  457. 'where' => $mixedWhereClause,
  458. 'order' => $sOrderClause,
  459. 'limit' => $sLimitClause
  460. );
  461. return $this->$sMethod(bx_replace_markers($sSqlMask, $aSqlMarkers));
  462. }
  463. $bJoinAsArray = !empty($mixedJoinClause) && is_array($mixedJoinClause);
  464. $sOrderSubclause = $sLimitSubclause = '';
  465. if(!$bCount) {
  466. $sOrderSubclause = $sOrderClause;
  467. $sLimitSubclause = isset($aParams['per_page']) ? 'LIMIT 0, ' . ($aParams['start'] + $aParams['per_page']) : '';
  468. }
  469. $aSqlParts = array();
  470. foreach($mixedWhereClause as $sKey => $sValue) {
  471. $aSqlMarkers = array(
  472. 'select' => $sSelectClause,
  473. 'join' => $bJoinAsArray ? (isset($mixedJoinClause[$sKey]) ? $mixedJoinClause[$sKey] : '') : $mixedJoinClause,
  474. 'where' => $sValue,
  475. 'order' => $sOrderSubclause,
  476. 'limit' => $sLimitSubclause
  477. );
  478. $sSqlPart = bx_replace_markers($sSqlMask, $aSqlMarkers);
  479. $aSqlParts[] = !$bCount ? $sSqlPart : (int)$this->$sMethod($sSqlPart);
  480. }
  481. return $bCount ? array_sum($aSqlParts) : $this->$sMethod(bx_replace_markers('(' . implode(') UNION (', $aSqlParts) . ') {order} {limit}', array(
  482. 'order' => str_replace('`' . $this->_sTable . '`.', '', $sOrderClause),
  483. 'limit' => str_replace('`' . $this->_sTable . '`.', '', $sLimitClause),
  484. )));
  485. }
  486. protected function _getFilterAddon($iOwnerId, $sFilter)
  487. {
  488. switch($sFilter) {
  489. /**
  490. * Direct posts in Timeline made by a timeline owner ($iOwnerId)
  491. */
  492. case BX_TIMELINE_FILTER_OWNER:
  493. $sFilterAddon = $this->prepareAsString(" AND `{$this->_sTable}`.`action`='' AND `{$this->_sTable}`.`object_id`=? ", $iOwnerId);
  494. break;
  495. /**
  496. * Direct posts in Timeline made by users except a timeline owner ($iOwnerId)
  497. */
  498. case BX_TIMELINE_FILTER_OTHER:
  499. $sFilterAddon = $this->prepareAsString(" AND `{$this->_sTable}`.`action`='' AND `{$this->_sTable}`.`object_id`<>? ", $iOwnerId);
  500. break;
  501. /**
  502. * All (Direct and System) posts in Timeline (owned by $iOwnerId) made by users except the viewer
  503. */
  504. case BX_TIMELINE_FILTER_OTHER_VIEWER:
  505. $sFilterAddon = $this->prepareAsString(" AND (`{$this->_sTable}`.`action`<>'' OR (`{$this->_sTable}`.`action`='' AND `{$this->_sTable}`.`object_id`<>?)) ", bx_get_logged_profile_id());
  506. break;
  507. case BX_TIMELINE_FILTER_ALL:
  508. default:
  509. $sFilterAddon = "";
  510. }
  511. return $sFilterAddon;
  512. }
  513. protected function _getSqlPartsEvents($aParams)
  514. {
  515. $sMethod = 'getAll';
  516. $sSelectClause = "`{$this->_sTable}`.*";
  517. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  518. switch($aParams['browse']) {
  519. case 'owner_id':
  520. $sWhereClause = $this->prepareAsString("AND `{$this->_sTable}`.`owner_id`=? ", $aParams['value']);
  521. break;
  522. case 'common_by_object':
  523. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  524. $sWhereClause = $this->prepareAsString("AND `{$this->_sTable}`.`system`='0' AND `{$this->_sTable}`.`object_id`=? ", $aParams['value']);
  525. break;
  526. case 'descriptor':
  527. $sMethod = 'getRow';
  528. $sWhereClause = "";
  529. if(isset($aParams['type']))
  530. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`type`=? ", $aParams['type']);
  531. if(isset($aParams['action']))
  532. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`action`=? ", $aParams['action']);
  533. if(isset($aParams['object_id']))
  534. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`object_id`=? ", $aParams['object_id']);
  535. $sLimitClause = "LIMIT 1";
  536. break;
  537. case 'reposted_by_track':
  538. $sJoinClause = $this->prepareAsString("INNER JOIN `" . $this->_sTableRepostsTrack . "` AS `trt` ON `{$this->_sTable}`.`id`=`trt`.`event_id` AND `trt`.`reposted_id`=?", $aParams['value']);
  539. break;
  540. case 'reposted_by_descriptor':
  541. $sWhereClause = "";
  542. if(isset($aParams['type']))
  543. $sWhereClause .= "AND `{$this->_sTable}`.`content` LIKE " . $this->escape('%' . $aParams['type'] . '%');
  544. if(isset($aParams['action']))
  545. $sWhereClause .= "AND `{$this->_sTable}`.`content` LIKE " . $this->escape('%' . $aParams['action'] . '%');
  546. break;
  547. case 'list':
  548. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = parent::_getSqlPartsEvents($aParams);
  549. 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)))
  550. $sSelectClause = "DISTINCT " . $sSelectClause;
  551. break;
  552. case 'ids':
  553. $sWhereClause = "AND `{$this->_sTable}`.`id` IN (" . $this->implode_escape($aParams['ids']) . ") ";
  554. break;
  555. default:
  556. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = parent::_getSqlPartsEvents($aParams);
  557. }
  558. $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`";
  559. if(in_array($aParams['browse'], array('list', 'ids')) && (!isset($aParams['newest']) || $aParams['newest'] === false)) {
  560. $sOrderClause = "";
  561. switch($aParams['type']) {
  562. case BX_TIMELINE_TYPE_HOT:
  563. $sOrderClause = "`{$this->_sTable}`.`sticked` DESC, `{$this->_sTableHotTrack}`.`value` DESC, ";
  564. break;
  565. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  566. case BX_BASE_MOD_NTFS_TYPE_CONNECTIONS:
  567. case BX_TIMELINE_TYPE_CHANNELS:
  568. case BX_TIMELINE_TYPE_FEED:
  569. case BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS:
  570. $sOrderClause = "`{$this->_sTable}`.`sticked` DESC, ";
  571. break;
  572. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  573. $sOrderClause = "`{$this->_sTable}`.`pinned` DESC, ";
  574. break;
  575. }
  576. $sOrderClause = "ORDER BY " . $sOrderClause . "`{$this->_sTable}`.`date` DESC, `{$this->_sTable}`.`id` DESC";
  577. }
  578. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  579. }
  580. protected function _getSqlPartsEventsList($aParams)
  581. {
  582. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  583. $mixedJoinClause = $mixedWhereClause = "";
  584. $sWhereClauseStatus = "AND `{$this->_sTable}`.`active`='1' ";
  585. $sWhereClauseStatus .= $this->prepareAsString("AND `{$this->_sTable}`.`status`=? ", isset($aParams['status']) ? $aParams['status'] : BX_TIMELINE_STATUS_ACTIVE);
  586. //--- Apply filter
  587. $sWhereClauseFilter = "";
  588. if(isset($aParams['filter']))
  589. $sWhereClauseFilter = $this->_getFilterAddon($aParams['owner_id'], $aParams['filter']);
  590. //--- Apply timeline
  591. $sWhereClauseTimeline = "";
  592. if(isset($aParams['timeline']) && !empty($aParams['timeline']) && strpos($aParams['timeline'], '-') !== false) {
  593. list($iY, $iM, $iD) = explode('-', $aParams['timeline']);
  594. $sWhereClauseTimeline = $this->prepareAsString("AND `date`<=? ", mktime(23, 59, 59, (int)$iM, (int)$iD, (int)$iY));
  595. }
  596. //--- Apply modules or handlers filter
  597. $sWhereClauseModules = "";
  598. if(!empty($aParams['modules']) && is_array($aParams['modules']))
  599. $sWhereClauseModules = "AND `" . $this->_sTable . "`.`type` IN (" . $this->implode_escape($aParams['modules']) . ") ";
  600. $sWhereClauseHidden = "";
  601. if(empty($sWhereClauseModules)) {
  602. $aHidden = $this->_oConfig->getHandlersHidden();
  603. $sWhereClauseHidden = !empty($aHidden) && is_array($aHidden) ? "AND `" . $this->_sTableHandlers . "`.`id` NOT IN (" . $this->implode_escape($aHidden) . ") " : "";
  604. }
  605. //--- Apply mute filter
  606. $sWhereClauseMuted = "";
  607. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('connection_mute'));
  608. if($oConnection) {
  609. $aMuted = $oConnection->getConnectedContent(bx_get_logged_profile_id());
  610. if(!empty($aMuted) && is_array($aMuted)) {
  611. $sMuted = "NOT IN (". $this->implode_escape($aMuted) . ")";
  612. $sWhereClauseMuted = "AND `{$this->_sTable}`.`owner_id` $sMuted AND `{$this->_sTable}`.`object_owner_id` $sMuted ";
  613. }
  614. }
  615. //--- Apply unpublished (date in future) filter
  616. $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());
  617. //--- Check type
  618. $mixedWhereSubclause = "";
  619. switch($aParams['type']) {
  620. //--- Feed: Hot
  621. case BX_TIMELINE_TYPE_HOT:
  622. //--- Apply privacy filter
  623. $aPrivacyGroups = array(BX_DOL_PG_ALL);
  624. if(isLogged())
  625. $aPrivacyGroups[] = BX_DOL_PG_MEMBERS;
  626. $aQueryParts = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'))->getContentByGroupAsSQLPart($aPrivacyGroups);
  627. $mixedWhereClause .= $aQueryParts['where'] . " ";
  628. //--- Select Hot posts.
  629. $mixedJoinClause .= " LEFT JOIN `{$this->_sTableHotTrack}` ON `{$this->_sTable}`.`id`=`{$this->_sTableHotTrack}`.`event_id`";
  630. $mixedWhereSubclause = "NOT ISNULL(`{$this->_sTableHotTrack}`.`value`)";
  631. //--- Select Promoted posts.
  632. $mixedWhereSubclause .= " OR `{$this->_sTable}`.`promoted` <> '0'";
  633. break;
  634. //--- Feed: Public
  635. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  636. //--- Apply privacy filter
  637. $aPrivacyGroups = array(BX_DOL_PG_ALL);
  638. if(isLogged())
  639. $aPrivacyGroups[] = BX_DOL_PG_MEMBERS;
  640. $aQueryParts = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'))->getContentByGroupAsSQLPart($aPrivacyGroups);
  641. $mixedWhereClause .= $aQueryParts['where'] . " ";
  642. if($this->_oConfig->isShowAll())
  643. break;
  644. //--- Select All System posts
  645. $mixedWhereSubclause = "`{$this->_sTable}`.`system`='1'";
  646. //--- Select Public (Direct) posts created on Home Page Timeline (Public Feed)
  647. $mixedWhereSubclause .= $this->prepareAsString(" OR `{$this->_sTable}`.`owner_id`=?", 0);
  648. //--- Select Promoted posts.
  649. $mixedWhereSubclause .= " OR `{$this->_sTable}`.`promoted` <> '0'";
  650. break;
  651. //--- Feed: Profile
  652. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  653. if(empty($aParams['owner_id']))
  654. break;
  655. //--- Select Own (System and Direct) posts from Profile's Timeline.
  656. $mixedWhereSubclause = $this->prepareAsString("(`{$this->_sTable}`.`owner_id` = ?)", $aParams['owner_id']);
  657. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  658. $mixedWhereSubclause .= $this->prepareAsString(" OR (`{$this->_sTable}`.`owner_id` = '0' AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` = ?, 1))", $aParams['owner_id']);
  659. break;
  660. //--- Feed: All Profile Connections
  661. case BX_BASE_MOD_NTFS_TYPE_CONNECTIONS:
  662. if(empty($aParams['owner_id']))
  663. break;
  664. $mixedJoinClause = array();
  665. $mixedWhereSubclause = array();
  666. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  667. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  668. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'owner_id', $aParams['owner_id']);
  669. $aJoin1 = $aQueryParts['join'];
  670. $mixedJoinClause['p1'] = "LEFT JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  671. $mixedWhereSubclause['p1'] = "NOT ISNULL(`" . $aJoin1['table_alias'] . "`.`content`)";
  672. //--- Exclude Own (Direct) posts on timelines of following members.
  673. //--- Note. Disabled for now.
  674. //$mixedWhereSubclause['p1'] .= $this->prepareAsString(" AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  675. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'object_id', $aParams['owner_id']);
  676. $aJoin2 = $aQueryParts['join'];
  677. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'object_id', $aParams['owner_id']);
  678. $aJoin2 = $aQueryParts['join'];
  679. $aJoin2['table_alias'] = 'cc';
  680. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  681. $mixedJoinClause['p2'] = "LEFT JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $this->_sTable . "`.`system`='0' AND " . $aJoin2['condition'];
  682. $mixedWhereSubclause['p2'] = "NOT ISNULL(`" . $aJoin2['table_alias'] . "`.`content`)";
  683. //--- Select Promoted posts.
  684. $mixedWhereSubclause['p3'] = "`{$this->_sTable}`.`promoted` <> '0'";
  685. break;
  686. //--- Feed: Profile Connections to Channel contexts only
  687. case BX_TIMELINE_TYPE_CHANNELS:
  688. if(empty($aParams['owner_id']))
  689. break;
  690. $mixedJoinClause = array();
  691. $mixedWhereSubclause = array();
  692. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  693. //--- Join System posts received by following channels.
  694. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'owner_id', $aParams['owner_id']);
  695. $aJoin1 = $aQueryParts['join'];
  696. $mixedJoinClause['p1'] = "LEFT JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  697. $mixedWhereSubclause['p1'] = "`{$this->_sTable}`.`type`='bx_channels' AND NOT ISNULL(`" . $aJoin1['table_alias'] . "`.`content`)";
  698. //--- Exclude Own (Direct) posts on timelines of following members.
  699. //--- Note. Disabled for now.
  700. //$mixedWhereSubclause['p1'] = $this->prepareAsString(" AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  701. //--- Select Promoted posts.
  702. $mixedWhereSubclause['p2'] = "`{$this->_sTable}`.`promoted` <> '0'";
  703. break;
  704. //--- Feed: Profile + Profile Connections to Non-Channel contexts
  705. case BX_TIMELINE_TYPE_FEED:
  706. if(empty($aParams['owner_id']))
  707. break;
  708. $mixedJoinClause = array();
  709. $mixedWhereSubclause = array();
  710. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  711. //--- Select Own (System and Direct) posts from Profile's Timeline.
  712. $sWhereSubclauseOwnProfile = $this->prepareAsString("(`{$this->_sTable}`.`owner_id` = ?)", $aParams['owner_id']);
  713. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  714. $sWhereSubclauseOwnPublic = $this->prepareAsString("(`{$this->_sTable}`.`owner_id` = '0' AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` = ?, 1))", $aParams['owner_id']);
  715. $mixedWhereSubclause['p1'] = "(" . $sWhereSubclauseOwnProfile . " OR " . $sWhereSubclauseOwnPublic . ")";
  716. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  717. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'owner_id', $aParams['owner_id']);
  718. $aJoin1 = $aQueryParts['join'];
  719. $mixedJoinClause['p2'] = "LEFT JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  720. $mixedWhereSubclause['p2'] = "`{$this->_sTable}`.`type`<>'bx_channels' AND NOT ISNULL(`" . $aJoin1['table_alias'] . "`.`content`)";
  721. //--- Exclude Own (Direct) posts on timelines of following members.
  722. //--- Note. Disabled for now and next check is used instead.
  723. //$mixedWhereSubclause['p2'] = $this->prepareAsString(" AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` <> ?, 1))", $aParams['owner_id']);
  724. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'object_id', $aParams['owner_id']);
  725. $aJoin2 = $aQueryParts['join'];
  726. $aJoin2['table_alias'] = 'cc';
  727. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  728. $mixedJoinClause['p3'] = "LEFT JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $this->_sTable . "`.`system`='0' AND " . $aJoin2['condition'];
  729. $mixedWhereSubclause['p3'] = "NOT ISNULL(`" . $aJoin2['table_alias'] . "`.`content`)";
  730. //--- Select Promoted posts.
  731. $mixedWhereSubclause['p4'] = "`{$this->_sTable}`.`promoted` <> '0'";
  732. break;
  733. //--- Feed: Profile + All Profile Connections
  734. case BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS:
  735. if(empty($aParams['owner_id']))
  736. break;
  737. $mixedJoinClause = array();
  738. $mixedWhereSubclause = array();
  739. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  740. //--- Select Own (System and Direct) posts from Profile's Timeline.
  741. $sWhereSubclauseOwnProfile = $this->prepareAsString("(`{$this->_sTable}`.`owner_id` = ?)", $aParams['owner_id']);
  742. //--- Select Own Public (Direct) posts from Home Page Timeline (Public Feed).
  743. $sWhereSubclauseOwnPublic = $this->prepareAsString("(`{$this->_sTable}`.`owner_id` = '0' AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` = ?, 1))", $aParams['owner_id']);
  744. $mixedWhereSubclause['p1'] = "(" . $sWhereSubclauseOwnProfile . " OR " . $sWhereSubclauseOwnPublic . ")";
  745. //--- Join System and Direct posts received by and made by following members. 'LEFT' join is essential to apply different conditions.
  746. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'owner_id', $aParams['owner_id']);
  747. $aJoin1 = $aQueryParts['join'];
  748. $mixedJoinClause['p2'] = "LEFT JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  749. $mixedWhereSubclause['p2'] = "NOT ISNULL(`" . $aJoin1['table_alias'] . "`.`content`)";
  750. //--- Exclude Own (Direct) posts on timelines of following members.
  751. //--- Note. Disabled for now and next check is used instead.
  752. //$mixedWhereSubclause['p2'] = $this->prepareAsString(" AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` <> ?, 1))", $aParams['owner_id']);
  753. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'object_id', $aParams['owner_id']);
  754. $aJoin2 = $aQueryParts['join'];
  755. $aJoin2['table_alias'] = 'cc';
  756. $aJoin2['condition'] = str_replace('`c`', '`' . $aJoin2['table_alias'] . '`', $aJoin2['condition']);
  757. $mixedJoinClause['p3'] = "LEFT JOIN `" . $aJoin2['table'] . "` AS `" . $aJoin2['table_alias'] . "` ON `" . $this->_sTable . "`.`system`='0' AND " . $aJoin2['condition'];
  758. $mixedWhereSubclause['p3'] = "NOT ISNULL(`" . $aJoin2['table_alias'] . "`.`content`)";
  759. //--- Select Promoted posts.
  760. $mixedWhereSubclause['p4'] = "`{$this->_sTable}`.`promoted` <> '0'";
  761. break;
  762. //--- Feed: Profile Connections to contexts from Selected module
  763. default:
  764. if(empty($aParams['owner_id']))
  765. break;
  766. $mixedJoinClause = [];
  767. $mixedWhereSubclause = [];
  768. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  769. //--- Join System posts received by following channels.
  770. $aQueryParts = $oConnection->getConnectedContentAsSQLPartsExt($this->_sPrefix . 'events', 'owner_id', $aParams['owner_id']);
  771. $aJoin1 = $aQueryParts['join'];
  772. $mixedJoinClause['p1'] = "INNER JOIN `sys_profiles` AS `p` ON `" . $this->_sTable ."`.`owner_id`=`p`.`id` AND `p`.`type`='" . $aParams['type'] . "'";
  773. $mixedJoinClause['p1'] .= " LEFT JOIN `" . $aJoin1['table'] . "` AS `" . $aJoin1['table_alias'] . "` ON " . $aJoin1['condition'];
  774. $mixedWhereSubclause['p1'] = "NOT ISNULL(`" . $aJoin1['table_alias'] . "`.`content`)";
  775. //--- Exclude Own (Direct) posts on timelines of following members.
  776. //--- Note. Disabled for now.
  777. //$mixedWhereSubclause['p1'] = $this->prepareAsString(" AND IF(`{$this->_sTable}`.`system`='0', `{$this->_sTable}`.`object_id` <> ?, 1)", $aParams['owner_id']);
  778. //--- Select Promoted posts.
  779. $mixedWhereSubclause['p2'] = "`" . $this->_sTable . "`.`promoted` <> '0'";
  780. break;
  781. }
  782. $aAlertParams = $aParams;
  783. unset($aAlertParams['type'], $aAlertParams['owner_id']);
  784. bx_alert($this->_oConfig->getName(), 'get_list_by_type', 0, 0, array(
  785. 'type' => $aParams['type'],
  786. 'owner_id' => $aParams['owner_id'],
  787. 'params' => $aAlertParams,
  788. 'table' => $this->_sTable,
  789. 'join_clause' => &$mixedJoinClause,
  790. 'where_clause' => &$mixedWhereClause,
  791. 'where_clause_status' => &$sWhereClauseStatus,
  792. 'where_clause_filter' => &$sWhereClauseFilter,
  793. 'where_clause_timeline' => &$sWhereClauseTimeline,
  794. 'where_clause_modules' => &$sWhereClauseModules,
  795. 'where_clause_hidden' => &$sWhereClauseHidden,
  796. 'where_clause_muted' => &$sWhereClauseMuted,
  797. 'where_clause_unpublished' => &$sWhereClauseUnpublished,
  798. 'where_subclause' => &$mixedWhereSubclause
  799. ));
  800. $mixedWhereClause .= $sWhereClauseStatus;
  801. $mixedWhereClause .= $sWhereClauseFilter;
  802. $mixedWhereClause .= $sWhereClauseTimeline;
  803. $mixedWhereClause .= $sWhereClauseModules;
  804. $mixedWhereClause .= $sWhereClauseHidden;
  805. $mixedWhereClause .= $sWhereClauseMuted;
  806. $mixedWhereClause .= $sWhereClauseUnpublished;
  807. if(!empty($mixedWhereSubclause)) {
  808. if(is_array($mixedWhereSubclause)) {
  809. $aWhereClause = array();
  810. foreach($mixedWhereSubclause as $sKey => $sValue)
  811. $aWhereClause[$sKey] = $mixedWhereClause . "AND " . $sValue . " ";
  812. $mixedWhereClause = $aWhereClause;
  813. }
  814. else
  815. $mixedWhereClause .= "AND (" . $mixedWhereSubclause . ") ";
  816. }
  817. return array($mixedJoinClause, $mixedWhereClause);
  818. }
  819. public function getMenuItemMaxOrder($sSetName)
  820. {
  821. return $this->getOne("SELECT IFNULL(MAX(`order`), 0) FROM `sys_menu_items` WHERE `set_name`=:set_name LIMIT 1", [
  822. 'set_name' => $sSetName
  823. ]);
  824. }
  825. public function getMenuItemId($sSetName, $sName)
  826. {
  827. return (int)$this->getOne("SELECT `id` FROM `sys_menu_items` WHERE `set_name`=:set_name AND `name`=:name LIMIT 1", [
  828. 'set_name' => $sSetName,
  829. 'name' => $sName
  830. ]);
  831. }
  832. public function insertMenuItem($sSetName, $sModule, $sName, $sTitle, $iOrder)
  833. {
  834. return $this->query("INSERT INTO `sys_menu_items` SET " . $this->arrayToSQL([
  835. 'set_name' => $sSetName,
  836. 'module' => $sModule,
  837. 'name' => $sName,
  838. 'title_system' => $sTitle,
  839. 'title' => $sTitle,
  840. 'link' => 'javascript:void(0)',
  841. 'onclick' => "javascript:{js_object_view}.changeFeed(this, '" . $sModule . "')",
  842. 'target' => '_self',
  843. 'order' => $iOrder
  844. ]));
  845. }
  846. public function deleteMenuItem($sSetName, $sName)
  847. {
  848. return $this->query("DELETE FROM `sys_menu_items` WHERE `set_name`=:set_name AND `name`=:name LIMIT 1", [
  849. 'set_name' => $sSetName,
  850. 'name' => $sName
  851. ]);
  852. }
  853. }
  854. /** @} */