BxDevFormsPreValues.php 6.1 KB

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