1
0

BxBaseStudioFormsPreValues.php 18 KB

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