1
0

BxBaseModGroupsGridConnections.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 BaseGroups Base classes for groups modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxBaseModGroupsGridConnections extends BxDolGridConnections
  12. {
  13. protected $_sContentModule;
  14. protected $_iGroupProfileId;
  15. protected $_oModule;
  16. protected $_aContentInfo = array();
  17. public function __construct ($aOptions, $oTemplate = false)
  18. {
  19. $this->_oModule = BxDolModule::getInstance($this->_sContentModule);
  20. $this->_sObjectConnections = $this->_oModule->_oConfig->CNF['OBJECT_CONNECTIONS'];
  21. parent::__construct ($aOptions, $oTemplate);
  22. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  23. if (!$iProfileId)
  24. return;
  25. $oProfile = BxDolProfile::getInstance($iProfileId);
  26. if (!$oProfile)
  27. return;
  28. $aProfileInfo = $oProfile->getInfo();
  29. $this->_iGroupProfileId = $oProfile->id();
  30. $this->_aContentInfo = BxDolService::call($aProfileInfo['type'], 'get_content_info_by_id', array($aProfileInfo['content_id']));
  31. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedEdit($this->_aContentInfo) || $oProfile->id() == bx_get_logged_profile_id())
  32. $this->_bOwner = true;
  33. $aSQLParts = $this->_oConnection->getConnectedInitiatorsAsSQLParts('p', 'id', $this->_iGroupProfileId, $this->_bOwner ? false : true);
  34. $this->addMarkers(array(
  35. 'profile_id' => $oProfile->id(),
  36. 'join_connections' => $aSQLParts['join'],
  37. 'content_module' => $this->_sContentModule,
  38. ));
  39. }
  40. protected function _getActionAccept ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  41. {
  42. if (isset($aRow[$this->_aOptions['field_id']]))
  43. $a['attr']['bx_grid_action_data'] = $aRow[$this->_aOptions['field_id']] . ':' . $this->_iGroupProfileId;
  44. return parent::_getActionAccept ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  45. }
  46. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  47. {
  48. if (isset($aRow[$this->_aOptions['field_id']]))
  49. $a['attr']['bx_grid_action_data'] = $aRow[$this->_aOptions['field_id']] . ':' . $this->_iGroupProfileId;
  50. return parent::_getActionDelete ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  51. }
  52. protected function _getActionToAdmins ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  53. {
  54. if ($this->_oModule->_oDb->isAdmin($this->_iGroupProfileId, $aRow[$this->_aOptions['field_id']]))
  55. return '';
  56. return $this->_getAction_Admins ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  57. }
  58. protected function _getActionFromAdmins ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  59. {
  60. if (!$this->_oModule->_oDb->isAdmin($this->_iGroupProfileId, $aRow[$this->_aOptions['field_id']]))
  61. return '';
  62. return $this->_getAction_Admins ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  63. }
  64. protected function _getAction_Admins ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  65. {
  66. if (!$this->_oConnection->isConnected($aRow[$this->_aOptions['field_id']], $this->_iGroupProfileId, true))
  67. return '';
  68. if (CHECK_ACTION_RESULT_ALLOWED !== $this->_oModule->checkAllowedManageAdmins($this->_iGroupProfileId))
  69. return '';
  70. if (isset($aRow[$this->_aOptions['field_id']]))
  71. $a['attr']['bx_grid_action_data'] = $aRow[$this->_aOptions['field_id']] . ':' . $this->_iGroupProfileId;
  72. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  73. }
  74. /**
  75. * 'To Admins' action handler
  76. */
  77. public function performActionToAdmins()
  78. {
  79. $this->_performActionAdmins('toAdmins');
  80. }
  81. /**
  82. * 'From Admins' action handler
  83. */
  84. public function performActionFromAdmins()
  85. {
  86. $this->_performActionAdmins('fromAdmins');
  87. }
  88. public function _performActionAdmins($sFunc)
  89. {
  90. list ($iId, $iGroupProfileId) = $this->_prepareIds();
  91. if (!$iId) {
  92. echoJson(array('msg' => _t('_sys_txt_error_occured')));
  93. exit;
  94. }
  95. if (CHECK_ACTION_RESULT_ALLOWED !== $this->_oModule->checkAllowedManageAdmins($iGroupProfileId)) {
  96. echoJson(array('msg' => _t('_sys_txt_access_denied')));
  97. exit;
  98. }
  99. if (!$this->_oModule->_oDb->$sFunc($iGroupProfileId, $iId)) {
  100. echoJson(array('msg' => _t('_sys_txt_error_occured')));
  101. }
  102. else {
  103. $sEmailTemplate = 'toAdmins' == $sFunc ? $this->_oModule->_oConfig->CNF['EMAIL_FAN_BECOME_ADMIN'] : $this->_oModule->_oConfig->CNF['EMAIL_ADMIN_BECOME_FAN'];
  104. list($iGroupProfileId, $iProfileId) = $this->_prepareGroupProfileAndMemberProfile($iGroupProfileId, $iId);
  105. if (bx_get_logged_profile_id() != $iProfileId) {
  106. // notify about admin status
  107. sendMailTemplate($sEmailTemplate, 0, $iProfileId, array(
  108. 'EntryUrl' => BxDolProfile::getInstance($iGroupProfileId)->getUrl(),
  109. 'EntryTitle' => BxDolProfile::getInstance($iGroupProfileId)->getDisplayName(),
  110. ), BX_EMAIL_NOTIFY);
  111. }
  112. // subscribe admins automatically
  113. if ('toAdmins' == $sFunc && ($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')))
  114. $oConn->addConnection($iProfileId, $iGroupProfileId);
  115. echoJson(array('grid' => $this->getCode(false), 'blink' => $iId));
  116. }
  117. }
  118. protected function _delete ($mixedId)
  119. {
  120. list ($iId, $iViewedId) = $this->_prepareIds();
  121. // send email notification
  122. $sEmailTemplate = $this->_oConnection->isConnected($iViewedId, $iId, true) ? $this->_oModule->_oConfig->CNF['EMAIL_FAN_REMOVE'] : $this->_oModule->_oConfig->CNF['EMAIL_JOIN_REJECT'];
  123. list($iGroupProfileId, $iProfileId) = $this->_prepareGroupProfileAndMemberProfile($iId, $iViewedId);
  124. if (bx_get_logged_profile_id() != $iProfileId) {
  125. sendMailTemplate($sEmailTemplate, 0, $iProfileId, array(
  126. 'EntryUrl' => BxDolProfile::getInstance($iGroupProfileId)->getUrl(),
  127. 'EntryTitle' => BxDolProfile::getInstance($iGroupProfileId)->getDisplayName(),
  128. ), BX_EMAIL_NOTIFY);
  129. }
  130. // delete admin associated with profile
  131. $this->_oModule->_oDb->deleteAdminsByProfileId($iProfileId);
  132. return parent::_delete ($mixedId);
  133. }
  134. /**
  135. * @return array where first element is group profile id and second element is some other persons profile id
  136. */
  137. protected function _prepareGroupProfileAndMemberProfile($iId1, $iId2)
  138. {
  139. if (BxDolProfile::getInstance($iId1)->getModule() == $this->_sContentModule)
  140. return array($iId1, $iId2);
  141. else
  142. return array($iId2, $iId1);
  143. }
  144. }
  145. /** @} */