BxBaseModGroupsGridConnections.php 16 KB

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