BxBaseModGroupsGridConnections.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 $_oModule;
  14. protected $_sContentModule;
  15. protected $_bRoles;
  16. protected $_aRoles;
  17. protected $_iGroupProfileId;
  18. protected $_aContentInfo = array();
  19. public function __construct ($aOptions, $oTemplate = false)
  20. {
  21. $this->_oModule = BxDolModule::getInstance($this->_sContentModule);
  22. $this->_sObjectConnections = $this->_oModule->_oConfig->CNF['OBJECT_CONNECTIONS'];
  23. parent::__construct ($aOptions, $oTemplate);
  24. if(!$this->_bInit)
  25. return;
  26. $this->_bRoles = $this->_oModule->_oConfig->isRoles();
  27. $this->_aRoles = $this->_oModule->_oConfig->getRoles();
  28. $this->_iGroupProfileId = $this->_oProfile->id();
  29. $this->_aContentInfo = $this->_oModule->_oDb->getContentInfoById($this->_oProfile->getContentId());
  30. if($this->_oModule->checkAllowedEdit($this->_aContentInfo) === CHECK_ACTION_RESULT_ALLOWED || $this->_iGroupProfileId == bx_get_logged_profile_id())
  31. $this->_bOwner = true;
  32. $aSQLParts = $this->_oConnection->getConnectedInitiatorsAsSQLParts('p', 'id', $this->_iGroupProfileId, $this->_bOwner ? false : true);
  33. $this->addMarkers(array(
  34. 'profile_id' => $this->_iGroupProfileId,
  35. 'join_connections' => $aSQLParts['join'],
  36. 'content_module' => $this->_sContentModule,
  37. ));
  38. }
  39. public function getCode ($isDisplayHeader = true)
  40. {
  41. $sResult = parent::getCode();
  42. if(!$sResult)
  43. return $sResult;
  44. if($this->_bRoles)
  45. $sResult .= $this->_oModule->_oTemplate->getJsCode('main', array(
  46. 'sObjNameGrid' => $this->_sObject,
  47. ));
  48. return $sResult;
  49. }
  50. public function performActionSetRole()
  51. {
  52. if(!$this->_bRoles)
  53. return echoJson(array());
  54. if($this->_oModule->checkAllowedManageAdmins($this->_iGroupProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  55. return echoJson(array('msg' => _t('_sys_txt_access_denied')));
  56. list($iId, $iViewedId) = $this->_prepareIds();
  57. if(!$iId)
  58. return echoJson(array('msg' => _t('_sys_txt_error_occured')));
  59. $sJsObject = $this->_oModule->_oConfig->getJsObject('main');
  60. $sHtmlIdPrefix = str_replace('_', '-', $this->_sContentModule) . '-set-role-';
  61. $aMenuItems = array();
  62. foreach($this->_aRoles as $iRole => $sRole)
  63. $aMenuItems[] = array(
  64. 'id' => $sHtmlIdPrefix . $iRole,
  65. 'name' => $sHtmlIdPrefix . $iRole,
  66. 'class' => '',
  67. 'link' => 'javascript:void(0)',
  68. 'onclick' => $sJsObject . '.onClickSetRole(' . $iId . ', ' . $iRole . ')',
  69. 'target' => '_self',
  70. 'title' => $sRole,
  71. 'active' => 1
  72. );
  73. if(empty($aMenuItems))
  74. return echoJson(array('msg' => _t('_sys_txt_error_occured')));
  75. $oMenu = new BxTemplMenu(array('template' => 'menu_vertical.html', 'menu_id'=> $sHtmlIdPrefix . 'menu', 'menu_items' => $aMenuItems));
  76. $iRole = $this->_oModule->_oDb->getRole($this->_iGroupProfileId, $iId);
  77. if(!empty($iRole))
  78. $oMenu->setSelected('', $sHtmlIdPrefix . $iRole);
  79. return echoJson(array('popup' => BxTemplFunctions::getInstance()->transBox($sHtmlIdPrefix . 'popup', $oMenu->getCode())));
  80. }
  81. public function performActionSetRoleSubmit()
  82. {
  83. if(!$this->_bRoles)
  84. return echoJson(array());
  85. if($this->_oModule->checkAllowedManageAdmins($this->_iGroupProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  86. return echoJson(array('msg' => _t('_sys_txt_access_denied')));
  87. list($iId, $iViewedId) = $this->_prepareIds();
  88. if(!$iId || bx_get('role') === false)
  89. return echoJson(array('msg' => _t('_sys_txt_error_occured')));
  90. $aResult = $this->_oModule->setRole($this->_iGroupProfileId, $iId, (int)bx_get('role'));
  91. if($aResult['code'] != 0)
  92. return echoJson(array('msg' => $aResult['msg']));
  93. echoJson(array('grid' => $this->getCode(false), 'blink' => $iId));
  94. }
  95. protected function _getCellRole($mixedValue, $sKey, $aField, $aRow)
  96. {
  97. $mixedValue = '';
  98. $iRole = $this->_oModule->_oDb->getRole($this->_iGroupProfileId, $aRow[$this->_aOptions['field_id']]);
  99. if(!empty($iRole) && !empty($this->_aRoles[$iRole]))
  100. $mixedValue = $this->_aRoles[$iRole];
  101. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  102. }
  103. protected function _getActionSetRole ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  104. {
  105. if ($this->_oModule->checkAllowedManageAdmins($this->_iGroupProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  106. return '';
  107. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  108. }
  109. protected function _getActionSetRoleSubmit ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  110. {
  111. return '';
  112. }
  113. protected function _getActionAccept ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  114. {
  115. if ($this->_oModule->checkAllowedManageFans($this->_iGroupProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  116. return '';
  117. if (isset($aRow[$this->_aOptions['field_id']]))
  118. $a['attr']['bx_grid_action_data'] = $aRow[$this->_aOptions['field_id']] . ':' . $this->_iGroupProfileId;
  119. return parent::_getActionAccept ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  120. }
  121. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  122. {
  123. if ($this->_oModule->checkAllowedManageFans($this->_iGroupProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  124. return '';
  125. if (isset($aRow[$this->_aOptions['field_id']]))
  126. $a['attr']['bx_grid_action_data'] = $aRow[$this->_aOptions['field_id']] . ':' . $this->_iGroupProfileId;
  127. return parent::_getActionDelete ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  128. }
  129. /**
  130. * Note. Methods related to 'To Admin'/'From Admin' functionality can be removed after UNA 12 will be released.
  131. */
  132. protected function _getActionToAdmins ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  133. {
  134. if ($this->_oModule->_oDb->isAdmin($this->_iGroupProfileId, $aRow[$this->_aOptions['field_id']]))
  135. return '';
  136. return $this->_getActionManageAdmins ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  137. }
  138. protected function _getActionFromAdmins ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  139. {
  140. if (!$this->_oModule->_oDb->isAdmin($this->_iGroupProfileId, $aRow[$this->_aOptions['field_id']]))
  141. return '';
  142. return $this->_getActionManageAdmins ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  143. }
  144. protected function _getActionManageAdmins ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  145. {
  146. if (!$this->_oConnection->isConnected($aRow[$this->_aOptions['field_id']], $this->_iGroupProfileId, true))
  147. return '';
  148. if ($this->_oModule->checkAllowedManageAdmins($this->_iGroupProfileId) !== CHECK_ACTION_RESULT_ALLOWED)
  149. return '';
  150. if (isset($aRow[$this->_aOptions['field_id']]))
  151. $a['attr']['bx_grid_action_data'] = $aRow[$this->_aOptions['field_id']] . ':' . $this->_iGroupProfileId;
  152. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  153. }
  154. /**
  155. * 'To Admins' action handler
  156. */
  157. public function performActionToAdmins()
  158. {
  159. $this->_performActionAdmins('toAdmins');
  160. }
  161. /**
  162. * 'From Admins' action handler
  163. */
  164. public function performActionFromAdmins()
  165. {
  166. $this->_performActionAdmins('fromAdmins');
  167. }
  168. public function _performActionAdmins($sFunc)
  169. {
  170. list ($iId, $iGroupProfileId) = $this->_prepareIds();
  171. if (!$iId) {
  172. echoJson(array('msg' => _t('_sys_txt_error_occured')));
  173. exit;
  174. }
  175. if (CHECK_ACTION_RESULT_ALLOWED !== $this->_oModule->checkAllowedManageAdmins($iGroupProfileId)) {
  176. echoJson(array('msg' => _t('_sys_txt_access_denied')));
  177. exit;
  178. }
  179. if (!$this->_oModule->_oDb->$sFunc($iGroupProfileId, $iId)) {
  180. echoJson(array('msg' => _t('_sys_txt_error_occured')));
  181. }
  182. else {
  183. $sEmailTemplate = 'toAdmins' == $sFunc ? $this->_oModule->_oConfig->CNF['EMAIL_FAN_BECOME_ADMIN'] : $this->_oModule->_oConfig->CNF['EMAIL_ADMIN_BECOME_FAN'];
  184. list($iGroupProfileId, $iProfileId) = $this->_prepareGroupProfileAndMemberProfile($iGroupProfileId, $iId);
  185. if (bx_get_logged_profile_id() != $iProfileId) {
  186. // notify about admin status
  187. sendMailTemplate($sEmailTemplate, 0, $iProfileId, array(
  188. 'EntryUrl' => BxDolProfile::getInstance($iGroupProfileId)->getUrl(),
  189. 'EntryTitle' => BxDolProfile::getInstance($iGroupProfileId)->getDisplayName(),
  190. ), BX_EMAIL_NOTIFY);
  191. }
  192. // subscribe admins automatically
  193. if ('toAdmins' == $sFunc && ($oConn = BxDolConnection::getObjectInstance('sys_profiles_subscriptions')))
  194. $oConn->addConnection($iProfileId, $iGroupProfileId);
  195. echoJson(array('grid' => $this->getCode(false), 'blink' => $iId));
  196. }
  197. }
  198. protected function _delete ($mixedId)
  199. {
  200. list ($iId, $iViewedId) = $this->_prepareIds();
  201. // send email notification
  202. $sEmailTemplate = $this->_oConnection->isConnected($iViewedId, $iId, true) ? $this->_oModule->_oConfig->CNF['EMAIL_FAN_REMOVE'] : $this->_oModule->_oConfig->CNF['EMAIL_JOIN_REJECT'];
  203. list($iGroupProfileId, $iProfileId) = $this->_prepareGroupProfileAndMemberProfile($iId, $iViewedId);
  204. if (bx_get_logged_profile_id() != $iProfileId) {
  205. sendMailTemplate($sEmailTemplate, 0, $iProfileId, array(
  206. 'EntryUrl' => BxDolProfile::getInstance($iGroupProfileId)->getUrl(),
  207. 'EntryTitle' => BxDolProfile::getInstance($iGroupProfileId)->getDisplayName(),
  208. ), BX_EMAIL_NOTIFY);
  209. }
  210. // delete admin associated with profile
  211. $this->_oModule->_oDb->deleteAdminsByProfileId($iProfileId);
  212. return parent::_delete ($mixedId);
  213. }
  214. /**
  215. * @return array where first element is group profile id and second element is some other persons profile id
  216. */
  217. protected function _prepareGroupProfileAndMemberProfile($iId1, $iId2)
  218. {
  219. if (BxDolProfile::getInstance($iId1)->getModule() == $this->_sContentModule)
  220. return array($iId1, $iId2);
  221. else
  222. return array($iId2, $iId1);
  223. }
  224. protected function _addJsCss()
  225. {
  226. parent::_addJsCss();
  227. if($this->_bRoles)
  228. $this->_oModule->_oTemplate->addJs('main.js');
  229. }
  230. }
  231. /** @} */