BxTimelineDb.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 $_sTablesShareTrack;
  14. protected $_aTablesMedia;
  15. protected $_aTablesMedia2Events;
  16. /*
  17. * Constructor.
  18. */
  19. function __construct(&$oConfig)
  20. {
  21. parent::__construct($oConfig);
  22. $this->_sTableSharesTrack = $this->_sPrefix . 'shares_track';
  23. $this->_aTablesMedia = array(
  24. BX_TIMELINE_MEDIA_PHOTO => $this->_sPrefix . 'photos',
  25. BX_TIMELINE_MEDIA_VIDEO => $this->_sPrefix . 'videos'
  26. );
  27. $this->_aTablesMedia2Events = array(
  28. BX_TIMELINE_MEDIA_PHOTO => $this->_sPrefix . 'photos2events',
  29. BX_TIMELINE_MEDIA_VIDEO => $this->_sPrefix . 'videos2events'
  30. );
  31. }
  32. public function deleteModuleEvents($aData)
  33. {
  34. foreach($aData['handlers'] as $aHandler) {
  35. //Delete system events.
  36. $this->deleteEvent(array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  37. //Delete shared events.
  38. $aEvents = $this->getEvents(array('browse' => 'shared_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  39. foreach($aEvents as $aEvent) {
  40. $aContent = unserialize($aEvent['content']);
  41. if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
  42. $this->deleteEvent(array('id' => (int)$aEvent['id']));
  43. }
  44. }
  45. }
  46. public function activateModuleEvents($aData, $bActivate = true)
  47. {
  48. $iActivate = $bActivate ? 1 : 0;
  49. foreach($aData['handlers'] as $aHandler) {
  50. //Activate (deactivate) system events.
  51. $this->updateEvent(array('active' => $iActivate), array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  52. //Activate (deactivate) shared events.
  53. $aEvents = $this->getEvents(array('browse' => 'shared_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  54. foreach($aEvents as $aEvent) {
  55. $aContent = unserialize($aEvent['content']);
  56. if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
  57. $this->updateEvent(array('active' => $iActivate), array('id' => (int)$aEvent['id']));
  58. }
  59. }
  60. }
  61. public function getMaxDuration($aParams)
  62. {
  63. $aParams['browse'] = 'last';
  64. if(isset($aParams['timeline']))
  65. unset($aParams['timeline']);
  66. $aEvent = $this->getEvents($aParams);
  67. if(empty($aEvent) || !is_array($aEvent))
  68. return 0;
  69. $iNowYear = date('Y', time());
  70. return (int)$aEvent['year'] < $iNowYear ? (int)$aEvent['year'] : 0;
  71. }
  72. //--- Share related methods ---//
  73. public function insertShareTrack($iEventId, $iAuthorId, $sAuthorIp, $iSharedId)
  74. {
  75. $iNow = time();
  76. $iAuthorNip = ip2long($sAuthorIp);
  77. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableSharesTrack}` SET `event_id` = ?, `author_id` = ?, `author_nip` = ?, `shared_id` = ?, `date` = ?", $iEventId, $iAuthorId, $iAuthorNip, $iSharedId, $iNow);
  78. return (int)$this->query($sQuery) > 0;
  79. }
  80. public function deleteShareTrack($iEventId)
  81. {
  82. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableSharesTrack}` WHERE `event_id` = ?", $iEventId);
  83. return (int)$this->query($sQuery) > 0;
  84. }
  85. public function updateShareCounter($iId, $iCounter, $iIncrement = 1)
  86. {
  87. return (int)$this->updateEvent(array('shares' => (int)$iCounter + $iIncrement), array('id' => $iId)) > 0;
  88. }
  89. public function getShared($sType, $sAction, $iObjectId)
  90. {
  91. $bSystem = $this->_oConfig->isSystem($sType, $sAction);
  92. if($bSystem)
  93. $aParams = array('browse' => 'descriptor', 'type' => $sType, 'action' => $sAction, 'object_id' => $iObjectId);
  94. else
  95. $aParams = array('browse' => 'id', 'value' => $iObjectId);
  96. $aShared = $this->getEvents($aParams);
  97. if($bSystem && (empty($aShared) || !is_array($aShared))) {
  98. $iOwnerId = 0;
  99. $iDate = 0;
  100. $iHidden = 1;
  101. $mixedResult = $this->_oConfig->getSystemDataByDescriptor($sType, $sAction, $iObjectId);
  102. if(is_array($mixedResult)) {
  103. $iOwnerId = !empty($mixedResult['owner_id']) ? (int)$mixedResult['owner_id'] : 0;
  104. $iDate = !empty($mixedResult['date']) ? (int)$mixedResult['date'] : 0;
  105. if(!empty($iOwnerId) && !empty($iDate))
  106. $iHidden = 0;
  107. }
  108. $iId = $this->insertEvent(array(
  109. 'owner_id' => $iOwnerId,
  110. 'type' => $sType,
  111. 'action' => $sAction,
  112. 'object_id' => $iObjectId,
  113. 'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault('object'),
  114. 'content' => '',
  115. 'title' => '',
  116. 'description' => '',
  117. 'date' => $iDate,
  118. 'hidden' => $iHidden
  119. ));
  120. $aShared = $this->getEvents(array('browse' => 'id', 'value' => $iId));
  121. }
  122. return $aShared;
  123. }
  124. function getSharedBy($iSharedId)
  125. {
  126. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableSharesTrack}` WHERE `shared_id`=?", $iSharedId);
  127. return $this->getColumn($sQuery);
  128. }
  129. function isShared($iSharedId, $iOwnerId, $iAuthorId)
  130. {
  131. $sQuery = $this->prepare("SELECT
  132. `te`.`id`
  133. FROM `{$this->_sTableSharesTrack}` AS `tst`
  134. LEFT JOIN `{$this->_sTable}` AS `te` ON `tst`.`event_id`=`te`.`id`
  135. WHERE `tst`.`author_id`=? AND `tst`.`shared_id`=? AND `te`.`owner_id`=?", $iAuthorId, $iSharedId, $iOwnerId);
  136. return (int)$this->getOne($sQuery) > 0;
  137. }
  138. //--- Photo uploader related methods ---//
  139. public function saveMedia($sType, $iEventId, $iItemId)
  140. {
  141. $sTable = $this->_aTablesMedia2Events[$sType];
  142. $sQuery = $this->prepare("INSERT INTO `" . $sTable . "` SET `event_id`=?, `media_id`=?", $iEventId, $iItemId);
  143. return (int)$this->query($sQuery) > 0;
  144. }
  145. public function deleteMedia($sType, $iEventId)
  146. {
  147. $sTable = $this->_aTablesMedia2Events[$sType];
  148. $sQuery = $this->prepare("DELETE FROM `" . $sTable . "` WHERE `event_id` = ?", $iEventId);
  149. return (int)$this->query($sQuery) > 0;
  150. }
  151. public function getMedia($sType, $iEventId, $iOffset = 0)
  152. {
  153. $sTableMedia = $this->_aTablesMedia[$sType];
  154. $sTableMedia2Events = $this->_aTablesMedia2Events[$sType];
  155. $sLimitAddon = '';
  156. if($iOffset != 0)
  157. $sLimitAddon = $this->prepareAsString(" OFFSET ?", $iOffset);
  158. $sQuery = $this->prepare("SELECT
  159. `tme`.`media_id` AS `id`
  160. FROM `" . $sTableMedia2Events . "` AS `tme`
  161. LEFT JOIN `" . $sTableMedia . "` AS `tm` ON `tme`.`media_id`=`tm`.`id`
  162. WHERE `tme`.`event_id`=?" . $sLimitAddon, $iEventId);
  163. return $this->getColumn($sQuery);
  164. }
  165. //--- Link attach related methods ---//
  166. public function getUnusedLinks($iUserId, $iLinkId = 0)
  167. {
  168. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  169. $aMethod['params'][1] = array(
  170. 'profile_id' => $iUserId
  171. );
  172. $sWhereAddon = '';
  173. if(!empty($iLinkId)) {
  174. $aMethod['name'] = 'getRow';
  175. $aMethod['params'][1]['id'] = $iLinkId;
  176. $sWhereAddon = " AND `tl`.`id`=:id";
  177. }
  178. $aMethod['params'][0] = "SELECT
  179. `tl`.`id` AS `id`,
  180. `tl`.`profile_id` AS `profile_id`,
  181. `tl`.`media_id` AS `media_id`,
  182. `tl`.`url` AS `url`,
  183. `tl`.`title` AS `title`,
  184. `tl`.`text` AS `text`,
  185. `tl`.`added` AS `added`
  186. FROM `" . $this->_sPrefix . "links` AS `tl`
  187. LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`
  188. WHERE `tl`.`profile_id`=:profile_id AND ISNULL(`tle`.`event_id`)" . $sWhereAddon . "
  189. ORDER BY `tl`.`added` DESC";
  190. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);;
  191. }
  192. public function deleteUnusedLinks($iUserId, $iLinkId = 0)
  193. {
  194. $aBindings = array(
  195. 'profile_id' => $iUserId
  196. );
  197. $sWhereAddon = '';
  198. if(!empty($iLinkId)) {
  199. $aBindings['id'] = $iLinkId;
  200. $sWhereAddon = " AND `id`=:id";
  201. }
  202. return $this->query("DELETE FROM `" . $this->_sPrefix . "links` WHERE `profile_id`=:profile_id" . $sWhereAddon, $aBindings);
  203. }
  204. public function saveLink($iEventId, $iLinkId)
  205. {
  206. $sQuery = $this->prepare("INSERT INTO `" . $this->_sPrefix . "links2events` SET `event_id`=?, `link_id`=?", $iEventId, $iLinkId);
  207. return (int)$this->query($sQuery) > 0;
  208. }
  209. public function deleteLinks($iEventId)
  210. {
  211. $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);
  212. return (int)$this->query($sQuery) > 0;
  213. }
  214. public function getLinks($iEventId)
  215. {
  216. $sQuery = $this->prepare("SELECT
  217. `tl`.`id` AS `id`,
  218. `tl`.`profile_id` AS `profile_id`,
  219. `tl`.`media_id` AS `media_id`,
  220. `tl`.`url` AS `url`,
  221. `tl`.`title` AS `title`,
  222. `tl`.`text` AS `text`,
  223. `tl`.`added` AS `added`
  224. FROM `" . $this->_sPrefix . "links` AS `tl`
  225. LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`
  226. WHERE `tle`.`event_id`=?", $iEventId);
  227. return $this->getAll($sQuery);
  228. }
  229. protected function _getFilterAddon($iOwnerId, $sFilter)
  230. {
  231. switch($sFilter) {
  232. case BX_TIMELINE_FILTER_OWNER:
  233. $sFilterAddon = $this->prepareAsString(" AND `{$this->_sTable}`.`action`='' AND `{$this->_sTable}`.`object_id`=? ", $iOwnerId);
  234. break;
  235. case BX_TIMELINE_FILTER_OTHER:
  236. $sFilterAddon = $this->prepareAsString(" AND `{$this->_sTable}`.`action`='' AND `{$this->_sTable}`.`object_id`<>? ", $iOwnerId);
  237. break;
  238. case BX_TIMELINE_FILTER_ALL:
  239. default:
  240. $sFilterAddon = "";
  241. }
  242. return $sFilterAddon;
  243. }
  244. protected function _getSqlPartsEvents($aParams)
  245. {
  246. $sMethod = 'getAll';
  247. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  248. switch($aParams['browse']) {
  249. case 'owner_id':
  250. $sWhereClause = $this->prepareAsString("AND `{$this->_sTable}`.`owner_id`=? ", $aParams['value']);
  251. break;
  252. case 'common_by_object':
  253. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  254. $sWhereClause = $this->prepareAsString("AND SUBSTRING(`{$this->_sTable}`.`type`, 1, " . strlen($sCommonPostPrefix) . ")='" . $sCommonPostPrefix . "' AND `{$this->_sTable}`.`object_id`=? ", $aParams['value']);
  255. break;
  256. case 'descriptor':
  257. $sMethod = 'getRow';
  258. $sWhereClause = "";
  259. if(isset($aParams['type']))
  260. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`type`=? ", $aParams['type']);
  261. if(isset($aParams['action']))
  262. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`action`=? ", $aParams['action']);
  263. if(isset($aParams['object_id']))
  264. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`object_id`=? ", $aParams['object_id']);
  265. $sLimitClause = "LIMIT 1";
  266. break;
  267. case 'shared_by_descriptor':
  268. $sWhereClause = "";
  269. if(isset($aParams['type']))
  270. $sWhereClause .= "AND `{$this->_sTable}`.`content` LIKE " . $this->escape('%' . $aParams['type'] . '%');
  271. if(isset($aParams['action']))
  272. $sWhereClause .= "AND `{$this->_sTable}`.`content` LIKE " . $this->escape('%' . $aParams['action'] . '%');
  273. break;
  274. default:
  275. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = parent::_getSqlPartsEvents($aParams);
  276. }
  277. $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`, ";
  278. if($aParams['browse'] == 'list')
  279. $sOrderClause = "ORDER BY `{$this->_sTable}`.`pinned` DESC, `{$this->_sTable}`.`date` DESC";
  280. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  281. }
  282. protected function _getSqlPartsEventsList($aParams)
  283. {
  284. $sJoinClause = "";
  285. $sWhereClause = "AND `{$this->_sTable}`.`hidden`='0' ";
  286. if(isset($aParams['active']))
  287. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`active`=? ", (int)$aParams['active']);
  288. //--- Apply filter
  289. if(isset($aParams['filter']))
  290. $sWhereClause .= $this->_getFilterAddon($aParams['owner_id'], $aParams['filter']);
  291. //--- Apply timeline
  292. if(isset($aParams['timeline']) && !empty($aParams['timeline'])) {
  293. $iYear = (int)$aParams['timeline'];
  294. $sWhereClause .= $this->prepareAsString("AND `date`<=? ", mktime(23, 59, 59, 12, 31, $iYear));
  295. }
  296. //--- Apply modules or handlers filter
  297. $sWhereModuleFilter = '';
  298. if(!empty($aParams['modules']) && is_array($aParams['modules']))
  299. $sWhereModuleFilter = "AND `" . $this->_sTable . "`.`type` IN (" . $this->implode_escape($aParams['modules']) . ") ";
  300. if($sWhereModuleFilter == '') {
  301. $aHidden = $this->_oConfig->getHandlersHidden();
  302. $sWhereModuleFilter = !empty($aHidden) && is_array($aHidden) ? "AND `" . $this->_sTableHandlers . "`.`id` NOT IN (" . $this->implode_escape($aHidden) . ") " : "";
  303. }
  304. if($sWhereModuleFilter != '')
  305. $sWhereClause .= $sWhereModuleFilter;
  306. //--- Apply privacy filter
  307. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'));
  308. $aQueryParts = $oPrivacy->getContentByGroupAsSQLPart(BX_DOL_PG_ALL);
  309. $sWhereClause .= $aQueryParts['where'] . " ";
  310. //--- Check type
  311. switch($aParams['type']) {
  312. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  313. if(empty($aParams['owner_id']))
  314. break;
  315. $sWhereClause .= $this->prepareAsString("AND `{$this->_sTable}`.`owner_id`=? ", $aParams['owner_id']);
  316. break;
  317. case BX_BASE_MOD_NTFS_TYPE_CONNECTIONS:
  318. if(empty($aParams['owner_id']))
  319. break;
  320. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  321. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sPrefix . "events", 'owner_id', $aParams['owner_id']);
  322. $sJoinClause .= ' ' . $aQueryParts['join'];
  323. $iUserId = bx_get_logged_profile_id();
  324. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  325. $sWhereClause .= $this->prepareAsString("AND IF(SUBSTRING(`{$this->_sTable}`.`type`, 1, " . strlen($sCommonPostPrefix) . ") = '" . $sCommonPostPrefix . "', `{$this->_sTable}`.`object_id` <> ?, 1) ", $iUserId);
  326. break;
  327. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  328. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  329. $sWhereClause .= $this->prepareAsString("AND (SUBSTRING(`{$this->_sTable}`.`type`, 1, " . strlen($sCommonPostPrefix) . ") <> '" . $sCommonPostPrefix . "' OR `{$this->_sTable}`.`owner_id`=?) ", 0);
  330. break;
  331. }
  332. return array($sJoinClause, $sWhereClause);
  333. }
  334. function updateSimilarObject($iId, &$oAlert, $sDuration = 'day')
  335. {
  336. $sType = $oAlert->sUnit;
  337. $sAction = $oAlert->sAction;
  338. //Check handler
  339. $aHandler = $this->_oConfig->getHandlers($sType . '_' . $sAction);
  340. if(empty($aHandler) || !is_array($aHandler) || (int)$aHandler['groupable'] != 1)
  341. return false;
  342. //Check content's extra values
  343. if(isset($aHandler['group_by']) && !empty($aHandler['group_by']) && (!isset($oAlert->aExtras[$aHandler['group_by']]) || empty($oAlert->aExtras[$aHandler['group_by']])))
  344. return false;
  345. $aBindings = array(
  346. 'object_id' => $oAlert->iObject,
  347. 'id' => $iId,
  348. 'owner_id' => $oAlert->iSender,
  349. 'type' => $sType,
  350. 'action' => $sAction
  351. );
  352. $sWhereClause = "";
  353. switch($sDuration) {
  354. case 'day':
  355. $aBindings['day_start'] = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  356. $aBindings['day_end'] = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
  357. $sWhereClause .= "AND `date`>:day_start AND `date`<:day_end ";
  358. break;
  359. }
  360. if(isset($aHandler['group_by'])) {
  361. $aBindings['content'] = '%' . $oAlert->aExtras[$aHandler['group_by']] . '%';
  362. $sWhereClause .= "AND `content` LIKE :content ";
  363. }
  364. $sSql = "UPDATE `{$this->_sTable}`
  365. SET
  366. `object_id`=CONCAT(`object_id`, ',:object_id'),
  367. `title`='',
  368. `description`='',
  369. `date`=UNIX_TIMESTAMP()
  370. WHERE
  371. `id`<>:id AND
  372. `owner_id`=:owner_id AND
  373. `type`=:type AND
  374. `action`=:action " . $sWhereClause;
  375. $mixedResult = $this->query($sSql, $aBindings);
  376. if((int)$mixedResult > 0)
  377. $this->deleteEvent(array('id' => $iId));
  378. return $mixedResult;
  379. }
  380. }
  381. /** @} */