Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BeforeTemplateRenderedListener.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Theming\Listener;
  25. use OCA\Theming\AppInfo\Application;
  26. use OCA\Theming\Service\BackgroundService;
  27. use OCA\Theming\Service\JSDataService;
  28. use OCA\Theming\Service\ThemeInjectionService;
  29. use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
  30. use OCP\AppFramework\Http\TemplateResponse;
  31. use OCP\AppFramework\Services\IInitialState;
  32. use OCP\EventDispatcher\Event;
  33. use OCP\EventDispatcher\IEventListener;
  34. use OCP\IConfig;
  35. use OCP\IUserSession;
  36. use Psr\Container\ContainerInterface;
  37. /** @template-implements IEventListener<BeforeTemplateRenderedEvent> */
  38. class BeforeTemplateRenderedListener implements IEventListener {
  39. private IInitialState $initialState;
  40. private ContainerInterface $container;
  41. private ThemeInjectionService $themeInjectionService;
  42. private IUserSession $userSession;
  43. private IConfig $config;
  44. public function __construct(
  45. IInitialState $initialState,
  46. ContainerInterface $container,
  47. ThemeInjectionService $themeInjectionService,
  48. IUserSession $userSession,
  49. IConfig $config
  50. ) {
  51. $this->initialState = $initialState;
  52. $this->container = $container;
  53. $this->themeInjectionService = $themeInjectionService;
  54. $this->userSession = $userSession;
  55. $this->config = $config;
  56. }
  57. public function handle(Event $event): void {
  58. $this->initialState->provideLazyInitialState(
  59. 'data',
  60. fn () => $this->container->get(JSDataService::class),
  61. );
  62. /** @var BeforeTemplateRenderedEvent $event */
  63. if ($event->getResponse()->getRenderAs() === TemplateResponse::RENDER_AS_USER) {
  64. $this->initialState->provideLazyInitialState('shortcutsDisabled', function () {
  65. if ($this->userSession->getUser()) {
  66. $uid = $this->userSession->getUser()->getUID();
  67. return $this->config->getUserValue($uid, Application::APP_ID, 'shortcuts_disabled', 'no') === 'yes';
  68. }
  69. return false;
  70. });
  71. }
  72. $this->themeInjectionService->injectHeaders();
  73. $user = $this->userSession->getUser();
  74. if (!empty($user)) {
  75. $userId = $user->getUID();
  76. /** User background */
  77. $this->initialState->provideInitialState(
  78. 'backgroundImage',
  79. $this->config->getUserValue($userId, Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT),
  80. );
  81. /** User color */
  82. $this->initialState->provideInitialState(
  83. 'backgroundColor',
  84. $this->config->getUserValue($userId, Application::APP_ID, 'background_color', BackgroundService::DEFAULT_COLOR),
  85. );
  86. /**
  87. * Admin background. `backgroundColor` if disabled,
  88. * mime type if defined and empty by default
  89. */
  90. $this->initialState->provideInitialState(
  91. 'themingDefaultBackground',
  92. $this->config->getAppValue('theming', 'backgroundMime', ''),
  93. );
  94. $this->initialState->provideInitialState(
  95. 'defaultShippedBackground',
  96. BackgroundService::DEFAULT_BACKGROUND_IMAGE,
  97. );
  98. /** List of all shipped backgrounds */
  99. $this->initialState->provideInitialState(
  100. 'shippedBackgrounds',
  101. BackgroundService::SHIPPED_BACKGROUNDS,
  102. );
  103. }
  104. // Making sure to inject just after core
  105. \OCP\Util::addScript('theming', 'theming', 'core');
  106. }
  107. }