You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BeforeTemplateRenderedListener.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. class BeforeTemplateRenderedListener implements IEventListener {
  38. private IInitialState $initialState;
  39. private ContainerInterface $container;
  40. private ThemeInjectionService $themeInjectionService;
  41. private IUserSession $userSession;
  42. private IConfig $config;
  43. public function __construct(
  44. IInitialState $initialState,
  45. ContainerInterface $container,
  46. ThemeInjectionService $themeInjectionService,
  47. IUserSession $userSession,
  48. IConfig $config
  49. ) {
  50. $this->initialState = $initialState;
  51. $this->container = $container;
  52. $this->themeInjectionService = $themeInjectionService;
  53. $this->userSession = $userSession;
  54. $this->config = $config;
  55. }
  56. public function handle(Event $event): void {
  57. $this->initialState->provideLazyInitialState(
  58. 'data',
  59. fn () => $this->container->get(JSDataService::class),
  60. );
  61. /** @var BeforeTemplateRenderedEvent $event */
  62. if ($event->getResponse()->getRenderAs() === TemplateResponse::RENDER_AS_USER) {
  63. $this->initialState->provideLazyInitialState('shortcutsDisabled', function () {
  64. if ($this->userSession->getUser()) {
  65. $uid = $this->userSession->getUser()->getUID();
  66. return $this->config->getUserValue($uid, Application::APP_ID, 'shortcuts_disabled', 'no') === 'yes';
  67. }
  68. return false;
  69. });
  70. }
  71. $this->themeInjectionService->injectHeaders();
  72. $user = $this->userSession->getUser();
  73. if (!empty($user)) {
  74. $userId = $user->getUID();
  75. /** User background */
  76. $this->initialState->provideInitialState(
  77. 'backgroundImage',
  78. $this->config->getUserValue($userId, Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT),
  79. );
  80. /** User color */
  81. $this->initialState->provideInitialState(
  82. 'backgroundColor',
  83. $this->config->getUserValue($userId, Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT),
  84. );
  85. /**
  86. * Admin background. `backgroundColor` if disabled,
  87. * mime type if defined and empty by default
  88. */
  89. $this->initialState->provideInitialState(
  90. 'themingDefaultBackground',
  91. $this->config->getAppValue('theming', 'backgroundMime', ''),
  92. );
  93. $this->initialState->provideInitialState(
  94. 'defaultShippedBackground',
  95. BackgroundService::DEFAULT_BACKGROUND,
  96. );
  97. /** List of all shipped backgrounds */
  98. $this->initialState->provideInitialState(
  99. 'shippedBackgrounds',
  100. BackgroundService::SHIPPED_BACKGROUNDS,
  101. );
  102. }
  103. // Making sure to inject just after core
  104. \OCP\Util::addScript('theming', 'theming', 'core');
  105. }
  106. }