BxBaseStudioFormsPreValues.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 TridentView Trident Studio Representation classes
  7. * @ingroup TridentStudio
  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');
  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. {
  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. $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. {
  136. $sAction = 'edit';
  137. $aValue = $this->_getItem('getValues');
  138. if($aValue === false) {
  139. $this->_echoResultJson(array());
  140. exit;
  141. }
  142. $aList = array();
  143. $this->oDb->getLists(array('type' => 'by_key', 'value' => $this->sList), $aList, false);
  144. $bUseInSets = (int)$aList['use_for_sets'] == 1;
  145. $aForm = array(
  146. 'form_attrs' => array(
  147. 'id' => 'adm-form-list-edit',
  148. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&list=' . $this->sList,
  149. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  150. ),
  151. 'params' => array (
  152. 'db' => array(
  153. 'table' => 'sys_form_pre_values',
  154. 'key' => 'id',
  155. 'uri' => '',
  156. 'uri_title' => '',
  157. 'submit_name' => 'do_submit'
  158. ),
  159. ),
  160. 'inputs' => array(
  161. 'id' => array(
  162. 'type' => 'hidden',
  163. 'name' => 'id',
  164. 'value' => $aValue['id'],
  165. 'db' => array (
  166. 'pass' => 'Int',
  167. ),
  168. ),
  169. 'LKey' => array(
  170. 'type' => 'text_translatable',
  171. 'name' => 'LKey',
  172. 'caption' => _t('_adm_form_txt_pre_values_lkey'),
  173. 'info' => _t('_adm_form_dsc_pre_values_lkey'),
  174. 'value' => $aValue['lkey'],
  175. 'required' => '1',
  176. 'db' => array (
  177. 'pass' => 'Xss',
  178. ),
  179. 'checker' => array (
  180. 'func' => 'LengthTranslatable',
  181. 'params' => array(3,100, 'LKey'),
  182. 'error' => _t('_adm_form_err_pre_values_lkey'),
  183. ),
  184. ),
  185. 'Empty' => array(
  186. 'type' => 'checkbox',
  187. 'name' => 'Empty',
  188. 'caption' => _t('_adm_form_txt_pre_values_empty'),
  189. 'info' => _t('_adm_form_dsc_pre_values_empty'),
  190. 'value' => 'on',
  191. 'checked' => empty($aValue['value']) ? '1' : '0',
  192. 'required' => '0'
  193. ),
  194. 'controls' => array(
  195. 'name' => 'controls',
  196. 'type' => 'input_set',
  197. array(
  198. 'type' => 'submit',
  199. 'name' => 'do_submit',
  200. 'value' => _t('_adm_form_btn_pre_lists_save'),
  201. ),
  202. array (
  203. 'type' => 'reset',
  204. 'name' => 'close',
  205. 'value' => _t('_adm_form_btn_pre_lists_cancel'),
  206. 'attrs' => array(
  207. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  208. 'class' => 'bx-def-margin-sec-left',
  209. ),
  210. )
  211. )
  212. )
  213. );
  214. $oForm = new BxTemplStudioFormView($aForm);
  215. $oForm->initChecker();
  216. if($oForm->isSubmittedAndValid()) {
  217. $aAdd = array();
  218. $bEmpty = $oForm->getCleanValue('Empty') == 'on';
  219. if($bEmpty && !empty($aValue['value']))
  220. $aAdd['Value'] = 0;
  221. else if(!$bEmpty && empty($aValue['value']))
  222. $aAdd['Value'] = $this->_getAvailableSetValue($this->sList);
  223. if(!empty($aAdd) && $bUseInSets) {
  224. $mixedValue = $aAdd['Value'];
  225. if(!$this->canUseForSet($mixedValue)) {
  226. if($this->oDb->isListUsedInSet($this->sList)) {
  227. $this->_echoResultJson(array('msg' => _t('_adm_form_err_pre_values_create_forbidden', BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX)), true);
  228. return;
  229. }
  230. $this->oDb->updateList($aList['id'], array('use_for_sets' => '0'));
  231. }
  232. }
  233. if($oForm->update($aValue['id'], $aAdd) !== false)
  234. $aRes = array('grid' => $this->getCode(false), 'blink' => $aValue['id']);
  235. else
  236. $aRes = array('msg' => _t('_adm_form_err_pre_values_edit'));
  237. $this->_echoResultJson($aRes, true);
  238. }
  239. else {
  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. {
  251. $sAction = 'delete';
  252. $iAffected = 0;
  253. $aIds = bx_get('ids');
  254. if(!$aIds || !is_array($aIds)) {
  255. $this->_echoResultJson(array());
  256. exit;
  257. }
  258. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  259. $aList = array();
  260. $this->oDb->getLists(array('type' => 'by_key', 'value' => $this->sList), $aList, false);
  261. $aIdsAffected = array ();
  262. foreach($aIds as $iId) {
  263. $aValue = array();
  264. $this->oDb->getValues(array('type' => 'by_id', 'value' => (int)$iId), $aValue);
  265. if(!is_array($aValue) || empty($aValue))
  266. continue;
  267. if((int)$this->_delete($iId) <= 0)
  268. continue;
  269. $oLanguage->deleteLanguageString($aValue['lkey']);
  270. $oLanguage->deleteLanguageString($aValue['lkey2']);
  271. if((int)$aList['use_for_sets'] != 1) {
  272. $bUseInSets = 1;
  273. $aValues = BxDolForm::getDataItems($this->sList);
  274. foreach($aValues as $mixedValue => $sTitle)
  275. if(!$this->canUseForSet($mixedValue)) {
  276. $bUseInSets = 0;
  277. break;
  278. }
  279. if($bUseInSets == 1)
  280. $this->oDb->updateList($aList['id'], array('use_for_sets' => $bUseInSets));
  281. }
  282. $aIdsAffected[] = $iId;
  283. $iAffected++;
  284. }
  285. $this->_echoResultJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t('_adm_form_err_pre_values_delete')));
  286. }
  287. function getJsObject()
  288. {
  289. return 'oBxDolStudioFormsValues';
  290. }
  291. function getListsSelector($sModule = '')
  292. {
  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. {
  321. return $this->_oTemplate->parseHtmlByName('forms_values.html', array(
  322. 'content' => parent::getCode($isDisplayHeader),
  323. 'js_object' => $this->getJsObject(),
  324. 'page_url' => $this->sUrlPage,
  325. 'grid_object' => $this->_sObject,
  326. 'params_divider' => $this->sParamsDivider
  327. ));
  328. }
  329. protected function _addJsCss()
  330. {
  331. parent::_addJsCss();
  332. $this->_oTemplate->addJs(array('jquery.form.min.js', 'forms_values.js'));
  333. $oForm = new BxTemplStudioFormView(array());
  334. $oForm->addCssJs();
  335. }
  336. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  337. {
  338. if($this->sList == '')
  339. $isDisabled = true;
  340. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  341. }
  342. protected function _getFilterControls ()
  343. {
  344. parent::_getFilterControls();
  345. $sContent = $this->getModulesSelectOne('getValues') . $this->getListsSelector($this->sModule);
  346. $oForm = new BxTemplStudioFormView(array());
  347. $aInputSearch = array(
  348. 'type' => 'text',
  349. 'name' => 'keyword',
  350. 'attrs' => array(
  351. 'id' => 'bx-grid-search-' . $this->_sObject,
  352. ),
  353. 'tr_attrs' => array(
  354. 'style' => 'display:none;'
  355. )
  356. );
  357. $sContent .= $oForm->genRow($aInputSearch);
  358. return $sContent;
  359. }
  360. protected function _getAvailableSetValue($sList)
  361. {
  362. $aValues = array();
  363. $this->oDb->getValues(array('type' =>'by_key_key_value', 'value' => $sList), $aValues, false);
  364. ksort($aValues);
  365. $iValue = 1;
  366. foreach($aValues as $aValue) {
  367. if((int)$aValue['value'] == 0)
  368. continue;
  369. if((int)$aValue['value'] != $iValue)
  370. break;
  371. $iValue++;
  372. }
  373. return $iValue;
  374. }
  375. protected function canUseForSet($mixedValue)
  376. {
  377. return is_numeric($mixedValue) && (int)$mixedValue >= 1 && (int)$mixedValue <= BX_DOL_STUDIO_FIELD_PRE_VALUE_INT_MAX;
  378. }
  379. }
  380. /** @} */