1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\SettingsBundle\Settings\Personal; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Security\Core\SecurityContextInterface; |
15
|
|
|
use ONGR\SettingsBundle\Settings\Personal\SettingsStructure; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Service responsible as a gateway to user settings. |
19
|
|
|
*/ |
20
|
|
|
class PersonalSettingsManager |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
const ROLE_GRANTED = 'ROLE_ADMIN'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
const STASH_NAME = 'ongr_settings'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var SecurityContextInterface |
34
|
|
|
*/ |
35
|
|
|
private $securityContext; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var SettingsStructure |
39
|
|
|
*/ |
40
|
|
|
private $settingsStructure; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array; |
44
|
|
|
*/ |
45
|
|
|
private $userSettings = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Stash for storing personal settings |
49
|
|
|
* |
50
|
|
|
* @var \Pool |
51
|
|
|
*/ |
52
|
|
|
private $pool; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param SettingsStructure $settingsStructure |
56
|
|
|
* @param $security |
57
|
|
|
* @param \Pool $pool |
58
|
|
|
*/ |
59
|
|
|
public function __construct($settingsStructure, $security, $pool) |
60
|
|
|
{ |
61
|
|
|
$this->settingsStructure = $settingsStructure; |
62
|
|
|
$this->securityContext = $security; |
63
|
|
|
$this->pool = $pool; |
64
|
|
|
$this->userSettings = $pool->getItem(self::STASH_NAME)->get(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $stash |
69
|
|
|
*/ |
70
|
|
|
public function setSettingsFromStash(array $stash) |
71
|
|
|
{ |
72
|
|
|
$this->userSettings = $stash; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $stash |
77
|
|
|
*/ |
78
|
|
|
public function addSettingsFromStash(array $stash) |
79
|
|
|
{ |
80
|
|
|
$this->userSettings = array_merge($this->userSettings, $stash); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $settings |
85
|
|
|
*/ |
86
|
|
|
public function setSettingsFromForm(array $settings) |
87
|
|
|
{ |
88
|
|
|
$this->userSettings = $settings; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* If user logged in, returns setting value from cookie. Else, returns false. |
93
|
|
|
* |
94
|
|
|
* @param string $settingName |
95
|
|
|
* @param bool $mustAuthorize |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function getSettingEnabled($settingName, $mustAuthorize = true) |
100
|
|
|
{ |
101
|
|
|
if ($mustAuthorize && !$this->isAuthenticated()) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (isset($this->userSettings[$settingName])) { |
106
|
|
|
return $this->userSettings[$settingName]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Saves the current settings to stash |
114
|
|
|
*/ |
115
|
|
|
public function save() |
116
|
|
|
{ |
117
|
|
|
$stashedSettings = $this->pool->getItem(self::STASH_NAME); |
118
|
|
|
$stashedSettings->set($this->userSettings); |
119
|
|
|
$this->pool->save($stashedSettings); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function isAuthenticated() |
126
|
|
|
{ |
127
|
|
|
return $this->securityContext->isGranted(self::ROLE_GRANTED); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public function getSettings() |
134
|
|
|
{ |
135
|
|
|
return $this->userSettings; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public function getSettingsMap() |
142
|
|
|
{ |
143
|
|
|
return $this->settingsStructure->getStructure(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
public function getCategoryMap() |
150
|
|
|
{ |
151
|
|
|
return $this->settingsStructure->getCategoriesStructure(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|