BxBaseStudioFormsFields.php 17 KB

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