BxBaseStudioFormsCategories.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. public function __construct ($aOptions, $oTemplate = false)
  36. {
  37. parent::__construct ($aOptions, $oTemplate);
  38. $this->sUrlPage = BX_DOL_URL_STUDIO . 'builder_forms.php?page=categories';
  39. }
  40. public function performActionAdd()
  41. {
  42. $sAction = 'add';
  43. if(!$this->canAdd()) {
  44. echoJson(array());
  45. exit;
  46. }
  47. $oForm = $this->_getForm($sAction);
  48. if (!$oForm)
  49. return '';
  50. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?' . bx_encode_url_params($_GET, array('ids', '_r'));
  51. $oForm->initChecker();
  52. if($oForm->isSubmittedAndValid()) {
  53. $mixedResult = $oForm->insert(array('added' => time(), 'module' => $this->sModule));
  54. if(is_numeric($mixedResult))
  55. $aRes = array('grid' => $this->getCode(false), 'blink' => $mixedResult);
  56. else
  57. $aRes = array('msg' => $mixedResult);
  58. echoJson($aRes);
  59. }
  60. else {
  61. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-categories-add-popup', _t('_adm_form_txt_categories_add_popup'), $this->_oTemplate->parseHtmlByName('form_add_category.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. $aCategory = $this->_oCategory->getData(array('type' => 'id', 'id' => $iId));
  82. if(empty($aCategory) || !is_array($aCategory))
  83. return echoJson(array());
  84. $this->sModule = $aCategory['module'];
  85. $oForm = $this->_getForm($sAction, $aCategory);
  86. $oForm->initChecker();
  87. if($oForm->isSubmittedAndValid()) {
  88. $mixedResult = $oForm->update($iId);
  89. if(is_numeric($mixedResult))
  90. $aRes = array('grid' => $this->getCode(false), 'blink' => $mixedResult);
  91. else
  92. $aRes = array('msg' => $mixedResult);
  93. echoJson($aRes);
  94. }
  95. else {
  96. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-categories-edit-popup', _t('_adm_form_txt_labels_edit_popup', _t($aCategory['value'])), $this->_oTemplate->parseHtmlByName('form_add_category.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. public function performActionDelete()
  106. {
  107. $this->_replaceMarkers ();
  108. $iAffected = 0;
  109. $aIds = bx_get('ids');
  110. if (!$aIds || !is_array($aIds)) {
  111. echoJson(array());
  112. exit;
  113. }
  114. foreach ($aIds as $mixedId){
  115. if ($this->_delete($mixedId)){
  116. $iAffected ++;
  117. $this->oDb->deleteCategories($mixedId);
  118. }
  119. }
  120. echo echoJson(array_merge(
  121. array(
  122. 'grid' => $this->getCode(false),
  123. ),
  124. $iAffected ? array() : array('msg' => _t("_sys_grid_delete_failed"))
  125. ));
  126. }
  127. protected function _switcherChecked2State($isChecked)
  128. {
  129. return $isChecked ? 'active' : 'hidden';
  130. }
  131. protected function _switcherState2Checked($mixedState)
  132. {
  133. return 'active' == $mixedState ? true : false;
  134. }
  135. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  136. {
  137. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  138. }
  139. protected function _getCellAuthor($mixedValue, $sKey, $aField, $aRow)
  140. {
  141. if ($aRow['author'] > 0){
  142. $oProfile = BxDolProfile::getInstanceMagic($aRow['author']);
  143. $sProfile = $oProfile->getDisplayName();
  144. $mixedValue = $this->_oTemplate->parseHtmlByName('account_link.html', array(
  145. 'href' => $oProfile->getUrl(),
  146. 'title' => $sProfile,
  147. 'content' => $sProfile,
  148. 'class' => 'bx-def-font-grayed'
  149. ));
  150. }
  151. else{
  152. $mixedValue = '';
  153. }
  154. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  155. }
  156. protected function _getCellModule($mixedValue, $sKey, $aField, $aRow)
  157. {
  158. $mixedValue = $this->_limitMaxLength($this->getModuleTitle($aRow['module']), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  159. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  160. }
  161. protected function _getCellValue($mixedValue, $sKey, $aField, $aRow)
  162. {
  163. return parent::_getCellDefault(_t($mixedValue), $sKey, $aField, $aRow);
  164. }
  165. protected function _getForm($sAction, $aCategory = array())
  166. {
  167. $aForm = array(
  168. 'form_attrs' => array(
  169. 'id' => 'adm-form-search-form-edit',
  170. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&module=' . $this->sModule . '&a=' . $sAction,
  171. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  172. ),
  173. 'params' => array (
  174. 'db' => array(
  175. 'table' => 'sys_categories',
  176. 'key' => 'id',
  177. 'uri' => '',
  178. 'uri_title' => '',
  179. 'submit_name' => 'do_submit'
  180. ),
  181. 'checker_helper' => 'BxStudioFormsCategoriesCheckerHelper'
  182. ),
  183. 'inputs' => array (
  184. 'id' => array(
  185. 'type' => 'hidden',
  186. 'name' => 'id',
  187. 'value' => isset($aCategory['id']) ? (int)$aCategory['id'] : 0,
  188. 'db' => array (
  189. 'pass' => 'Int',
  190. ),
  191. ),
  192. 'value' => array(
  193. 'type' => 'text_translatable',
  194. 'name' => 'value',
  195. 'caption' => _t('_adm_form_txt_categories_value'),
  196. 'info' => '',
  197. 'value' => isset($aCategory['value']) ? $aCategory['value'] : '',
  198. 'required' => '1',
  199. 'db' => array (
  200. 'pass' => 'Xss',
  201. ),
  202. 'checker' => array (
  203. 'func' => 'AvailUnique',
  204. 'params' => array(isset($aCategory['id']) ? (int)$aCategory['id'] : 0),
  205. 'error' => _t('_adm_form_err_categories_value'),
  206. ),
  207. ),
  208. 'controls' => array(
  209. 'name' => 'controls',
  210. 'type' => 'input_set',
  211. array(
  212. 'type' => 'submit',
  213. 'name' => 'do_submit',
  214. 'value' => _t('_adm_form_btn_labels_submit'),
  215. ),
  216. array (
  217. 'type' => 'reset',
  218. 'name' => 'close',
  219. 'value' => _t('_adm_form_btn_labels_cancel'),
  220. 'attrs' => array(
  221. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  222. 'class' => 'bx-def-margin-sec-left',
  223. ),
  224. )
  225. )
  226. )
  227. );
  228. return new BxTemplStudioFormView($aForm);
  229. }
  230. protected function _addJsCss()
  231. {
  232. parent::_addJsCss();
  233. $this->_oTemplate->addJs(array('jquery.form.min.js', 'forms_categories.js'));
  234. $oForm = new BxTemplStudioFormView(array());
  235. $oForm->addCssJs();
  236. }
  237. protected function canAdd()
  238. {
  239. return $this->sModule != '';
  240. }
  241. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  242. {
  243. if(!$this->canAdd())
  244. $isDisabled = true;
  245. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  246. }
  247. protected function _getFilterControls ()
  248. {
  249. parent::_getFilterControls();
  250. $aInputModules = $this->getModulesSelectOneArray('getCategories', false, false);
  251. foreach($aInputModules['values'] as $sKey => $sValue){
  252. $bEnable = false;
  253. $oModule = BxDolModule::getInstance($sKey);
  254. if (isset($oModule->_oConfig) && isset($oModule->_oConfig->CNF)){
  255. $CNF = $oModule->_oConfig->CNF;
  256. if (isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] == true){
  257. $bEnable = true;
  258. }
  259. }
  260. if (!$bEnable)
  261. unset($aInputModules['values'][$sKey]);
  262. }
  263. $aInputModules['values'] = array_merge(array('' => _t('_adm_txt_select_module')), $aInputModules['values']);
  264. $oForm = new BxTemplStudioFormView(array());
  265. $sContent = $oForm->genRow($aInputModules);
  266. $oForm = new BxTemplStudioFormView(array());
  267. $aInputSearch = array(
  268. 'type' => 'text',
  269. 'name' => 'keyword',
  270. 'attrs' => array(
  271. 'id' => 'bx-grid-search-' . $this->_sObject,
  272. ),
  273. 'tr_attrs' => array(
  274. 'style' => 'display:none;'
  275. )
  276. );
  277. $sContent .= $oForm->genRow($aInputSearch);
  278. return $sContent;
  279. }
  280. function getJsObject()
  281. {
  282. return 'oBxDolStudioFormsCategories';
  283. }
  284. function getCode($isDisplayHeader = true)
  285. {
  286. return $this->_oTemplate->parseHtmlByName('forms_categories.html', array(
  287. 'content' => parent::getCode($isDisplayHeader),
  288. 'js_object' => $this->getJsObject(),
  289. 'grid_object' => $this->_sObject,
  290. 'params_divider' => $this->sParamsDivider
  291. ));
  292. }
  293. }
  294. /** @} */