BxBaseStudioFormsFields.php 17 KB

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