BxBaseStudioFormsFields.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. bx_import('BxTemplStudioFormsField');
  11. define('BX_DOL_STUDIO_FORMS_FIELDS_JS_OBJECT', 'oBxDolStudioFormsFields');
  12. class BxBaseStudioFormsFields extends BxDolStudioFormsFields
  13. {
  14. protected $sClass;
  15. protected $sUrlPage;
  16. function __construct($aOptions, $oTemplate = false)
  17. {
  18. parent::__construct($aOptions, $oTemplate);
  19. $this->_aConfirmMessages['delete'] = _t('_adm_form_txt_confirm_delete');
  20. $this->_aOptions['actions_single']['edit']['attr']['title'] = _t('_adm_form_btn_field_edit');
  21. $this->_aOptions['actions_single']['delete']['attr']['title'] = _t('_adm_form_btn_field_delete');
  22. $this->sClass = 'BxTemplStudioFormsField';
  23. $this->sUrlPage = BX_DOL_URL_STUDIO . 'builder_forms.php?page=fields';
  24. }
  25. public function performActionAdd()
  26. {
  27. $sAction = 'add';
  28. $sType = '';
  29. if(($mixedType = bx_get('type')) !== false)
  30. $sType = bx_process_input($mixedType);
  31. $oClass = $this->_getFieldObject($sType, array('module' => $this->sModule, 'object' => $this->sObject, 'display' => $this->sDisplay));
  32. if($oClass === false || !$oClass->canAdd()) {
  33. echoJson(array('msg' => _t('_adm_form_err_field_add_not_allowed')));
  34. exit;
  35. }
  36. $mixedResult = $oClass->getCode($sAction, $this->_sObject);
  37. if(is_string($mixedResult))
  38. echoJson(array('popup' => array('html' => $mixedResult, 'options' => array('closeOnOuterClick' => false))));
  39. else if(is_int($mixedResult) || is_bool($mixedResult)) {
  40. $aResult = $mixedResult !== false ? array('grid' => $this->getCode(false), 'blink' => (int)$mixedResult) : array('msg' => _t('_adm_form_err_field_add'));
  41. echoJson($aResult);
  42. }
  43. }
  44. public function performActionEdit()
  45. {
  46. $sAction = 'edit';
  47. $aIds = bx_get('ids');
  48. if(!$aIds || !is_array($aIds)) {
  49. $iId = (int)bx_get('di_id');
  50. if(!$iId) {
  51. echoJson(array());
  52. exit;
  53. }
  54. $aIds = array($iId);
  55. }
  56. $iId = $aIds[0];
  57. $aField = array();
  58. $this->oDb->getInputs(array('type' => 'by_object_id', 'object' => $this->sObject, 'id' => (int)$iId), $aField, false);
  59. if(empty($aField) || !is_array($aField)) {
  60. echoJson(array());
  61. exit;
  62. }
  63. $sType = '';
  64. if(($mixedType = bx_get('type')) !== false)
  65. $aField['type'] = bx_process_input($mixedType);
  66. $oClass = $this->_getFieldObject($aField['type'], array('module' => $this->sModule, 'object' => $this->sObject, 'display' => $this->sDisplay), $aField);
  67. if($oClass === false) {
  68. echoJson(array());
  69. exit;
  70. }
  71. $mixedResult = $oClass->getCode($sAction, $this->_sObject);
  72. if(is_string($mixedResult))
  73. echoJson(array('popup' => array('html' => $mixedResult, 'options' => array('closeOnOuterClick' => false))));
  74. else if(is_bool($mixedResult)) {
  75. $aResult = $mixedResult ? array('grid' => $this->getCode(false), 'blink' => $iId) : array('msg' => _t('_adm_form_err_field_edit'));
  76. echoJson($aResult);
  77. }
  78. }
  79. public function performActionDelete()
  80. {
  81. $sAction = 'delete';
  82. $iAffected = 0;
  83. $aIds = bx_get('ids');
  84. if(!$aIds || !is_array($aIds)) {
  85. echoJson(array());
  86. exit;
  87. }
  88. $aIdsAffected = array ();
  89. foreach($aIds as $iId) {
  90. $aField = array();
  91. $this->oDb->getInputs(array('type' => 'by_object_id', 'object' => $this->sObject, 'id' => (int)$iId), $aField, false);
  92. if(empty($aField) || !is_array($aField))
  93. continue;
  94. if((int)$aField['deletable'] != 1)
  95. continue;
  96. $oClass = $this->_getFieldObject($aField['type'], array('module' => $this->sModule, 'object' => $this->sObject, 'display' => $this->sDisplay));
  97. if($oClass === false || (int)$this->_delete($iId) <= 0 || !$this->oDb->deleteInputs(array('type' => 'by_id', 'value' => $aField['id'], 'object' => $aField['object'], 'name' => $aField['name'])))
  98. continue;
  99. $oClass->alterRemove($aField['name']);
  100. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  101. if(!empty($aField['caption']))
  102. $oLanguage->deleteLanguageString($aField['caption']);
  103. if(!empty($aField['info']))
  104. $oLanguage->deleteLanguageString($aField['info']);
  105. if(!empty($aField['checker_error']))
  106. $oLanguage->deleteLanguageString($aField['checker_error']);
  107. $aIdsAffected[] = $iId;
  108. $iAffected++;
  109. }
  110. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t('_adm_from_err_field_delete')));
  111. }
  112. public function performActionShowTo()
  113. {
  114. $sAction = 'show_to';
  115. $aIds = bx_get('ids');
  116. if(!$aIds || !is_array($aIds)) {
  117. $iId = (int)bx_get('id');
  118. if(!$iId) {
  119. echoJson(array());
  120. exit;
  121. }
  122. $aIds = array($iId);
  123. }
  124. $iId = $aIds[0];
  125. $aField = array();
  126. $this->oDb->getInputs(array('type' => 'by_object_id', 'object' => $this->sObject, 'id' => (int)$iId), $aField, false);
  127. if(empty($aField) || !is_array($aField)) {
  128. echoJson(array());
  129. exit;
  130. }
  131. bx_import('BxDolStudioUtils');
  132. $aForm = array(
  133. 'form_attrs' => array(
  134. 'id' => 'adm-form-field-visibility',
  135. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&object=' . $this->sObject . '&display=' . $this->sDisplay,
  136. 'method' => 'post'
  137. ),
  138. 'params' => array (
  139. 'db' => array(
  140. 'table' => 'sys_form_display_inputs',
  141. 'key' => 'id',
  142. 'uri' => '',
  143. 'uri_title' => '',
  144. 'submit_name' => 'do_submit'
  145. ),
  146. ),
  147. 'inputs' => array (
  148. 'id' => array(
  149. 'type' => 'hidden',
  150. 'name' => 'id',
  151. 'value' => $iId,
  152. 'db' => array (
  153. 'pass' => 'Int',
  154. ),
  155. ),
  156. 'visible_for' => array(
  157. 'type' => 'select',
  158. 'name' => 'visible_for',
  159. 'caption' => _t('_adm_form_txt_field_visible_for'),
  160. 'info' => '',
  161. 'value' => $aField['visible_for_levels'] == BX_DOL_INT_MAX ? BX_DOL_STUDIO_VISIBLE_ALL : BX_DOL_STUDIO_VISIBLE_SELECTED,
  162. 'values' => array(
  163. array('key' => BX_DOL_STUDIO_VISIBLE_ALL, 'value' => _t('_adm_form_txt_field_visible_for_all')),
  164. array('key' => BX_DOL_STUDIO_VISIBLE_SELECTED, 'value' => _t('_adm_form_txt_field_visible_for_selected')),
  165. ),
  166. 'required' => '0',
  167. 'attrs' => array(
  168. 'onchange' => $this->getJsObject() . '.onChangeVisibleFor(this)'
  169. ),
  170. 'db' => array (
  171. 'pass' => 'Xss',
  172. )
  173. ),
  174. 'visible_for_levels' => array(
  175. 'type' => 'checkbox_set',
  176. 'name' => 'visible_for_levels',
  177. 'caption' => _t('_adm_form_txt_field_visible_for_levels'),
  178. 'info' => _t('_adm_form_dsc_field_visible_for_levels'),
  179. 'value' => '',
  180. 'values' => array(),
  181. 'tr_attrs' => array(
  182. 'style' => $aField['visible_for_levels'] == BX_DOL_INT_MAX ? 'display:none' : ''
  183. ),
  184. 'db' => array (
  185. 'pass' => 'Int',
  186. ),
  187. ),
  188. 'controls' => array(
  189. 'name' => 'controls',
  190. 'type' => 'input_set',
  191. array(
  192. 'type' => 'submit',
  193. 'name' => 'do_submit',
  194. 'value' => _t('_adm_form_btn_field_save'),
  195. ),
  196. array (
  197. 'type' => 'reset',
  198. 'name' => 'close',
  199. 'value' => _t('_adm_form_btn_field_cancel'),
  200. 'attrs' => array(
  201. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  202. 'class' => 'bx-def-margin-sec-left',
  203. ),
  204. )
  205. )
  206. )
  207. );
  208. BxDolStudioUtils::getVisibilityValues($aField['visible_for_levels'], $aForm['inputs']['visible_for_levels']['values'], $aForm['inputs']['visible_for_levels']['value']);
  209. $oForm = new BxTemplStudioFormView($aForm);
  210. $oForm->initChecker();
  211. if($oForm->isSubmittedAndValid()) {
  212. if($oForm->updateWithVisibility($iId) !== false)
  213. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  214. else
  215. $aRes = array('msg' => _t('_adm_form_err_field_show_to'));
  216. echoJson($aRes);
  217. }
  218. else {
  219. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-field-show-to-popup', _t('_adm_form_txt_field_show_to_popup', _t($aField['caption_system'])), $this->_oTemplate->parseHtmlByName('form_add_field.html', array(
  220. 'form_id' => $aForm['form_attrs']['id'],
  221. 'form' => $oForm->getCode(true),
  222. 'object' => $this->_sObject,
  223. 'action' => $sAction
  224. )));
  225. echoJson(array('popup' => $sContent));
  226. }
  227. }
  228. function getJsObject()
  229. {
  230. return BX_DOL_STUDIO_FORMS_FIELDS_JS_OBJECT;
  231. }
  232. function getDisplaysSelector($sModule = '')
  233. {
  234. $oForm = new BxTemplStudioFormView(array());
  235. $aInputDisplays = array(
  236. 'type' => 'select',
  237. 'name' => 'display',
  238. 'attrs' => array(
  239. 'id' => 'bx-grid-display-' . $this->_sObject,
  240. 'onChange' => 'javascript:' . $this->getJsObject() . '.onChangeDisplay()'
  241. ),
  242. 'value' => $this->sObject . $this->sParamsDivider . $this->sDisplay,
  243. 'values' => array(
  244. '' => _t('_adm_form_txt_select_display'),
  245. )
  246. );
  247. $aDisplays = array();
  248. if(!empty($sModule))
  249. $this->oDb->getDisplays(array('type' => 'by_module_with_forms', 'value' => $sModule), $aDisplays, false);
  250. else
  251. $aInputDisplays['attrs']['disabled'] = 'disabled';
  252. if(!empty($aDisplays)) {
  253. $aDisplaysGrouped = $aCounter = array();
  254. $this->oDb->getInputs(array('type' => 'counter_by_displays'), $aCounter, false);
  255. foreach($aDisplays as $aDisplay)
  256. $aDisplaysGrouped[_t($aDisplay['form_title'])][] = $aDisplay;
  257. ksort($aDisplaysGrouped);
  258. foreach($aDisplaysGrouped as $sForm => $aDisplays) {
  259. if(!empty($aDisplays))
  260. $aInputDisplays['values'][] = array('type' => 'group_header', 'value' => _t($aDisplays[0]['form_title']) . " (" . (isset($aCounter[$aDisplays[0]['name']]) ? $aCounter[$aDisplays[0]['name']] : "0") . ")");
  261. $aDisplaysSubgroup = array();
  262. foreach($aDisplays as $aDisplay)
  263. $aDisplaysSubgroup[$aDisplay['object'] . $this->sParamsDivider . $aDisplay['name']] = _t($aDisplay['title']);
  264. asort($aDisplaysSubgroup);
  265. $aInputDisplays['values'] = array_merge($aInputDisplays['values'], $aDisplaysSubgroup);
  266. if(!empty($aDisplays))
  267. $aInputDisplays['values'][] = array('type' => 'group_end');
  268. }
  269. }
  270. return $oForm->genRow($aInputDisplays);
  271. }
  272. function getCode($isDisplayHeader = true)
  273. {
  274. return $this->_oTemplate->parseHtmlByName('forms_fields.html', array(
  275. 'content' => parent::getCode($isDisplayHeader),
  276. 'js_object' => $this->getJsObject(),
  277. 'page_url' => $this->sUrlPage,
  278. 'grid_object' => $this->_sObject,
  279. 'params_divider' => $this->sParamsDivider
  280. ));
  281. }
  282. protected function _addJsCss()
  283. {
  284. parent::_addJsCss();
  285. $this->_oTemplate->addCss(array('menu.css'));
  286. $this->_oTemplate->addJs(array('jquery.form.min.js', 'forms_fields.js'));
  287. $oForm = new BxTemplStudioFormView(array());
  288. $oForm->addCssJs();
  289. }
  290. protected function _getFieldObject ($sType, $aParams = array(), $aField = array())
  291. {
  292. $sClass = $this->sClass . $this->getClassName($sType);
  293. if(!class_exists($sClass))
  294. return false;
  295. $oClass = new $sClass($aParams, $aField);
  296. $oClass->init();
  297. return $oClass;
  298. }
  299. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  300. {
  301. if(!$this->_isEditable($aRow))
  302. return parent::_getCellDefault('', $sKey, $aField, $aRow);
  303. return parent::_getCellSwitcher($mixedValue, $sKey, $aField, $aRow);
  304. }
  305. protected function _getCellType ($mixedValue, $sKey, $aField, $aRow)
  306. {
  307. $mixedValue = $this->_oTemplate->getIcon('ui-' . $aRow['type'] . '.png', array('alt' => _t('_adm_form_txt_field_type_' . $aRow['type'])));
  308. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  309. }
  310. protected function _getCellModule($mixedValue, $sKey, $aField, $aRow)
  311. {
  312. $mixedValue = $this->_limitMaxLength($this->getModuleTitle($aRow['module']), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  313. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  314. }
  315. protected function _getCellVisibleForLevels ($mixedValue, $sKey, $aField, $aRow)
  316. {
  317. if(!$this->_isEditable($aRow))
  318. return parent::_getCellDefault('', $sKey, $aField, $aRow);
  319. $mixedValue = $this->_oTemplate->parseLink('javascript:void(0)', BxDolStudioUtils::getVisibilityTitle($aRow['visible_for_levels']), array(
  320. 'title' => _t('_adm_form_txt_fields_manage_visibility'),
  321. 'bx_grid_action_single' => 'show_to',
  322. 'bx_grid_action_data' => $aRow['id']
  323. ));
  324. return parent::_getCellDefault ($mixedValue, $sKey, $aField, $aRow);
  325. }
  326. protected function _getActionAdd ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  327. {
  328. if($this->sDisplay == '')
  329. $isDisabled = true;
  330. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  331. }
  332. protected function _getActionEdit ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  333. {
  334. if($sType == 'single' && !$this->_isEditable($aRow))
  335. return '';
  336. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  337. }
  338. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  339. {
  340. if($sType == 'single' && !$this->_isDeletable($aRow))
  341. return '';
  342. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  343. }
  344. protected function _getActionShowTo ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  345. {
  346. return '';
  347. }
  348. protected function _getActionsDisabledBehavior($aRow)
  349. {
  350. return false;
  351. }
  352. protected function _getFilterControls ()
  353. {
  354. parent::_getFilterControls();
  355. $sContent = $this->getModulesSelectOne('getInputs') . $this->getDisplaysSelector($this->sModule);
  356. $oForm = new BxTemplStudioFormView(array());
  357. $aInputSearch = array(
  358. 'type' => 'text',
  359. 'name' => 'keyword',
  360. 'attrs' => array(
  361. 'id' => 'bx-grid-search-' . $this->_sObject,
  362. ),
  363. 'tr_attrs' => array(
  364. 'style' => 'display:none;'
  365. )
  366. );
  367. $sContent .= $oForm->genRow($aInputSearch);
  368. return $sContent;
  369. }
  370. protected function _isEditable(&$aRow)
  371. {
  372. return (int)$aRow['editable'] != 0;
  373. }
  374. protected function _isDeletable(&$aRow)
  375. {
  376. return (int)$aRow['deletable'] != 0;
  377. }
  378. }
  379. /** @} */