1
0

BxBaseStudioFormsLabels.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaView UNA Studio Representation classes
  7. * @ingroup UnaStudio
  8. * @{
  9. */
  10. bx_import('BxTemplStudioFormView');
  11. class BxStudioFormsLabelsCheckerHelper extends BxDolFormCheckerHelper
  12. {
  13. public function checkAvailUnique($s)
  14. {
  15. $aLabel = BxDolLabel::getInstance()->getLabels(array(
  16. 'type' => 'value',
  17. 'value' => $s
  18. ));
  19. return empty($aLabel) || !is_array($aLabel);
  20. }
  21. }
  22. class BxBaseStudioFormsLabels extends BxDolStudioFormsLabels
  23. {
  24. protected $_sBaseUrl;
  25. function __construct($aOptions, $oTemplate = false)
  26. {
  27. parent::__construct($aOptions, $oTemplate);
  28. $this->_sBaseUrl = BX_DOL_URL_STUDIO . bx_append_url_params('builder_forms.php', array(
  29. 'page' => 'labels'
  30. ));
  31. }
  32. public function performActionAdd()
  33. {
  34. $sAction = 'add';
  35. $oForm = $this->_getForm($sAction);
  36. $oForm->initChecker();
  37. if($oForm->isSubmittedAndValid()) {
  38. $aValsToAdd = array('module' => 'custom');
  39. $iParent = (int)$oForm->getCleanValue('parent');
  40. if($iParent != 0) {
  41. $aParent = $this->_oLabel->getLabels(array('type' => 'id', 'id' => $iParent));
  42. $aValsToAdd['level'] = $aParent['level'] + 1;
  43. }
  44. else
  45. $aValsToAdd['level'] = 0;
  46. $aValsToAdd['order'] = $this->_oLabel->getLabels(array('type' => 'parent_order', 'parent' => $iParent)) + 1;
  47. $iId = $oForm->insert($aValsToAdd);
  48. if($iId !== false) {
  49. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  50. $this->_oLabel->onAdd($iId);
  51. }
  52. else
  53. $aRes = array('msg' => _t('_adm_form_err_labels_add'));
  54. echoJson($aRes);
  55. }
  56. else {
  57. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-labels-add-popup', _t('_adm_form_txt_labels_add_popup'), $this->_oTemplate->parseHtmlByName('form_add_label.html', array(
  58. 'form_id' => $oForm->aFormAttrs['id'],
  59. 'form' => $oForm->getCode(true),
  60. 'object' => $this->_sObject,
  61. 'action' => $sAction
  62. )));
  63. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  64. }
  65. }
  66. public function performActionEdit()
  67. {
  68. $sAction = 'edit';
  69. $aIds = bx_get('ids');
  70. if(!$aIds || !is_array($aIds)) {
  71. $iId = (int)bx_get('id');
  72. if(!$iId)
  73. return echoJson(array());
  74. $aIds = array($iId);
  75. }
  76. $iId = (int)array_shift($aIds);
  77. $aLabel = $this->_oLabel->getLabels(array('type' => 'id', 'id' => $iId));
  78. if(empty($aLabel) || !is_array($aLabel))
  79. return echoJson(array());
  80. $oForm = $this->_getForm($sAction, $aLabel);
  81. $oForm->initChecker();
  82. if($oForm->isSubmittedAndValid()) {
  83. if($oForm->update($iId) !== false) {
  84. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  85. $this->_oLabel->onEdit($iId);
  86. }
  87. else
  88. $aRes = array('msg' => _t('_adm_form_err_labels_edit'));
  89. echoJson($aRes);
  90. }
  91. else {
  92. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-labels-edit-popup', _t('_adm_form_txt_labels_edit_popup', $aLabel['value']), $this->_oTemplate->parseHtmlByName('form_add_label.html', array(
  93. 'form_id' => $oForm->aFormAttrs['id'],
  94. 'form' => $oForm->getCode(true),
  95. 'object' => $this->_sObject,
  96. 'action' => $sAction
  97. )));
  98. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  99. }
  100. }
  101. function getJsObject()
  102. {
  103. return 'oBxDolStudioFormsLabels';
  104. }
  105. function getCode($isDisplayHeader = true)
  106. {
  107. return $this->_oTemplate->parseHtmlByName('forms_labels.html', array(
  108. 'content' => parent::getCode($isDisplayHeader),
  109. 'js_object' => $this->getJsObject(),
  110. 'grid_object' => $this->_sObject,
  111. 'params_divider' => $this->sParamsDivider
  112. ));
  113. }
  114. protected function _addJsCss()
  115. {
  116. parent::_addJsCss();
  117. $this->_oTemplate->addJs(array('jquery.form.min.js'));
  118. $oForm = new BxTemplStudioFormView(array());
  119. $oForm->addCssJs();
  120. }
  121. protected function _getCellModule($mixedValue, $sKey, $aField, $aRow)
  122. {
  123. $mixedValue = $this->_limitMaxLength($this->getModuleTitle($aRow['module']), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  124. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  125. }
  126. protected function _getCellItems ($mixedValue, $sKey, $aField, $aRow)
  127. {
  128. $iItems = $this->_oLabel->getLabels(array('type' => 'parent', 'parent' => $aRow['id'], 'count_only' => true));
  129. $sLink = bx_append_url_params($this->_sBaseUrl, array('parent' => $aRow['id']));
  130. $mixedValue = $this->_oTemplate->parseLink($sLink, _t('_adm_form_txt_labels_n_items', $iItems), array(
  131. 'title' => _t('_adm_form_txt_labels_manage_items')
  132. ));
  133. return parent::_getCellDefault ($mixedValue, $sKey, $aField, $aRow);
  134. }
  135. protected function _getForm($sAction, $aLabel = array())
  136. {
  137. $aForm = array(
  138. 'form_attrs' => array(
  139. 'id' => 'adm-form-search-form-edit',
  140. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&parent=' . $this->_iParent,
  141. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  142. ),
  143. 'params' => array (
  144. 'db' => array(
  145. 'table' => 'sys_labels',
  146. 'key' => 'id',
  147. 'uri' => '',
  148. 'uri_title' => '',
  149. 'submit_name' => 'do_submit'
  150. ),
  151. 'checker_helper' => 'BxStudioFormsLabelsCheckerHelper'
  152. ),
  153. 'inputs' => array (
  154. 'id' => array(
  155. 'type' => 'hidden',
  156. 'name' => 'id',
  157. 'value' => isset($aLabel['id']) ? (int)$aLabel['id'] : 0,
  158. 'db' => array (
  159. 'pass' => 'Int',
  160. ),
  161. ),
  162. 'parent' => array(
  163. 'type' => 'select',
  164. 'name' => 'parent',
  165. 'caption' => _t('_adm_form_txt_labels_parent'),
  166. 'info' => '',
  167. 'value' => $this->_iParent,
  168. 'db' => array (
  169. 'pass' => 'Int',
  170. ),
  171. ),
  172. 'value' => array(
  173. 'type' => 'text',
  174. 'name' => 'value',
  175. 'caption' => _t('_adm_form_txt_labels_value'),
  176. 'info' => '',
  177. 'value' => isset($aLabel['value']) ? $aLabel['value'] : '',
  178. 'required' => '1',
  179. 'db' => array (
  180. 'pass' => 'Xss',
  181. ),
  182. 'checker' => array (
  183. 'func' => 'AvailUnique',
  184. 'params' => array(),
  185. 'error' => _t('_adm_form_err_labels_value'),
  186. ),
  187. ),
  188. 'controls' => array(
  189. 'name' => 'controls',
  190. 'type' => 'input_set',
  191. array(
  192. 'type' => 'submit',
  193. 'name' => 'do_submit',
  194. 'value' => _t('_adm_form_btn_labels_submit'),
  195. ),
  196. array (
  197. 'type' => 'reset',
  198. 'name' => 'close',
  199. 'value' => _t('_adm_form_btn_labels_cancel'),
  200. 'attrs' => array(
  201. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  202. 'class' => 'bx-def-margin-sec-left',
  203. ),
  204. )
  205. )
  206. )
  207. );
  208. if($sAction == 'add') {
  209. if(empty($this->_iParent))
  210. $aForm['inputs']['parent']['values'][] = array('key' => 0, 'value' => _t('_adm_form_txt_labels_parent_empty'));
  211. else
  212. $aForm['inputs']['parent']['values'][] = array('key' => $this->_iParent, 'value' => $this->_aParent['value']);
  213. $aForm['inputs']['parent']['values'][] = array('key' => '', 'value' => '----', 'attrs' => array('disabled' => 'disabled'));
  214. $aLabels = $this->_oLabel->getLabels(array('type' => 'parent', 'parent' => $this->_iParent));
  215. foreach($aLabels as $aLabel)
  216. $aForm['inputs']['parent']['values'][] = array(
  217. 'key' => $aLabel['id'],
  218. 'value' => $aLabel['value']
  219. );
  220. }
  221. else
  222. $aForm['inputs']['parent']['type'] = 'hidden';
  223. return new BxTemplStudioFormView($aForm);
  224. }
  225. }
  226. /** @} */