BxBaseStudioFormsPreValues.php 17 KB

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