BxBaseStudioFormsCategories.php 11 KB

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