1
0

BxBaseModGroupsGridQuestionsManage.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 BxBaseModGroupsGridQuestionsManage extends BxTemplGrid
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_sParamsDivider = '#-#';
  16. protected $_iGroupProfileId;
  17. protected $_iGroupContentId;
  18. protected $_aGroupContentInfo;
  19. public function __construct ($aOptions, $oTemplate = false)
  20. {
  21. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  22. parent::__construct ($aOptions, $oTemplate);
  23. $this->_iGroupProfileId = 0;
  24. $this->_iGroupContentId = 0;
  25. $this->_aGroupContentInfo = [];
  26. if(($iGroupProfileId = bx_get('profile_id')) !== false)
  27. $this->setProfileId($iGroupProfileId);
  28. }
  29. public function getFormBlockTitleAPI($sAction, $iId = 0)
  30. {
  31. $CNF = &$this->_oModule->_oConfig->CNF;
  32. $sResult = '';
  33. switch($sAction) {
  34. case 'add':
  35. $sResult = _t($CNF['T']['popup_title_question_add']);
  36. break;
  37. case 'edit':
  38. $sResult = _t($CNF['T']['popup_title_question_edit']);
  39. break;
  40. }
  41. return $sResult;
  42. }
  43. public function getFormCallBackUrlAPI($sAction, $iId = 0)
  44. {
  45. return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&profile_id=' . $this->_iGroupProfileId . '&a=' . $sAction . '&id=' . $iId;
  46. }
  47. public function setProfileId($iProfileId)
  48. {
  49. $this->_iGroupProfileId = (int)$iProfileId;
  50. $this->_aQueryAppend['profile_id'] = $this->_iGroupProfileId;
  51. if(($oGroupProfile = BxDolProfile::getInstance($this->_iGroupProfileId)) !== false) {
  52. $this->_iGroupContentId = (int)$oGroupProfile->getContentId();
  53. $this->_aGroupContentInfo = $this->_oModule->_oDb->getContentInfoById($this->_iGroupContentId);
  54. }
  55. }
  56. public function performActionAdd()
  57. {
  58. $CNF = &$this->_oModule->_oConfig->CNF;
  59. $sAction = 'add';
  60. if(($mixedResult = $this->_oModule->checkAllowedEdit($this->_aGroupContentInfo)) !== CHECK_ACTION_RESULT_ALLOWED)
  61. return $this->_bIsApi ? [bx_api_get_msg($mixedResult)] : echoJson(['msg' => $mixedResult]);
  62. $sForm = $CNF['OBJECT_FORM_QUESTION_DISPLAY_ADD'];
  63. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_QUESTION'], $CNF['OBJECT_FORM_QUESTION_DISPLAY_ADD']);
  64. $oForm->setId($sForm);
  65. $oForm->setName($sForm);
  66. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', ['o' => $this->_sObject, 'a' => $sAction, 'profile_id' => $this->_iGroupProfileId]));
  67. $oForm->initChecker();
  68. if($oForm->isSubmittedAndValid()) {
  69. $iId = (int)$oForm->insert(['content_id' => $this->_iGroupContentId, 'added' => time(), 'order' => $this->_oModule->_oDb->getQuestionOrderMax($this->_iGroupContentId) + 1]);
  70. if($iId != 0)
  71. $aResult = ['grid' => $this->getCode(false), 'blink' => $iId];
  72. else
  73. $aResult = ['msg' => _t($CNF['T']['err_cannot_perform'])];
  74. return $this->_bIsApi ? [] : echoJson($aResult);
  75. }
  76. if($this->_bIsApi)
  77. return $this->getFormBlockAPI($oForm, $sAction);
  78. bx_import('BxTemplFunctions');
  79. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('popup_question'), _t($CNF['T']['popup_title_question_add']), $this->_oModule->_oTemplate->parseHtmlByName('popup_qnr_question.html', [
  80. 'form_id' => $oForm->getId(),
  81. 'form' => $oForm->getCode(true),
  82. 'object' => $this->_sObject,
  83. 'action' => $sAction
  84. ]));
  85. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  86. }
  87. public function performActionEdit()
  88. {
  89. $CNF = &$this->_oModule->_oConfig->CNF;
  90. $sAction = 'edit';
  91. if(($mixedResult = $this->_oModule->checkAllowedEdit($this->_aGroupContentInfo)) !== CHECK_ACTION_RESULT_ALLOWED)
  92. return $this->_bIsApi ? [bx_api_get_msg($mixedResult)] : echoJson(['msg' => $mixedResult]);
  93. $aIds = $this->_getIds();
  94. if($aIds === false)
  95. return $this->_bIsApi ? [] : echoJson([]);
  96. $iItem = array_shift($aIds);
  97. $aItem = $this->_oModule->_oDb->getQuestions(['sample' => 'id', 'id' => $iItem]);
  98. if(!is_array($aItem) || empty($aItem))
  99. return $this->_bIsApi ? [] : echoJson([]);
  100. $sForm = $CNF['OBJECT_FORM_QUESTION_DISPLAY_EDIT'];
  101. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_QUESTION'], $CNF['OBJECT_FORM_QUESTION_DISPLAY_EDIT']);
  102. $oForm->setId($sForm);
  103. $oForm->setName($sForm);
  104. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', ['o' => $this->_sObject, 'a' => $sAction, 'profile_id' => $this->_iGroupProfileId, 'id' => $iItem]));
  105. $oForm->initChecker($aItem);
  106. if($oForm->isSubmittedAndValid()) {
  107. if($oForm->update($aItem['id']) !== false)
  108. $aResult = ['grid' => $this->getCode(false), 'blink' => $aItem['id']];
  109. else
  110. $aResult = ['msg' => _t($CNF['T']['err_cannot_perform'])];
  111. return $this->_bIsApi ? [] : echoJson($aResult);
  112. }
  113. if($this->_bIsApi)
  114. return $this->getFormBlockAPI($oForm, $sAction, $iItem);
  115. bx_import('BxTemplFunctions');
  116. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('popup_question'), _t($CNF['T']['popup_title_question_edit']), $this->_oModule->_oTemplate->parseHtmlByName('popup_qnr_question.html', [
  117. 'form_id' => $oForm->getId(),
  118. 'form' => $oForm->getCode(true),
  119. 'object' => $this->_sObject,
  120. 'action' => $sAction
  121. ]));
  122. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  123. }
  124. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  125. {
  126. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString("AND `content_id`=? AND `action`='add' ", $this->_iGroupContentId);
  127. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  128. }
  129. protected function _isVisibleGrid ($a)
  130. {
  131. return $this->_oModule->checkAllowedEdit($this->_aGroupContentInfo) == CHECK_ACTION_RESULT_ALLOWED;
  132. }
  133. protected function _getIds()
  134. {
  135. $aIds = bx_get('ids');
  136. if(!$aIds || !is_array($aIds)) {
  137. $iId = (int)bx_get('id');
  138. if(!$iId)
  139. return false;
  140. $aIds = [$iId];
  141. }
  142. return $aIds;
  143. }
  144. }
  145. /** @} */