1
0

BxBaseModGroupsGridQuestionsManage.php 6.3 KB

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