1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HiPanel core package |
4
|
|
|
* |
5
|
|
|
* @link https://hipanel.com/ |
6
|
|
|
* @package hipanel-core |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\components; |
12
|
|
|
|
13
|
|
|
use hiqdev\thememanager\models\OrientationInterface; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\base\Component; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class UiOptionsStorage. |
19
|
|
|
*/ |
20
|
|
|
class UiOptionsStorage extends Component implements OrientationInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string the cache key that will be used to cache storage values |
24
|
|
|
*/ |
25
|
|
|
public $cacheKey; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public $settingsStorageKey = 'uiOptions'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $storage; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function init() |
41
|
|
|
{ |
42
|
|
|
if (!isset($this->cacheKey)) { |
43
|
|
|
$this->cacheKey = ['uiOptionsStorage', Yii::$app->user->id]; |
|
|
|
|
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return \yii\caching\Cache |
49
|
|
|
*/ |
50
|
|
|
protected function getCache() |
51
|
|
|
{ |
52
|
|
|
return Yii::$app->getCache(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return SettingsStorageInterface |
57
|
|
|
*/ |
58
|
|
|
protected function getSettingsStorage() |
59
|
|
|
{ |
60
|
|
|
return Yii::$app->get('settingsStorage'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Ensures that [[storage]] contains actual orientations storage. |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
private function ensureStorage() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
if (!isset($this->storage)) { |
71
|
|
|
$this->storage = $this->getCache()->get($this->cacheKey); |
|
|
|
|
72
|
|
|
|
73
|
|
|
if ($this->storage === false) { |
74
|
|
|
$this->storage = $this->getSettingsStorage()->getBounded($this->settingsStorageKey); |
|
|
|
|
75
|
|
|
$this->cache(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->storage; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Saves current [[storage]]. |
84
|
|
|
*/ |
85
|
|
|
protected function saveStorage() |
86
|
|
|
{ |
87
|
|
|
$this->getSettingsStorage()->setBounded($this->settingsStorageKey, $this->storage); |
88
|
|
|
$this->cache(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Caches current [[storage]]. |
93
|
|
|
*/ |
94
|
|
|
protected function cache() |
95
|
|
|
{ |
96
|
|
|
$this->getCache()->set($this->cacheKey, $this->storage, 86400); // 1 day |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Sets orientation for the $route. |
101
|
|
|
* |
102
|
|
|
* @param string $route |
103
|
|
|
* @param string $orientation |
|
|
|
|
104
|
|
|
*/ |
105
|
|
|
public function set($route, $options = []) |
106
|
|
|
{ |
107
|
|
|
$this->ensureStorage(); |
108
|
|
|
|
109
|
|
|
$this->storage[$route] = $options; |
110
|
|
|
$this->saveStorage(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Gets orientation for the $route. |
115
|
|
|
* |
116
|
|
|
* @param $route |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function get($route) |
120
|
|
|
{ |
121
|
|
|
$this->ensureStorage(); |
122
|
|
|
|
123
|
|
|
return isset($this->storage[$route]) ? $this->storage[$route] : null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
View Code Duplication |
public function getDefaultOption() |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$settings = Yii::$app->themeManager->getSettings(); |
132
|
|
|
|
133
|
|
|
if (property_exists($settings, 'filterOrientation') && in_array($settings->filterOrientation, [self::ORIENTATION_HORIZONTAL, self::ORIENTATION_VERTICAL], true)) { |
134
|
|
|
return $settings->filterOrientation; |
135
|
|
|
} else { |
136
|
|
|
return self::ORIENTATION_HORIZONTAL; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function getOption($option) |
141
|
|
|
{ |
142
|
|
|
return $this->get(Yii::$app->request->pathInfo)[$option]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getAllUiOptions() |
146
|
|
|
{ |
147
|
|
|
return $this->getSettingsStorage()->getBounded($this->settingsStorageKey); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..