BxTimelineResponse.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. parent::__construct();
  16. $this->_oModule = BxDolModule::getInstance('bx_timeline');
  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. $CNF = &$this->_oModule->_oConfig->CNF;
  26. $sMethod = '_process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  27. if(method_exists($this, $sMethod))
  28. return $this->$sMethod($oAlert);
  29. $iObjectAuthorId = $this->_getObjectOwnerId($oAlert->aExtras);
  30. $iObjectPrivacyView = $this->_getObjectPrivacyView($oAlert->aExtras);
  31. if($iObjectPrivacyView == BX_DOL_PG_HIDDEN)
  32. return;
  33. $aHandler = $this->_oModule->_oConfig->getHandlers($oAlert->sUnit . '_' . $oAlert->sAction);
  34. switch($aHandler['type']) {
  35. case BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT:
  36. $iOwnerId = abs($oAlert->iSender);
  37. if($iObjectPrivacyView < 0)
  38. $iOwnerId = abs($iObjectPrivacyView);
  39. $sContent = !empty($oAlert->aExtras) && is_array($oAlert->aExtras) ? serialize(bx_process_input($oAlert->aExtras)) : '';
  40. $iId = $this->_oModule->_oDb->insertEvent(array(
  41. 'owner_id' => $iOwnerId,
  42. 'type' => $oAlert->sUnit,
  43. 'action' => $oAlert->sAction,
  44. 'object_id' => $oAlert->iObject,
  45. 'object_privacy_view' => $iObjectPrivacyView,
  46. 'content' => $sContent,
  47. 'title' => '',
  48. 'description' => ''
  49. ));
  50. if(!empty($iId))
  51. $this->_oModule->onPost($iId);
  52. break;
  53. case BX_BASE_MOD_NTFS_HANDLER_TYPE_UPDATE:
  54. $aEvent = $this->_oModule->_oDb->getEvents(array('browse' => 'descriptor', 'type' => $oAlert->sUnit, 'object_id' => $oAlert->iObject));
  55. if(empty($aEvent) || !is_array($aEvent))
  56. break;
  57. $aParamsSet = array(
  58. 'content' => !empty($oAlert->aExtras) && is_array($oAlert->aExtras) ? serialize(bx_process_input($oAlert->aExtras)) : ''
  59. );
  60. if($iObjectPrivacyView > 0 && !empty($iObjectAuthorId) && $iObjectAuthorId == $oAlert->iSender)
  61. $aParamsSet = array_merge($aParamsSet, array(
  62. 'owner_id' => $oAlert->iSender,
  63. 'object_privacy_view' => $iObjectPrivacyView
  64. ));
  65. else if($iObjectPrivacyView < 0)
  66. $aParamsSet = array_merge($aParamsSet, array(
  67. 'owner_id' => abs($iObjectPrivacyView),
  68. 'object_privacy_view' => $iObjectPrivacyView
  69. ));
  70. $this->_oModule->_oDb->updateEvent($aParamsSet, array('type' => $oAlert->sUnit, 'object_id' => $oAlert->iObject));
  71. //--- Delete feed cached
  72. $this->_oModule->_oDb->deleteCache(array('context_id' => 0)); //--- Delete cache for Public feed
  73. $this->_oModule->_oDb->deleteCache(array('context_id' => $aEvent[$CNF['FIELD_OWNER_ID']])); //--- Delete cache for old context
  74. if(isset($aParamsSet['owner_id']))
  75. $this->_oModule->_oDb->deleteCache(array('context_id' => $aParamsSet['owner_id'])); //--- Delete cache for new context
  76. //--- Delete item cache.
  77. $sCacheItemKey = $this->_oModule->_oConfig->getCacheItemKey($aEvent['id']);
  78. $this->_oModule->getCacheItemObject()->delData($sCacheItemKey);
  79. break;
  80. case BX_BASE_MOD_NTFS_HANDLER_TYPE_DELETE:
  81. if($oAlert->sUnit == 'profile' && $oAlert->sAction == 'delete') {
  82. $aEvents = $this->_oModule->_oDb->getEvents(array('browse' => 'owner_id', 'value' => $oAlert->iObject));
  83. foreach($aEvents as $aEvent)
  84. $this->_oModule->deleteEvent($aEvent);
  85. if(isset($oAlert->aExtras['delete_with_content']) && $oAlert->aExtras['delete_with_content']) {
  86. $aEvents = $this->_oModule->_oDb->getEvents(array('browse' => 'common_by_object', 'value' => $oAlert->iObject));
  87. foreach($aEvents as $aEvent)
  88. $this->_oModule->deleteEvent($aEvent);
  89. }
  90. //--- Delete cached
  91. $this->_oModule->_oDb->deleteCache(array('context_id' => $oAlert->iObject));
  92. $this->_oModule->_oDb->deleteCache(array('profile_id' => $oAlert->iObject));
  93. break;
  94. }
  95. $aHandlers = $this->_oModule->_oDb->getHandlers(array('type' => 'by_group_key_type', 'group' => $aHandler['group']));
  96. $aEvent = $this->_oModule->_oDb->getEvents(array(
  97. 'browse' => 'descriptor',
  98. 'type' => $oAlert->sUnit,
  99. 'action' => $aHandlers[BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT]['alert_action'],
  100. 'object_id' => $oAlert->iObject
  101. ));
  102. if(empty($aEvent) || !is_array($aEvent))
  103. break;
  104. $this->_oModule->deleteEvent($aEvent);
  105. break;
  106. }
  107. }
  108. protected function _processSystemClearCache($oAlert)
  109. {
  110. if(!in_array($oAlert->aExtras['type'], array('all', 'custom')))
  111. return;
  112. //--- Clear item cache.
  113. $this->_oModule->getCacheItemObject()->removeAllByPrefix($this->_oModule->_oConfig->getPrefix('cache_item'));
  114. //--- Clear feed cache.
  115. $this->_oModule->_oDb->clearCache();
  116. }
  117. protected function _processBxTimelineVideosMp4Transcoded($oAlert)
  118. {
  119. $this->_onVideoTranscoded($oAlert->iObject, $oAlert->aExtras['ret']);
  120. }
  121. protected function _onVideoTranscoded($iMediaId, $bResult, $aParams = array())
  122. {
  123. $CNF = &$this->_oModule->_oConfig->CNF;
  124. if(!isset($CNF['FIELD_STATUS']))
  125. return;
  126. $aMedia = $this->_oModule->_oDb->getMediaById(BX_TIMELINE_MEDIA_VIDEO, $iMediaId);
  127. if(empty($aMedia) || !is_array($aMedia))
  128. return;
  129. $iContentId = (int)$aMedia['event_id'];
  130. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  131. if(empty($aContentInfo) || !is_array($aContentInfo))
  132. return;
  133. if(!isset($aContentInfo[$CNF['FIELD_STATUS']]) || $aContentInfo[$CNF['FIELD_STATUS']] != 'awaiting')
  134. return;
  135. $iNow = time();
  136. $bNotify = $iNow - $aContentInfo[$CNF['FIELD_ADDED']] > $this->_oModule->_oConfig->getDpnTime();
  137. $iSystemBotProfileId = (int)getParam('sys_profile_bot');
  138. $iAuthorProfileId = $aContentInfo[$CNF['FIELD_' . ((int)$aContentInfo[$CNF['FIELD_SYSTEM']] == 0 ? 'OBJECT_ID' : 'OWNER_ID')]];
  139. if(!$bResult) {
  140. if((int)$this->_oModule->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'failed'), array($CNF['FIELD_ID'] => $iContentId)) > 0) {
  141. $this->_oModule->onFailed($iContentId);
  142. if($bNotify)
  143. bx_alert($this->_oModule->getName(), 'publish_failed', $aContentInfo[$CNF['FIELD_ID']], $iSystemBotProfileId, array(
  144. 'object_author_id' => $iAuthorProfileId,
  145. 'privacy_view' => BX_DOL_PG_ALL
  146. ));
  147. }
  148. return;
  149. }
  150. if(isset($CNF['FIELD_PUBLISHED']) && isset($aContentInfo[$CNF['FIELD_PUBLISHED']]) && $aContentInfo[$CNF['FIELD_PUBLISHED']] > $iNow)
  151. return;
  152. $oTranscoder = BxDolTranscoder::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']);
  153. $aMediasToCheck = $this->_oModule->_oDb->getMedia(BX_TIMELINE_MEDIA_VIDEO, $iContentId);
  154. foreach($aMediasToCheck as $iMediaToCheck)
  155. if(!$oTranscoder->isFileReady($iMediaToCheck))
  156. return;
  157. if(!$this->_oModule->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'active'), array($CNF['FIELD_ID'] => $iContentId)))
  158. return;
  159. $this->_oModule->onPublished($iContentId);
  160. if($bNotify)
  161. bx_alert($this->_oModule->getName(), 'publish_succeeded', $aContentInfo[$CNF['FIELD_ID']], $iSystemBotProfileId, array(
  162. 'object_author_id' => $iAuthorProfileId,
  163. 'privacy_view' => BX_DOL_PG_ALL
  164. ));
  165. }
  166. }
  167. /** @} */