BxBaseStudioFormsCategories.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 = array())
  167. {
  168. $aForm = array(
  169. 'form_attrs' => array(
  170. 'id' => 'adm-form-cats-form-' . $sAction,
  171. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&module=' . $this->sModule . '&a=' . $sAction,
  172. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  173. ),
  174. 'params' => array (
  175. 'db' => array(
  176. 'table' => 'sys_categories',
  177. 'key' => 'id',
  178. 'uri' => '',
  179. 'uri_title' => '',
  180. 'submit_name' => 'do_submit'
  181. ),
  182. 'checker_helper' => 'BxStudioFormsCategoriesCheckerHelper'
  183. ),
  184. 'inputs' => array (
  185. 'id' => array(
  186. 'type' => 'hidden',
  187. 'name' => 'id',
  188. 'value' => isset($aCategory['id']) ? (int)$aCategory['id'] : 0,
  189. 'db' => array (
  190. 'pass' => 'Int',
  191. ),
  192. ),
  193. 'value' => array(
  194. 'type' => 'text_translatable',
  195. 'name' => 'value',
  196. 'caption' => _t('_adm_form_txt_categories_value'),
  197. 'info' => '',
  198. 'value' => isset($aCategory['value']) ? $aCategory['value'] : '',
  199. 'required' => '1',
  200. 'db' => array (
  201. 'pass' => 'Xss',
  202. ),
  203. 'checker' => array (
  204. 'func' => 'AvailUnique',
  205. 'params' => array(isset($aCategory['id']) ? (int)$aCategory['id'] : 0),
  206. 'error' => _t('_adm_form_err_categories_value'),
  207. ),
  208. ),
  209. 'controls' => array(
  210. 'name' => 'controls',
  211. 'type' => 'input_set',
  212. array(
  213. 'type' => 'submit',
  214. 'name' => 'do_submit',
  215. 'value' => _t('_adm_form_btn_labels_submit'),
  216. ),
  217. array (
  218. 'type' => 'reset',
  219. 'name' => 'close',
  220. 'value' => _t('_adm_form_btn_labels_cancel'),
  221. 'attrs' => array(
  222. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  223. 'class' => 'bx-def-margin-sec-left',
  224. ),
  225. )
  226. )
  227. )
  228. );
  229. return new BxTemplStudioFormView($aForm);
  230. }
  231. protected function _addJsCss()
  232. {
  233. parent::_addJsCss();
  234. $this->_oTemplate->addJs(array('jquery.form.min.js', 'forms_categories.js'));
  235. $oForm = new BxTemplStudioFormView(array());
  236. $oForm->addCssJs();
  237. }
  238. protected function canAdd()
  239. {
  240. return $this->sModule != '';
  241. }
  242. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  243. {
  244. if(!$this->canAdd())
  245. $isDisabled = true;
  246. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  247. }
  248. protected function _getFilterControls ()
  249. {
  250. parent::_getFilterControls();
  251. $aInputModules = $this->getModulesSelectOneArray('getCategories', false, false);
  252. foreach($aInputModules['values'] as $sKey => $sValue){
  253. $bEnable = false;
  254. $oModule = BxDolModule::getInstance($sKey);
  255. if (isset($oModule->_oConfig) && isset($oModule->_oConfig->CNF)){
  256. $CNF = $oModule->_oConfig->CNF;
  257. if (isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] == true){
  258. $bEnable = true;
  259. }
  260. }
  261. if (!$bEnable)
  262. unset($aInputModules['values'][$sKey]);
  263. }
  264. $aInputModules['values'] = array_merge(array('' => _t('_adm_txt_select_module')), $aInputModules['values']);
  265. $oForm = new BxTemplStudioFormView(array());
  266. $sContent = $oForm->genRow($aInputModules);
  267. $oForm = new BxTemplStudioFormView(array());
  268. $aInputSearch = array(
  269. 'type' => 'text',
  270. 'name' => 'keyword',
  271. 'attrs' => array(
  272. 'id' => 'bx-grid-search-' . $this->_sObject,
  273. ),
  274. 'tr_attrs' => array(
  275. 'style' => 'display:none;'
  276. )
  277. );
  278. $sContent .= $oForm->genRow($aInputSearch);
  279. return $sContent;
  280. }
  281. function getJsObject()
  282. {
  283. return 'oBxDolStudioFormsCategories';
  284. }
  285. function getCode($isDisplayHeader = true)
  286. {
  287. return $this->_oTemplate->parseHtmlByName('forms_categories.html', array(
  288. 'content' => parent::getCode($isDisplayHeader),
  289. 'js_object' => $this->getJsObject(),
  290. 'grid_object' => $this->_sObject,
  291. 'params_divider' => $this->sParamsDivider
  292. ));
  293. }
  294. }
  295. /** @} */