BxBaseStudioFormsPreValues.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 UnaView UNA Studio Representation classes
  7. * @ingroup UnaStudio
  8. * @{
  9. */
  10. class BxBaseStudioFormsPreValues extends BxDolStudioFormsPreValues
  11. {
  12. protected $sUrlPage;
  13. function __construct($aOptions, $oTemplate = false)
  14. {
  15. parent::__construct($aOptions, $oTemplate);
  16. $this->_aOptions['actions_single']['edit']['attr']['title'] = _t('_adm_form_btn_pre_values_edit_item');
  17. $this->_aOptions['actions_single']['delete']['attr']['title'] = _t('_adm_form_btn_pre_values_delete_item');
  18. $this->sUrlPage = BX_DOL_URL_STUDIO . 'builder_forms.php?page=pre_values';
  19. }
  20. public function performActionAdd()
  21. {
  22. $sAction = 'add';
  23. if(!$this->canAdd()) {
  24. echoJson(array());
  25. exit;
  26. }
  27. $bUseInSets = (int)$this->aList['use_for_sets'] == 1;
  28. bx_import('BxTemplStudioFormView');
  29. $aForm = array(
  30. 'form_attrs' => array(
  31. 'id' => 'adm-form-pre-value-create',
  32. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&list=' . $this->sList,
  33. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  34. ),
  35. 'params' => array (
  36. 'db' => array(
  37. 'table' => 'sys_form_pre_values',
  38. 'key' => 'id',
  39. 'uri' => '',
  40. 'uri_title' => '',
  41. 'submit_name' => 'do_submit'
  42. ),
  43. ),
  44. 'inputs' => array (
  45. 'Key' => array(
  46. 'type' => 'hidden',
  47. 'name' => 'Key',
  48. 'value' => $this->sList,
  49. 'db' => array (
  50. 'pass' => 'Xss',
  51. ),
  52. ),
  53. 'Value' => array(
  54. 'type' => 'hidden',
  55. 'name' => 'Value',
  56. 'value' => $this->_getAvailableSetValue($this->sList),
  57. 'db' => array (
  58. 'pass' => 'Int',
  59. )
  60. ),
  61. 'LKey' => array(
  62. 'type' => 'text_translatable',
  63. 'name' => 'LKey',
  64. 'caption' => _t('_adm_form_txt_pre_values_lkey'),
  65. 'info' => _t('_adm_form_dsc_pre_values_lkey'),
  66. 'value' => '_adm_form_txt_pre_value',
  67. 'required' => '1',
  68. 'db' => array (
  69. 'pass' => 'Xss',
  70. ),
  71. 'checker' => array (
  72. 'func' => 'LengthTranslatable',
  73. 'params' => array(1,100, 'LKey'),
  74. 'error' => _t('_adm_form_err_pre_values_lkey'),
  75. ),
  76. ),
  77. 'Empty' => array(
  78. 'type' => 'checkbox',
  79. 'name' => 'Empty',
  80. 'caption' => _t('_adm_form_txt_pre_values_empty'),
  81. 'info' => _t('_adm_form_dsc_pre_values_empty'),
  82. 'value' => 'on',
  83. 'required' => '0'
  84. ),
  85. 'controls' => array(
  86. 'name' => 'controls',
  87. 'type' => 'input_set',
  88. array(
  89. 'type' => 'submit',
  90. 'name' => 'do_submit',
  91. 'value' => _t('_adm_form_btn_pre_values_add'),
  92. ),
  93. array (
  94. 'type' => 'reset',
  95. 'name' => 'close',
  96. 'value' => _t('_adm_form_btn_pre_values_cancel'),
  97. 'attrs' => array(
  98. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  99. 'class' => 'bx-def-margin-sec-left',
  100. ),
  101. )
  102. )
  103. )
  104. );
  105. $oForm = new BxTemplStudioFormView($aForm);
  106. $oForm->initChecker();
  107. if($oForm->isSubmittedAndValid()) {
  108. if($oForm->getCleanValue('Empty') == 'on')
  109. BxDolForm::setSubmittedValue('Value', '', $oForm->aFormAttrs['method']);
  110. if($bUseInSets) {
  111. $mixedValue = $oForm->getCleanValue('Value');
  112. if(!$this->canUseForSet($mixedValue)) {
  113. if($this->oDb->isListUsedInSet($this->sList)) {
  114. echoJson(array('msg' => _t('_adm_form_err_pre_values_create_forbidden', BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX)));
  115. return;
  116. }
  117. $this->oDb->updateList($this->aList['id'], array('use_for_sets' => '0'));
  118. }
  119. }
  120. $iId = (int)$oForm->insert(array('LKey2' => '', 'Order' => $this->oDb->getValuesOrderMax($this->sList) + 1));
  121. if($iId != 0)
  122. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  123. else
  124. $aRes = array('msg' => _t('_adm_form_err_pre_values_create'));
  125. echoJson($aRes);
  126. }
  127. else {
  128. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-pre-value-create-popup', _t('_adm_form_txt_pre_values_create_popup'), $this->_oTemplate->parseHtmlByName('form_add_value.html', array(
  129. 'form_id' => $aForm['form_attrs']['id'],
  130. 'form' => $oForm->getCode(true),
  131. 'object' => $this->_sObject,
  132. 'action' => $sAction
  133. )));
  134. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  135. }
  136. }
  137. public function performActionEdit()
  138. {
  139. $sAction = 'edit';
  140. $aValue = $this->_getItem('getValues');
  141. if($aValue === false) {
  142. echoJson(array());
  143. exit;
  144. }
  145. $bUseInSets = (int)$this->aList['use_for_sets'] == 1;
  146. bx_import('BxTemplStudioFormView');
  147. $aForm = array(
  148. 'form_attrs' => array(
  149. 'id' => 'adm-form-list-edit',
  150. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&list=' . $this->sList,
  151. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  152. ),
  153. 'params' => array (
  154. 'db' => array(
  155. 'table' => 'sys_form_pre_values',
  156. 'key' => 'id',
  157. 'uri' => '',
  158. 'uri_title' => '',
  159. 'submit_name' => 'do_submit'
  160. ),
  161. ),
  162. 'inputs' => array(
  163. 'id' => array(
  164. 'type' => 'hidden',
  165. 'name' => 'id',
  166. 'value' => $aValue['id'],
  167. 'db' => array (
  168. 'pass' => 'Int',
  169. ),
  170. ),
  171. 'LKey' => array(
  172. 'type' => 'text_translatable',
  173. 'name' => 'LKey',
  174. 'caption' => _t('_adm_form_txt_pre_values_lkey'),
  175. 'info' => _t('_adm_form_dsc_pre_values_lkey'),
  176. 'value' => $aValue['lkey'],
  177. 'required' => '1',
  178. 'db' => array (
  179. 'pass' => 'Xss',
  180. ),
  181. 'checker' => array (
  182. 'func' => 'LengthTranslatable',
  183. 'params' => array(3,100, 'LKey'),
  184. 'error' => _t('_adm_form_err_pre_values_lkey'),
  185. ),
  186. ),
  187. 'Empty' => array(
  188. 'type' => 'checkbox',
  189. 'name' => 'Empty',
  190. 'caption' => _t('_adm_form_txt_pre_values_empty'),
  191. 'info' => _t('_adm_form_dsc_pre_values_empty'),
  192. 'value' => 'on',
  193. 'checked' => empty($aValue['value']) ? '1' : '0',
  194. 'required' => '0'
  195. ),
  196. 'controls' => array(
  197. 'name' => 'controls',
  198. 'type' => 'input_set',
  199. array(
  200. 'type' => 'submit',
  201. 'name' => 'do_submit',
  202. 'value' => _t('_adm_form_btn_pre_lists_save'),
  203. ),
  204. array (
  205. 'type' => 'reset',
  206. 'name' => 'close',
  207. 'value' => _t('_adm_form_btn_pre_lists_cancel'),
  208. 'attrs' => array(
  209. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  210. 'class' => 'bx-def-margin-sec-left',
  211. ),
  212. )
  213. )
  214. )
  215. );
  216. $oForm = new BxTemplStudioFormView($aForm);
  217. $oForm->initChecker();
  218. if($oForm->isSubmittedAndValid()) {
  219. $aAdd = array();
  220. $bEmpty = $oForm->getCleanValue('Empty') == 'on';
  221. if($bEmpty && !empty($aValue['value']))
  222. $aAdd['Value'] = 0;
  223. else if(!$bEmpty && empty($aValue['value']))
  224. $aAdd['Value'] = $this->_getAvailableSetValue($this->sList);
  225. if(!empty($aAdd) && $bUseInSets) {
  226. $mixedValue = $aAdd['Value'];
  227. if(!$this->canUseForSet($mixedValue)) {
  228. if($this->oDb->isListUsedInSet($this->sList)) {
  229. echoJson(array('msg' => _t('_adm_form_err_pre_values_create_forbidden', BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX)));
  230. return;
  231. }
  232. $this->oDb->updateList($this->aList['id'], array('use_for_sets' => '0'));
  233. }
  234. }
  235. if($oForm->update($aValue['id'], $aAdd) !== false)
  236. $aRes = array('grid' => $this->getCode(false), 'blink' => $aValue['id']);
  237. else
  238. $aRes = array('msg' => _t('_adm_form_err_pre_values_edit'));
  239. echoJson($aRes);
  240. }
  241. else {
  242. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-pre-value-edit-popup', _t('_adm_form_txt_pre_values_edit_popup', _t($aValue['lkey'])), $this->_oTemplate->parseHtmlByName('form_add_value.html', array(
  243. 'form_id' => $aForm['form_attrs']['id'],
  244. 'form' => $oForm->getCode(true),
  245. 'object' => $this->_sObject,
  246. 'action' => $sAction
  247. )));
  248. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  249. }
  250. }
  251. public function performActionDelete()
  252. {
  253. $sAction = 'delete';
  254. $iAffected = 0;
  255. $aIds = bx_get('ids');
  256. if(!$aIds || !is_array($aIds)) {
  257. echoJson(array());
  258. exit;
  259. }
  260. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  261. $aIdsAffected = array ();
  262. foreach($aIds as $iId) {
  263. $aValue = array();
  264. $this->oDb->getValues(array('type' => 'by_id', 'value' => (int)$iId), $aValue);
  265. if(!is_array($aValue) || empty($aValue))
  266. continue;
  267. if((int)$this->_delete($iId) <= 0)
  268. continue;
  269. $oLanguage->deleteLanguageString($aValue['lkey']);
  270. $oLanguage->deleteLanguageString($aValue['lkey2']);
  271. if((int)$this->aList['use_for_sets'] != 1) {
  272. $bUseInSets = 1;
  273. $aValues = BxDolForm::getDataItems($this->sList);
  274. foreach($aValues as $mixedValue => $sTitle)
  275. if(!$this->canUseForSet($mixedValue)) {
  276. $bUseInSets = 0;
  277. break;
  278. }
  279. if($bUseInSets == 1)
  280. $this->oDb->updateList($this->aList['id'], array('use_for_sets' => $bUseInSets));
  281. }
  282. $aIdsAffected[] = $iId;
  283. $iAffected++;
  284. }
  285. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t('_adm_form_err_pre_values_delete')));
  286. }
  287. function getJsObject()
  288. {
  289. return 'oBxDolStudioFormsValues';
  290. }
  291. function getListsSelector($sModule = '')
  292. {
  293. $oForm = new BxTemplStudioFormView(array());
  294. $aInputLists = array(
  295. 'type' => 'select',
  296. 'name' => 'list',
  297. 'attrs' => array(
  298. 'id' => 'bx-grid-list-' . $this->_sObject,
  299. 'onChange' => 'javascript:' . $this->getJsObject() . '.onChangeList()'
  300. ),
  301. 'value' => $this->sList,
  302. 'values' => array()
  303. );
  304. $aLists = array();
  305. if(!empty($sModule))
  306. $this->oDb->getLists(array('type' => 'by_module', 'value' => $sModule), $aLists, false);
  307. else
  308. $aInputLists['attrs']['disabled'] = 'disabled';
  309. if(!empty($aLists)) {
  310. $aCounter = array();
  311. $this->oDb->getValues(array('type' => 'counter_by_lists'), $aCounter, false);
  312. foreach($aLists as $aList)
  313. $aInputLists['values'][$aList['key']] = _t($aList['title']) . " (" . (isset($aCounter[$aList['key']]) ? $aCounter[$aList['key']] : "0") . ")";
  314. asort($aInputLists['values']);
  315. }
  316. $aInputLists['values'] = array_merge(array('' => _t('_adm_form_txt_select_list')), $aInputLists['values']);
  317. return $oForm->genRow($aInputLists);
  318. }
  319. function getCode($isDisplayHeader = true)
  320. {
  321. return $this->_oTemplate->parseHtmlByName('forms_values.html', array(
  322. 'content' => parent::getCode($isDisplayHeader),
  323. 'js_object' => $this->getJsObject(),
  324. 'page_url' => $this->sUrlPage,
  325. 'grid_object' => $this->_sObject,
  326. 'params_divider' => $this->sParamsDivider
  327. ));
  328. }
  329. protected function _addJsCss()
  330. {
  331. parent::_addJsCss();
  332. $this->_oTemplate->addJs(array('jquery.form.min.js', 'forms_values.js'));
  333. $oForm = new BxTemplStudioFormView(array());
  334. $oForm->addCssJs();
  335. }
  336. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  337. {
  338. if(!$this->canAdd())
  339. $isDisabled = true;
  340. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  341. }
  342. protected function _getFilterControls ()
  343. {
  344. parent::_getFilterControls();
  345. $sContent = $this->getModulesSelectOne('getValues') . $this->getListsSelector($this->sModule);
  346. $oForm = new BxTemplStudioFormView(array());
  347. $aInputSearch = array(
  348. 'type' => 'text',
  349. 'name' => 'keyword',
  350. 'attrs' => array(
  351. 'id' => 'bx-grid-search-' . $this->_sObject,
  352. ),
  353. 'tr_attrs' => array(
  354. 'style' => 'display:none;'
  355. )
  356. );
  357. $sContent .= $oForm->genRow($aInputSearch);
  358. return $sContent;
  359. }
  360. protected function _getAvailableSetValue($sList)
  361. {
  362. $aValues = array();
  363. $this->oDb->getValues(array('type' =>'by_key_key_value', 'value' => $sList), $aValues, false);
  364. ksort($aValues);
  365. $iValue = 1;
  366. foreach($aValues as $aValue) {
  367. if((int)$aValue['value'] == 0)
  368. continue;
  369. if((int)$aValue['value'] != $iValue)
  370. break;
  371. $iValue++;
  372. }
  373. return $iValue;
  374. }
  375. protected function canAdd()
  376. {
  377. return $this->sList != '' && (int)$this->aList['extendable'] != 0;
  378. }
  379. protected function canUseForSet($mixedValue)
  380. {
  381. return is_numeric($mixedValue) && (int)$mixedValue >= 1 && (int)$mixedValue <= BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX;
  382. }
  383. }
  384. /** @} */