1
0

BxBaseStudioFormsPreValues.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinView Dolphin Studio Representation classes
  7. * @ingroup DolphinStudio
  8. * @{
  9. */
  10. bx_import('BxDolStudioFormsPreValues');
  11. bx_import('BxTemplStudioFormView');
  12. class BxBaseStudioFormsPreValues extends BxDolStudioFormsPreValues
  13. {
  14. protected $sUrlPage;
  15. function __construct($aOptions, $oTemplate = false)
  16. {
  17. parent::__construct($aOptions, $oTemplate);
  18. $this->_aOptions['actions_single']['edit']['attr']['title'] = _t('_adm_form_btn_pre_values_edit');
  19. $this->_aOptions['actions_single']['delete']['attr']['title'] = _t('_adm_form_btn_pre_values_delete');
  20. $this->sUrlPage = BX_DOL_URL_STUDIO . 'builder_forms.php?page=pre_values';
  21. }
  22. public function performActionAdd()
  23. {
  24. $sAction = 'add';
  25. $aList = array();
  26. $this->oDb->getLists(array('type' => 'by_key', 'value' => $this->sList), $aList, false);
  27. $bUseInSets = (int)$aList['use_for_sets'] == 1;
  28. $aForm = array(
  29. 'form_attrs' => array(
  30. 'id' => 'adm-form-pre-value-create',
  31. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&list=' . $this->sList,
  32. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  33. ),
  34. 'params' => array (
  35. 'db' => array(
  36. 'table' => 'sys_form_pre_values',
  37. 'key' => 'id',
  38. 'uri' => '',
  39. 'uri_title' => '',
  40. 'submit_name' => 'do_submit'
  41. ),
  42. ),
  43. 'inputs' => array (
  44. 'Key' => array(
  45. 'type' => 'hidden',
  46. 'name' => 'Key',
  47. 'value' => $this->sList,
  48. 'db' => array (
  49. 'pass' => 'Xss',
  50. ),
  51. ),
  52. 'Value' => array(
  53. 'type' => 'hidden',
  54. 'name' => 'Value',
  55. 'value' => $this->_getAvailableSetValue($this->sList),
  56. 'db' => array (
  57. 'pass' => 'Int',
  58. )
  59. ),
  60. 'LKey' => array(
  61. 'type' => 'text_translatable',
  62. 'name' => 'LKey',
  63. 'caption' => _t('_adm_form_txt_pre_values_lkey'),
  64. 'info' => _t('_adm_form_dsc_pre_values_lkey'),
  65. 'value' => '_adm_form_txt_pre_value',
  66. 'required' => '1',
  67. 'db' => array (
  68. 'pass' => 'Xss',
  69. ),
  70. 'checker' => array (
  71. 'func' => 'LengthTranslatable',
  72. 'params' => array(3,100, 'LKey'),
  73. 'error' => _t('_adm_form_err_pre_values_lkey'),
  74. ),
  75. ),
  76. 'Empty' => array(
  77. 'type' => 'checkbox',
  78. 'name' => 'Empty',
  79. 'caption' => _t('_adm_form_txt_pre_values_empty'),
  80. 'info' => _t('_adm_form_dsc_pre_values_empty'),
  81. 'value' => 'on',
  82. 'required' => '0'
  83. ),
  84. 'controls' => array(
  85. 'name' => 'controls',
  86. 'type' => 'input_set',
  87. array(
  88. 'type' => 'submit',
  89. 'name' => 'do_submit',
  90. 'value' => _t('_adm_form_btn_pre_values_add'),
  91. ),
  92. array (
  93. 'type' => 'reset',
  94. 'name' => 'close',
  95. 'value' => _t('_adm_form_btn_pre_values_cancel'),
  96. 'attrs' => array(
  97. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  98. 'class' => 'bx-def-margin-sec-left',
  99. ),
  100. )
  101. )
  102. )
  103. );
  104. $oForm = new BxTemplStudioFormView($aForm);
  105. $oForm->initChecker();
  106. if($oForm->isSubmittedAndValid()) {
  107. if($oForm->getCleanValue('Empty') == 'on')
  108. BxDolForm::setSubmittedValue('Value', '', $oForm->aFormAttrs['method']);
  109. if($bUseInSets) {
  110. $mixedValue = $oForm->getCleanValue('Value');
  111. if(!$this->canUseForSet($mixedValue)) {
  112. if($this->oDb->isListUsedInSet($this->sList)) {
  113. $this->_echoResultJson(array('msg' => _t('_adm_form_err_pre_values_create_forbidden', BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX)), true);
  114. return;
  115. }
  116. $this->oDb->updateList($aList['id'], array('use_for_sets' => '0'));
  117. }
  118. }
  119. $iId = (int)$oForm->insert(array('LKey2' => '', 'Order' => $this->oDb->getValuesOrderMax($this->sList) + 1));
  120. if($iId != 0)
  121. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  122. else
  123. $aRes = array('msg' => _t('_adm_form_err_pre_values_create'));
  124. $this->_echoResultJson($aRes, true);
  125. } else {
  126. bx_import('BxTemplStudioFunctions');
  127. $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(
  128. 'form_id' => $aForm['form_attrs']['id'],
  129. 'form' => $oForm->getCode(true),
  130. 'object' => $this->_sObject,
  131. 'action' => $sAction
  132. )));
  133. $this->_echoResultJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))), true);
  134. }
  135. }
  136. public function performActionEdit()
  137. {
  138. $sAction = 'edit';
  139. $aValue = $this->_getItem('getValues');
  140. if($aValue === false) {
  141. $this->_echoResultJson(array());
  142. exit;
  143. }
  144. $aList = array();
  145. $this->oDb->getLists(array('type' => 'by_key', 'value' => $this->sList), $aList, false);
  146. $bUseInSets = (int)$aList['use_for_sets'] == 1;
  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. $this->_echoResultJson(array('msg' => _t('_adm_form_err_pre_values_create_forbidden', BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX)), true);
  230. return;
  231. }
  232. $this->oDb->updateList($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. $this->_echoResultJson($aRes, true);
  240. } else {
  241. bx_import('BxTemplStudioFunctions');
  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. $this->_echoResultJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))), true);
  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. $this->_echoResultJson(array());
  258. exit;
  259. }
  260. bx_import('BxDolForm');
  261. bx_import('BxDolStudioLanguagesUtils');
  262. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  263. $aList = array();
  264. $this->oDb->getLists(array('type' => 'by_key', 'value' => $this->sList), $aList, false);
  265. $aIdsAffected = array ();
  266. foreach($aIds as $iId) {
  267. $aValue = array();
  268. $this->oDb->getValues(array('type' => 'by_id', 'value' => (int)$iId), $aValue);
  269. if(!is_array($aValue) || empty($aValue))
  270. continue;
  271. if((int)$this->_delete($iId) <= 0)
  272. continue;
  273. $oLanguage->deleteLanguageString($aValue['lkey']);
  274. $oLanguage->deleteLanguageString($aValue['lkey2']);
  275. if((int)$aList['use_for_sets'] != 1) {
  276. $bUseInSets = 1;
  277. $aValues = BxDolForm::getDataItems($this->sList);
  278. foreach($aValues as $mixedValue => $sTitle)
  279. if(!$this->canUseForSet($mixedValue)) {
  280. $bUseInSets = 0;
  281. break;
  282. }
  283. if($bUseInSets == 1)
  284. $this->oDb->updateList($aList['id'], array('use_for_sets' => $bUseInSets));
  285. }
  286. $aIdsAffected[] = $iId;
  287. $iAffected++;
  288. }
  289. $this->_echoResultJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t('_adm_form_err_pre_values_delete')));
  290. }
  291. function getJsObject()
  292. {
  293. return 'oBxDolStudioFormsValues';
  294. }
  295. function getListsSelector($sModule = '')
  296. {
  297. bx_import('BxTemplStudioFormView');
  298. $oForm = new BxTemplStudioFormView(array());
  299. $aInputLists = array(
  300. 'type' => 'select',
  301. 'name' => 'list',
  302. 'attrs' => array(
  303. 'id' => 'bx-grid-list-' . $this->_sObject,
  304. 'onChange' => 'javascript:' . $this->getJsObject() . '.onChangeList()'
  305. ),
  306. 'value' => $this->sList,
  307. 'values' => array()
  308. );
  309. $aLists = array();
  310. if(!empty($sModule))
  311. $this->oDb->getLists(array('type' => 'by_module', 'value' => $sModule), $aLists, false);
  312. else
  313. $aInputLists['attrs']['disabled'] = 'disabled';
  314. if(!empty($aLists)) {
  315. $aCounter = array();
  316. $this->oDb->getValues(array('type' => 'counter_by_lists'), $aCounter, false);
  317. foreach($aLists as $aList)
  318. $aInputLists['values'][$aList['key']] = _t($aList['title']) . " (" . (isset($aCounter[$aList['key']]) ? $aCounter[$aList['key']] : "0") . ")";
  319. asort($aInputLists['values']);
  320. }
  321. $aInputLists['values'] = array_merge(array('' => _t('_adm_form_txt_select_list')), $aInputLists['values']);
  322. return $oForm->genRow($aInputLists);
  323. }
  324. function getCode($isDisplayHeader = true)
  325. {
  326. return $this->_oTemplate->parseHtmlByName('forms_values.html', array(
  327. 'content' => parent::getCode($isDisplayHeader),
  328. 'js_object' => $this->getJsObject(),
  329. 'page_url' => $this->sUrlPage,
  330. 'grid_object' => $this->_sObject,
  331. 'params_divider' => $this->sParamsDivider
  332. ));
  333. }
  334. protected function _addJsCss()
  335. {
  336. parent::_addJsCss();
  337. $this->_oTemplate->addJs(array('jquery.form.min.js', 'forms_values.js'));
  338. bx_import('BxTemplStudioFormView');
  339. $oForm = new BxTemplStudioFormView(array());
  340. $oForm->addCssJs();
  341. }
  342. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  343. {
  344. if($this->sList == '')
  345. $isDisabled = true;
  346. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  347. }
  348. protected function _getFilterControls ()
  349. {
  350. parent::_getFilterControls();
  351. $sContent = $this->getModulesSelectOne('getValues') . $this->getListsSelector($this->sModule);
  352. bx_import('BxTemplStudioFormView');
  353. $oForm = new BxTemplStudioFormView(array());
  354. $aInputSearch = array(
  355. 'type' => 'text',
  356. 'name' => 'keyword',
  357. 'attrs' => array(
  358. 'id' => 'bx-grid-search-' . $this->_sObject,
  359. ),
  360. 'tr_attrs' => array(
  361. 'style' => 'display:none;'
  362. )
  363. );
  364. $sContent .= $oForm->genRow($aInputSearch);
  365. return $sContent;
  366. }
  367. protected function _getAvailableSetValue($sList)
  368. {
  369. $aValues = array();
  370. $this->oDb->getValues(array('type' =>'by_key_key_value', 'value' => $sList), $aValues, false);
  371. ksort($aValues);
  372. $iValue = 1;
  373. foreach($aValues as $aValue) {
  374. if((int)$aValue['value'] == 0)
  375. continue;
  376. if((int)$aValue['value'] != $iValue)
  377. break;
  378. $iValue++;
  379. }
  380. return $iValue;
  381. }
  382. protected function canUseForSet($mixedValue)
  383. {
  384. return is_numeric($mixedValue) && (int)$mixedValue >= 1 && (int)$mixedValue <= BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX;
  385. }
  386. }
  387. /** @} */