BxBaseModProfileMenuSnippetMeta.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. return $this->getUnitMetaItemText(_t('_sys_menu_item_title_sm_friends', $iFriends));
  79. }
  80. protected function _getMenuItemFriendsMutual($aItem)
  81. {
  82. $CNF = &$this->_oModule->_oConfig->CNF;
  83. if(!$this->_bContentPublic || !$this->_oContentProfile)
  84. return false;
  85. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  86. if(!$oConnection)
  87. return false;
  88. $iFriends = $oConnection->getCommonContentCount($this->_oContentProfile->id(), bx_get_logged_profile_id(), true);
  89. if(!$iFriends && !$this->_bShowZeros)
  90. return false;
  91. return $this->getUnitMetaItemText(_t('_sys_menu_item_title_sm_friends_mutual', $iFriends));
  92. }
  93. protected function _getMenuItemSubscribers($aItem)
  94. {
  95. $CNF = &$this->_oModule->_oConfig->CNF;
  96. if(!$this->_bContentPublic || !$this->_oContentProfile)
  97. return false;
  98. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  99. if(!$oConnection)
  100. return false;
  101. $iSubscribers = $oConnection->getConnectedInitiatorsCount($this->_oContentProfile->id());
  102. if(!$iSubscribers && !$this->_bShowZeros)
  103. return false;
  104. $sIcon = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIconAsHtml(!empty($aItem['icon']) ? $aItem['icon'] : '');
  105. return $this->getUnitMetaItemCustom($oConnection->getCounter($this->_oContentProfile->id(), false, [
  106. 'caption' => '_sys_menu_item_title_sm_subscribers',
  107. 'custom_icon' => $sIcon
  108. ], BX_CONNECTIONS_CONTENT_TYPE_INITIATORS));
  109. }
  110. protected function _getMenuItemConnection($sConnection, $sAction, &$aItem)
  111. {
  112. $CNF = &$this->_oModule->_oConfig->CNF;
  113. if(!isLogged() || $this->_oModule->{$this->_aConnectionToFunctionCheck[$sConnection][$sAction]}($this->_aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  114. return false;
  115. $iContentProfile = $this->_oContentProfile->id();
  116. $oConnection = BxDolConnection::getObjectInstance($sConnection);
  117. if(!$oConnection)
  118. return false;
  119. $sTitle = $this->{$this->_aConnectionToFunctionTitle[$sConnection]}($sAction, $oConnection);
  120. if(empty($sTitle))
  121. return false;
  122. return [
  123. $this->getUnitMetaItemButton($sTitle, array(
  124. 'class' => !empty($aItem['primary']) ? 'bx-btn-primary' : '',
  125. 'onclick' => $this->getMenuItemConnectionJsCode($sConnection, $sAction, $iContentProfile, $aItem)
  126. )),
  127. 'bx-menu-item-button'
  128. ];
  129. }
  130. protected function _getMenuItemProfilesFriendsTitle($sAction, &$oConnection)
  131. {
  132. $iProfile = bx_get_logged_profile_id();
  133. $iContentProfile = $this->_oContentProfile->id();
  134. $aResult = array();
  135. if($oConnection->isConnectedNotMutual($iProfile, $iContentProfile))
  136. $aResult = array(
  137. 'add' => '',
  138. 'remove' => _t('_sys_menu_item_title_sm_unfriend_cancel'),
  139. );
  140. else if($oConnection->isConnectedNotMutual($iContentProfile, $iProfile))
  141. $aResult = array(
  142. 'add' => _t('_sys_menu_item_title_sm_befriend_confirm'),
  143. 'remove' => _t('_sys_menu_item_title_sm_unfriend_reject'),
  144. );
  145. else if($oConnection->isConnected($iProfile, $iContentProfile, true))
  146. $aResult = array(
  147. 'add' => '',
  148. 'remove' => _t('_sys_menu_item_title_sm_unfriend'),
  149. );
  150. else
  151. $aResult = array(
  152. 'add' => _t('_sys_menu_item_title_sm_befriend'),
  153. 'remove' => '',
  154. );
  155. return !empty($sAction) && isset($aResult[$sAction]) ? $aResult[$sAction] : $aResult;
  156. }
  157. protected function _getMenuItemProfilesSubscriptionsTitle($sAction, &$oConnection)
  158. {
  159. $CNF = &$this->_oModule->_oConfig->CNF;
  160. $iProfile = bx_get_logged_profile_id();
  161. $iContentProfile = $this->_oContentProfile->id();
  162. $aResult = array();
  163. if($oConnection->isConnected($iProfile, $iContentProfile))
  164. $aResult = array(
  165. 'add' => '',
  166. 'remove' => _t(!empty($CNF['T']['menu_item_title_unsubscribe']) ? $CNF['T']['menu_item_title_unsubscribe'] : '_sys_menu_item_title_sm_unsubscribe'),
  167. );
  168. else
  169. $aResult = array(
  170. 'add' => _t(!empty($CNF['T']['menu_item_title_subscribe']) ? $CNF['T']['menu_item_title_subscribe'] : '_sys_menu_item_title_sm_subscribe'),
  171. 'remove' => '',
  172. );
  173. return !empty($sAction) && !empty($aResult[$sAction]) ? $aResult[$sAction] : $aResult;
  174. }
  175. protected function _getMenuItemMembership($aItem)
  176. {
  177. $CNF = &$this->_oModule->_oConfig->CNF;
  178. if(!$this->_bContentPublic || !$this->_oContentProfile)
  179. return false;
  180. $aMembership = BxDolAcl::getInstance()->getMemberMembershipInfo($this->_oContentProfile->id());
  181. return $aMembership ? $this->getUnitMetaItemText(_t($aMembership['name'])) : false;
  182. }
  183. }
  184. /** @} */