BxBaseModProfileMenuSnippetMeta.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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' => 'element'], [
  136. 'title' => $sTitle,
  137. 'data' => [
  138. 'type' => 'connections',
  139. 'o' => $sConnection,
  140. 'a' => $sAction,
  141. 'iid' => bx_get_logged_profile_id(),
  142. 'cid' => $iContentProfile,
  143. 'title' => $sTitle,
  144. 'primary' => !empty($aItem['primary']),
  145. ]
  146. ]);
  147. return [
  148. $this->getUnitMetaItemButton($sTitle, array(
  149. 'class' => !empty($aItem['primary']) ? 'bx-btn-primary' : '',
  150. 'onclick' => $this->getMenuItemConnectionJsCode($sConnection, $sAction, $iContentProfile, $aItem)
  151. )),
  152. 'bx-menu-item-button'
  153. ];
  154. }
  155. protected function _getMenuItemProfilesFriendsTitle($sAction, &$oConnection)
  156. {
  157. $iProfile = bx_get_logged_profile_id();
  158. $iContentProfile = $this->_oContentProfile->id();
  159. $aResult = array();
  160. if($oConnection->isConnectedNotMutual($iProfile, $iContentProfile))
  161. $aResult = array(
  162. 'add' => '',
  163. 'remove' => _t('_sys_menu_item_title_sm_unfriend_cancel'),
  164. );
  165. else if($oConnection->isConnectedNotMutual($iContentProfile, $iProfile))
  166. $aResult = array(
  167. 'add' => _t('_sys_menu_item_title_sm_befriend_confirm'),
  168. 'remove' => _t('_sys_menu_item_title_sm_unfriend_reject'),
  169. );
  170. else if($oConnection->isConnected($iProfile, $iContentProfile, true))
  171. $aResult = array(
  172. 'add' => '',
  173. 'remove' => _t('_sys_menu_item_title_sm_unfriend'),
  174. );
  175. else
  176. $aResult = array(
  177. 'add' => _t('_sys_menu_item_title_sm_befriend'),
  178. 'remove' => '',
  179. );
  180. return !empty($sAction) && isset($aResult[$sAction]) ? $aResult[$sAction] : $aResult;
  181. }
  182. protected function _getMenuItemProfilesSubscriptionsTitle($sAction, &$oConnection)
  183. {
  184. $CNF = &$this->_oModule->_oConfig->CNF;
  185. $iProfile = bx_get_logged_profile_id();
  186. $iContentProfile = $this->_oContentProfile->id();
  187. $aResult = array();
  188. if($oConnection->isConnected($iProfile, $iContentProfile))
  189. $aResult = array(
  190. 'add' => '',
  191. 'remove' => _t(!empty($CNF['T']['menu_item_title_unsubscribe']) ? $CNF['T']['menu_item_title_unsubscribe'] : '_sys_menu_item_title_sm_unsubscribe'),
  192. );
  193. else
  194. $aResult = array(
  195. 'add' => _t(!empty($CNF['T']['menu_item_title_subscribe']) ? $CNF['T']['menu_item_title_subscribe'] : '_sys_menu_item_title_sm_subscribe'),
  196. 'remove' => '',
  197. );
  198. return !empty($sAction) && !empty($aResult[$sAction]) ? $aResult[$sAction] : $aResult;
  199. }
  200. protected function _getMenuItemMembership($aItem)
  201. {
  202. $CNF = &$this->_oModule->_oConfig->CNF;
  203. if(!$this->_bContentPublic || !$this->_oContentProfile)
  204. return false;
  205. $aMembership = BxDolAcl::getInstance()->getMemberMembershipInfo($this->_oContentProfile->id());
  206. if(empty($aMembership) || !is_array($aMembership))
  207. return false;
  208. $sTitle = _t($aMembership['name']);
  209. if($this->_bIsApi)
  210. return $this->_getMenuItemAPI($aItem, 'text', [
  211. 'title' => $sTitle
  212. ]);
  213. return $this->getUnitMetaItemText($sTitle);
  214. }
  215. }
  216. /** @} */