1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace App\Controller\Component; |
14
|
|
|
|
15
|
|
|
use App\Controller\AppController; |
16
|
|
|
use Cake\Controller\Component; |
17
|
|
|
use Cake\Core\InstanceConfigTrait; |
18
|
|
|
use Saito\User\CurrentUser\CurrentUserInterface; |
19
|
|
|
|
20
|
|
|
class ThemesComponent extends Component |
21
|
|
|
{ |
22
|
|
|
use InstanceConfigTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Default configuration for InstanceConfigTrait |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $_defaultConfig = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritDoc} |
33
|
|
|
*/ |
34
|
|
|
public function initialize(array $config) |
35
|
|
|
{ |
36
|
|
|
$this->setConfig($config); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Sets theme |
41
|
|
|
* |
42
|
|
|
* @param CurrentUserInterface $user current user |
43
|
|
|
* @param string $theme theme to set |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function set(CurrentUserInterface $user, $theme = null): void |
47
|
|
|
{ |
48
|
|
|
if ($theme === null) { |
49
|
|
|
$theme = $this->getThemeForUser($user); |
50
|
|
|
} else { |
51
|
|
|
$theme = $this->_resolveTheme($theme, $this->getAvailable($user)); |
52
|
|
|
} |
53
|
|
|
$this->_setTheme($theme); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Applies the global default theme as activate theme. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function setDefault(): void |
62
|
|
|
{ |
63
|
|
|
$this->_setTheme($this->getDefaultTheme()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Activates theme. |
68
|
|
|
* |
69
|
|
|
* @param string $theme theme name |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
protected function _setTheme($theme): void |
73
|
|
|
{ |
74
|
|
|
$this->_registry->getController()->viewBuilder()->setTheme($theme); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get theme for specific user. |
79
|
|
|
* |
80
|
|
|
* @param CurrentUserInterface $user current user |
81
|
|
|
* @return string current theme for user |
82
|
|
|
*/ |
83
|
|
|
public function getThemeForUser(CurrentUserInterface $user): string |
84
|
|
|
{ |
85
|
|
|
$theme = $user->get('user_theme'); |
86
|
|
|
$available = $this->getAvailable($user); |
87
|
|
|
|
88
|
|
|
return $this->_resolveTheme($theme, $available); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Gets all available themes for user. |
93
|
|
|
* |
94
|
|
|
* @param CurrentUserInterface $user current user |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function getAvailable(CurrentUserInterface $user): array |
98
|
|
|
{ |
99
|
|
|
$available = []; |
100
|
|
|
|
101
|
|
|
if ($user->isLoggedIn()) { |
102
|
|
|
$global = $this->getConfig('available', []); |
103
|
|
|
$userThemes = $this->getConfig('users', []); |
104
|
|
|
$userId = $user->getId(); |
105
|
|
|
$userThemes = isset($userThemes[$userId]) ? $userThemes[$userId] : []; |
106
|
|
|
$available = array_merge($global, $userThemes); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$available[] = $this->getDefaultTheme(); |
110
|
|
|
$available = array_unique($available); |
111
|
|
|
|
112
|
|
|
return $available; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get default theme. |
117
|
|
|
* |
118
|
|
|
* @return string default theme |
119
|
|
|
*/ |
120
|
|
|
protected function getDefaultTheme(): string |
121
|
|
|
{ |
122
|
|
|
return $this->getConfig('default'); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Resolves theme |
127
|
|
|
* |
128
|
|
|
* @param string $theme theme to resolve |
129
|
|
|
* @param array $available available themes |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
protected function _resolveTheme($theme, array $available): string |
133
|
|
|
{ |
134
|
|
|
return in_array($theme, $available) ? $theme : $this->getDefaultTheme(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|