BxBaseStudioFormsLabels.php 11 KB

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