1
0

BxBaseStudioFormsPreValues.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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([]);
  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. 'icon_type' => [
  78. 'type' => 'select',
  79. 'name' => 'icon_type',
  80. 'caption' => _t('_adm_form_txt_pre_values_icon_type'),
  81. 'info' => '',
  82. 'values' => [
  83. ['key' => '', 'value' => _t('_sys_please_select')],
  84. ['key' => 'icon', 'value' => _t('_adm_form_txt_pre_values_icon_type_icon')],
  85. ['key' => 'emoji', 'value' => _t('_adm_form_txt_pre_values_icon_type_emoji')],
  86. ['key' => 'image', 'value' => _t('_adm_form_txt_pre_values_icon_type_image')]
  87. ],
  88. 'value' => '',
  89. 'required' => '0',
  90. 'attrs' => [
  91. 'onchange' => 'javascript:' . $this->getJsObject() . '.onChangeIconType(this)'
  92. ]
  93. ],
  94. 'icon_icon' => [
  95. 'type' => 'text',
  96. 'name' => 'icon_icon',
  97. 'caption' => _t('_adm_form_txt_pre_values_icon'),
  98. 'info' => _t('_adm_form_dsc_pre_values_icon'),
  99. 'value' => '',
  100. 'required' => '0',
  101. 'code' => 1,
  102. 'tr_attrs' => ['class' => 'bx-pre-values-icon bx-pvi-icon', 'style' => 'display:none']
  103. ],
  104. 'icon_emoji' => [
  105. 'type' => 'textarea',
  106. 'name' => 'icon_emoji',
  107. 'caption' => _t('_adm_form_txt_pre_values_icon'),
  108. 'info' => _t('_adm_form_dsc_pre_values_icon'),
  109. 'value' => '',
  110. 'required' => '0',
  111. 'code' => 1,
  112. 'tr_attrs' => ['class' => 'bx-pre-values-icon bx-pvi-emoji', 'style' => 'display:none']
  113. ],
  114. 'icon_image' => [
  115. 'type' => 'textarea',
  116. 'name' => 'icon_image',
  117. 'caption' => _t('_adm_form_txt_pre_values_icon'),
  118. 'info' => _t('_adm_form_dsc_pre_values_icon'),
  119. 'value' => '',
  120. 'required' => '0',
  121. 'code' => 1,
  122. 'tr_attrs' => ['class' => 'bx-pre-values-icon bx-pvi-image', 'style' => 'display:none']
  123. ],
  124. 'Empty' => array(
  125. 'type' => 'checkbox',
  126. 'name' => 'Empty',
  127. 'caption' => _t('_adm_form_txt_pre_values_empty'),
  128. 'info' => _t('_adm_form_dsc_pre_values_empty'),
  129. 'value' => 'on',
  130. 'required' => '0'
  131. ),
  132. 'controls' => array(
  133. 'name' => 'controls',
  134. 'type' => 'input_set',
  135. array(
  136. 'type' => 'submit',
  137. 'name' => 'do_submit',
  138. 'value' => _t('_adm_form_btn_pre_values_add'),
  139. ),
  140. array (
  141. 'type' => 'reset',
  142. 'name' => 'close',
  143. 'value' => _t('_adm_form_btn_pre_values_cancel'),
  144. 'attrs' => array(
  145. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  146. 'class' => 'bx-def-margin-sec-left',
  147. ),
  148. )
  149. )
  150. )
  151. );
  152. $oForm = new BxTemplStudioFormView($aForm);
  153. $oForm->initChecker();
  154. if($oForm->isSubmittedAndValid()) {
  155. $aAdd = ['LKey2' => '', 'Order' => $this->oDb->getValuesOrderMax($this->sList) + 1];
  156. if(($sIconType = $oForm->getCleanValue('icon_type'))) {
  157. $aData['use'] = $sIconType;
  158. foreach($this->aIconTypes as $sType)
  159. if(($sIcon = $oForm->getCleanValue('icon_' . $sType)) !== false)
  160. $aData[$sType] = $sIcon;
  161. $aAdd['Data'] = serialize($aData);
  162. }
  163. if($oForm->getCleanValue('Empty') == 'on')
  164. BxDolForm::setSubmittedValue('Value', '', $oForm->aFormAttrs['method']);
  165. if($bUseInSets) {
  166. $mixedValue = $oForm->getCleanValue('Value');
  167. if(!$this->canUseForSet($mixedValue)) {
  168. if($this->oDb->isListUsedInSet($this->sList)) {
  169. echoJson(array('msg' => _t('_adm_form_err_pre_values_create_forbidden', BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX)));
  170. return;
  171. }
  172. $this->oDb->updateList($this->aList['id'], array('use_for_sets' => '0'));
  173. }
  174. }
  175. $iId = (int)$oForm->insert($aAdd);
  176. if($iId != 0) {
  177. $this->onDataListChanged($oForm->getCleanValue('Key'));
  178. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  179. } else {
  180. $aRes = array('msg' => _t('_adm_form_err_pre_values_create'));
  181. }
  182. echoJson($aRes);
  183. }
  184. else {
  185. $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(
  186. 'form_id' => $aForm['form_attrs']['id'],
  187. 'form' => $oForm->getCode(true),
  188. 'object' => $this->_sObject,
  189. 'action' => $sAction
  190. )));
  191. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  192. }
  193. }
  194. public function performActionEdit()
  195. {
  196. $sAction = 'edit';
  197. $aValue = $this->_getItem('getValues');
  198. if($aValue === false) {
  199. echoJson([]);
  200. exit;
  201. }
  202. $bUseInSets = (int)$this->aList['use_for_sets'] == 1;
  203. $aData = !empty($aValue['Data']) && bx_is_serialized($aValue['Data']) ? unserialize($aValue['Data']) : [];
  204. $sIconType = !empty($aData['use']) ? $aData['use'] : '';
  205. bx_import('BxTemplStudioFormView');
  206. $aForm = array(
  207. 'form_attrs' => array(
  208. 'id' => 'adm-form-list-edit',
  209. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&list=' . $this->sList,
  210. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  211. ),
  212. 'params' => array (
  213. 'db' => array(
  214. 'table' => 'sys_form_pre_values',
  215. 'key' => 'id',
  216. 'uri' => '',
  217. 'uri_title' => '',
  218. 'submit_name' => 'do_submit'
  219. ),
  220. ),
  221. 'inputs' => array(
  222. 'id' => array(
  223. 'type' => 'hidden',
  224. 'name' => 'id',
  225. 'value' => $aValue['id'],
  226. 'db' => array (
  227. 'pass' => 'Int',
  228. ),
  229. ),
  230. 'LKey' => array(
  231. 'type' => 'text_translatable',
  232. 'name' => 'LKey',
  233. 'caption' => _t('_adm_form_txt_pre_values_lkey'),
  234. 'info' => _t('_adm_form_dsc_pre_values_lkey'),
  235. 'value' => $aValue['lkey'],
  236. 'required' => '1',
  237. 'db' => array (
  238. 'pass' => 'Xss',
  239. ),
  240. 'checker' => array (
  241. 'func' => 'LengthTranslatable',
  242. 'params' => array(3,100, 'LKey'),
  243. 'error' => _t('_adm_form_err_pre_values_lkey'),
  244. ),
  245. ),
  246. 'icon_type' => [
  247. 'type' => 'select',
  248. 'name' => 'icon_type',
  249. 'caption' => _t('_adm_form_txt_pre_values_icon_type'),
  250. 'info' => '',
  251. 'values' => [
  252. ['key' => '', 'value' => _t('_sys_please_select')],
  253. ['key' => 'icon', 'value' => _t('_adm_form_txt_pre_values_icon_type_icon')],
  254. ['key' => 'emoji', 'value' => _t('_adm_form_txt_pre_values_icon_type_emoji')],
  255. ['key' => 'image', 'value' => _t('_adm_form_txt_pre_values_icon_type_image')]
  256. ],
  257. 'value' => $sIconType,
  258. 'required' => '0',
  259. 'attrs' => [
  260. 'onchange' => 'javascript:' . $this->getJsObject() . '.onChangeIconType(this)'
  261. ]
  262. ],
  263. 'icon_icon' => [
  264. 'type' => 'text',
  265. 'name' => 'icon_icon',
  266. 'caption' => _t('_adm_form_txt_pre_values_icon'),
  267. 'info' => _t('_adm_form_dsc_pre_values_icon'),
  268. 'value' => !empty($aData['icon']) ? $aData['icon'] : '',
  269. 'required' => '0',
  270. 'code' => 1,
  271. 'tr_attrs' => ['class' => 'bx-pre-values-icon bx-pvi-icon', 'style' => $sIconType != 'icon' ? 'display:none' : '']
  272. ],
  273. 'icon_emoji' => [
  274. 'type' => 'textarea',
  275. 'name' => 'icon_emoji',
  276. 'caption' => _t('_adm_form_txt_pre_values_icon'),
  277. 'info' => _t('_adm_form_dsc_pre_values_icon'),
  278. 'value' => !empty($aData['emoji']) ? $aData['emoji'] : '',
  279. 'required' => '0',
  280. 'code' => 1,
  281. 'tr_attrs' => ['class' => 'bx-pre-values-icon bx-pvi-emoji', 'style' => $sIconType != 'emoji' ? 'display:none' : '']
  282. ],
  283. 'icon_image' => [
  284. 'type' => 'textarea',
  285. 'name' => 'icon_image',
  286. 'caption' => _t('_adm_form_txt_pre_values_icon'),
  287. 'info' => _t('_adm_form_dsc_pre_values_icon'),
  288. 'value' => !empty($aData['image']) ? $aData['image'] : '',
  289. 'required' => '0',
  290. 'code' => 1,
  291. 'tr_attrs' => ['class' => 'bx-pre-values-icon bx-pvi-image', 'style' => $sIconType != 'image' ? 'display:none' : '']
  292. ],
  293. 'Empty' => array(
  294. 'type' => 'checkbox',
  295. 'name' => 'Empty',
  296. 'caption' => _t('_adm_form_txt_pre_values_empty'),
  297. 'info' => _t('_adm_form_dsc_pre_values_empty'),
  298. 'value' => 'on',
  299. 'checked' => empty($aValue['value']) ? '1' : '0',
  300. 'required' => '0'
  301. ),
  302. 'controls' => array(
  303. 'name' => 'controls',
  304. 'type' => 'input_set',
  305. array(
  306. 'type' => 'submit',
  307. 'name' => 'do_submit',
  308. 'value' => _t('_adm_form_btn_pre_lists_save'),
  309. ),
  310. array (
  311. 'type' => 'reset',
  312. 'name' => 'close',
  313. 'value' => _t('_adm_form_btn_pre_lists_cancel'),
  314. 'attrs' => array(
  315. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  316. 'class' => 'bx-def-margin-sec-left',
  317. ),
  318. )
  319. )
  320. )
  321. );
  322. $oForm = new BxTemplStudioFormView($aForm);
  323. $oForm->initChecker();
  324. if($oForm->isSubmittedAndValid()) {
  325. $aAdd = [];
  326. if(($sIconType = $oForm->getCleanValue('icon_type'))) {
  327. $aData['use'] = $sIconType;
  328. foreach($this->aIconTypes as $sType)
  329. if(($sIcon = $oForm->getCleanValue('icon_' . $sType)) !== false)
  330. $aData[$sType] = $sIcon;
  331. $aAdd['Data'] = serialize($aData);
  332. }
  333. $bEmpty = $oForm->getCleanValue('Empty') == 'on';
  334. if($bEmpty)
  335. $aAdd['Value'] = empty($aValue['value']) ? $aValue['value'] : 0;
  336. else
  337. $aAdd['Value'] = !empty($aValue['value']) ? $aValue['value'] : $this->_getAvailableSetValue($this->sList);
  338. if(!empty($aAdd) && $bUseInSets) {
  339. $mixedValue = $aAdd['Value'];
  340. if(!$this->canUseForSet($mixedValue)) {
  341. if($this->oDb->isListUsedInSet($this->sList)) {
  342. echoJson(array('msg' => _t('_adm_form_err_pre_values_create_forbidden', BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX)));
  343. return;
  344. }
  345. $this->oDb->updateList($this->aList['id'], array('use_for_sets' => '0'));
  346. }
  347. }
  348. if($oForm->update($aValue['id'], $aAdd) !== false) {
  349. $this->onDataListChanged($this->sList);
  350. $aRes = array('grid' => $this->getCode(false), 'blink' => $aValue['id']);
  351. } else {
  352. $aRes = array('msg' => _t('_adm_form_err_pre_values_edit'));
  353. }
  354. echoJson($aRes);
  355. }
  356. else {
  357. $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(
  358. 'form_id' => $aForm['form_attrs']['id'],
  359. 'form' => $oForm->getCode(true),
  360. 'object' => $this->_sObject,
  361. 'action' => $sAction
  362. )));
  363. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  364. }
  365. }
  366. public function performActionReorder()
  367. {
  368. $mixed = parent::performActionReorder();
  369. $this->onDataListChanged($this->sList);
  370. return $mixed;
  371. }
  372. public function performActionDelete()
  373. {
  374. $sAction = 'delete';
  375. $iAffected = 0;
  376. $aIds = bx_get('ids');
  377. if(!$aIds || !is_array($aIds)) {
  378. echoJson(array());
  379. exit;
  380. }
  381. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  382. $aIdsAffected = array ();
  383. foreach($aIds as $iId) {
  384. $aValue = array();
  385. $this->oDb->getValues(array('type' => 'by_id', 'value' => (int)$iId), $aValue);
  386. if(!is_array($aValue) || empty($aValue))
  387. continue;
  388. if((int)$this->_delete($iId) <= 0)
  389. continue;
  390. $this->onDataListChanged($this->sList);
  391. $oLanguage->deleteLanguageString($aValue['lkey']);
  392. $oLanguage->deleteLanguageString($aValue['lkey2']);
  393. if((int)$this->aList['use_for_sets'] != 1) {
  394. $bUseInSets = 1;
  395. $aValues = BxDolForm::getDataItems($this->sList);
  396. foreach($aValues as $mixedValue => $sTitle)
  397. if(!$this->canUseForSet($mixedValue)) {
  398. $bUseInSets = 0;
  399. break;
  400. }
  401. if($bUseInSets == 1)
  402. $this->oDb->updateList($this->aList['id'], array('use_for_sets' => $bUseInSets));
  403. }
  404. $aIdsAffected[] = $iId;
  405. $iAffected++;
  406. }
  407. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t('_adm_form_err_pre_values_delete')));
  408. }
  409. function getJsObject()
  410. {
  411. return 'oBxDolStudioFormsValues';
  412. }
  413. function getListsSelector($sModule = '')
  414. {
  415. $oForm = new BxTemplStudioFormView(array());
  416. $aInputLists = array(
  417. 'type' => 'select',
  418. 'name' => 'list',
  419. 'attrs' => array(
  420. 'id' => 'bx-grid-list-' . $this->_sObject,
  421. 'onChange' => 'javascript:' . $this->getJsObject() . '.onChangeList()'
  422. ),
  423. 'value' => $this->sList,
  424. 'values' => array()
  425. );
  426. $aLists = array();
  427. if(!empty($sModule))
  428. $this->oDb->getLists(array('type' => 'by_module', 'value' => $sModule), $aLists, false);
  429. else
  430. $aInputLists['attrs']['disabled'] = 'disabled';
  431. if(!empty($aLists)) {
  432. $aCounter = array();
  433. $this->oDb->getValues(array('type' => 'counter_by_lists'), $aCounter, false);
  434. foreach($aLists as $aList)
  435. $aInputLists['values'][$aList['key']] = _t($aList['title']) . " (" . (isset($aCounter[$aList['key']]) ? $aCounter[$aList['key']] : "0") . ")";
  436. asort($aInputLists['values']);
  437. }
  438. $aInputLists['values'] = array_merge(array('' => _t('_adm_form_txt_select_list')), $aInputLists['values']);
  439. return $oForm->genRow($aInputLists);
  440. }
  441. function getCode($isDisplayHeader = true)
  442. {
  443. return $this->_oTemplate->parseHtmlByName('forms_values.html', array(
  444. 'content' => parent::getCode($isDisplayHeader),
  445. 'js_object' => $this->getJsObject(),
  446. 'page_url' => $this->sUrlPage,
  447. 'grid_object' => $this->_sObject,
  448. 'params_divider' => $this->sParamsDivider
  449. ));
  450. }
  451. protected function _addJsCss()
  452. {
  453. parent::_addJsCss();
  454. $this->_oTemplate->addJs(array('jquery.form.min.js', 'forms_values.js'));
  455. $oForm = new BxTemplStudioFormView(array());
  456. $oForm->addCssJs();
  457. }
  458. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  459. {
  460. if(!$this->canAdd())
  461. $isDisabled = true;
  462. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  463. }
  464. protected function _getFilterControls ()
  465. {
  466. parent::_getFilterControls();
  467. $sContent = $this->getModulesSelectOne('getValues') . $this->getListsSelector($this->sModule);
  468. $oForm = new BxTemplStudioFormView(array());
  469. $aInputSearch = array(
  470. 'type' => 'text',
  471. 'name' => 'keyword',
  472. 'attrs' => array(
  473. 'id' => 'bx-grid-search-' . $this->_sObject,
  474. 'aria-label' => _t('_adm_grid_lbl_search'),
  475. ),
  476. 'tr_attrs' => array(
  477. 'style' => 'display:none;'
  478. )
  479. );
  480. $sContent .= $oForm->genRow($aInputSearch);
  481. return $sContent;
  482. }
  483. protected function _getAvailableSetValue($sList)
  484. {
  485. $aValues = array();
  486. $this->oDb->getValues(array('type' =>'by_key_key_value', 'value' => $sList), $aValues, false);
  487. ksort($aValues);
  488. $iValue = 1;
  489. foreach($aValues as $aValue) {
  490. if((int)$aValue['value'] == 0)
  491. continue;
  492. if((int)$aValue['value'] != $iValue)
  493. break;
  494. $iValue++;
  495. }
  496. return $iValue;
  497. }
  498. protected function canAdd()
  499. {
  500. return $this->sList != '' && (int)$this->aList['extendable'] != 0;
  501. }
  502. protected function canUseForSet($mixedValue)
  503. {
  504. return is_numeric($mixedValue) && (int)$mixedValue >= 1 && (int)$mixedValue <= BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX;
  505. }
  506. protected function onDataListChanged($sKey)
  507. {
  508. $this->oDb->cleanCache('sys_form_pre_values_with_key_' . $sKey);
  509. $this->oDb->cleanCache('sys_form_pre_values_' . $sKey);
  510. }
  511. }
  512. /** @} */