BxBaseStudioFormsFields.php 18 KB

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