BxBaseStudioFormsFields.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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->getInputs(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->getInputs(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->deleteInputs(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->getInputs(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. 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. $this->_echoResultJson($aRes, true);
  217. }
  218. else {
  219. bx_import('BxTemplStudioFunctions');
  220. $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(
  221. 'form_id' => $aForm['form_attrs']['id'],
  222. 'form' => $oForm->getCode(true),
  223. 'object' => $this->_sObject,
  224. 'action' => $sAction
  225. )));
  226. $this->_echoResultJson(array('popup' => $sContent), true);
  227. }
  228. }
  229. function getJsObject() {
  230. return BX_DOL_STUDIO_FORMS_FIELDS_JS_OBJECT;
  231. }
  232. function getDisplaysSelector($sModule = '') {
  233. bx_import('BxTemplStudioFormView');
  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. return $this->_oTemplate->parseHtmlByName('forms_fields.html', array(
  274. 'content' => parent::getCode($isDisplayHeader),
  275. 'js_object' => $this->getJsObject(),
  276. 'page_url' => $this->sUrlPage,
  277. 'grid_object' => $this->_sObject,
  278. 'params_divider' => $this->sParamsDivider
  279. ));
  280. }
  281. protected function _addJsCss() {
  282. parent::_addJsCss();
  283. $this->_oTemplate->addCss(array('menu.css'));
  284. $this->_oTemplate->addJs(array('jquery.form.js', 'forms_fields.js'));
  285. bx_import('BxTemplStudioFormView');
  286. $oForm = new BxTemplStudioFormView(array());
  287. $oForm->addCssJs();
  288. }
  289. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow) {
  290. if((int)$aRow['editable'] == 0)
  291. return parent::_getCellDefault('', $sKey, $aField, $aRow);
  292. return parent::_getCellSwitcher($mixedValue, $sKey, $aField, $aRow);
  293. }
  294. protected function _getCellType ($mixedValue, $sKey, $aField, $aRow) {
  295. $mixedValue = $this->_oTemplate->getIcon('ui-' . $aRow['type'] . '.png', array('alt' => _t('_adm_form_txt_field_type_' . $aRow['type'])));
  296. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  297. }
  298. protected function _getCellModule($mixedValue, $sKey, $aField, $aRow) {
  299. $mixedValue = $this->_limitMaxLength($this->getModuleTitle($aRow['module']), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  300. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  301. }
  302. protected function _getCellVisibleForLevels ($mixedValue, $sKey, $aField, $aRow) {
  303. if((int)$aRow['editable'] == 0)
  304. return parent::_getCellDefault('', $sKey, $aField, $aRow);
  305. $mixedValue = $this->_oTemplate->parseHtmlByName('bx_a.html', array(
  306. 'href' => 'javascript:void(0)',
  307. 'title' => _t('_adm_form_txt_fields_manage_visibility'),
  308. 'bx_repeat:attrs' => array(
  309. array('key' => 'bx_grid_action_single', 'value' => 'show_to'),
  310. array('key' => 'bx_grid_action_data', 'value' => $aRow['id'])
  311. ),
  312. 'content' => BxDolStudioUtils::getVisibilityTitle($aRow['visible_for_levels'])
  313. ));
  314. return parent::_getCellDefault ($mixedValue, $sKey, $aField, $aRow);
  315. }
  316. protected function _getActionAdd ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array()) {
  317. if($this->sDisplay == '')
  318. $isDisabled = true;
  319. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  320. }
  321. protected function _getActionEdit ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array()) {
  322. if($sType == 'single' && (int)$aRow['editable'] == 0)
  323. return '';
  324. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  325. }
  326. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array()) {
  327. if($sType == 'single' && (int)$aRow['deletable'] == 0)
  328. return '';
  329. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  330. }
  331. protected function _getActionShowTo ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array()) {
  332. return '';
  333. }
  334. protected function _getActionsDisabledBehavior($aRow) {
  335. return false;
  336. }
  337. protected function _getFilterControls () {
  338. parent::_getFilterControls();
  339. $sContent = $this->getModulesSelectOne('getInputs') . $this->getDisplaysSelector($this->sModule);
  340. bx_import('BxTemplStudioFormView');
  341. $oForm = new BxTemplStudioFormView(array());
  342. $aInputSearch = array(
  343. 'type' => 'text',
  344. 'name' => 'keyword',
  345. 'attrs' => array(
  346. 'id' => 'bx-grid-search-' . $this->_sObject,
  347. ),
  348. 'tr_attrs' => array(
  349. 'style' => 'display:none;'
  350. )
  351. );
  352. $sContent .= $oForm->genRow($aInputSearch);
  353. return $sContent;
  354. }
  355. }
  356. /** @} */