BxBaseStudioFormsFields.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 TridentView Trident Studio Representation classes
  7. * @ingroup TridentStudio
  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. $sClass = $this->sClass . $this->getClassName($sType);
  32. $oClass = new $sClass(array('module' => $this->sModule, 'object' => $this->sObject, 'display' => $this->sDisplay));
  33. if(!$oClass->canAdd()) {
  34. $this->_echoResultJson(array('msg' => _t('_adm_form_err_field_add_not_allowed')), true);
  35. exit;
  36. }
  37. $mixedResult = $oClass->getCode($sAction, $this->_sObject);
  38. if(is_string($mixedResult))
  39. $this->_echoResultJson(array('popup' => array('html' => $mixedResult, 'options' => array('closeOnOuterClick' => false))), true);
  40. else if(is_int($mixedResult) || is_bool($mixedResult)) {
  41. $aResult = $mixedResult !== false ? array('grid' => $this->getCode(false), 'blink' => (int)$mixedResult) : array('msg' => _t('_adm_form_err_field_add'));
  42. $this->_echoResultJson($aResult, true);
  43. }
  44. }
  45. public function performActionEdit()
  46. {
  47. $sAction = 'edit';
  48. $aIds = bx_get('ids');
  49. if(!$aIds || !is_array($aIds)) {
  50. $iId = (int)bx_get('di_id');
  51. if(!$iId) {
  52. $this->_echoResultJson(array());
  53. exit;
  54. }
  55. $aIds = array($iId);
  56. }
  57. $iId = $aIds[0];
  58. $aField = array();
  59. $this->oDb->getInputs(array('type' => 'by_object_id', 'object' => $this->sObject, 'id' => (int)$iId), $aField, false);
  60. if(empty($aField) || !is_array($aField)) {
  61. $this->_echoResultJson(array());
  62. exit;
  63. }
  64. $sType = '';
  65. if(($mixedType = bx_get('type')) !== false)
  66. $aField['type'] = bx_process_input($mixedType);
  67. $sClass = $this->sClass . $this->getClassName($aField['type']);
  68. if(!class_exists($sClass)) {
  69. $this->_echoResultJson(array());
  70. exit;
  71. }
  72. $oClass = new $sClass(array('module' => $this->sModule, 'object' => $this->sObject, 'display' => $this->sDisplay), $aField);
  73. $mixedResult = $oClass->getCode($sAction, $this->_sObject);
  74. if(is_string($mixedResult))
  75. $this->_echoResultJson(array('popup' => array('html' => $mixedResult, 'options' => array('closeOnOuterClick' => false))), true);
  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. $this->_echoResultJson($aResult, true);
  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. $this->_echoResultJson(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. $sClass = $this->sClass . $this->getClassName($aField['type']);
  99. $oClass = new $sClass(array('module' => $this->sModule, 'object' => $this->sObject, 'display' => $this->sDisplay));
  100. if((int)$this->_delete($iId) <= 0 || !$this->oDb->deleteInputs(array('type' => 'by_id', 'value' => $aField['id'], 'object' => $aField['object'], 'name' => $aField['name'])))
  101. continue;
  102. $oClass->alterRemove($aField['name']);
  103. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  104. if(!empty($aField['caption']))
  105. $oLanguage->deleteLanguageString($aField['caption']);
  106. if(!empty($aField['info']))
  107. $oLanguage->deleteLanguageString($aField['info']);
  108. if(!empty($aField['checker_error']))
  109. $oLanguage->deleteLanguageString($aField['checker_error']);
  110. $aIdsAffected[] = $iId;
  111. $iAffected++;
  112. }
  113. $this->_echoResultJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t('_adm_from_err_field_delete')));
  114. }
  115. public function performActionShowTo()
  116. {
  117. $sAction = 'show_to';
  118. $aIds = bx_get('ids');
  119. if(!$aIds || !is_array($aIds)) {
  120. $iId = (int)bx_get('id');
  121. if(!$iId) {
  122. $this->_echoResultJson(array());
  123. exit;
  124. }
  125. $aIds = array($iId);
  126. }
  127. $iId = $aIds[0];
  128. $aField = array();
  129. $this->oDb->getInputs(array('type' => 'by_object_id', 'object' => $this->sObject, 'id' => (int)$iId), $aField, false);
  130. if(empty($aField) || !is_array($aField)) {
  131. $this->_echoResultJson(array());
  132. exit;
  133. }
  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. $this->_echoResultJson($aRes, true);
  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. $this->_echoResultJson(array('popup' => $sContent), true);
  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 _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  293. {
  294. if((int)$aRow['editable'] == 0)
  295. return parent::_getCellDefault('', $sKey, $aField, $aRow);
  296. return parent::_getCellSwitcher($mixedValue, $sKey, $aField, $aRow);
  297. }
  298. protected function _getCellType ($mixedValue, $sKey, $aField, $aRow)
  299. {
  300. $mixedValue = $this->_oTemplate->getIcon('ui-' . $aRow['type'] . '.png', array('alt' => _t('_adm_form_txt_field_type_' . $aRow['type'])));
  301. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  302. }
  303. protected function _getCellModule($mixedValue, $sKey, $aField, $aRow)
  304. {
  305. $mixedValue = $this->_limitMaxLength($this->getModuleTitle($aRow['module']), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  306. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  307. }
  308. protected function _getCellVisibleForLevels ($mixedValue, $sKey, $aField, $aRow)
  309. {
  310. if((int)$aRow['editable'] == 0)
  311. return parent::_getCellDefault('', $sKey, $aField, $aRow);
  312. $mixedValue = $this->_oTemplate->parseHtmlByName('bx_a.html', array(
  313. 'href' => 'javascript:void(0)',
  314. 'title' => _t('_adm_form_txt_fields_manage_visibility'),
  315. 'bx_repeat:attrs' => array(
  316. array('key' => 'bx_grid_action_single', 'value' => 'show_to'),
  317. array('key' => 'bx_grid_action_data', 'value' => $aRow['id'])
  318. ),
  319. 'content' => BxDolStudioUtils::getVisibilityTitle($aRow['visible_for_levels'])
  320. ));
  321. return parent::_getCellDefault ($mixedValue, $sKey, $aField, $aRow);
  322. }
  323. protected function _getActionAdd ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  324. {
  325. if($this->sDisplay == '')
  326. $isDisabled = true;
  327. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  328. }
  329. protected function _getActionEdit ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  330. {
  331. if($sType == 'single' && (int)$aRow['editable'] == 0)
  332. return '';
  333. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  334. }
  335. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  336. {
  337. if($sType == 'single' && (int)$aRow['deletable'] == 0)
  338. return '';
  339. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  340. }
  341. protected function _getActionShowTo ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  342. {
  343. return '';
  344. }
  345. protected function _getActionsDisabledBehavior($aRow)
  346. {
  347. return false;
  348. }
  349. protected function _getFilterControls ()
  350. {
  351. parent::_getFilterControls();
  352. $sContent = $this->getModulesSelectOne('getInputs') . $this->getDisplaysSelector($this->sModule);
  353. $oForm = new BxTemplStudioFormView(array());
  354. $aInputSearch = array(
  355. 'type' => 'text',
  356. 'name' => 'keyword',
  357. 'attrs' => array(
  358. 'id' => 'bx-grid-search-' . $this->_sObject,
  359. ),
  360. 'tr_attrs' => array(
  361. 'style' => 'display:none;'
  362. )
  363. );
  364. $sContent .= $oForm->genRow($aInputSearch);
  365. return $sContent;
  366. }
  367. }
  368. /** @} */