BxAlbumsModule.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 Albums Albums
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolAcl');
  12. /**
  13. * Albums module
  14. */
  15. class BxAlbumsModule extends BxBaseModTextModule
  16. {
  17. protected $_aContexts = array('popular', 'public', 'author');
  18. function __construct(&$aModule)
  19. {
  20. parent::__construct($aModule);
  21. }
  22. /**
  23. * Display form for adding media to the album.
  24. * @param $iContentId album content id where media will be added, if it's not provided then it's determined from 'id' GET variable
  25. * @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.
  26. */
  27. public function serviceEntityAddFiles ($iContentId = 0)
  28. {
  29. return $this->_serviceEntityForm ('editDataForm', $iContentId, 'bx_albums_entry_add_images');
  30. }
  31. /**
  32. * Delete file and album association, also file views, votes, comments, meta data are also deleted
  33. * @param $iFileId file ID
  34. * @return true on success of false on error
  35. */
  36. public function serviceDeleteFileAssociations($iFileId)
  37. {
  38. $CNF = &$this->_oConfig->CNF;
  39. if (!($aMediaInfo = $this->_oDb->getMediaInfoSimpleByFileId($iFileId))) // file is already deleted
  40. return true;
  41. if (!$this->_oDb->deassociateFileWithContent(0, $iFileId))
  42. return false;
  43. if (!empty($CNF['OBJECT_VIEWS_MEDIA'])) {
  44. $o = BxDolView::getObjectInstance($CNF['OBJECT_VIEWS_MEDIA'], $aMediaInfo['id']);
  45. if ($o) $o->onObjectDelete();
  46. }
  47. if (!empty($CNF['OBJECT_VOTES_MEDIA'])) {
  48. $o = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES_MEDIA'], $aMediaInfo['id']);
  49. if ($o) $o->onObjectDelete();
  50. }
  51. if (!empty($CNF['OBJECT_COMMENTS_MEDIA'])) {
  52. $o = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS_MEDIA'], $aMediaInfo['id']);
  53. if ($o) $o->onObjectDelete();
  54. }
  55. if (!empty($CNF['OBJECT_METATAGS_MEDIA'])) {
  56. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS_MEDIA']);
  57. $oMetatags->onDeleteContent($aMediaInfo['id']);
  58. }
  59. if (!empty($CNF['OBJECT_METATAGS_MEDIA_CAMERA'])) {
  60. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS_MEDIA_CAMERA']);
  61. $oMetatags->onDeleteContent($aMediaInfo['id']);
  62. }
  63. return true;
  64. }
  65. /**
  66. * Display media EXIF information.
  67. * @param $iMediaId media ID, if it's omitted then it's taken from 'id' GET variable.
  68. * @return HTML string with EXIF info. On error empty string is returned.
  69. */
  70. public function serviceMediaExif ($iMediaId = 0)
  71. {
  72. return $this->_serviceTemplateFunc ('mediaExif', $iMediaId, 'getMediaInfoById');
  73. }
  74. /**
  75. * Display media comments block.
  76. * @param $iMediaId media ID, if it's omitted then it's taken from 'id' GET variable.
  77. * @return HTML string with comments. On error false or empty string is returned.
  78. */
  79. public function serviceMediaComments ($iMediaId = 0)
  80. {
  81. return $this->_entityComments($this->_oConfig->CNF['OBJECT_COMMENTS_MEDIA'], $iMediaId);
  82. }
  83. /**
  84. * Display media author block.
  85. * @param $iMediaId media ID, if it's omitted then it's taken from 'id' GET variable.
  86. * @return HTML string with block content. On error false or empty string is returned.
  87. */
  88. public function serviceMediaAuthor ($iMediaId = 0)
  89. {
  90. return $this->_serviceTemplateFunc ('mediaAuthor', $iMediaId, 'getMediaInfoById');
  91. }
  92. /**
  93. * Display media social sharing buttons block.
  94. * @param $iMediaId media ID, if it's omitted then it's taken from 'id' GET variable.
  95. * @return HTML string with block content. On error false or empty string is returned.
  96. */
  97. public function serviceMediaSocialSharing ($iMediaId = 0, $bEnableCommentsBtn = false, $bEnableSocialSharing = true, $bShowAsButton = true)
  98. {
  99. if (!$iMediaId)
  100. $iMediaId = bx_process_input(bx_get('id'), BX_DATA_INT);
  101. if (!$iMediaId)
  102. return false;
  103. $aMediaInfo = $this->_oDb->getMediaInfoById($iMediaId);
  104. if (!$aMediaInfo)
  105. return false;
  106. $CNF = &$this->_oConfig->CNF;
  107. return $this->_entitySocialSharing ($iMediaId, array(
  108. 'show_as_button' => $bShowAsButton,
  109. 'id_timeline' => 0,
  110. 'id_thumb' => $aMediaInfo['file_id'],
  111. 'title' => $aMediaInfo['title'],
  112. 'object_storage' => false,
  113. 'object_transcoder' => $CNF['OBJECT_IMAGES_TRANSCODER_BIG'],
  114. 'object_vote' => $CNF['OBJECT_VOTES_MEDIA'],
  115. 'object_favorite' => $CNF['OBJECT_FAVORITES_MEDIA'],
  116. 'object_report' => '',
  117. 'object_comments' => $bEnableCommentsBtn ? $CNF['OBJECT_COMMENTS_MEDIA'] : '',
  118. 'object_feature' => $CNF['OBJECT_FEATURED_MEDIA'],
  119. 'uri_view_entry' => $CNF['URI_VIEW_MEDIA'],
  120. 'social_sharing' => $bEnableSocialSharing
  121. ));
  122. }
  123. /**
  124. * Display media preview block.
  125. * @param $iMediaId media ID, if it's omitted then it's taken from 'id' GET variable.
  126. * @param $mixedContext current context to navigate to next and previous file, possible values: popular, public, author. If omitted then value from 'context' GTE variable is taken. Default context is 'album' - when nothin is specified.
  127. * @return HTML string with block content. On error false or empty string is returned.
  128. */
  129. public function serviceMediaView ($iMediaId = 0, $mixedContext = false)
  130. {
  131. if (!$iMediaId)
  132. $iMediaId = bx_process_input(bx_get('id'), BX_DATA_INT);
  133. if (!$iMediaId)
  134. return false;
  135. if (!$mixedContext) {
  136. $mixedContext = bx_process_input(bx_get('context'));
  137. if (!in_array($mixedContext, $this->_aContexts)) // when no context specified, it is assumed that it is an album context
  138. $mixedContext = bx_process_input($mixedContext, BX_DATA_INT); // numeric context is reserved for future use
  139. }
  140. return $this->_oTemplate->entryMediaView ($iMediaId, $mixedContext);
  141. }
  142. public function checkAllowedSetThumb ($iContentId = 0)
  143. {
  144. return CHECK_ACTION_RESULT_NOT_ALLOWED;
  145. }
  146. /**
  147. * Display block for browsing recently uploaded media files.
  148. * @param $sUnitView unit view mode: gallery
  149. * @param $bDisplayEmptyMsg display 'empty' message when nothing to browse, or return empty string.
  150. * @param $bAjaxPaginate use AJAX or regular paginate.
  151. * @return HTML string with block content. On error false or empty string is returned.
  152. */
  153. public function serviceBrowseRecentMedia ($sUnitView = false, $bDisplayEmptyMsg = true, $bAjaxPaginate = true)
  154. {
  155. return $this->_serviceBrowse ('recent', array('unit_view' => $sUnitView), BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate, 'SearchResultMedia');
  156. }
  157. /**
  158. * Display featured media.
  159. * For the list of params @see BxAlbumsModule::serviceBrowseRecentMedia
  160. */
  161. public function serviceBrowseFeaturedMedia ($sUnitView = false, $bDisplayEmptyMsg = true, $bAjaxPaginate = true)
  162. {
  163. return $this->_serviceBrowse ('featured', array('unit_view' => $sUnitView), BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate, 'SearchResultMedia');
  164. }
  165. /**
  166. * Display popular media.
  167. * For the list of params @see BxAlbumsModule::serviceBrowseRecentMedia
  168. */
  169. public function serviceBrowsePopularMedia ($sUnitView = false, $bDisplayEmptyMsg = true, $bAjaxPaginate = true)
  170. {
  171. return $this->_serviceBrowse ('popular', array('unit_view' => $sUnitView), BX_DB_PADDING_DEF, $bDisplayEmptyMsg, $bAjaxPaginate, 'SearchResultMedia');
  172. }
  173. /**
  174. * Display favorite media for particular profile.
  175. * @param $iProfileId profile ID, if omitted then 'profile_id' GET variable is used
  176. * @param $aParams additional params for browsing such as 'unit_view'
  177. */
  178. public function serviceBrowseFavoriteMedia ($iProfileId = 0, $aParams = array())
  179. {
  180. $oProfile = null;
  181. if((int)$iProfileId)
  182. $oProfile = BxDolProfile::getInstance($iProfileId);
  183. if(!$oProfile && bx_get('profile_id') !== false)
  184. $oProfile = BxDolProfile:: getInstance(bx_process_input(bx_get('profile_id'), BX_DATA_INT));
  185. if(!$oProfile)
  186. $oProfile = BxDolProfile::getInstance();
  187. if(!$oProfile)
  188. return '';
  189. return $this->_serviceBrowse ('favorite', array_merge(array('user' => $oProfile->id()), $aParams), BX_DB_PADDING_DEF, true, true, 'SearchResultMedia');
  190. }
  191. public function serviceGetNotificationsData()
  192. {
  193. $sModule = $this->_aModule['name'];
  194. $sEventPrivacy = $sModule . '_allow_view_event_to';
  195. if(BxDolPrivacy::getObjectInstance($sEventPrivacy) === false)
  196. $sEventPrivacy = '';
  197. $aResult = parent::serviceGetNotificationsData();
  198. $aResult['handlers'] = array_merge($aResult['handlers'], array(
  199. array('group' => $sModule . '_comment_media', 'type' => 'insert', 'alert_unit' => $sModule . '_media', 'alert_action' => 'commentPost', 'module_name' => $sModule, 'module_method' => 'get_notifications_comment_media', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  200. array('group' => $sModule . '_comment_media', 'type' => 'delete', 'alert_unit' => $sModule . '_media', 'alert_action' => 'commentRemoved'),
  201. array('group' => $sModule . '_vote_media', 'type' => 'insert', 'alert_unit' => $sModule . '_media', 'alert_action' => 'doVote', 'module_name' => $sModule, 'module_method' => 'get_notifications_vote_media', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  202. array('group' => $sModule . '_vote_media', 'type' => 'delete', 'alert_unit' => $sModule . '_media', 'alert_action' => 'undoVote'),
  203. ));
  204. $aResult['alerts'] = array_merge($aResult['alerts'], array(
  205. array('unit' => $sModule . '_media', 'action' => 'commentPost'),
  206. array('unit' => $sModule . '_media', 'action' => 'commentRemoved'),
  207. array('unit' => $sModule . '_media', 'action' => 'doVote'),
  208. array('unit' => $sModule . '_media', 'action' => 'undoVote'),
  209. ));
  210. return $aResult;
  211. }
  212. public function serviceGetNotificationsCommentMedia($aEvent)
  213. {
  214. $CNF = &$this->_oConfig->CNF;
  215. $iMediaId = (int)$aEvent['object_id'];
  216. $aMediaInfo = $this->_oDb->getMediaInfoById($iMediaId);
  217. if(empty($aMediaInfo) || !is_array($aMediaInfo))
  218. return array();
  219. $oComment = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS_MEDIA'], $iMediaId);
  220. if(!$oComment || !$oComment->isEnabled())
  221. return array();
  222. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_MEDIA'] . '&id=' . $aMediaInfo['id']);
  223. $sEntryCaption = isset($aMediaInfo['title']) ? $aMediaInfo['title'] : _t('_bx_albums_media');
  224. return array(
  225. 'entry_sample' => $CNF['T']['txt_media_single'],
  226. 'entry_url' => $sEntryUrl,
  227. 'entry_caption' => $sEntryCaption,
  228. 'entry_author' => $aMediaInfo['author'],
  229. 'subentry_sample' => $CNF['T']['txt_media_comment_single'],
  230. 'subentry_url' => $oComment->getViewUrl((int)$aEvent['subobject_id']),
  231. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  232. );
  233. }
  234. public function serviceGetNotificationsVoteMedia($aEvent)
  235. {
  236. $CNF = &$this->_oConfig->CNF;
  237. $iMediaId = (int)$aEvent['object_id'];
  238. $aMediaInfo = $this->_oDb->getMediaInfoById($iMediaId);
  239. if(empty($aMediaInfo) || !is_array($aMediaInfo))
  240. return array();
  241. $oVote = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES_MEDIA'], $iMediaId);
  242. if(!$oVote || !$oVote->isEnabled())
  243. return array();
  244. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_MEDIA'] . '&id=' . $aMediaInfo['id']);
  245. $sEntryCaption = isset($aMediaInfo['title']) ? $aMediaInfo['title'] : _t('_bx_albums_media');
  246. return array(
  247. 'entry_sample' => $CNF['T']['txt_media_single'],
  248. 'entry_url' => $sEntryUrl,
  249. 'entry_caption' => $sEntryCaption,
  250. 'entry_author' => $aMediaInfo['author'],
  251. 'subentry_sample' => $CNF['T']['txt_media_vote_single'],
  252. 'subentry_url' => '',
  253. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  254. );
  255. }
  256. public function actionGetSiblingMedia($iMediaId, $mixedContext)
  257. {
  258. $aSiblings = false;
  259. $sErrorMsg = false;
  260. if (!($aMediaInfo = $this->_oDb->getMediaInfoById((int)$iMediaId)))
  261. $sErrorMsg = _t('_sys_txt_error_occured');
  262. if (empty($sErrorMsg) && !($aContentInfo = $this->_oDb->getContentInfoById($aMediaInfo['content_id'])))
  263. $sErrorMsg = _t('_sys_txt_error_occured');
  264. if (empty($sErrorMsg) && (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedView($aContentInfo))))
  265. $sErrorMsg = $sMsg;
  266. if (empty($sErrorMsg)) {
  267. $aSiblings = array (
  268. 'next' => $this->_oTemplate->getNextPrevMedia($aMediaInfo, true, $mixedContext),
  269. 'prev' => $this->_oTemplate->getNextPrevMedia($aMediaInfo, false, $mixedContext),
  270. );
  271. }
  272. $a = $sErrorMsg ? array('error' => $sErrorMsg) : array('next' => $aSiblings['next'], 'prev' => $aSiblings['prev']);
  273. $s = json_encode($a);
  274. header('Content-type: text/html; charset=utf-8');
  275. echo $s;
  276. }
  277. public function actionRssMedia ()
  278. {
  279. $aArgs = func_get_args();
  280. $this->_rss($aArgs, 'SearchResultMedia');
  281. }
  282. protected function _buildRssParams($sMode, $aArgs)
  283. {
  284. if ($aParams = parent::_buildRssParams($sMode, $aArgs))
  285. return $aParams;
  286. $sMode = bx_process_input($sMode);
  287. switch ($sMode) {
  288. case 'album':
  289. $aParams = array('album_id' => isset($aArgs[0]) ? (int)$aArgs[0] : '');
  290. break;
  291. }
  292. return $aParams;
  293. }
  294. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  295. {
  296. $CNF = &$this->_oConfig->CNF;
  297. if (!($aMediaList = $this->_oDb->getMediaListByContentId($aContentInfo[$CNF['FIELD_ID']])))
  298. return array();
  299. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  300. $aMediaList = array_slice($aMediaList, 0, 3);
  301. $aOutput = array();
  302. foreach ($aMediaList as $aMedia) {
  303. $aOutput[] = array (
  304. 'url' => $this->_oTemplate->getViewMediaUrl($CNF, $aMedia['id']),
  305. 'src' => $oTranscoder->getFileUrl($aMedia['file_id']),
  306. );
  307. }
  308. return $aOutput;
  309. }
  310. }
  311. /** @} */