BxTimelineMenuItemShare.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. /**
  12. * 'Item' menu.
  13. */
  14. class BxTimelineMenuItemShare extends BxTemplMenu
  15. {
  16. protected $_sModule;
  17. protected $_oModule;
  18. protected $_iEvent;
  19. protected $_aEvent;
  20. public function __construct($aObject, $oTemplate = false)
  21. {
  22. $this->_sModule = 'bx_timeline';
  23. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  24. parent::__construct($aObject, $this->_oModule->_oTemplate);
  25. $iContentId = 0;
  26. if(bx_get('content_id') !== false)
  27. $iContentId = bx_process_input(bx_get('content_id'), BX_DATA_INT);
  28. if(!empty($iContentId)) {
  29. $aBrowseParams = array('name' => '', 'view' => '', 'type' => '');
  30. foreach($aBrowseParams as $sKey => $sValue)
  31. if(bx_get($sKey) !== false)
  32. $aBrowseParams[$sKey] = $this->_oModule->_oConfig->prepareParam($sKey);
  33. $this->setEventById($iContentId, $aBrowseParams);
  34. }
  35. }
  36. public function setEventById($iEventId, $aBrowseParams = array())
  37. {
  38. $this->_iEvent = $iEventId;
  39. $this->_aEvent = $this->_oModule->_oDb->getEvents(array('browse' => 'id', 'value' => $this->_iEvent));
  40. $this->_setBrowseParams($aBrowseParams);
  41. $sRepostOnclick = $sEtSend = '';
  42. if(!empty($this->_aEvent) && is_array($this->_aEvent)) {
  43. $sType = $this->_aEvent['type'];
  44. $sAction = $this->_aEvent['action'];
  45. $this->_aEvent['url'] = $this->_oModule->_oConfig->getItemViewUrl($this->_aEvent);
  46. if($this->_oModule->_oConfig->isSystem($sType, $sAction)) {
  47. $aResult = $this->_oModule->_oConfig->getSystemData($this->_aEvent);
  48. if(!empty($aResult['url']))
  49. $this->_aEvent['url'] = $aResult['url'];
  50. }
  51. //--- Repost and 'Repost To' items
  52. $iOwnerId = $this->_oModule->getUserId(); //--- in whose timeline the content will be reposted
  53. $iObjectId = $this->_oModule->_oConfig->isSystem($sType, $sAction) ? $this->_aEvent['object_id'] : $this->_aEvent['id'];
  54. $sRepostOnclick = $this->_oModule->serviceGetRepostJsClick($iOwnerId, $sType, $sAction, $iObjectId);
  55. $sRepostWithOnclick = $this->_oModule->serviceGetRepostWithJsClick($iOwnerId, $sType, $sAction, $iObjectId);
  56. $sRepostToOnclick = $this->_oModule->serviceGetRepostToJsClick($iOwnerId, $sType, $sAction, $iObjectId);
  57. //--- Send item
  58. $sAetSend = urlencode(base64_encode(json_encode([
  59. 'name' => 'bx_timeline_send',
  60. 'params' => [
  61. 'ViewLink' => $this->_aEvent['url']
  62. ]
  63. ])));
  64. }
  65. if($this->_bIsApi && ($sRootUrl = bx_api_get_base_url()) !== false) {
  66. if(substr(BX_DOL_URL_ROOT, -1) == '/' && substr($sRootUrl, -1) != '/')
  67. $sRootUrl .= '/';
  68. $this->_aEvent['url'] = str_replace(BX_DOL_URL_ROOT, $sRootUrl, $this->_aEvent['url']);
  69. }
  70. $this->addMarkers([
  71. 'content_id' => $this->_iEvent,
  72. 'content_url' => $this->_aEvent['url'],
  73. 'view_js_object' => $this->_oModule->_oConfig->getJsObjectView($aBrowseParams),
  74. 'repost_onclick' => $sRepostOnclick,
  75. 'repost_with_onclick' => $sRepostWithOnclick,
  76. 'repost_to_onclick' => $sRepostToOnclick,
  77. 'et_send' => $sAetSend
  78. ]);
  79. }
  80. public function getMenuItems ()
  81. {
  82. $aItems = parent::getMenuItems();
  83. $aMarkers = array(
  84. 'id' => $this->_iEvent,
  85. 'module' => $this->_oModule->_oConfig->getName(),
  86. 'url' => $this->_aEvent['url'],
  87. 'title' => $this->_aEvent['title'],
  88. 'img_url' => '',
  89. 'img_url_encoded' => ''
  90. );
  91. $aEventData = $this->_oModule->_oTemplate->getDataCached($this->_aEvent);
  92. if(!empty($aEventData['content']['images']) && is_array($aEventData['content']['images'])) {
  93. $aImage = array_shift($aEventData['content']['images']);
  94. $sImgUrl = isset($aImage['src_medium']) ? $aImage['src_medium'] : $aImage['src'];;
  95. if(!empty($sImgUrl))
  96. $aMarkers = array_merge($aMarkers, array(
  97. 'img_url' => $sImgUrl,
  98. 'img_url_encoded' => rawurlencode($sImgUrl),
  99. ));
  100. }
  101. if(($oSocial = BxDolMenu::getObjectInstance('sys_social_sharing')) !== false) {
  102. $oSocial->addMarkers($aMarkers);
  103. $aItems = array_merge($aItems, $oSocial->getMenuItems());
  104. }
  105. if($this->_bIsApi) {
  106. $aItemsApi = [];
  107. foreach($aItems as $aItem)
  108. switch($aItem['name']) {
  109. case 'item-repost':
  110. $iOwnerId = $this->_oModule->getUserId();
  111. $sType = $this->_aEvent['type'];
  112. $sAction = $this->_aEvent['action'];
  113. $iObjectId = 0;
  114. $sCommonPrefix = $this->_oModule->_oConfig->getPrefix('common_post');
  115. if(str_replace($sCommonPrefix, '', $sType) == BX_TIMELINE_PARSE_TYPE_REPOST) {
  116. $aRepostedData = unserialize($this->_aEvent['content']);
  117. $sType = $aRepostedData['type'];
  118. $sAction = $aRepostedData['action'];
  119. $iObjectId = $aRepostedData['object_id'];
  120. }
  121. else
  122. $iObjectId = $this->_oModule->_oConfig->isSystem($sType, $sAction) ? $this->_aEvent['object_id'] : $this->_aEvent['id'];
  123. $aItemsApi['item-repost'] = array_merge($aItem, [
  124. 'data' => [
  125. 'author_id' => $iOwnerId,
  126. 'owner_id' => $iOwnerId,
  127. 'type' => $sType,
  128. 'action' => $sAction,
  129. 'object_id' => $iObjectId
  130. ]
  131. ]);
  132. break;
  133. case 'item-copy':
  134. $aItemsApi['item-copy'] = array_merge($aItem, [
  135. 'link' => $aMarkers['url'],
  136. ]);
  137. break;
  138. /**
  139. * TODO: Update when unified Share popup will be used here.
  140. */
  141. case 'social-sharing-facebook':
  142. case 'social-sharing-twitter':
  143. case 'social-sharing-pinterest':
  144. case 'social-sharing-linked_in':
  145. case 'social-sharing-whatsapp':
  146. if(isset($aItemsApi['item-share']))
  147. break;
  148. $aItemsApi['item-share'] = array_merge($aItem, [
  149. 'name' => 'item-share',
  150. 'title' => _t('_sys_menu_item_title_social_sharing'),
  151. 'link' => $aMarkers['url'],
  152. ]);
  153. break;
  154. }
  155. return array_values($aItemsApi);
  156. }
  157. return $aItems;
  158. }
  159. protected function _setBrowseParams($aBrowseParams = array())
  160. {
  161. $aMarkers = array();
  162. foreach($aBrowseParams as $sKey => $mixedValue)
  163. if(!is_array($aMarkers))
  164. $aMarkers[$sKey] = $mixedValue;
  165. $this->addMarkers($aMarkers);
  166. $this->addMarkers(array(
  167. 'js_object_view' => $this->_oModule->_oConfig->getJsObjectView($aBrowseParams)
  168. ));
  169. }
  170. /**
  171. * Check if menu items is visible.
  172. * @param $a menu item array
  173. * @return boolean
  174. */
  175. protected function _isVisible ($a)
  176. {
  177. if(!parent::_isVisible($a))
  178. return false;
  179. if(empty($this->_aEvent) || !is_array($this->_aEvent))
  180. return false;
  181. $sCheckFuncName = '';
  182. $aCheckFuncParams = array();
  183. switch ($a['name']) {
  184. case 'item-repost':
  185. $sCheckFuncName = 'isAllowedRepost';
  186. $aCheckFuncParams = array($this->_aEvent);
  187. break;
  188. case 'item-send':
  189. $sCheckFuncName = 'isAllowedSend';
  190. $aCheckFuncParams = array($this->_aEvent);
  191. break;
  192. }
  193. if(!$sCheckFuncName || !method_exists($this->_oModule, $sCheckFuncName))
  194. return true;
  195. return call_user_func_array(array($this->_oModule, $sCheckFuncName), $aCheckFuncParams) === true;
  196. }
  197. }
  198. /** @} */