BxBaseModProfileMenuSnippetMeta.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php use Symfony\Component\Finder\Tests\FakeAdapter\DummyAdapter;
  2. defined('BX_DOL') or die('hack attempt');
  3. /**
  4. * Copyright (c) UNA, Inc - https://una.io
  5. * MIT License - https://opensource.org/licenses/MIT
  6. *
  7. * @defgroup BaseProfile Base classes for profile modules
  8. * @ingroup UnaModules
  9. *
  10. * @{
  11. */
  12. class BxBaseModProfileMenuSnippetMeta extends BxBaseModGeneralMenuSnippetMeta
  13. {
  14. protected $_bContentPublic;
  15. protected $_oContentProfile;
  16. protected $_aConnectionToFunctionCheck;
  17. protected $_aConnectionToFunctionTitle;
  18. public function __construct($aObject, $oTemplate = false)
  19. {
  20. parent::__construct($aObject, $oTemplate);
  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. public function setContentPublic($bContentPublic)
  44. {
  45. $this->_bContentPublic = $bContentPublic;
  46. }
  47. protected function _getMenuItemBefriend($aItem)
  48. {
  49. return $this->_getMenuItemConnection('sys_profiles_friends', 'add', $aItem);
  50. }
  51. protected function _getMenuItemUnfriend($aItem)
  52. {
  53. return $this->_getMenuItemConnection('sys_profiles_friends', 'remove', $aItem);
  54. }
  55. protected function _getMenuItemSubscribe($aItem)
  56. {
  57. return $this->_getMenuItemConnection('sys_profiles_subscriptions', 'add', $aItem);
  58. }
  59. protected function _getMenuItemUnsubscribe($aItem)
  60. {
  61. return $this->_getMenuItemConnection('sys_profiles_subscriptions', 'remove', $aItem);
  62. }
  63. protected function _getMenuItemFriends($aItem)
  64. {
  65. $CNF = &$this->_oModule->_oConfig->CNF;
  66. if(!$this->_bContentPublic || !$this->_oContentProfile)
  67. return false;
  68. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_friends');
  69. if(!$oConnection)
  70. return false;
  71. $iFriends = $oConnection->getConnectedInitiatorsCount($this->_oContentProfile->id(), true);
  72. if(!$iFriends)
  73. return false;
  74. return $this->_oTemplate->getUnitMetaItemText(_t('_sys_menu_item_title_sm_friends', $iFriends));
  75. }
  76. protected function _getMenuItemSubscribers($aItem)
  77. {
  78. $CNF = &$this->_oModule->_oConfig->CNF;
  79. if(!$this->_bContentPublic || !$this->_oContentProfile)
  80. return false;
  81. $oConnection = BxDolConnection::getObjectInstance('sys_profiles_subscriptions');
  82. if(!$oConnection)
  83. return false;
  84. $iSubscribers = $oConnection->getConnectedInitiatorsCount($this->_oContentProfile->id());
  85. if(!$iSubscribers)
  86. return false;
  87. return $this->_oTemplate->getUnitMetaItemText(_t('_sys_menu_item_title_sm_subscribers', $iSubscribers));
  88. }
  89. protected function _getMenuItemConnection($sConnection, $sAction, &$aItem)
  90. {
  91. $CNF = &$this->_oModule->_oConfig->CNF;
  92. if(!isLogged() || $this->_oModule->{$this->_aConnectionToFunctionCheck[$sConnection][$sAction]}($this->_aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  93. return false;
  94. $iProfile = bx_get_logged_profile_id();
  95. $iContentProfile = $this->_oContentProfile->id();
  96. $oConnection = BxDolConnection::getObjectInstance($sConnection);
  97. if(!$oConnection)
  98. return false;
  99. $sTitle = $this->{$this->_aConnectionToFunctionTitle[$sConnection]}($sAction, $oConnection);
  100. if(empty($sTitle))
  101. return false;
  102. return $this->_oTemplate->getUnitMetaItemButtonSmall($sTitle, array(
  103. 'onclick' => "bx_conn_action(this, '" . $sConnection . "', '" . $sAction . "', '" . $iContentProfile . "')"
  104. ));
  105. }
  106. protected function _getMenuItemProfilesFriendsTitle($sAction, &$oConnection)
  107. {
  108. $iProfile = bx_get_logged_profile_id();
  109. $iContentProfile = $this->_oContentProfile->id();
  110. $aResult = array();
  111. if($oConnection->isConnectedNotMutual($iProfile, $iContentProfile))
  112. $aResult = array(
  113. 'add' => '',
  114. 'remove' => _t('_sys_menu_item_title_sm_unfriend_cancel'),
  115. );
  116. else if($oConnection->isConnectedNotMutual($iContentProfile, $iProfile))
  117. $aResult = array(
  118. 'add' => _t('_sys_menu_item_title_sm_befriend_confirm'),
  119. 'remove' => _t('_sys_menu_item_title_sm_unfriend_reject'),
  120. );
  121. else if($oConnection->isConnected($iProfile, $iContentProfile, true))
  122. $aResult = array(
  123. 'add' => '',
  124. 'remove' => _t('_sys_menu_item_title_sm_unfriend'),
  125. );
  126. else
  127. $aResult = array(
  128. 'add' => _t('_sys_menu_item_title_sm_befriend'),
  129. 'remove' => '',
  130. );
  131. return !empty($sAction) && isset($aResult[$sAction]) ? $aResult[$sAction] : $aResult;
  132. }
  133. protected function _getMenuItemProfilesSubscriptionsTitle($sAction, &$oConnection)
  134. {
  135. $iProfile = bx_get_logged_profile_id();
  136. $iContentProfile = $this->_oContentProfile->id();
  137. $aResult = array();
  138. if($oConnection->isConnected($iProfile, $iContentProfile))
  139. $aResult = array(
  140. 'add' => '',
  141. 'remove' => _t('_sys_menu_item_title_sm_unsubscribe'),
  142. );
  143. else
  144. $aResult = array(
  145. 'add' => _t('_sys_menu_item_title_sm_subscribe'),
  146. 'remove' => '',
  147. );
  148. return !empty($sAction) && !empty($aResult[$sAction]) ? $aResult[$sAction] : $aResult;
  149. }
  150. }
  151. /** @} */