BxBaseStudioFormsLabels.php 11 KB

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