BxStoriesModule.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 Stories Stories
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolAcl');
  12. /**
  13. * Stories module
  14. */
  15. class BxStoriesModule extends BxBaseModTextModule
  16. {
  17. protected $_aContexts = array('popular', 'public', 'author');
  18. function __construct(&$aModule)
  19. {
  20. parent::__construct($aModule);
  21. }
  22. public function actionEmbed($iContentId, $sUnitTemplate = '', $sAddCode = '')
  23. {
  24. return $this->_oTemplate->getJsCode('main') . parent::actionEmbed($iContentId);
  25. }
  26. public function actionPlay($iContentId)
  27. {
  28. $CNF = &$this->_oConfig->CNF;
  29. $sJsObject = $this->_oConfig->getJsObject('main');
  30. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  31. if(($sMsg = $this->checkAllowedEdit($aContentInfo)) !== CHECK_ACTION_RESULT_ALLOWED)
  32. return echoJson(['msg' => $sMsg]);
  33. return echoJson(['popup' => [
  34. 'html' => $this->_oTemplate->entryPlay($aContentInfo),
  35. 'options' => [
  36. 'closeOnOuterClick' => true,
  37. 'removeOnClose' => true,
  38. 'onShow' => $sJsObject . '.playInit()',
  39. 'onHide' => $sJsObject . '.playDestroy()'
  40. ]
  41. ]]);
  42. }
  43. public function actionEmbedMedia($iContentId)
  44. {
  45. $oTemplate = BxDolTemplate::getInstance();
  46. $aContentInfo = $this->_oDb->getMediaInfoById($iContentId);
  47. if(empty($aContentInfo))
  48. $oTemplate->getEmbed(false);
  49. if(empty($sUnitTemplate))
  50. $sUnitTemplate = 'unit_media_gallery.html';
  51. $oTemplate->getEmbed($this->_oTemplate->unit($aContentInfo, true, $sUnitTemplate));
  52. }
  53. public function actionEditMedia($iMediaId)
  54. {
  55. $CNF = &$this->_oConfig->CNF;
  56. $iMediaId = (int)$iMediaId;
  57. $aMediaInfo = $this->_oDb->getMediaInfoById($iMediaId);
  58. $aContentInfo = $this->_oDb->getContentInfoById($aMediaInfo['content_id']);
  59. if(($sMsg = $this->checkAllowedEdit($aContentInfo)) !== CHECK_ACTION_RESULT_ALLOWED)
  60. return echoJson(['msg' => $sMsg]);
  61. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_MEDIA'], $CNF['OBJECT_FORM_MEDIA_DISPLAY_EDIT']);
  62. $oForm->initForm('edit', $iMediaId);
  63. $oForm->initChecker();
  64. if($oForm->isSubmittedAndValid()) {
  65. if($oForm->update($iMediaId) !== false) {
  66. $aMediaInfo = $this->_oDb->getMediaInfoById($iMediaId);
  67. $aRes = array('reload' => 1);
  68. }
  69. else
  70. $aRes = array('msg' => _t('_bx_stories_txt_err_cannot_perform_action'));
  71. return echoJson($aRes);
  72. }
  73. $sContent = BxTemplStudioFunctions::getInstance()->transBox('bx-stories-edit-media-popup', $this->_oTemplate->parseHtmlByName('media_edit.html', array(
  74. 'form_id' => $oForm->aFormAttrs['id'],
  75. 'form' => $oForm->getCode(true)
  76. )));
  77. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  78. }
  79. public function actionDeleteMedia($iMediaId)
  80. {
  81. $CNF = &$this->_oConfig->CNF;
  82. $sUploader = reset($CNF['OBJECT_UPLOADERS']);
  83. $aMediaInfo = $this->_oDb->getMediaInfoById($iMediaId);
  84. if(!$sUploader || empty($aMediaInfo) || !is_array($aMediaInfo))
  85. return echoJson([]);
  86. $oUploader = BxDolUploader::getObjectInstance($sUploader, $CNF['OBJECT_STORAGE'], '');
  87. if($oUploader === false)
  88. return echoJson(['msg' => _t('_sys_txt_error_occured')]);
  89. $oUploader->deleteGhost($aMediaInfo['file_id'], bx_get_logged_profile_id());
  90. return echoJson(['redirect' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aMediaInfo['content_id']))]);
  91. }
  92. public function actionGetSiblingMedia($iMediaId, $mixedContext)
  93. {
  94. $aSiblings = false;
  95. $sErrorMsg = false;
  96. if (!($aMediaInfo = $this->_oDb->getMediaInfoById((int)$iMediaId)))
  97. $sErrorMsg = _t('_sys_txt_error_occured');
  98. if (empty($sErrorMsg) && !($aContentInfo = $this->_oDb->getContentInfoById($aMediaInfo['content_id'])))
  99. $sErrorMsg = _t('_sys_txt_error_occured');
  100. if (empty($sErrorMsg) && (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedView($aContentInfo))))
  101. $sErrorMsg = $sMsg;
  102. if (empty($sErrorMsg)) {
  103. $aSiblings = array (
  104. 'next' => $this->_oTemplate->getNextPrevMedia($aMediaInfo, true, $mixedContext),
  105. 'prev' => $this->_oTemplate->getNextPrevMedia($aMediaInfo, false, $mixedContext),
  106. );
  107. }
  108. $a = $sErrorMsg ? array('error' => $sErrorMsg) : array('next' => $aSiblings['next'], 'prev' => $aSiblings['prev']);
  109. $s = json_encode($a);
  110. header('Content-type: text/html; charset=utf-8');
  111. echo $s;
  112. }
  113. public function serviceGetSafeServices()
  114. {
  115. return array_merge(parent::serviceGetSafeServices(), [
  116. 'EntityAddFiles' => '',
  117. ]);
  118. }
  119. /**
  120. * Entry actions and social sharing block
  121. */
  122. public function serviceEntityAllActions ($mixedContent = false, $aParams = array())
  123. {
  124. if(!empty($mixedContent)) {
  125. if(!is_array($mixedContent))
  126. $mixedContent = array((int)$mixedContent, array());
  127. }
  128. else {
  129. $mixedContent = $this->_getContent();
  130. if($mixedContent === false)
  131. return false;
  132. }
  133. list($iContentId, $aContentInfo) = $mixedContent;
  134. $aMedias = $this->_oDb->getMediaListByContentId($iContentId);
  135. if(!empty($aMedias) && is_array($aMedias)) {
  136. $aMedia = array_shift($aMedias);
  137. if(!empty($aMedia['file_id']))
  138. $aParams = array_merge(array(
  139. 'entry_thumb' => $aMedia['file_id']
  140. ), $aParams);
  141. }
  142. return parent::serviceEntityAllActions(array($iContentId, $aContentInfo), $aParams);
  143. }
  144. public function serviceMediaAllActions ($mixedContent = false, $aParams = [])
  145. {
  146. $iMediaId = 0;
  147. $aMediaInfo = [];
  148. $bContent = !empty($mixedContent);
  149. if($bContent && is_array($mixedContent))
  150. list($iMediaId, $aMediaInfo) = $mixedContent;
  151. else {
  152. if($bContent)
  153. $iMediaId = (int)$mixedContent;
  154. else
  155. $iMediaId = bx_process_input(bx_get('id'), BX_DATA_INT);
  156. if(!$iMediaId)
  157. return false;
  158. $aMediaInfo = $this->_oDb->getMediaInfoById($iMediaId);
  159. }
  160. if(!$iMediaId || !$aMediaInfo)
  161. return false;
  162. $CNF = &$this->_oConfig->CNF;
  163. return parent::serviceEntityAllActions ([$iMediaId, $aMediaInfo], array_merge([
  164. 'object_menu' => $CNF['OBJECT_MENU_ACTIONS_VIEW_MEDIA'],
  165. 'object_transcoder' => $CNF['OBJECT_IMAGES_TRANSCODER_BIG'],
  166. 'entry_url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aMediaInfo[$CNF['FIELD_MEDIA_CONTENT_ID']])),
  167. 'entry_title' => $aMediaInfo['title'],
  168. 'entry_thumb' => $aMediaInfo['file_id']
  169. ], $aParams));
  170. }
  171. /**
  172. * @page service Service Calls
  173. * @section bx_stories Stories
  174. * @subsection bx_stories-forms Forms
  175. * @subsubsection bx_stories-entity_add_files entity_add_files
  176. *
  177. * @code bx_srv('bx_stories', 'entity_add_files', [...]); @endcode
  178. *
  179. * Display form for adding media to the story.
  180. * @param $iContentId story content id where media will be added, if it's not provided then it's determined from 'id' GET variable
  181. * @return HTML string with form, all necessary CSS and JS files are automatically added to the HEAD section of the site HTML. On error false or empty string is returned.
  182. *
  183. * @see BxStoriesModule::serviceEntityAddFiles
  184. */
  185. /**
  186. * @ref bx_stories-entity_add_files "entity_add_files"
  187. */
  188. public function serviceEntityAddFiles ($iContentId = 0)
  189. {
  190. return $this->_serviceEntityForm ('editDataForm', $iContentId, 'bx_stories_entry_add_media');
  191. }
  192. /**
  193. * Delete file and story association, also file views, votes, comments, meta data are also deleted
  194. * @param $iFileId file ID
  195. * @return true on success of false on error
  196. */
  197. public function serviceDeleteFileAssociations($iFileId)
  198. {
  199. if (!($aMediaInfo = $this->_oDb->getMediaInfoSimpleByFileId($iFileId))) // file is already deleted
  200. return true;
  201. if ($this->_serviceDeleteFileAssociations($iFileId, $aMediaInfo) === false)
  202. return false;
  203. BxDolPage::deleteSeoLink ($this->getName(), 'bx_stories_media', $aMediaInfo['id']);
  204. return true;
  205. }
  206. public function checkAllowedSetThumb ($iContentId = 0)
  207. {
  208. return CHECK_ACTION_RESULT_NOT_ALLOWED;
  209. }
  210. public function serviceGetTimelineData()
  211. {
  212. return [];
  213. }
  214. public function getMediaDuration($aMediaInfo)
  215. {
  216. $CNF = &$this->_oConfig->CNF;
  217. if(empty($aMediaInfo) || !is_array($aMediaInfo))
  218. return 0;
  219. $sField = 'duration';
  220. if(!empty($aMediaInfo[$sField]))
  221. return (int)$aMediaInfo[$sField];
  222. $iMedia = $aMediaInfo['id'];
  223. $sMedia = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4'])->getFileUrl($iMedia);
  224. if(empty($sMedia))
  225. return 0;
  226. $iDuration = (int)BxDolTranscoderVideo::getDuration($sMedia);
  227. if(!empty($iDuration))
  228. $this->_oDb->updateMedia(array($sField => $iDuration), array('id' => $iMedia));
  229. return $iDuration;
  230. }
  231. }
  232. /** @} */