1
0

BxBaseMenuSetBadges.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Menu to set badges for content
  11. */
  12. class BxBaseMenuSetBadges extends BxTemplMenu
  13. {
  14. protected $iContentId;
  15. protected $_aSelectedIds = array();
  16. public function __construct ($aObject, $oTemplate)
  17. {
  18. parent::__construct ($aObject, $oTemplate);
  19. }
  20. public function getCode ($iContentId = 0)
  21. {
  22. $this->iContentId = $iContentId;
  23. if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && ($iContentId = bx_get('content_id', 'post')) && ($iBadgeId = bx_get('badge_id', 'post')) && ($sModule = bx_get('module', 'post'))) {
  24. $iContentId = urldecode($iContentId);
  25. $iBadgeId = urldecode($iBadgeId);
  26. echoJson($this->setBadge($iContentId, $iBadgeId, $sModule));
  27. exit();
  28. }
  29. return parent::getCode ();
  30. }
  31. public function getMenuItems ()
  32. {
  33. $this->loadData();
  34. return parent::getMenuItems();
  35. }
  36. protected function loadData()
  37. {
  38. $iContentId = $this->iContentId;
  39. $sModule = '';
  40. if(bx_get('content_id') !== false)
  41. $iContentId = bx_process_input(bx_get('content_id'), BX_DATA_INT);
  42. if(bx_get('module') !== false)
  43. $sModule = bx_process_input(bx_get('module'));
  44. $oBadges = BxDolBadges::getInstance();
  45. $aBadges = $oBadges->getData(array('type' => 'by_module&object', 'object_id' => $iContentId, 'module' => $sModule));
  46. $aItems = array();
  47. foreach ($aBadges as $aBadge) {
  48. $aItems[] = array(
  49. 'id' => $aBadge['id'],
  50. 'name' => $aBadge['id'],
  51. 'class' => '',
  52. 'title' => '<div class="flex items-center"><input class="mr-2" onclick=bx_set_badge("' . $sModule . '",' . $iContentId . ',' . $aBadge['id'] . ') type="checkbox" ' . ($aBadge['badge_id'] != '' ? 'checked' : '') . '>' . BxDolService::call('system', 'get_badge', array($aBadge), 'TemplServices') .'</div>',
  53. 'addon' => '',
  54. 'selected' => true,
  55. 'link' => 'javascript:void(0);',
  56. 'onclick' => 'javascript:void(0);'
  57. );
  58. if ($aBadge['badge_id'] != ''){
  59. array_push($this->_aSelectedIds, $aBadge['id']);
  60. }
  61. }
  62. $this->_aObject['menu_items'] = $aItems;
  63. }
  64. protected function setBadge($iContentId, $iBadgeId , $sModule)
  65. {
  66. $iPerformerId = bx_get_logged_profile_id();
  67. $aCheck = checkActionModule($iPerformerId, 'set badge', 'system', false);
  68. if(!isAdmin() && $aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  69. return array('code' => 1, 'msg' => $aCheck[CHECK_ACTION_MESSAGE]);
  70. $oBadges = BxDolBadges::getInstance();
  71. $aBadges = $oBadges->getData(array('type' => 'by_module&object&badge', 'object_id' => $iContentId, 'module' => $sModule, 'badge_id' => $iBadgeId));
  72. if(count($aBadges) > 0)
  73. $oBadges->delete(array('type' => 'by_module&object&badge', 'module'=> $sModule, 'object_id' => $iContentId, 'badge_id' => $iBadgeId));
  74. else
  75. $oBadges->add($iBadgeId, $iContentId, $sModule);
  76. checkActionModule($iPerformerId, 'set badge', 'system', true); // perform action
  77. $aResult = array('code' => 0, 'html' => BxDolService::call($sModule, 'get_badges', array($iContentId)));
  78. return $aResult;
  79. }
  80. protected function _isSelected ($a)
  81. {
  82. return false;
  83. }
  84. }
  85. /** @} */