BxTimelineDb.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup Timeline Timeline
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxBaseModNotificationsDb');
  12. class BxTimelineDb extends BxBaseModNotificationsDb
  13. {
  14. protected $_sTablesShareTrack;
  15. /*
  16. * Constructor.
  17. */
  18. function __construct(&$oConfig)
  19. {
  20. parent::__construct($oConfig);
  21. $this->_sTableSharesTrack = $this->_sPrefix . 'shares_track';
  22. }
  23. public function getMaxDuration($aParams)
  24. {
  25. $aParams['browse'] = 'last';
  26. if(isset($aParams['timeline']))
  27. unset($aParams['timeline']);
  28. $aEvent = $this->getEvents($aParams);
  29. if(empty($aEvent) || !is_array($aEvent))
  30. return 0;
  31. $iNowYear = date('Y', time());
  32. return (int)$aEvent['year'] < $iNowYear ? (int)$aEvent['year'] : 0;
  33. }
  34. //--- Share related methods ---//
  35. public function insertShareTrack($iEventId, $iAuthorId, $sAuthorIp, $iSharedId)
  36. {
  37. $iNow = time();
  38. $iAuthorNip = ip2long($sAuthorIp);
  39. $sQuery = $this->prepare("INSERT INTO `{$this->_sTableSharesTrack}` SET `event_id` = ?, `author_id` = ?, `author_nip` = ?, `shared_id` = ?, `date` = ?", $iEventId, $iAuthorId, $iAuthorNip, $iSharedId, $iNow);
  40. return (int)$this->query($sQuery) > 0;
  41. }
  42. public function deleteShareTrack($iEventId)
  43. {
  44. $sQuery = $this->prepare("DELETE FROM `{$this->_sTableSharesTrack}` WHERE `event_id` = ?", $iEventId);
  45. return (int)$this->query($sQuery) > 0;
  46. }
  47. public function updateShareCounter($iId, $iCounter, $iIncrement = 1)
  48. {
  49. return (int)$this->updateEvent(array('shares' => (int)$iCounter + $iIncrement), array('id' => $iId)) > 0;
  50. }
  51. public function getShared($sType, $sAction, $iObjectId)
  52. {
  53. $bSystem = $this->_oConfig->isSystem($sType, $sAction);
  54. if($bSystem)
  55. $aParams = array('browse' => 'descriptor', 'type' => $sType, 'action' => $sAction, 'object_id' => $iObjectId);
  56. else
  57. $aParams = array('browse' => 'id', 'value' => $iObjectId);
  58. $aShared = $this->getEvents($aParams);
  59. if($bSystem && (empty($aShared) || !is_array($aShared))) {
  60. $iId = $this->insertEvent(array(
  61. 'owner_id' => 0,
  62. 'type' => $sType,
  63. 'action' => $sAction,
  64. 'object_id' => $iObjectId,
  65. 'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault(),
  66. 'content' => '',
  67. 'title' => '',
  68. 'description' => '',
  69. 'hidden' => 1
  70. ));
  71. $aShared = $this->getEvents(array('browse' => 'id', 'value' => $iId));
  72. }
  73. return $aShared;
  74. }
  75. function getSharedBy($iSharedId)
  76. {
  77. $sQuery = $this->prepare("SELECT `author_id` FROM `{$this->_sTableSharesTrack}` WHERE `shared_id`=?", $iSharedId);
  78. return $this->getColumn($sQuery);
  79. }
  80. //--- Photo uploader related methods ---//
  81. public function savePhoto($iEventId, $iPhId)
  82. {
  83. $sQuery = $this->prepare("INSERT INTO `" . $this->_sPrefix . "photos2events` SET `event_id`=?, `photo_id`=?", $iEventId, $iPhId);
  84. return (int)$this->query($sQuery) > 0;
  85. }
  86. public function deletePhotos($iEventId)
  87. {
  88. $sQuery = $this->prepare("DELETE FROM `" . $this->_sPrefix . "photos2events` WHERE `event_id` = ?", $iEventId);
  89. return (int)$this->query($sQuery) > 0;
  90. }
  91. public function getPhotos($iEventId, $iOffset = 0)
  92. {
  93. $sLimitAddon = '';
  94. if($iOffset != 0)
  95. $sLimitAddon = $this->prepare(" OFFSET ?", $iOffset);
  96. $sQuery = $this->prepare("SELECT
  97. `tpe`.`photo_id` AS `id`
  98. FROM `" . $this->_sPrefix . "photos2events` AS `tpe`
  99. LEFT JOIN `" . $this->_sPrefix . "photos` AS `tp` ON `tpe`.`photo_id` = `tp`.`id`
  100. WHERE `tpe`.`event_id` = ?" . $sLimitAddon, $iEventId);
  101. return $this->getColumn($sQuery);
  102. }
  103. //--- Link attach related methods ---//
  104. public function getUnusedLinks($iUserId, $iLinkId = 0)
  105. {
  106. $sMethod = 'getAll';
  107. $sWhereAddon = '';
  108. if(!empty($iLinkId)) {
  109. $sMethod = 'getRow';
  110. $sWhereAddon = $this->prepare(" AND `tl`.`id`=?", $iLinkId);
  111. }
  112. $sQuery = $this->prepare("SELECT
  113. `tl`.`id` AS `id`,
  114. `tl`.`profile_id` AS `profile_id`,
  115. `tl`.`url` AS `url`,
  116. `tl`.`title` AS `title`,
  117. `tl`.`text` AS `text`,
  118. `tl`.`added` AS `added`
  119. FROM `" . $this->_sPrefix . "links` AS `tl`
  120. LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`
  121. WHERE `tl`.`profile_id`=? AND ISNULL(`tle`.`event_id`)" . $sWhereAddon . "
  122. ORDER BY `tl`.`added` DESC", $iUserId);
  123. return $this->$sMethod($sQuery);
  124. }
  125. public function deleteUnusedLinks($iUserId, $iLinkId = 0)
  126. {
  127. $sWhereAddon = '';
  128. if(!empty($iLinkId))
  129. $sWhereAddon = $this->prepare(" AND `id`=?", $iLinkId);
  130. $sQuery = $this->prepare("DELETE FROM `" . $this->_sPrefix . "links` WHERE `profile_id`=?" . $sWhereAddon, $iUserId);
  131. return $this->query($sQuery);
  132. }
  133. public function saveLink($iEventId, $iLinkId)
  134. {
  135. $sQuery = $this->prepare("INSERT INTO `" . $this->_sPrefix . "links2events` SET `event_id`=?, `link_id`=?", $iEventId, $iLinkId);
  136. return (int)$this->query($sQuery) > 0;
  137. }
  138. public function deleteLinks($iEventId)
  139. {
  140. $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);
  141. return (int)$this->query($sQuery) > 0;
  142. }
  143. public function getLinks($iEventId)
  144. {
  145. $sQuery = $this->prepare("SELECT
  146. `tl`.`id` AS `id`,
  147. `tl`.`profile_id` AS `profile_id`,
  148. `tl`.`url` AS `url`,
  149. `tl`.`title` AS `title`,
  150. `tl`.`text` AS `text`,
  151. `tl`.`added` AS `added`
  152. FROM `" . $this->_sPrefix . "links` AS `tl`
  153. LEFT JOIN `" . $this->_sPrefix . "links2events` AS `tle` ON `tl`.`id`=`tle`.`link_id`
  154. WHERE `tle`.`event_id`=?", $iEventId);
  155. return $this->getAll($sQuery);
  156. }
  157. protected function _getFilterAddon($iOwnerId, $sFilter)
  158. {
  159. switch($sFilter) {
  160. case BX_TIMELINE_FILTER_OWNER:
  161. $sFilterAddon = " AND `{$this->_sTable}`.`action`='' AND `{$this->_sTable}`.`object_id`='" . $iOwnerId . "' ";
  162. break;
  163. case BX_TIMELINE_FILTER_OTHER:
  164. $sFilterAddon = " AND `{$this->_sTable}`.`action`='' AND `{$this->_sTable}`.`object_id`<>'" . $iOwnerId . "' ";
  165. break;
  166. case BX_TIMELINE_FILTER_ALL:
  167. default:
  168. $sFilterAddon = "";
  169. }
  170. return $sFilterAddon;
  171. }
  172. protected function _getSqlPartsEvents($aParams)
  173. {
  174. $sMethod = 'getAll';
  175. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  176. switch($aParams['browse']) {
  177. case 'owner_id':
  178. $sWhereClause = $this->prepare("AND `{$this->_sTable}`.`owner_id`=? ", $aParams['value']);
  179. break;
  180. case 'common_by_object':
  181. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  182. $sWhereClause = $this->prepare("AND SUBSTRING(`{$this->_sTable}`.`type`, 1, " . strlen($sCommonPostPrefix) . ")='" . $sCommonPostPrefix . "' AND `{$this->_sTable}`.`object_id`=? ", $aParams['value']);
  183. break;
  184. case 'descriptor':
  185. $sMethod = 'getRow';
  186. $sWhereClause = "";
  187. if(isset($aParams['type']))
  188. $sWhereClause .= $this->prepare("AND `{$this->_sTable}`.`type`=? ", $aParams['type']);
  189. if(isset($aParams['action']))
  190. $sWhereClause .= $this->prepare("AND `{$this->_sTable}`.`action`=? ", $aParams['action']);
  191. if(isset($aParams['object_id']))
  192. $sWhereClause .= $this->prepare("AND `{$this->_sTable}`.`object_id`=? ", $aParams['object_id']);
  193. $sLimitClause = "LIMIT 1";
  194. break;
  195. case 'shared_by_descriptor':
  196. $sWhereClause = "";
  197. if(isset($aParams['type']))
  198. $sWhereClause .= "AND `{$this->_sTable}`.`content` LIKE '%" . $this->escape($aParams['type']) . "%'";
  199. if(isset($aParams['action']))
  200. $sWhereClause .= "AND `{$this->_sTable}`.`content` LIKE '%" . $this->escape($aParams['action']) . "%'";
  201. break;
  202. default:
  203. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = parent::_getSqlPartsEvents($aParams);
  204. }
  205. $sSelectClause .= "YEAR(FROM_UNIXTIME(`{$this->_sTable}`.`date`)) AS `year`, ";
  206. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  207. }
  208. protected function _getSqlPartsEventsList($aParams)
  209. {
  210. $sJoinClause = $sWhereClause = "";
  211. if(isset($aParams['active']))
  212. $sWhereClause .= $this->prepare("AND `{$this->_sTable}`.`active`=? ", (int)$aParams['active']);
  213. //--- Apply filter
  214. if(isset($aParams['filter']))
  215. $sWhereClause .= $this->_getFilterAddon($aParams['owner_id'], $aParams['filter']);
  216. //--- Apply timeline
  217. if(isset($aParams['timeline']) && !empty($aParams['timeline'])) {
  218. $iYear = (int)$aParams['timeline'];
  219. $sWhereClause .= $this->prepare("AND `date`<=? ", mktime(23, 59, 59, 12, 31, $iYear));
  220. }
  221. //--- Apply modules or handlers filter
  222. $sWhereModuleFilter = '';
  223. if(!empty($aParams['modules']) && is_array($aParams['modules']))
  224. $sWhereModuleFilter = "AND `" . $this->_sTable . "`.`type` IN (" . $this->implode_escape($aParams['modules']) . ") ";
  225. if($sWhereModuleFilter == '') {
  226. $aHidden = $this->_oConfig->getHandlersHidden();
  227. $sWhereModuleFilter = !empty($aHidden) && is_array($aHidden) ? "AND `" . $this->_sTableHandlers . "`.`id` NOT IN (" . $this->implode_escape($aHidden) . ") " : "";
  228. }
  229. if($sWhereModuleFilter != '')
  230. $sWhereClause .= $sWhereModuleFilter;
  231. //--- Check type
  232. switch($aParams['type']) {
  233. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  234. if(empty($aParams['owner_id']))
  235. break;
  236. $sWhereClause .= $this->prepare("AND `{$this->_sTable}`.`owner_id`=? ", $aParams['owner_id']);
  237. break;
  238. case BX_BASE_MOD_NTFS_TYPE_CONNECTIONS:
  239. if(empty($aParams['owner_id']))
  240. break;
  241. bx_import('BxDolConnection');
  242. $oConnection = BxDolConnection::getObjectInstance($this->_oConfig->getObject('conn_subscriptions'));
  243. $aQueryParts = $oConnection->getConnectedContentAsSQLParts($this->_sPrefix . "events", 'owner_id', $aParams['owner_id']);
  244. $sJoinClause .= ' ' . $aQueryParts['join'];
  245. $iUserId = bx_get_logged_profile_id();
  246. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  247. $sWhereClause .= "AND IF(SUBSTRING(`{$this->_sTable}`.`type`, 1, " . strlen($sCommonPostPrefix) . ") = '" . $sCommonPostPrefix . "', `{$this->_sTable}`.`object_id` <> " . $iUserId . ", 1) ";
  248. break;
  249. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  250. $sCommonPostPrefix = $this->_oConfig->getPrefix('common_post');
  251. $sWhereClause .= "AND SUBSTRING(`{$this->_sTable}`.`type`, 1, " . strlen($sCommonPostPrefix) . ") <> '" . $sCommonPostPrefix . "' ";
  252. break;
  253. }
  254. return array($sJoinClause, $sWhereClause);
  255. }
  256. function updateSimilarObject($iId, &$oAlert, $sDuration = 'day')
  257. {
  258. $sType = $oAlert->sUnit;
  259. $sAction = $oAlert->sAction;
  260. //Check handler
  261. $aHandler = $this->_oConfig->getHandlers($sType . '_' . $sAction);
  262. if(empty($aHandler) || !is_array($aHandler) || (int)$aHandler['groupable'] != 1)
  263. return false;
  264. //Check content's extra values
  265. if(isset($aHandler['group_by']) && !empty($aHandler['group_by']) && (!isset($oAlert->aExtras[$aHandler['group_by']]) || empty($oAlert->aExtras[$aHandler['group_by']])))
  266. return false;
  267. $sWhereClause = "";
  268. switch($sDuration) {
  269. case 'day':
  270. $iDayStart = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
  271. $iDayEnd = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
  272. $sWhereClause .= "AND `date`>" . $iDayStart . " AND `date`<" . $iDayEnd . " ";
  273. break;
  274. }
  275. if(isset($aHandler['group_by']))
  276. $sWhereClause .= "AND `content` LIKE '%" . $oAlert->aExtras[$aHandler['group_by']] . "%' ";
  277. $sSql = "UPDATE `{$this->_sTable}`
  278. SET
  279. `object_id`=CONCAT(`object_id`, '," . $oAlert->iObject . "'),
  280. `title`='',
  281. `description`='',
  282. `date`=UNIX_TIMESTAMP()
  283. WHERE
  284. `id`<>'" . $iId . "' AND
  285. `owner_id`='" . $oAlert->iSender . "' AND
  286. `type`='" . $sType . "' AND
  287. `action`='" . $sAction . "' " . $sWhereClause;
  288. $mixedResult = $this->query($sSql);
  289. if((int)$mixedResult > 0)
  290. $this->deleteEvent(array('id' => $iId));
  291. return $mixedResult;
  292. }
  293. }
  294. /** @} */