BxBaseModProfileMenuSnippetMeta.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. protected $_aConnectionToFunctionTitle;
  17. public function __construct($aObject, $oTemplate = false)
  18. {
  19. parent::__construct($aObject, $oTemplate);
  20. $this->_bShowZeros = true;
  21. $this->_bContentPublic = false;
  22. $this->_aConnectionToFunctionCheck = array(
  23. 'sys_profiles_friends' => array(
  24. 'add' => 'checkAllowedFriendAdd',
  25. 'remove' => 'checkAllowedFriendRemove'
  26. ),
  27. 'sys_profiles_subscriptions' => array(
  28. 'add' => 'checkAllowedSubscribeAdd',
  29. 'remove' => 'checkAllowedSubscribeRemove'
  30. )
  31. );
  32. $this->_aConnectionToFunctionTitle = array(
  33. 'sys_profiles_friends' => '_getMenuItemProfilesFriendsTitle',
  34. 'sys_profiles_subscriptions' => '_getMenuItemProfilesSubscriptionsTitle'
  35. );
  36. }
  37. public function setContentId($iContentId)
  38. {
  39. parent::setContentId($iContentId);
  40. if(!empty($this->_iContentId))
  41. $this->_oContentProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->_sModule);
  42. }
  43. protected function getMenuItemConnectionJsCode($sConnection, $sAction, $iContentProfile, $aItem)
  44. {
  45. return 'bx_conn_action(this, \'' . $sConnection . '\', \'' . $sAction . '\', \'' . $iContentProfile . '\', false, function(oData, eLink) {$(eLink).parents(\'.bx-menu-item:first\').remove();})';
  46. }
  47. public function setContentPublic($bContentPublic)
  48. {
  49. $this->_bContentPublic = $bContentPublic;
  50. }
  51. protected function _getMenuItemBefriend($aItem)
  52. {
  53. return $this->_getMenuItemConnection('sys_profiles_friends', 'add', $aItem);
  54. }
  55. protected function _getMenuItemUnfriend($aItem)
  56. {
  57. return $this->_getMenuItemConnection('sys_profiles_friends', 'remove', $aItem);
  58. }
  59. protected function _getMenuItemSubscribe($aItem)
  60. {
  61. return $this->_getMenuItemConnection('sys_profiles_subscriptions', 'add', $aItem);
  62. }
  63. protected function _getMenuItemUnsubscribe($aItem)
  64. {
  65. return $this->_getMenuItemConnection('sys_profiles_subscriptions', 'remove', $aItem);
  66. }
  67. protected function _getMenuItemFriends($aItem)
  68. {
  69. $CNF = &$this->_oModule->_oConfig->CNF;
  70. if(!$this->_bContentPublic || !$this->_oContentProfile)
  71. return false;
  72. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  73. if(!$oConnection)
  74. return false;
  75. $iFriends = $oConnection->getConnectedInitiatorsCount($this->_oContentProfile->id(), true);
  76. if(!$iFriends && !$this->_bShowZeros)
  77. return false;
  78. $sTitle = _t('_sys_menu_item_title_sm_friends', $iFriends);
  79. if($this->_bIsApi)
  80. return $this->_getMenuItemAPI($aItem, 'text', [
  81. 'title' => $sTitle
  82. ]);
  83. return $this->getUnitMetaItemText($sTitle);
  84. }
  85. protected function _getMenuItemFriendsMutual($aItem)
  86. {
  87. $CNF = &$this->_oModule->_oConfig->CNF;
  88. if(!$this->_bContentPublic || !$this->_oContentProfile)
  89. return false;
  90. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  91. if(!$oConnection)
  92. return false;
  93. $iFriends = $oConnection->getCommonContentCount($this->_oContentProfile->id(), bx_get_logged_profile_id(), true);
  94. if(!$iFriends && !$this->_bShowZeros)
  95. return false;
  96. $sTitle = _t('_sys_menu_item_title_sm_friends_mutual', $iFriends);
  97. if($this->_bIsApi)
  98. return $this->_getMenuItemAPI($aItem, 'text', [
  99. 'title' => $sTitle
  100. ]);
  101. return $this->getUnitMetaItemText($sTitle);
  102. }
  103. protected function _getMenuItemSubscribers($aItem)
  104. {
  105. $CNF = &$this->_oModule->_oConfig->CNF;
  106. if(!$this->_bContentPublic || !$this->_oContentProfile)
  107. return false;
  108. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  109. if(!$oConnection)
  110. return false;
  111. $iSubscribers = $oConnection->getConnectedInitiatorsCount($this->_oContentProfile->id());
  112. if(!$iSubscribers && !$this->_bShowZeros)
  113. return false;
  114. $sIcon = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIconAsHtml(!empty($aItem['icon']) ? $aItem['icon'] : '');
  115. if($this->_bIsApi)
  116. return false;
  117. return $this->getUnitMetaItemCustom($oConnection->getCounter($this->_oContentProfile->id(), false, [
  118. 'caption' => '_sys_menu_item_title_sm_subscribers',
  119. 'custom_icon' => $sIcon
  120. ], BX_CONNECTIONS_CONTENT_TYPE_INITIATORS));
  121. }
  122. protected function _getMenuItemConnection($sConnection, $sAction, &$aItem)
  123. {
  124. $CNF = &$this->_oModule->_oConfig->CNF;
  125. if(!isLogged() || $this->_oModule->{$this->_aConnectionToFunctionCheck[$sConnection][$sAction]}($this->_aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  126. return false;
  127. $iContentProfile = $this->_oContentProfile->id();
  128. $oConnection = BxDolConnection::getObjectInstance($sConnection);
  129. if(!$oConnection)
  130. return false;
  131. $sTitle = $this->{$this->_aConnectionToFunctionTitle[$sConnection]}($sAction, $oConnection);
  132. if(empty($sTitle))
  133. return false;
  134. if($this->_bIsApi)
  135. return $this->_getMenuItemAPI($aItem, ['display' => 'button', 'content' => 'text'], [
  136. 'title' => $sTitle,
  137. 'primary' => !empty($aItem['primary'])
  138. ]);
  139. return [
  140. $this->getUnitMetaItemButton($sTitle, array(
  141. 'class' => !empty($aItem['primary']) ? 'bx-btn-primary' : '',
  142. 'onclick' => $this->getMenuItemConnectionJsCode($sConnection, $sAction, $iContentProfile, $aItem)
  143. )),
  144. 'bx-menu-item-button'
  145. ];
  146. }
  147. protected function _getMenuItemProfilesFriendsTitle($sAction, &$oConnection)
  148. {
  149. $iProfile = bx_get_logged_profile_id();
  150. $iContentProfile = $this->_oContentProfile->id();
  151. $aResult = array();
  152. if($oConnection->isConnectedNotMutual($iProfile, $iContentProfile))
  153. $aResult = array(
  154. 'add' => '',
  155. 'remove' => _t('_sys_menu_item_title_sm_unfriend_cancel'),
  156. );
  157. else if($oConnection->isConnectedNotMutual($iContentProfile, $iProfile))
  158. $aResult = array(
  159. 'add' => _t('_sys_menu_item_title_sm_befriend_confirm'),
  160. 'remove' => _t('_sys_menu_item_title_sm_unfriend_reject'),
  161. );
  162. else if($oConnection->isConnected($iProfile, $iContentProfile, true))
  163. $aResult = array(
  164. 'add' => '',
  165. 'remove' => _t('_sys_menu_item_title_sm_unfriend'),
  166. );
  167. else
  168. $aResult = array(
  169. 'add' => _t('_sys_menu_item_title_sm_befriend'),
  170. 'remove' => '',
  171. );
  172. return !empty($sAction) && isset($aResult[$sAction]) ? $aResult[$sAction] : $aResult;
  173. }
  174. protected function _getMenuItemProfilesSubscriptionsTitle($sAction, &$oConnection)
  175. {
  176. $CNF = &$this->_oModule->_oConfig->CNF;
  177. $iProfile = bx_get_logged_profile_id();
  178. $iContentProfile = $this->_oContentProfile->id();
  179. $aResult = array();
  180. if($oConnection->isConnected($iProfile, $iContentProfile))
  181. $aResult = array(
  182. 'add' => '',
  183. 'remove' => _t(!empty($CNF['T']['menu_item_title_unsubscribe']) ? $CNF['T']['menu_item_title_unsubscribe'] : '_sys_menu_item_title_sm_unsubscribe'),
  184. );
  185. else
  186. $aResult = array(
  187. 'add' => _t(!empty($CNF['T']['menu_item_title_subscribe']) ? $CNF['T']['menu_item_title_subscribe'] : '_sys_menu_item_title_sm_subscribe'),
  188. 'remove' => '',
  189. );
  190. return !empty($sAction) && !empty($aResult[$sAction]) ? $aResult[$sAction] : $aResult;
  191. }
  192. protected function _getMenuItemMembership($aItem)
  193. {
  194. $CNF = &$this->_oModule->_oConfig->CNF;
  195. if(!$this->_bContentPublic || !$this->_oContentProfile)
  196. return false;
  197. $aMembership = BxDolAcl::getInstance()->getMemberMembershipInfo($this->_oContentProfile->id());
  198. if(empty($aMembership) || !is_array($aMembership))
  199. return false;
  200. $sTitle = _t($aMembership['name']);
  201. if($this->_bIsApi)
  202. return $this->_getMenuItemAPI($aItem, 'text', [
  203. 'title' => $sTitle
  204. ]);
  205. return $this->getUnitMetaItemText($sTitle);
  206. }
  207. }
  208. /** @} */