BxBaseModGroupsGridConnections.php 14 KB

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