BxBaseStudioFormsCategories.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 BxStudioFormsCategoriesCheckerHelper extends BxDolFormCheckerHelper
  12. {
  13. public function checkAvailUnique($s, $aExclude = array())
  14. {
  15. if(!is_array($aExclude))
  16. $aExclude = array($aExclude);
  17. $aLanguages = BxDolStudioLanguagesUtils::getInstance()->getLanguages();
  18. foreach($aLanguages as $sLangName => $sLangTitle) {
  19. $sValue = BxDolForm::getSubmittedValue('value' . '-' . $sLangName, BX_DOL_STUDIO_METHOD_DEFAULT);
  20. $aCategory = BxDolCategories::getInstance()->getData(array(
  21. 'type' => 'value_and_module',
  22. 'module' => bx_get('module'),
  23. 'value' => $sValue
  24. ));
  25. $bNoInUsers = empty($aCategory) || !is_array($aCategory) || in_array($aCategory['id'], $aExclude);
  26. // also need check uniq in system categories to
  27. if(!$bNoInUsers)
  28. return false;
  29. }
  30. return true;
  31. }
  32. }
  33. class BxBaseStudioFormsCategories extends BxDolStudioFormsCategories
  34. {
  35. protected $sUrlPage;
  36. public function __construct ($aOptions, $oTemplate = false)
  37. {
  38. parent::__construct ($aOptions, $oTemplate);
  39. $this->sUrlPage = BX_DOL_URL_STUDIO . 'builder_forms.php?page=categories';
  40. }
  41. public function performActionAdd()
  42. {
  43. $sAction = 'add';
  44. if(!$this->canAdd()) {
  45. echoJson(array());
  46. exit;
  47. }
  48. $oForm = $this->_getForm($sAction);
  49. if (!$oForm)
  50. return '';
  51. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?' . bx_encode_url_params($_GET, array('ids', '_r'));
  52. $oForm->initChecker();
  53. if($oForm->isSubmittedAndValid()) {
  54. $mixedResult = $oForm->insert(array('added' => time(), 'module' => $this->sModule));
  55. if(is_numeric($mixedResult))
  56. $aRes = array('grid' => $this->getCode(false), 'blink' => $mixedResult);
  57. else
  58. $aRes = array('msg' => $mixedResult);
  59. echoJson($aRes);
  60. }
  61. else {
  62. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-categories-add-popup', _t('_adm_form_txt_categories_add_popup'), $this->_oTemplate->parseHtmlByName('form_add_category.html', array(
  63. 'form_id' => $oForm->aFormAttrs['id'],
  64. 'form' => $oForm->getCode(true),
  65. 'object' => $this->_sObject,
  66. 'action' => $sAction
  67. )));
  68. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  69. }
  70. }
  71. public function performActionEdit()
  72. {
  73. $sAction = 'edit';
  74. $aIds = bx_get('ids');
  75. if(!$aIds || !is_array($aIds)) {
  76. $iId = (int)bx_get('id');
  77. if(!$iId)
  78. return echoJson(array());
  79. $aIds = array($iId);
  80. }
  81. $iId = (int)array_shift($aIds);
  82. $aCategory = $this->_oCategory->getData(array('type' => 'id', 'id' => $iId));
  83. if(empty($aCategory) || !is_array($aCategory))
  84. return echoJson(array());
  85. $this->sModule = $aCategory['module'];
  86. $oForm = $this->_getForm($sAction, $aCategory);
  87. $oForm->initChecker();
  88. if($oForm->isSubmittedAndValid()) {
  89. $mixedResult = $oForm->update($iId);
  90. if(is_numeric($mixedResult))
  91. $aRes = array('grid' => $this->getCode(false), 'blink' => $mixedResult);
  92. else
  93. $aRes = array('msg' => $mixedResult);
  94. echoJson($aRes);
  95. }
  96. else {
  97. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-categories-edit-popup', _t('_adm_form_txt_categories_edit_popup', _t($aCategory['value'])), $this->_oTemplate->parseHtmlByName('form_add_category.html', array(
  98. 'form_id' => $oForm->aFormAttrs['id'],
  99. 'form' => $oForm->getCode(true),
  100. 'object' => $this->_sObject,
  101. 'action' => $sAction
  102. )));
  103. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  104. }
  105. }
  106. public function performActionDelete()
  107. {
  108. $this->_replaceMarkers ();
  109. $iAffected = 0;
  110. $aIds = bx_get('ids');
  111. if (!$aIds || !is_array($aIds)) {
  112. echoJson(array());
  113. exit;
  114. }
  115. foreach ($aIds as $mixedId){
  116. if ($this->_delete($mixedId)){
  117. $iAffected ++;
  118. $this->oDb->deleteCategories($mixedId);
  119. }
  120. }
  121. echo echoJson(array_merge(
  122. array(
  123. 'grid' => $this->getCode(false),
  124. ),
  125. $iAffected ? array() : array('msg' => _t("_sys_grid_delete_failed"))
  126. ));
  127. }
  128. protected function _switcherChecked2State($isChecked)
  129. {
  130. return $isChecked ? 'active' : 'hidden';
  131. }
  132. protected function _switcherState2Checked($mixedState)
  133. {
  134. return 'active' == $mixedState ? true : false;
  135. }
  136. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  137. {
  138. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  139. }
  140. protected function _getCellAuthor($mixedValue, $sKey, $aField, $aRow)
  141. {
  142. if ($aRow['author'] > 0){
  143. $oProfile = BxDolProfile::getInstanceMagic($aRow['author']);
  144. $sProfile = $oProfile->getDisplayName();
  145. $mixedValue = $this->_oTemplate->parseHtmlByName('account_link.html', array(
  146. 'href' => $oProfile->getUrl(),
  147. 'title' => $sProfile,
  148. 'content' => $sProfile,
  149. 'class' => 'bx-def-font-grayed'
  150. ));
  151. }
  152. else{
  153. $mixedValue = '';
  154. }
  155. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  156. }
  157. protected function _getCellModule($mixedValue, $sKey, $aField, $aRow)
  158. {
  159. $mixedValue = $this->_limitMaxLength($this->getModuleTitle($aRow['module']), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  160. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  161. }
  162. protected function _getCellValue($mixedValue, $sKey, $aField, $aRow)
  163. {
  164. return parent::_getCellDefault(_t($mixedValue), $sKey, $aField, $aRow);
  165. }
  166. protected function _getForm($sAction, $aCategory = [])
  167. {
  168. $sIconType = !empty($aCategory['icon_type']) ? $aCategory['icon_type'] : '';
  169. $aForm = array(
  170. 'form_attrs' => array(
  171. 'id' => 'adm-form-cats-form-' . $sAction,
  172. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&module=' . $this->sModule . '&a=' . $sAction,
  173. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  174. ),
  175. 'params' => array (
  176. 'db' => array(
  177. 'table' => 'sys_categories',
  178. 'key' => 'id',
  179. 'uri' => '',
  180. 'uri_title' => '',
  181. 'submit_name' => 'do_submit'
  182. ),
  183. 'checker_helper' => 'BxStudioFormsCategoriesCheckerHelper'
  184. ),
  185. 'inputs' => array (
  186. 'id' => array(
  187. 'type' => 'hidden',
  188. 'name' => 'id',
  189. 'value' => isset($aCategory['id']) ? (int)$aCategory['id'] : 0,
  190. 'db' => array (
  191. 'pass' => 'Int',
  192. ),
  193. ),
  194. 'value' => array(
  195. 'type' => 'text_translatable',
  196. 'name' => 'value',
  197. 'caption' => _t('_adm_form_txt_categories_value'),
  198. 'info' => '',
  199. 'value' => isset($aCategory['value']) ? $aCategory['value'] : '',
  200. 'required' => '1',
  201. 'db' => array (
  202. 'pass' => 'Xss',
  203. ),
  204. 'checker' => array (
  205. 'func' => 'AvailUnique',
  206. 'params' => array(isset($aCategory['id']) ? (int)$aCategory['id'] : 0),
  207. 'error' => _t('_adm_form_err_categories_value'),
  208. ),
  209. ),
  210. 'icon_type' => [
  211. 'type' => 'select',
  212. 'name' => 'icon_type',
  213. 'caption' => _t('_adm_form_txt_pre_values_icon_type'),
  214. 'info' => '',
  215. 'values' => [
  216. ['key' => '', 'value' => _t('_sys_please_select')],
  217. ['key' => 'icon', 'value' => _t('_adm_form_txt_pre_values_icon_type_icon')],
  218. ['key' => 'emoji', 'value' => _t('_adm_form_txt_pre_values_icon_type_emoji')],
  219. ['key' => 'image', 'value' => _t('_adm_form_txt_pre_values_icon_type_image')]
  220. ],
  221. 'value' => isset($aCategory['icon_type']) ? $aCategory['icon_type'] : '',
  222. 'required' => '0',
  223. 'db' => [
  224. 'pass' => 'Xss',
  225. ]
  226. ],
  227. 'icon' => [
  228. 'type' => 'textarea',
  229. 'name' => 'icon',
  230. 'caption' => _t('_adm_form_txt_pre_values_icon'),
  231. 'info' => '',
  232. 'value' => isset($aCategory['icon']) ? $aCategory['icon'] : '',
  233. 'required' => '0',
  234. 'code' => 1,
  235. 'db' => [
  236. 'pass' => 'Xss',
  237. ]
  238. ],
  239. 'controls' => array(
  240. 'name' => 'controls',
  241. 'type' => 'input_set',
  242. array(
  243. 'type' => 'submit',
  244. 'name' => 'do_submit',
  245. 'value' => _t('_adm_form_btn_labels_submit'),
  246. ),
  247. array (
  248. 'type' => 'reset',
  249. 'name' => 'close',
  250. 'value' => _t('_adm_form_btn_labels_cancel'),
  251. 'attrs' => array(
  252. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  253. 'class' => 'bx-def-margin-sec-left',
  254. ),
  255. )
  256. )
  257. )
  258. );
  259. return new BxTemplStudioFormView($aForm);
  260. }
  261. protected function _addJsCss()
  262. {
  263. parent::_addJsCss();
  264. $this->_oTemplate->addJs(array('jquery.form.min.js', 'forms_categories.js'));
  265. $oForm = new BxTemplStudioFormView(array());
  266. $oForm->addCssJs();
  267. }
  268. protected function canAdd()
  269. {
  270. return $this->sModule != '';
  271. }
  272. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  273. {
  274. if(!$this->canAdd())
  275. $isDisabled = true;
  276. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  277. }
  278. protected function _getFilterControls ()
  279. {
  280. parent::_getFilterControls();
  281. $aInputModules = $this->getModulesSelectOneArray('getCategories', false, false);
  282. $aValues = [['key' => '', 'value' => _t('_adm_txt_select_module')]];
  283. foreach($aInputModules['values'] as $sName => $sTitle)
  284. if(($sKey = 'PARAM_MULTICAT_ENABLED') && ($oModule = BxDolModule::getInstance($sName)) && ($CNF = $oModule->_oConfig->CNF ?? false) && isset($CNF[$sKey]) && $CNF[$sKey])
  285. $aValues[] = ['key' => $sName, 'value' => $sTitle];
  286. $aInputModules['values'] = $aValues;
  287. $oForm = new BxTemplStudioFormView([]);
  288. return $oForm->genRow($aInputModules) . parent::_getSearchInput();
  289. }
  290. function getJsObject()
  291. {
  292. return 'oBxDolStudioFormsCategories';
  293. }
  294. function getCode($isDisplayHeader = true)
  295. {
  296. return $this->_oTemplate->parseHtmlByName('forms_categories.html', array(
  297. 'content' => parent::getCode($isDisplayHeader),
  298. 'js_object' => $this->getJsObject(),
  299. 'grid_object' => $this->_sObject,
  300. 'params_divider' => $this->sParamsDivider
  301. ));
  302. }
  303. }
  304. /** @} */