BxBaseStudioBadgesGrid.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 BxBaseStudioBadgesGrid extends BxDolStudioBadgesGrid
  12. {
  13. function __construct($aOptions, $oTemplate = false)
  14. {
  15. parent::__construct($aOptions, $oTemplate);
  16. }
  17. public function performActionAdd()
  18. {
  19. $sAction = 'add';
  20. if(!$this->canAdd()) {
  21. echoJson(array());
  22. exit;
  23. }
  24. $oForm = $this->_getForm($sAction);
  25. if (!$oForm)
  26. return '';
  27. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?' . bx_encode_url_params($_GET, array('ids', '_r'));
  28. $oForm->initChecker();
  29. if($oForm->isSubmittedAndValid()) {
  30. if(!empty($_FILES['icon_image']['tmp_name'])) {
  31. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  32. $mixedIcon = $oStorage->storeFileFromForm($_FILES['icon_image'], false, 0);
  33. if($mixedIcon === false) {
  34. echoJson(array('msg' => _t('_adm_nav_err_items_icon_image') . $oStorage->getErrorString()));
  35. return;
  36. }
  37. $oStorage->afterUploadCleanup($mixedIcon, 0);
  38. BxDolForm::setSubmittedValue('icon', $mixedIcon, $oForm->aFormAttrs['method']);
  39. }
  40. $mixedResult = $oForm->insert(array('added' => time(), 'module' => $this->sModule));
  41. if(is_numeric($mixedResult))
  42. $aRes = array('grid' => $this->getCode(false), 'blink' => $mixedResult);
  43. else
  44. $aRes = array('msg' => $mixedResult);
  45. echoJson($aRes);
  46. }
  47. else {
  48. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-badges-add-popup', _t('_adm_form_txt_badges_add_popup'), $this->_oTemplate->parseHtmlByName('form_add_badge.html', array(
  49. 'form_id' => $oForm->aFormAttrs['id'],
  50. 'form' => $oForm->getCode(true),
  51. 'object' => $this->_sObject,
  52. 'action' => $sAction
  53. )));
  54. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  55. }
  56. }
  57. public function performActionEdit()
  58. {
  59. $sAction = 'edit';
  60. $aIds = bx_get('ids');
  61. if(!$aIds || !is_array($aIds)) {
  62. $iId = (int)bx_get('id');
  63. if(!$iId)
  64. return echoJson(array());
  65. $aIds = array($iId);
  66. }
  67. $iId = (int)array_shift($aIds);
  68. $aBadge = $this->_oBadges->getData(array('type' => 'id', 'id' => $iId));
  69. if(empty($aBadge) || !is_array($aBadge))
  70. return echoJson(array());
  71. $this->sModule = $aBadge['module'];
  72. $oForm = $this->_getForm($sAction, $aBadge);
  73. $oForm->initChecker();
  74. if($oForm->isSubmittedAndValid()) {
  75. $bIconImageCur = is_numeric($aBadge['icon']) && (int)$aBadge['icon'] != 0;
  76. $bIconImageNew = !empty($_FILES['icon_image']['tmp_name']);
  77. $sIconFont = $oForm->getCleanValue('icon');
  78. $bIconFont = !empty($sIconFont);
  79. if($bIconImageCur && ($bIconImageNew || $bIconFont)) {
  80. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  81. if(!$oStorage->deleteFile((int)$aBadge['icon'], 0)) {
  82. echoJson(array('msg' => _t('_adm_nav_err_items_icon_image_remove')));
  83. return;
  84. }
  85. }
  86. $sIcon = $sIconFont;
  87. if($bIconImageNew) {
  88. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  89. $sIcon = $oStorage->storeFileFromForm($_FILES['icon_image'], false, 0);
  90. if($sIcon === false) {
  91. echoJson(array('msg' => _t('_adm_nav_err_items_icon_image') . $oStorage->getErrorString()));
  92. return;
  93. }
  94. $oStorage->afterUploadCleanup($sIcon, 0);
  95. } else if($bIconImageCur && !$bIconFont)
  96. $sIcon = $aBadge['icon'];
  97. BxDolForm::setSubmittedValue('icon', $sIcon, $oForm->aFormAttrs['method']);
  98. $mixedResult = $oForm->update($iId);
  99. if(is_numeric($mixedResult))
  100. $aRes = array('grid' => $this->getCode(false), 'blink' => $mixedResult);
  101. else
  102. $aRes = array('msg' => $mixedResult);
  103. echoJson($aRes);
  104. }
  105. else {
  106. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-badges-edit-popup', _t('_adm_form_txt_badges_edit_popup', _t($aBadge['text'])), $this->_oTemplate->parseHtmlByName('form_add_badge.html', array(
  107. 'form_id' => $oForm->aFormAttrs['id'],
  108. 'form' => $oForm->getCode(true),
  109. 'object' => $this->_sObject,
  110. 'action' => $sAction
  111. )));
  112. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  113. }
  114. }
  115. public function performActionDeleteIcon()
  116. {
  117. $sAction = 'delete_icon';
  118. $aIds = bx_get('ids');
  119. if(empty($aIds[0])) {
  120. echoJson(array());
  121. exit;
  122. }
  123. $iId = (int)$aIds[0];
  124. $aItem = array();
  125. $iItem = $this->oDb->getData(array('type' => 'id', 'id' => $iId), $aItem);
  126. if(empty($aItem)){
  127. echoJson(array());
  128. exit;
  129. }
  130. if(is_numeric($aItem['icon']) && (int)$aItem['icon'] != 0)
  131. if(!BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES)->deleteFile((int)$aItem['icon'], 0)) {
  132. echoJson(array());
  133. exit;
  134. }
  135. if($this->oDb->update($aItem['id'], array('icon' => '')) !== false)
  136. echoJson(array('grid' => $this->getCode(false), 'blink' => $iId, 'preview' => $this->_getIconPreview($aItem['id']), 'eval' => $this->getJsObject() . ".onDeleteIcon(oData)"));
  137. }
  138. public function performActionDelete()
  139. {
  140. $this->_replaceMarkers ();
  141. $iAffected = 0;
  142. $aIds = bx_get('ids');
  143. if (!$aIds || !is_array($aIds)) {
  144. echoJson(array());
  145. exit;
  146. }
  147. foreach ($aIds as $mixedId){
  148. if ($this->_delete($mixedId)){
  149. $iAffected ++;
  150. $this->_oBadges->delete(array('type' => 'id', 'id' => $mixedId));
  151. }
  152. }
  153. echo echoJson(array_merge(
  154. array(
  155. 'grid' => $this->getCode(false),
  156. ),
  157. $iAffected ? array() : array('msg' => _t("_sys_grid_delete_failed"))
  158. ));
  159. }
  160. protected function _getCellModule ($mixedValue, $sKey, $aField, $aRow)
  161. {
  162. $oModule = BxDolModule::getInstance($aRow['module']);
  163. if($oModule && $oModule instanceof iBxDolContentInfoService){
  164. $mixedValue = $oModule->_aModule['title'];
  165. }
  166. else{
  167. $mixedValue = $aRow['module'];
  168. }
  169. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  170. }
  171. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  172. {
  173. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  174. }
  175. protected function _getCellText($mixedValue, $sKey, $aField, $aRow)
  176. {
  177. return parent::_getCellDefault(_t($mixedValue), $sKey, $aField, $aRow);
  178. }
  179. protected function _getCellIcon ($mixedValue, $sKey, $aField, $aRow)
  180. {
  181. $mixedValue = $this->_oTemplate->getIcon($mixedValue, array('class' => 'bx-item-icon'));
  182. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  183. }
  184. protected function _getCellView ($mixedValue, $sKey, $aField, $aRow)
  185. {
  186. $mixedValue = BxDolService::call('system', 'get_badge', array($aRow), 'TemplServices');
  187. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  188. }
  189. protected function _addJsCss()
  190. {
  191. parent::_addJsCss();
  192. $this->_oTemplate->addJs(array('jquery.form.min.js', 'forms_badges.js'));
  193. $oForm = new BxTemplStudioFormView(array());
  194. $oForm->addCssJs();
  195. }
  196. function getJsObject()
  197. {
  198. return 'oBxDolStudioBadgesManageTools';
  199. }
  200. protected function canAdd()
  201. {
  202. return $this->sModule != '';
  203. }
  204. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  205. {
  206. if(!$this->canAdd())
  207. $isDisabled = true;
  208. return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  209. }
  210. protected function _getActionDeleteIcon ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  211. {
  212. return '';
  213. }
  214. protected function _getFilterControls ()
  215. {
  216. parent::_getFilterControls();
  217. $aInputModules = $this->getModulesSelectOneArray('getData', false, false);
  218. foreach($aInputModules['values'] as $sKey => $sValue){
  219. $oModule = BxDolModule::getInstance($sKey);
  220. if(!($oModule instanceof iBxDolContentInfoService)){
  221. unset($aInputModules['values'][$sKey]);
  222. }
  223. else{
  224. $CNF = $oModule->_oConfig->CNF;
  225. if(!isset($CNF['BADGES_AVALIABLE']) || !(bool)$CNF['BADGES_AVALIABLE']){
  226. unset($aInputModules['values'][$sKey]);
  227. }
  228. }
  229. }
  230. $aInputModules['values'] = array_merge(array('' => _t('_adm_txt_select_module')), $aInputModules['values']);
  231. $oForm = new BxTemplStudioFormView(array());
  232. $sContent = $oForm->genRow($aInputModules);
  233. $oForm = new BxTemplStudioFormView(array());
  234. $aInputSearch = array(
  235. 'type' => 'text',
  236. 'name' => 'keyword',
  237. 'attrs' => array(
  238. 'id' => 'bx-grid-search-' . $this->_sObject,
  239. ),
  240. );
  241. $sContent .= $oForm->genRow($aInputSearch);
  242. return $sContent;
  243. }
  244. function getCode($isDisplayHeader = true)
  245. {
  246. return $this->_oTemplate->parseHtmlByName('forms_badges.html', array(
  247. 'content' => parent::getCode($isDisplayHeader),
  248. 'js_object' => $this->getJsObject(),
  249. 'grid_object' => $this->_sObject,
  250. 'params_divider' => $this->sParamsDivider
  251. ));
  252. }
  253. protected function _getForm($sAction, $aBadge = array())
  254. {
  255. $aForm = array(
  256. 'form_attrs' => array(
  257. 'id' => 'adm-form-search-form-edit',
  258. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&module=' . $this->sModule . '&a=' . $sAction,
  259. 'method' => BX_DOL_STUDIO_METHOD_DEFAULT
  260. ),
  261. 'params' => array (
  262. 'db' => array(
  263. 'table' => 'sys_badges',
  264. 'key' => 'id',
  265. 'uri' => '',
  266. 'uri_title' => '',
  267. 'submit_name' => 'do_submit'
  268. ),
  269. 'checker_helper' => 'BxDolFormCheckerHelper'
  270. ),
  271. 'inputs' => array (
  272. 'id' => array(
  273. 'type' => 'hidden',
  274. 'name' => 'id',
  275. 'value' => isset($aBadge['id']) ? (int)$aBadge['id'] : 0,
  276. 'db' => array (
  277. 'pass' => 'Int',
  278. ),
  279. ),
  280. 'text' => array(
  281. 'type' => 'text',
  282. 'name' => 'text',
  283. 'caption' => _t('_adm_form_txt_badges_text'),
  284. 'info' => '',
  285. 'value' => isset($aBadge['text']) ? $aBadge['text'] : '',
  286. 'required' => '0',
  287. 'db' => array (
  288. 'pass' => 'Xss',
  289. )
  290. ),
  291. 'color' => array(
  292. 'type' => 'rgb-list',
  293. 'name' => 'color',
  294. 'caption' => _t('_adm_form_txt_badges_color'),
  295. 'info' => '',
  296. 'value' => isset($aBadge['color']) ? $aBadge['color'] : '',
  297. 'required' => '0',
  298. 'db' => array (
  299. 'pass' => 'Xss',
  300. )
  301. ),
  302. 'is_icon_only' => array(
  303. 'type' => 'switcher',
  304. 'name' => 'is_icon_only',
  305. 'caption' => _t('_adm_form_txt_badges_is_icon_only'),
  306. 'info' => '',
  307. 'value' => '1',
  308. 'checked' => isset($aBadge['is_icon_only']) && $aBadge['is_icon_only'] == '1',
  309. 'required' => '0',
  310. 'db' => array (
  311. 'pass' => 'Xss',
  312. )
  313. ),
  314. 'icon' => array(
  315. 'type' => 'textarea',
  316. 'name' => 'icon',
  317. 'caption' => _t('_adm_form_txt_badges_icon'),
  318. 'info' => _t('_adm_form_dsc_badges_icon'),
  319. 'value' => '',
  320. 'code' => 1,
  321. 'required' => '0',
  322. 'db' => array (
  323. 'pass' => 'Xss',
  324. )
  325. ),
  326. 'icon_image' => array(
  327. 'type' => 'file',
  328. 'name' => 'icon_image',
  329. 'caption' => _t('_adm_form_txt_badges_icon_image'),
  330. 'info' => _t('_adm_form_dsc_badges_icon_image'),
  331. 'value' => '',
  332. 'checker' => array (
  333. 'func' => '',
  334. 'params' => '',
  335. 'error' => _t('_adm_form_err_badges_icon_image'),
  336. ),
  337. ),
  338. 'icon_preview' => array(
  339. 'type' => 'custom',
  340. 'name' => 'icon_preview',
  341. 'caption' => _t('_adm_form_txt_badges_icon_image_old'),
  342. 'content' => ''
  343. ),
  344. 'controls' => array(
  345. 'name' => 'controls',
  346. 'type' => 'input_set',
  347. array(
  348. 'type' => 'submit',
  349. 'name' => 'do_submit',
  350. 'value' => _t('_adm_form_btn_badges_submit'),
  351. ),
  352. array (
  353. 'type' => 'reset',
  354. 'name' => 'close',
  355. 'value' => _t('_adm_form_btn_badges_cancel'),
  356. 'attrs' => array(
  357. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  358. 'class' => 'bx-def-margin-sec-left',
  359. ),
  360. )
  361. )
  362. )
  363. );
  364. switch($sAction) {
  365. case 'add':
  366. unset($aForm['inputs']['icon_preview']);
  367. break;
  368. case 'edit':
  369. $sIconImage = $sIconFont = "";
  370. if(!empty($aBadge['icon'])) {
  371. if(is_numeric($aBadge['icon']) && (int)$aBadge['icon'] != 0) {
  372. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  373. $sIconImage = $oStorage->getFileUrlById((int)$aBadge['icon']);
  374. }
  375. else {
  376. $sIconFont = $aBadge['icon'];
  377. $aForm['inputs']['icon']['value'] = $sIconFont;
  378. }
  379. }
  380. $aForm['inputs']['icon_preview']['content'] = $this->_getIconPreview($aBadge['id'], $sIconImage, $sIconFont);
  381. break;
  382. }
  383. return new BxTemplStudioFormView($aForm);
  384. }
  385. protected function _getIconPreview($iId, $sIconImage = '', $sIcon = '')
  386. {
  387. $bIconImage = !empty($sIconImage);
  388. $aIcons = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon($sIcon);
  389. $sIconHtml = $aIcons[2] . $aIcons[3] . $aIcons[4];
  390. $bIconHtml = !empty($sIconHtml) && !$bIconImage;
  391. return $this->_oTemplate->parseHtmlByName('item_icon_preview.html', array(
  392. 'id' => $iId,
  393. 'bx_if:show_icon_empty' => array(
  394. 'condition' => !$bIconImage && !$bIconHtml,
  395. 'content' => array()
  396. ),
  397. 'bx_if:show_icon_image' => array(
  398. 'condition' => $bIconImage,
  399. 'content' => array(
  400. 'js_object' => $this->getJsObject(),
  401. 'url' => $sIconImage,
  402. 'id' => $iId
  403. )
  404. ),
  405. 'bx_if:show_icon_html' => array(
  406. 'condition' => $bIconHtml,
  407. 'content' => array(
  408. 'icon' => $sIconHtml
  409. )
  410. )
  411. ));
  412. }
  413. }
  414. /** @} */