BxBaseModGroupsGridConnections.php 19 KB

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