BxDevFormsPreValues.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 Developer Developer
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxDevFormsPreValues extends BxTemplStudioFormsPreValues
  12. {
  13. function __construct($aOptions, $oTemplate = false)
  14. {
  15. parent::__construct($aOptions, $oTemplate);
  16. $this->oModule = BxDolModule::getInstance('bx_developer');
  17. $this->sUrlPage = BX_DOL_URL_STUDIO . 'module.php?name=' . $this->oModule->_oConfig->getName() . '&page=forms&form_page=pre_values';
  18. $sModule = bx_get('form_module');
  19. if(!empty($sModule)) {
  20. $this->sModule = bx_process_input($sModule);
  21. $this->_aQueryAppend['module'] = $this->sModule;
  22. }
  23. $sList = bx_get('form_list');
  24. if(!empty($sList)) {
  25. $this->sList = bx_process_input($sList);
  26. $this->_aQueryAppend['list'] = $this->sList;
  27. }
  28. }
  29. public function performActionAdd()
  30. {
  31. $sAction = 'add';
  32. $sFormObject = $this->oModule->_oConfig->getObject('form_forms_prevalue');
  33. $sFormDisplay = $this->oModule->_oConfig->getObject('form_display_forms_prevalue_add');
  34. $oForm = BxDolForm::getObjectInstance($sFormObject, $sFormDisplay, $this->oModule->_oTemplate);
  35. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&list=' . $this->sList;
  36. $this->onLoad($oForm->aInputs);
  37. $oForm->initChecker();
  38. if($oForm->isSubmittedAndValid()) {
  39. if(!$this->onAdd($oForm))
  40. return;
  41. if(($iId = (int)$oForm->insert()) != 0)
  42. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  43. else
  44. $aRes = array('msg' => _t('_bx_dev_frm_err_prevalues_create'));
  45. echoJson($aRes);
  46. } else {
  47. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('bx-dev-frm-prevalue-create-popup', _t('_bx_dev_frm_txt_prevalues_create_popup'), $this->oModule->_oTemplate->parseHtmlByName('form_add_value.html', array(
  48. 'form_id' => $oForm->aFormAttrs['id'],
  49. 'form' => $oForm->getCode(true),
  50. 'object' => $this->_sObject,
  51. 'action' => $sAction
  52. )));
  53. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  54. }
  55. }
  56. public function performActionEdit()
  57. {
  58. $sAction = 'edit';
  59. $sFormObject = $this->oModule->_oConfig->getObject('form_forms_prevalue');
  60. $sFormDisplay = $this->oModule->_oConfig->getObject('form_display_forms_prevalue_edit');
  61. $aValue = $this->_getItem('getValues');
  62. if($aValue === false) {
  63. echoJson(array());
  64. exit;
  65. }
  66. $oForm = BxDolForm::getObjectInstance($sFormObject, $sFormDisplay, $this->oModule->_oTemplate);
  67. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&list=' . $this->sList;
  68. $oForm->aInputs['controls'][0]['value'] = _t('_bx_dev_frm_btn_prelists_save');
  69. $this->onLoad($oForm->aInputs);
  70. $oForm->initChecker($aValue);
  71. if($oForm->isSubmittedAndValid()) {
  72. if($oForm->update($aValue['id']) !== false) {
  73. $this->onSave($oForm);
  74. $aRes = array('grid' => $this->getCode(false), 'blink' => $aValue['id']);
  75. } else
  76. $aRes = array('msg' => _t('_bx_dev_frm_err_prevalues_edit'));
  77. echoJson($aRes);
  78. } else {
  79. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('bx-dev-frm-prevalue-edit-popup', _t('_bx_dev_frm_txt_prevalues_edit_popup', _t($aValue['lkey'])), $this->oModule->_oTemplate->parseHtmlByName('form_add_value.html', array(
  80. 'form_id' => $oForm->aFormAttrs['id'],
  81. 'form' => $oForm->getCode(true),
  82. 'object' => $this->_sObject,
  83. 'action' => $sAction
  84. )));
  85. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  86. }
  87. }
  88. protected function canAdd()
  89. {
  90. return $this->sList != '';
  91. }
  92. private function onLoad(&$aInputs)
  93. {
  94. $aLists = array();
  95. $this->oDb->getLists(array('type' => 'all'), $aLists, false);
  96. foreach($aLists as $aList)
  97. $aInputs['Key']['values'][$aList['key']] = _t($aList['title']);
  98. asort($aInputs['Key']['values']);
  99. $aInputs['Key']['values'] = array_merge(array('' => _t('_bx_dev_frm_txt_prevalues_key_select')), $aInputs['Key']['values']);
  100. $aInputs['Key']['value'] = $this->sList;
  101. $aInputs['Value']['value'] = $this->_getAvailableSetValue($this->sList);
  102. }
  103. private function onAdd(&$oForm)
  104. {
  105. $aList = array();
  106. $this->oDb->getLists(array('type' => 'by_key', 'value' => $oForm->getCleanValue('Key')), $aList, false);
  107. if((int)$aList['use_for_sets'] == 1) {
  108. $mixedValue = $oForm->getCleanValue('Value');
  109. if(!$this->canUseForSet($mixedValue)) {
  110. if($this->oDb->isListUsedInSet($this->sList)) {
  111. echoJson(array('msg' => _t('_bx_dev_frm_err_prevalues_create_forbidden', BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX)));
  112. return false;
  113. }
  114. $this->oDb->updateList($aList['id'], array('use_for_sets' => '0'));
  115. }
  116. }
  117. return true;
  118. }
  119. private function onSave(&$oForm)
  120. {
  121. $aList = array();
  122. $this->oDb->getLists(array('type' => 'by_key', 'value' => $oForm->getCleanValue('Key')), $aList, false);
  123. $iUseInSets = 1;
  124. $aValues = BxDolForm::getDataItems($aList['key']);
  125. foreach($aValues as $mixedValue => $sTitle)
  126. if(!$this->canUseForSet($mixedValue)) {
  127. $iUseInSets = 0;
  128. break;
  129. }
  130. if($iUseInSets != (int)$aList['use_for_sets'])
  131. $this->oDb->updateList($aList['id'], array('use_for_sets' => $iUseInSets));
  132. }
  133. }
  134. /** @} */