BxBaseModProfileMenuSnippetMeta.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 BaseProfile Base classes for profile modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxBaseModProfileMenuSnippetMeta extends BxBaseModGeneralMenuSnippetMeta
  12. {
  13. protected $_bContentPublic;
  14. protected $_oContentProfile;
  15. protected $_aConnectionToFunctionCheck;
  16. public function __construct($aObject, $oTemplate = false)
  17. {
  18. parent::__construct($aObject, $oTemplate);
  19. $this->_iButtonsMax = 2;
  20. $this->_bShowZeros = true;
  21. $this->_bContentPublic = false;
  22. $this->_oContentProfile = null;
  23. $this->_aConnectionToFunctionCheck = $this->_oModule->_oConfig->getConnectionToFunctionCheck();
  24. }
  25. public function setContentId($iContentId)
  26. {
  27. parent::setContentId($iContentId);
  28. if(!empty($this->_iContentId))
  29. $this->_oContentProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->_sModule);
  30. }
  31. protected function getMenuItemConnectionJsCode($sConnection, $sAction, $iContentProfile, $aItem)
  32. {
  33. return 'bx_conn_action(this, \'' . $sConnection . '\', \'' . $sAction . '\', \'' . $iContentProfile . '\', false, function(oData, eLink) {$(eLink).parents(\'.bx-menu-item:first\').remove();})';
  34. }
  35. protected function getMenuItemRecommendationJsCode($sObject, $sAction, $iContentId, $aItem)
  36. {
  37. return 'bx_recommendation_action(this, \'' . $sObject . '\', \'' . $sAction . '\', \'' . $iContentId . '\', false, function(oData, eLink) {$(eLink).parents(\'.bx-base-pofile-unit-with-cover:first\').remove();})';
  38. }
  39. public function setContentPublic($bContentPublic)
  40. {
  41. $this->_bContentPublic = $bContentPublic;
  42. }
  43. protected function _getMenuItemBefriend($aItem)
  44. {
  45. return $this->_getMenuItemConnection('sys_profiles_friends', 'add', $aItem);
  46. }
  47. protected function _getMenuItemUnfriend($aItem)
  48. {
  49. return $this->_getMenuItemConnection('sys_profiles_friends', 'remove', $aItem);
  50. }
  51. protected function _getMenuItemSubscribe($aItem)
  52. {
  53. return $this->_getMenuItemConnection('sys_profiles_subscriptions', 'add', $aItem);
  54. }
  55. protected function _getMenuItemUnsubscribe($aItem)
  56. {
  57. return $this->_getMenuItemConnection('sys_profiles_subscriptions', 'remove', $aItem);
  58. }
  59. protected function _getMenuItemIgnoreBefriend($aItem)
  60. {
  61. return $this->_getMenuItemIgnoreRecommendation('sys_friends', $aItem);
  62. }
  63. protected function _getMenuItemIgnoreSubscribe($aItem)
  64. {
  65. return $this->_getMenuItemIgnoreRecommendation('sys_subscriptions', $aItem);
  66. }
  67. protected function _getMenuItemFriends($aItem)
  68. {
  69. if(!$this->_isVisibleInContext($aItem))
  70. return false;
  71. if(!$this->_bContentPublic || !$this->_oContentProfile)
  72. return false;
  73. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  74. if(!$oConnection)
  75. return false;
  76. $iProfileId = $this->_oContentProfile->id();
  77. $iFriends = $oConnection->getConnectedInitiatorsCount($iProfileId, true);
  78. if(!$iFriends && !$this->_bShowZeros)
  79. return false;
  80. $CNF = &$this->_oModule->_oConfig->CNF;
  81. $sTitle = _t('_sys_menu_item_title_sm_friends', $iFriends);
  82. $sUrl = $this->_oContentProfile->getUrl();
  83. if(!empty($CNF['URI_VIEW_FRIENDS']))
  84. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_FRIENDS'] . '&profile_id=' . $iProfileId));
  85. if($this->_bIsApi)
  86. return $this->_getMenuItemAPI($aItem, 'text', [
  87. 'title' => $sTitle,
  88. 'link' => bx_api_get_relative_url($sUrl),
  89. ]);
  90. return $this->getUnitMetaItemText($sTitle);
  91. }
  92. protected function _getMenuItemFriendsMutual($aItem)
  93. {
  94. if(!$this->_isVisibleInContext($aItem))
  95. return false;
  96. if(!$this->_bContentPublic || !$this->_oContentProfile)
  97. return false;
  98. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  99. if(!$oConnection)
  100. return false;
  101. $iProfileId = $this->_oContentProfile->id();
  102. $iFriends = $oConnection->getCommonContentCount($iProfileId, bx_get_logged_profile_id(), true);
  103. if(!$iFriends && !$this->_bShowZeros)
  104. return false;
  105. $CNF = &$this->_oModule->_oConfig->CNF;
  106. $sTitle = _t('_sys_menu_item_title_sm_friends_mutual', $iFriends);
  107. $sUrl = $this->_oContentProfile->getUrl();
  108. if(!empty($CNF['URI_VIEW_FRIENDS']))
  109. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_FRIENDS'] . '&profile_id=' . $iProfileId));
  110. if($this->_bIsApi)
  111. return $this->_getMenuItemAPI($aItem, 'text', [
  112. 'title' => $sTitle,
  113. 'link' => bx_api_get_relative_url($sUrl),
  114. ]);
  115. return $this->getUnitMetaItemText($sTitle);
  116. }
  117. protected function _getMenuItemSubscribers($aItem)
  118. {
  119. if(!$this->_isVisibleInContext($aItem))
  120. return false;
  121. if(!$this->_bContentPublic || !$this->_oContentProfile)
  122. return false;
  123. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  124. if(!$oConnection)
  125. return false;
  126. $iSubscribers = $oConnection->getConnectedInitiatorsCount($this->_oContentProfile->id());
  127. if(!$iSubscribers && !$this->_bShowZeros)
  128. return false;
  129. $sIcon = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIconAsHtml(!empty($aItem['icon']) ? $aItem['icon'] : '');
  130. if($this->_bIsApi)
  131. return false;
  132. return $this->getUnitMetaItemCustom($oConnection->getCounter($this->_oContentProfile->id(), false, [
  133. 'caption' => '_sys_menu_item_title_sm_subscribers',
  134. 'custom_icon' => $sIcon
  135. ], BX_CONNECTIONS_CONTENT_TYPE_INITIATORS));
  136. }
  137. protected function _getMenuItemMembership($aItem)
  138. {
  139. if(!$this->_isVisibleInContext($aItem))
  140. return false;
  141. if(!$this->_bContentPublic || !$this->_oContentProfile)
  142. return false;
  143. $aMembership = BxDolAcl::getInstance()->getMemberMembershipInfo($this->_oContentProfile->id());
  144. if(empty($aMembership) || !is_array($aMembership))
  145. return false;
  146. $sTitle = _t($aMembership['name']);
  147. if($this->_bIsApi)
  148. return $this->_getMenuItemAPI($aItem, 'text', [
  149. 'title' => $sTitle
  150. ]);
  151. return $this->getUnitMetaItemText($sTitle);
  152. }
  153. protected function _getMenuItemConnection($sConnection, $sAction, &$aItem)
  154. {
  155. if(!$this->_isVisibleInContext($aItem))
  156. return false;
  157. if(!isLogged() || $this->_oModule->{$this->_aConnectionToFunctionCheck[$sConnection][$sAction]}($this->_aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  158. return false;
  159. $iContentProfile = $this->_oContentProfile->id();
  160. $sTitle = $this->_oModule->getMenuItemTitleByConnection($sConnection, $sAction, $iContentProfile);
  161. if(empty($sTitle))
  162. return false;
  163. if($this->_bIsApi)
  164. return $this->_getMenuItemAPI($aItem, ['display' => 'element'], [
  165. 'title' => $sTitle,
  166. 'data' => [
  167. 'type' => 'connections',
  168. 'o' => $sConnection,
  169. 'a' => $sAction,
  170. 'iid' => bx_get_logged_profile_id(),
  171. 'cid' => $iContentProfile,
  172. 'title' => $sTitle,
  173. 'primary' => !empty($aItem['primary']),
  174. ]
  175. ]);
  176. $mixedItem = $this->getUnitMetaItemButton($sTitle, [
  177. 'class' => !empty($aItem['primary']) ? 'bx-btn-primary' : '',
  178. 'onclick' => $this->getMenuItemConnectionJsCode($sConnection, $sAction, $iContentProfile, $aItem)
  179. ]);
  180. return $mixedItem !== false ? [$mixedItem, 'bx-menu-item-button'] : false;
  181. }
  182. protected function _getMenuItemIgnoreRecommendation($sObject, $aItem)
  183. {
  184. if(!isLogged() || !$this->_isVisibleInContext($aItem))
  185. return false;
  186. $oRecommendation = BxDolRecommendation::getObjectInstance($sObject);
  187. if(!$oRecommendation)
  188. return false;
  189. $sConnection = $oRecommendation->getConnection();
  190. if($this->_oModule->{$this->_aConnectionToFunctionCheck[$sConnection]['add']}($this->_aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  191. return false;
  192. $sAction = 'ignore';
  193. $sTitle = _t($aItem['title']);
  194. $iContentProfile = $this->_oContentProfile->id();
  195. if($this->_bIsApi)
  196. return $this->_getMenuItemAPI($aItem, ['display' => 'element'], [
  197. 'title' => $sTitle,
  198. 'data' => [
  199. 'type' => 'recommendation',
  200. 'o' => $sObject,
  201. 'a' => $sAction,
  202. 'iid' => bx_get_logged_profile_id(),
  203. 'cid' => $iContentProfile,
  204. 'title' => $sTitle,
  205. 'primary' => !empty($aItem['primary']),
  206. ]
  207. ]);
  208. $mixedItem = $this->getUnitMetaItemButton($sTitle, [
  209. 'class' => !empty($aItem['primary']) ? 'bx-btn-primary' : '',
  210. 'onclick' => $this->getMenuItemRecommendationJsCode($sObject, $sAction, $iContentProfile, $aItem)
  211. ]);
  212. return $mixedItem !== false ? [$mixedItem, 'bx-menu-item-button'] : false;
  213. }
  214. }
  215. /** @} */