BxTimelineResponse.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 BxTimelineResponse extends BxBaseModNotificationsResponse
  12. {
  13. public function __construct()
  14. {
  15. $this->_sModule = 'bx_timeline';
  16. parent::__construct();
  17. }
  18. /**
  19. * Overwtire the method of parent class.
  20. *
  21. * @param BxDolAlerts $oAlert an instance of alert.
  22. */
  23. public function response($oAlert)
  24. {
  25. bx_alert($this->_sModule, 'before_register_alert', 0, 0, array(
  26. 'unit' => $oAlert->sUnit,
  27. 'action' => $oAlert->sAction,
  28. 'alert' => &$oAlert,
  29. ));
  30. $CNF = &$this->_oModule->_oConfig->CNF;
  31. $sMethod = '_process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  32. if(method_exists($this, $sMethod))
  33. return $this->$sMethod($oAlert);
  34. $iObjectAuthorId = $this->_getObjectOwnerId($oAlert->aExtras);
  35. $iObjectPrivacyView = $this->_getObjectPrivacyView($oAlert->aExtras);
  36. if($iObjectPrivacyView == BX_DOL_PG_HIDDEN)
  37. return;
  38. $aHandler = $this->_oModule->_oConfig->getHandlers($oAlert->sUnit . '_' . $oAlert->sAction);
  39. if(empty($aHandler) || !is_array($aHandler))
  40. return;
  41. $iSilentMode = $this->_oModule->getSilentMode($oAlert->aExtras);
  42. if(in_array($iSilentMode, array(BX_BASE_MOD_NTFS_SLTMODE_ABSOLUTE, BX_TIMELINE_SLTMODE_ABSOLUTE)))
  43. return;
  44. switch($aHandler['type']) {
  45. case BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT:
  46. $iOwnerId = abs($oAlert->iSender);
  47. if($iObjectPrivacyView < 0)
  48. $iOwnerId = abs($iObjectPrivacyView);
  49. $sContent = '';
  50. if(!empty($oAlert->aExtras) && is_array($oAlert->aExtras))
  51. $sContent = serialize(bx_process_input($oAlert->aExtras));
  52. $sSource = '';
  53. if(!empty($oAlert->aExtras['timeline_group']))
  54. $sSource = $oAlert->aExtras['timeline_group']['by'];
  55. else
  56. $sSource = $oAlert->sUnit . '_' . abs($iObjectAuthorId) . '_' . $oAlert->iObject;
  57. $iDate = time();
  58. $aEvent = [
  59. 'owner_id' => $iOwnerId,
  60. 'type' => $oAlert->sUnit,
  61. 'action' => $oAlert->sAction,
  62. 'object_id' => $oAlert->iObject,
  63. 'object_owner_id' => $iObjectAuthorId,
  64. 'object_privacy_view' => $iObjectPrivacyView,
  65. 'object_cf' => $this->_getObjectCf($oAlert->aExtras),
  66. 'content' => $sContent,
  67. 'source' => $sSource,
  68. 'title' => '',
  69. 'description' => '',
  70. 'date' => $iDate,
  71. 'reacted' => $iDate,
  72. 'status' => $this->_getStatus($oAlert->aExtras),
  73. 'status_admin' => $this->_getStatus($oAlert->aExtras, 'status_admin'),
  74. ];
  75. $sMethod = '_prepareEvent' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  76. if(method_exists($this, $sMethod))
  77. $this->$sMethod($oAlert, $aEvent);
  78. $sMethod = 'get_timeline_insert_data';
  79. if(bx_is_srv($oAlert->sUnit, $sMethod))
  80. $aEvent = bx_srv($oAlert->sUnit, $sMethod, [$oAlert, $aHandler, $aEvent]);
  81. bx_alert($this->_sModule, 'before_insert_data', 0, 0, [
  82. 'unit' => $oAlert->sUnit,
  83. 'action' => $oAlert->sAction,
  84. 'alert' => $oAlert,
  85. 'data' => &$aEvent,
  86. 'data_ref' => &$aEvent,
  87. ]);
  88. $iId = $this->_oModule->_oDb->insertEvent($aEvent);
  89. if(!empty($iId))
  90. $this->_oModule->onPost($iId);
  91. break;
  92. case BX_BASE_MOD_NTFS_HANDLER_TYPE_UPDATE:
  93. $sMethod = '_getEventUpdate' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  94. if(!method_exists($this, $sMethod))
  95. $sMethod = '_getEventUpdate';
  96. $aEvent = $this->$sMethod($oAlert, $aHandler);
  97. if(empty($aEvent) || !is_array($aEvent))
  98. break;
  99. $aContent = [];
  100. if(!empty($aEvent['content']) && is_string($aEvent['content']))
  101. $aContent = unserialize($aEvent['content']);
  102. if(!empty($oAlert->aExtras) && is_array($oAlert->aExtras))
  103. $aContent = array_merge($aContent, bx_process_input($oAlert->aExtras));
  104. $aParamsSet = ['content' => serialize($aContent)];
  105. $aParamsSetBySource = [];
  106. if($iObjectAuthorId != $aEvent['object_owner_id']) {
  107. $aParamsSet['object_owner_id'] = $iObjectAuthorId;
  108. $aParamsSetBySource['object_owner_id'] = $iObjectAuthorId;
  109. }
  110. if($iObjectPrivacyView > 0)
  111. $aParamsSet = array_merge($aParamsSet, [
  112. 'owner_id' => $iObjectAuthorId,
  113. 'object_privacy_view' => $iObjectPrivacyView
  114. ]);
  115. else if($iObjectPrivacyView < 0)
  116. $aParamsSet = array_merge($aParamsSet, [
  117. 'owner_id' => abs($iObjectPrivacyView),
  118. 'object_privacy_view' => $iObjectPrivacyView
  119. ]);
  120. $bSource = false;
  121. $sSource = $aEvent[$CNF['FIELD_SOURCE']];
  122. if(($bSource = !empty($sSource)) !== false) {
  123. $sSourceNew = '';
  124. if(!empty($oAlert->aExtras['timeline_group']))
  125. $sSourceNew = $oAlert->aExtras['timeline_group']['by'];
  126. if($sSourceNew && strcmp($sSourceNew, $sSource) != 0) {
  127. $aParamsSet['source'] = $sSourceNew;
  128. $aParamsSetBySource['source'] = $sSourceNew;
  129. }
  130. }
  131. foreach(['status', 'status_admin'] as $sStatusKey)
  132. if(!empty($aContent[$sStatusKey])) {
  133. $aParamsSet[$sStatusKey] = $aContent[$sStatusKey];
  134. $aParamsSetBySource[$sStatusKey] = $aContent[$sStatusKey];
  135. }
  136. $sMethod = 'get_timeline_update_data';
  137. if(bx_is_srv($oAlert->sUnit, $sMethod))
  138. list($aParamsSet, $aParamsSetBySource) = bx_srv($oAlert->sUnit, $sMethod, [$oAlert, $aHandler, $aEvent, [$aParamsSet, $aParamsSetBySource]]);
  139. bx_alert($this->_sModule, 'before_update_data', 0, 0, [
  140. 'unit' => $oAlert->sUnit,
  141. 'action' => $oAlert->sAction,
  142. 'alert' => $oAlert,
  143. 'data' => &$aParamsSet,
  144. 'data_by_source' => &$aParamsSetBySource,
  145. 'data_ref' => &$aParamsSet,
  146. 'data_by_source_ref' => &$aParamsSetBySource,
  147. ]);
  148. $this->_oModule->_oDb->updateEvent($aParamsSet, ['id' => $aEvent[$CNF['FIELD_ID']]]);
  149. //--- Delete item cache.
  150. $this->_oModule->deleteCacheItem($aEvent[$CNF['FIELD_ID']]);
  151. //--- Update related events.
  152. if($bSource && !empty($aParamsSetBySource)) {
  153. $aEventsBySource = $this->_oModule->_oDb->getEvents(['browse' => 'source', 'value' => $sSource]);
  154. foreach($aEventsBySource as $aEventBySource) {
  155. if($aEventBySource[$CNF['FIELD_ID']] == $aEvent[$CNF['FIELD_ID']])
  156. continue;
  157. $this->_oModule->_oDb->updateEvent($aParamsSetBySource, ['id' => $aEventBySource[$CNF['FIELD_ID']]]);
  158. //--- Delete item cache.
  159. $this->_oModule->deleteCacheItem($aEventBySource[$CNF['FIELD_ID']]);
  160. }
  161. }
  162. $this->_oModule->rebuildSlice();
  163. break;
  164. case BX_BASE_MOD_NTFS_HANDLER_TYPE_DELETE:
  165. if($oAlert->sUnit == 'profile' && $oAlert->sAction == 'delete') {
  166. $aEvents = $this->_oModule->_oDb->getEvents(array('browse' => 'owner_id', 'value' => $oAlert->iObject));
  167. foreach($aEvents as $aEvent)
  168. $this->_oModule->deleteEvent($aEvent);
  169. if(!isset($oAlert->aExtras['delete_with_content']) || !$oAlert->aExtras['delete_with_content'])
  170. break;
  171. $aEvents = $this->_oModule->_oDb->getEvents(array('browse' => 'common_by_object', 'value' => $oAlert->iObject));
  172. foreach($aEvents as $aEvent)
  173. $this->_oModule->deleteEvent($aEvent);
  174. break;
  175. }
  176. $sMethod = '_getEventDelete' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  177. if(!method_exists($this, $sMethod))
  178. $sMethod = '_getEventDelete';
  179. $aEvent = $this->$sMethod($oAlert, $aHandler);
  180. if(empty($aEvent) || !is_array($aEvent))
  181. break;
  182. $sMethod = 'get_timeline_delete_data';
  183. if(bx_is_srv($oAlert->sUnit, $sMethod))
  184. $aEvent = bx_srv($oAlert->sUnit, $sMethod, [$oAlert, $aHandler, $aEvent]);
  185. $this->_oModule->deleteEvent($aEvent);
  186. break;
  187. }
  188. }
  189. protected function _prepareEventCommentAdded($oAlert, &$aEvent)
  190. {
  191. $aEvent = array_merge($aEvent, array(
  192. 'object_id' => $oAlert->aExtras['comment_uniq_id'],
  193. 'object_owner_id' => $oAlert->aExtras['comment_author_id'],
  194. ));
  195. }
  196. protected function _getEventUpdateCommentEdited(&$oAlert, &$aHandler)
  197. {
  198. $aHandlers = $this->_oModule->_oDb->getHandlers([
  199. 'type' => 'by_group_key_type',
  200. 'group' => $aHandler['group']
  201. ]);
  202. return $this->_oModule->_oDb->getEvents([
  203. 'browse' => 'descriptor',
  204. 'type' => $oAlert->sUnit,
  205. 'action' => $aHandlers[BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT]['alert_action'],
  206. 'object_id' => $oAlert->aExtras['comment_uniq_id']
  207. ]);
  208. }
  209. protected function _getEventDelete(&$oAlert, &$aHandler)
  210. {
  211. $aHandlers = $this->_oModule->_oDb->getHandlers([
  212. 'type' => 'by_group_key_type',
  213. 'group' => $aHandler['group']
  214. ]);
  215. return $this->_oModule->_oDb->getEvents([
  216. 'browse' => 'descriptor',
  217. 'type' => $oAlert->sUnit,
  218. 'action' => $aHandlers[BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT]['alert_action'],
  219. 'object_id' => $oAlert->iObject
  220. ]);
  221. }
  222. protected function _getEventDeleteCommentDeleted(&$oAlert, &$aHandler)
  223. {
  224. $aHandlers = $this->_oModule->_oDb->getHandlers([
  225. 'type' => 'by_group_key_type',
  226. 'group' => $aHandler['group']
  227. ]);
  228. return $this->_oModule->_oDb->getEvents([
  229. 'browse' => 'descriptor',
  230. 'type' => $oAlert->sUnit,
  231. 'action' => $aHandlers[BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT]['alert_action'],
  232. 'object_id' => $oAlert->aExtras['comment_uniq_id']
  233. ]);
  234. }
  235. protected function _getStatus($aExtras, $sField = 'status')
  236. {
  237. return ($sValue = $this->_getFieldValue($aExtras, $sField)) !== false ? $sValue : 'active';
  238. }
  239. protected function _getFieldValue($aExtras, $sField)
  240. {
  241. return isset($aExtras[$sField]) ? $aExtras[$sField] : false;
  242. }
  243. protected function _processSystemClearCache($oAlert)
  244. {
  245. if(!in_array($oAlert->aExtras['type'], array('all', 'custom')))
  246. return;
  247. $this->_clearCache();
  248. }
  249. protected function _processSystemEnable($oAlert)
  250. {
  251. $aModuleConfig = $oAlert->aExtras['config'];
  252. if(empty($aModuleConfig) || !is_array($aModuleConfig))
  253. return false;
  254. $sName = $aModuleConfig['name'];
  255. if(!bx_srv_ii('system', 'is_module_context', [$sName]))
  256. return false;
  257. return $this->_oModule->serviceFeedsMenuAdd($sName);
  258. }
  259. protected function _processSystemDisable($oAlert)
  260. {
  261. $aModuleConfig = $oAlert->aExtras['config'];
  262. if(empty($aModuleConfig) || !is_array($aModuleConfig))
  263. return false;
  264. $sName = $aModuleConfig['name'];
  265. if(!bx_srv_ii('system', 'is_module_context', [$sName]))
  266. return false;
  267. return $this->_oModule->serviceFeedsMenuDelete($sName);
  268. }
  269. protected function _processSystemSaveSetting($oAlert)
  270. {
  271. $CNF = &$this->_oModule->_oConfig->CNF;
  272. if($CNF['PARAM_SEARCHABLE_FIELDS'] != $oAlert->aExtras['option'])
  273. return;
  274. $this->_oModule->_oDb->alterFulltextIndex();
  275. }
  276. protected function _processAccountConfirm($oAlert)
  277. {
  278. $this->_clearCache();
  279. }
  280. protected function _processAccountUnconfirm($oAlert)
  281. {
  282. $this->_clearCache();
  283. }
  284. protected function _processProfileApprove($oAlert)
  285. {
  286. $this->_clearCache();
  287. }
  288. protected function _processProfileDisapprove($oAlert)
  289. {
  290. $this->_clearCache();
  291. }
  292. protected function _processProfileActivate($oAlert)
  293. {
  294. $this->_clearCache();
  295. }
  296. protected function _processProfileSuspend($oAlert)
  297. {
  298. $this->_clearCache();
  299. }
  300. protected function _clearCache()
  301. {
  302. //--- Clear item cache.
  303. $this->_oModule->getCacheItemObject()->removeAllByPrefix($this->_oModule->_oConfig->getPrefix('cache_item'));
  304. }
  305. protected function _processBxTimelineVideosMp4Transcoded($oAlert)
  306. {
  307. $this->_onVideoTranscoded($oAlert->iObject, $oAlert->aExtras['ret']);
  308. }
  309. protected function _onVideoTranscoded($iMediaId, $bResult, $aParams = array())
  310. {
  311. $CNF = &$this->_oModule->_oConfig->CNF;
  312. if(!isset($CNF['FIELD_STATUS']))
  313. return;
  314. $aMedia = $this->_oModule->_oDb->getMediaById($CNF['FIELD_VIDEO'], $iMediaId);
  315. if(empty($aMedia) || !is_array($aMedia))
  316. return;
  317. $iContentId = (int)$aMedia['event_id'];
  318. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  319. if(empty($aContentInfo) || !is_array($aContentInfo))
  320. return;
  321. if(!isset($aContentInfo[$CNF['FIELD_STATUS']]) || $aContentInfo[$CNF['FIELD_STATUS']] != 'awaiting')
  322. return;
  323. $iNow = time();
  324. $bNotify = $iNow - $aContentInfo[$CNF['FIELD_ADDED']] > $this->_oModule->_oConfig->getDpnTime();
  325. $iSystemBotProfileId = (int)getParam('sys_profile_bot');
  326. $iAuthorProfileId = $aContentInfo[$CNF['FIELD_' . ((int)$aContentInfo[$CNF['FIELD_SYSTEM']] == 0 ? 'OBJECT_ID' : 'OWNER_ID')]];
  327. if(!$bResult) {
  328. if((int)$this->_oModule->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'failed'), array($CNF['FIELD_ID'] => $iContentId)) > 0) {
  329. $this->_oModule->onFailed($iContentId);
  330. if($bNotify)
  331. bx_alert($this->_sModule, 'publish_failed', $aContentInfo[$CNF['FIELD_ID']], $iSystemBotProfileId, array(
  332. 'object_author_id' => $iAuthorProfileId,
  333. 'privacy_view' => BX_DOL_PG_ALL
  334. ));
  335. }
  336. return;
  337. }
  338. if(isset($CNF['FIELD_PUBLISHED']) && isset($aContentInfo[$CNF['FIELD_PUBLISHED']]) && $aContentInfo[$CNF['FIELD_PUBLISHED']] > $iNow)
  339. return;
  340. $oTranscoder = BxDolTranscoder::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']);
  341. $aMediasToCheck = $this->_oModule->_oDb->getMedia($CNF['FIELD_VIDEO'], $iContentId, 0, true);
  342. foreach($aMediasToCheck as $aMediaToCheck)
  343. if($oTranscoder->isMimeTypeSupported($aMediaToCheck['mime_type']) && !$oTranscoder->isFileReady($aMediaToCheck['id']))
  344. return;
  345. if(!$this->_oModule->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'active'), array($CNF['FIELD_ID'] => $iContentId)))
  346. return;
  347. $this->_oModule->onPublished($iContentId);
  348. if($bNotify)
  349. bx_alert($this->_sModule, 'publish_succeeded', $aContentInfo[$CNF['FIELD_ID']], $iSystemBotProfileId, array(
  350. 'object_author_id' => $iAuthorProfileId,
  351. 'privacy_view' => BX_DOL_PG_ALL
  352. ));
  353. }
  354. }
  355. /** @} */