1
0

BxBaseModGroupsGridQuestionsManage.php 5.8 KB

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