BxBaseModGroupsGridConnections.php 15 KB

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