1
0

BxBaseStudioFormsPreValues.php 22 KB

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