BxBaseStudioFormsFields.php 17 KB

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