1
0

BxBaseStudioFormsFields.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. bx_import('BxDolStudioUtils');
  135. $aForm = array(
  136. 'form_attrs' => array(
  137. 'id' => 'adm-form-field-visibility',
  138. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&object=' . $this->sObject . '&display=' . $this->sDisplay,
  139. 'method' => 'post'
  140. ),
  141. 'params' => array (
  142. 'db' => array(
  143. 'table' => 'sys_form_display_inputs',
  144. 'key' => 'id',
  145. 'uri' => '',
  146. 'uri_title' => '',
  147. 'submit_name' => 'do_submit'
  148. ),
  149. ),
  150. 'inputs' => array (
  151. 'id' => array(
  152. 'type' => 'hidden',
  153. 'name' => 'id',
  154. 'value' => $iId,
  155. 'db' => array (
  156. 'pass' => 'Int',
  157. ),
  158. ),
  159. 'visible_for' => array(
  160. 'type' => 'select',
  161. 'name' => 'visible_for',
  162. 'caption' => _t('_adm_form_txt_field_visible_for'),
  163. 'info' => '',
  164. 'value' => $aField['visible_for_levels'] == BX_DOL_INT_MAX ? BX_DOL_STUDIO_VISIBLE_ALL : BX_DOL_STUDIO_VISIBLE_SELECTED,
  165. 'values' => array(
  166. array('key' => BX_DOL_STUDIO_VISIBLE_ALL, 'value' => _t('_adm_form_txt_field_visible_for_all')),
  167. array('key' => BX_DOL_STUDIO_VISIBLE_SELECTED, 'value' => _t('_adm_form_txt_field_visible_for_selected')),
  168. ),
  169. 'required' => '0',
  170. 'attrs' => array(
  171. 'onchange' => $this->getJsObject() . '.onChangeVisibleFor(this)'
  172. ),
  173. 'db' => array (
  174. 'pass' => 'Xss',
  175. )
  176. ),
  177. 'visible_for_levels' => array(
  178. 'type' => 'checkbox_set',
  179. 'name' => 'visible_for_levels',
  180. 'caption' => _t('_adm_form_txt_field_visible_for_levels'),
  181. 'info' => _t('_adm_form_dsc_field_visible_for_levels'),
  182. 'value' => '',
  183. 'values' => array(),
  184. 'tr_attrs' => array(
  185. 'style' => $aField['visible_for_levels'] == BX_DOL_INT_MAX ? 'display:none' : ''
  186. ),
  187. 'db' => array (
  188. 'pass' => 'Int',
  189. ),
  190. ),
  191. 'controls' => array(
  192. 'name' => 'controls',
  193. 'type' => 'input_set',
  194. array(
  195. 'type' => 'submit',
  196. 'name' => 'do_submit',
  197. 'value' => _t('_adm_form_btn_field_save'),
  198. ),
  199. array (
  200. 'type' => 'reset',
  201. 'name' => 'close',
  202. 'value' => _t('_adm_form_btn_field_cancel'),
  203. 'attrs' => array(
  204. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  205. 'class' => 'bx-def-margin-sec-left',
  206. ),
  207. )
  208. )
  209. )
  210. );
  211. BxDolStudioUtils::getVisibilityValues($aField['visible_for_levels'], $aForm['inputs']['visible_for_levels']['values'], $aForm['inputs']['visible_for_levels']['value']);
  212. $oForm = new BxTemplStudioFormView($aForm);
  213. $oForm->initChecker();
  214. if($oForm->isSubmittedAndValid()) {
  215. if($oForm->updateWithVisibility($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. $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(
  223. 'form_id' => $aForm['form_attrs']['id'],
  224. 'form' => $oForm->getCode(true),
  225. 'object' => $this->_sObject,
  226. 'action' => $sAction
  227. )));
  228. $this->_echoResultJson(array('popup' => $sContent), true);
  229. }
  230. }
  231. function getJsObject()
  232. {
  233. return BX_DOL_STUDIO_FORMS_FIELDS_JS_OBJECT;
  234. }
  235. function getDisplaysSelector($sModule = '')
  236. {
  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->getInputs(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. {
  277. return $this->_oTemplate->parseHtmlByName('forms_fields.html', array(
  278. 'content' => parent::getCode($isDisplayHeader),
  279. 'js_object' => $this->getJsObject(),
  280. 'page_url' => $this->sUrlPage,
  281. 'grid_object' => $this->_sObject,
  282. 'params_divider' => $this->sParamsDivider
  283. ));
  284. }
  285. protected function _addJsCss()
  286. {
  287. parent::_addJsCss();
  288. $this->_oTemplate->addCss(array('menu.css'));
  289. $this->_oTemplate->addJs(array('jquery.form.min.js', 'forms_fields.js'));
  290. $oForm = new BxTemplStudioFormView(array());
  291. $oForm->addCssJs();
  292. }
  293. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  294. {
  295. if((int)$aRow['editable'] == 0)
  296. return parent::_getCellDefault('', $sKey, $aField, $aRow);
  297. return parent::_getCellSwitcher($mixedValue, $sKey, $aField, $aRow);
  298. }
  299. protected function _getCellType ($mixedValue, $sKey, $aField, $aRow)
  300. {
  301. $mixedValue = $this->_oTemplate->getIcon('ui-' . $aRow['type'] . '.png', array('alt' => _t('_adm_form_txt_field_type_' . $aRow['type'])));
  302. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  303. }
  304. protected function _getCellModule($mixedValue, $sKey, $aField, $aRow)
  305. {
  306. $mixedValue = $this->_limitMaxLength($this->getModuleTitle($aRow['module']), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  307. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  308. }
  309. protected function _getCellVisibleForLevels ($mixedValue, $sKey, $aField, $aRow)
  310. {
  311. if((int)$aRow['editable'] == 0)
  312. return parent::_getCellDefault('', $sKey, $aField, $aRow);
  313. $mixedValue = $this->_oTemplate->parseHtmlByName('bx_a.html', array(
  314. 'href' => 'javascript:void(0)',
  315. 'title' => _t('_adm_form_txt_fields_manage_visibility'),
  316. 'bx_repeat:attrs' => array(
  317. array('key' => 'bx_grid_action_single', 'value' => 'show_to'),
  318. array('key' => 'bx_grid_action_data', 'value' => $aRow['id'])
  319. ),
  320. 'content' => BxDolStudioUtils::getVisibilityTitle($aRow['visible_for_levels'])
  321. ));
  322. return parent::_getCellDefault ($mixedValue, $sKey, $aField, $aRow);
  323. }
  324. protected function _getActionAdd ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  325. {
  326. if($this->sDisplay == '')
  327. $isDisabled = true;
  328. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  329. }
  330. protected function _getActionEdit ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  331. {
  332. if($sType == 'single' && (int)$aRow['editable'] == 0)
  333. return '';
  334. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  335. }
  336. protected function _getActionDelete ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  337. {
  338. if($sType == 'single' && (int)$aRow['deletable'] == 0)
  339. return '';
  340. return parent::_getActionDefault($sType, $sKey, $a, false, $isDisabled, $aRow);
  341. }
  342. protected function _getActionShowTo ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  343. {
  344. return '';
  345. }
  346. protected function _getActionsDisabledBehavior($aRow)
  347. {
  348. return false;
  349. }
  350. protected function _getFilterControls ()
  351. {
  352. parent::_getFilterControls();
  353. $sContent = $this->getModulesSelectOne('getInputs') . $this->getDisplaysSelector($this->sModule);
  354. $oForm = new BxTemplStudioFormView(array());
  355. $aInputSearch = array(
  356. 'type' => 'text',
  357. 'name' => 'keyword',
  358. 'attrs' => array(
  359. 'id' => 'bx-grid-search-' . $this->_sObject,
  360. ),
  361. 'tr_attrs' => array(
  362. 'style' => 'display:none;'
  363. )
  364. );
  365. $sContent .= $oForm->genRow($aInputSearch);
  366. return $sContent;
  367. }
  368. }
  369. /** @} */