1
0

BxBaseStudioFormsPreLists.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 BxBaseStudioFormsPreLists extends BxDolStudioFormsPreLists
  11. {
  12. protected $_sFilter;
  13. protected $sUrlViewValues;
  14. function __construct($aOptions, $oTemplate = false)
  15. {
  16. parent::__construct($aOptions, $oTemplate);
  17. $this->_aOptions['actions_single']['edit']['attr']['title'] = _t('_adm_form_btn_pre_lists_edit');
  18. $this->_aOptions['actions_single']['delete']['attr']['title'] = _t('_adm_form_btn_pre_lists_delete');
  19. $this->_sFilter = '';
  20. if(($sFilter = $this->_getFilterValue()))
  21. $this->_sFilter = $sFilter;
  22. $this->sUrlViewValues = BX_DOL_URL_STUDIO . 'builder_forms.php?page=pre_values&module=%s&list=%s';
  23. }
  24. public function performActionAdd()
  25. {
  26. $sAction = 'add';
  27. bx_import('BxDolStudioUtils');
  28. bx_import('BxTemplStudioFormView');
  29. $aForm = array(
  30. 'form_attrs' => array(
  31. 'id' => 'adm-form-pre-list-create',
  32. 'action' => BX_DOL_URL_ROOT . bx_append_url_params('grid.php', [
  33. 'o' => $this->_sObject,
  34. 'a' => $sAction,
  35. $this->_aOptions['filter_get'] => $this->_sFilter
  36. ]),
  37. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  38. ),
  39. 'params' => array (
  40. 'db' => array(
  41. 'table' => 'sys_form_pre_lists',
  42. 'key' => 'id',
  43. 'uri' => '',
  44. 'uri_title' => '',
  45. 'submit_name' => 'do_submit'
  46. ),
  47. ),
  48. 'inputs' => array (
  49. 'module' => array(
  50. 'type' => 'hidden',
  51. 'name' => 'module',
  52. 'value' => BX_DOL_STUDIO_MODULE_CUSTOM,
  53. 'db' => array (
  54. 'pass' => 'Xss',
  55. ),
  56. ),
  57. 'use_for_sets' => array(
  58. 'type' => 'hidden',
  59. 'name' => 'use_for_sets',
  60. 'value' => 1,
  61. 'db' => array (
  62. 'pass' => 'Int',
  63. ),
  64. ),
  65. 'title' => array(
  66. 'type' => 'text_translatable',
  67. 'name' => 'title',
  68. 'caption' => _t('_adm_form_txt_pre_lists_title'),
  69. 'info' => _t('_adm_form_dsc_pre_lists_title'),
  70. 'value' => '_adm_form_txt_pre_lists',
  71. 'required' => '1',
  72. 'db' => array (
  73. 'pass' => 'Xss',
  74. ),
  75. 'checker' => array (
  76. 'func' => 'LengthTranslatable',
  77. 'params' => array(3,100, 'title'),
  78. 'error' => _t('_adm_form_err_pre_lists_title'),
  79. ),
  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_lists_add'),
  88. ),
  89. array (
  90. 'type' => 'reset',
  91. 'name' => 'close',
  92. 'value' => _t('_adm_form_btn_pre_lists_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. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  105. $sLanguage = $oLanguage->getCurrentLangName(false);
  106. $sKey = BxDolForm::getSubmittedValue('title-' . $sLanguage, $aForm['form_attrs']['method']);
  107. $sKey = uriGenerate($sKey, 'sys_form_pre_lists', 'key', ['empty' => 'key']);
  108. $iId = (int)$oForm->insert(array('key' => $sKey));
  109. if($iId != 0)
  110. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  111. else
  112. $aRes = array('msg' => _t('_adm_form_err_pre_lists_create'));
  113. echoJson($aRes);
  114. }
  115. else {
  116. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-pre-list-create-popup', _t('_adm_form_txt_pre_lists_create_popup'), $this->_oTemplate->parseHtmlByName('form_add_list.html', array(
  117. 'form_id' => $aForm['form_attrs']['id'],
  118. 'form' => $oForm->getCode(true),
  119. 'object' => $this->_sObject,
  120. 'action' => $sAction
  121. )));
  122. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  123. }
  124. }
  125. public function performActionEdit()
  126. {
  127. $sAction = 'edit';
  128. $aList = $this->_getItem('getLists');
  129. if($aList === false) {
  130. echoJson(array());
  131. exit;
  132. }
  133. bx_import('BxTemplStudioFormView');
  134. $aForm = array(
  135. 'form_attrs' => array(
  136. 'id' => 'adm-form-list-edit',
  137. 'action' => BX_DOL_URL_ROOT . bx_append_url_params('grid.php', [
  138. 'o' => $this->_sObject,
  139. 'a' => $sAction,
  140. $this->_aOptions['filter_get'] => $this->_sFilter
  141. ]),
  142. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  143. ),
  144. 'params' => array (
  145. 'db' => array(
  146. 'table' => 'sys_form_pre_lists',
  147. 'key' => 'id',
  148. 'uri' => '',
  149. 'uri_title' => '',
  150. 'submit_name' => 'do_submit'
  151. ),
  152. ),
  153. 'inputs' => array (
  154. 'id' => array(
  155. 'type' => 'hidden',
  156. 'name' => 'id',
  157. 'value' => $aList['id'],
  158. 'db' => array (
  159. 'pass' => 'Int',
  160. ),
  161. ),
  162. 'title' => array(
  163. 'type' => 'text_translatable',
  164. 'name' => 'title',
  165. 'caption' => _t('_adm_form_txt_pre_lists_title'),
  166. 'info' => _t('_adm_form_dsc_pre_lists_title'),
  167. 'value' => $aList['title'],
  168. 'required' => '1',
  169. 'db' => array (
  170. 'pass' => 'Xss',
  171. ),
  172. 'checker' => array (
  173. 'func' => 'LengthTranslatable',
  174. 'params' => array(3,100, 'title'),
  175. 'error' => _t('_adm_form_err_pre_lists_title'),
  176. ),
  177. ),
  178. 'controls' => array(
  179. 'name' => 'controls',
  180. 'type' => 'input_set',
  181. array(
  182. 'type' => 'submit',
  183. 'name' => 'do_submit',
  184. 'value' => _t('_adm_form_btn_pre_lists_save'),
  185. ),
  186. array (
  187. 'type' => 'reset',
  188. 'name' => 'close',
  189. 'value' => _t('_adm_form_btn_pre_lists_cancel'),
  190. 'attrs' => array(
  191. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  192. 'class' => 'bx-def-margin-sec-left',
  193. ),
  194. )
  195. )
  196. )
  197. );
  198. $oForm = new BxTemplStudioFormView($aForm);
  199. $oForm->initChecker();
  200. if($oForm->isSubmittedAndValid()) {
  201. if($oForm->update($aList['id']) !== false)
  202. $aRes = array('grid' => $this->getCode(false), 'blink' => $aList['id']);
  203. else
  204. $aRes = array('msg' => _t('_adm_form_err_pre_lists_edit'));
  205. echoJson($aRes);
  206. }
  207. else {
  208. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-pre-list-edit-popup', _t('_adm_form_txt_pre_lists_edit_popup', _t($aList['title'])), $this->_oTemplate->parseHtmlByName('form_add_list.html', array(
  209. 'form_id' => $aForm['form_attrs']['id'],
  210. 'form' => $oForm->getCode(true),
  211. 'object' => $this->_sObject,
  212. 'action' => $sAction
  213. )));
  214. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  215. }
  216. }
  217. public function performActionDelete()
  218. {
  219. $sAction = 'delete';
  220. $iAffected = 0;
  221. $aIds = bx_get('ids');
  222. if(!$aIds || !is_array($aIds)) {
  223. echoJson(array());
  224. exit;
  225. }
  226. $aIdsAffected = array ();
  227. foreach($aIds as $iId) {
  228. $aList = array();
  229. $this->oDb->getLists(array('type' => 'by_id', 'value' => (int)$iId), $aList, false);
  230. if(!is_array($aList) || empty($aList))
  231. continue;
  232. if(!$this->_canDelete($aList))
  233. continue;
  234. if(!$this->oDb->deleteValues(array('type' => 'by_key', 'value' => $aList['key'])) || (int)$this->_delete($iId) <= 0)
  235. continue;
  236. BxDolStudioLanguagesUtils::getInstance()->deleteLanguageString($aList['title']);
  237. $aIdsAffected[] = $iId;
  238. $iAffected++;
  239. }
  240. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t('_adm_from_err_pre_lists_delete')));
  241. }
  242. function getJsObject()
  243. {
  244. return 'oBxDolStudioFormsPreLists';
  245. }
  246. function getCode($isDisplayHeader = true)
  247. {
  248. return $this->_oTemplate->parseHtmlByName('forms_lists.html', array(
  249. 'content' => parent::getCode($isDisplayHeader),
  250. 'js_object' => $this->getJsObject(),
  251. 'grid_object' => $this->_sObject,
  252. 'params_divider' => $this->sParamsDivider
  253. ));
  254. }
  255. protected function _addJsCss()
  256. {
  257. parent::_addJsCss();
  258. $this->_oTemplate->addJs(array('jquery.form.min.js', 'forms_lists.js'));
  259. $oForm = new BxTemplStudioFormView(array());
  260. $oForm->addCssJs();
  261. }
  262. protected function _getCellValues ($mixedValue, $sKey, $aField, $aRow)
  263. {
  264. $sLink = sprintf($this->sUrlViewValues, $aRow['module'], $aRow['key']);
  265. $mixedValue = $this->_oTemplate->parseLink($sLink, _t('_adm_form_txt_pre_lists_n_values', $aRow['values_count']), array(
  266. 'title' => _t('_adm_form_txt_pre_lists_manage_values')
  267. ));
  268. return parent::_getCellDefault ($mixedValue, $sKey, $aField, $aRow);
  269. }
  270. protected function _getCellModule($mixedValue, $sKey, $aField, $aRow)
  271. {
  272. $mixedValue = $this->_limitMaxLength($this->getModuleTitle($aRow['module']), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  273. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  274. }
  275. protected function _getCellUseForSets ($mixedValue, $sKey, $aField, $aRow)
  276. {
  277. $aChanger = array(
  278. '0' => '_adm_form_txt_pre_lists_no',
  279. '1' => '_adm_form_txt_pre_lists_yes'
  280. );
  281. $mixedValue = $this->_limitMaxLength(_t($aChanger[$mixedValue]), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  282. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  283. }
  284. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  285. {
  286. if(!$this->_canDelete($aRow))
  287. return '';
  288. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  289. }
  290. protected function _getFilterControls ()
  291. {
  292. $sContent = parent::_getFilterControls();
  293. $sJsObject = $this->getJsObject();
  294. $oForm = new BxTemplStudioFormView(array());
  295. $aInputModules = array(
  296. 'type' => 'select',
  297. 'name' => 'module',
  298. 'attrs' => array(
  299. 'id' => 'bx-grid-module-' . $this->_sObject,
  300. 'onChange' => 'javascript:$(this).off(\'keyup focusout\'); ' . $sJsObject . '.onChangeFilter()',
  301. 'onBlur' => 'javascript:' . $sJsObject . '.onChangeFilter()',
  302. ),
  303. 'value' => '',
  304. 'values' => $this->getModules()
  305. );
  306. $aCounter = array();
  307. $this->oDb->getLists(array('type' => 'counter_by_modules'), $aCounter, false);
  308. foreach($aInputModules['values'] as $sKey => $sValue)
  309. $aInputModules['values'][$sKey] = $aInputModules['values'][$sKey] . " (" . (isset($aCounter[$sKey]) ? $aCounter[$sKey] : "0") . ")";
  310. $aInputModules['values'] = array_merge(array('' => _t('_adm_form_txt_all_modules')), $aInputModules['values']);
  311. return $oForm->genRow($aInputModules) . $sContent;
  312. }
  313. protected function _canDelete($aList)
  314. {
  315. bx_import('BxDolStudioUtils');
  316. return $aList['module'] == BX_DOL_STUDIO_MODULE_CUSTOM;
  317. }
  318. }
  319. /** @} */