-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPreferences.php
More file actions
233 lines (192 loc) · 6.54 KB
/
Preferences.php
File metadata and controls
233 lines (192 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
namespace Foolz\FoolFrame\Model;
use Foolz\Cache\Cache;
use Foolz\FoolFrame\Model\Validation\Validator;
use Foolz\Plugin\Hook;
use Symfony\Component\HttpFoundation\Request;
class Preferences extends Model
{
/**
* @var Config
*/
protected $config;
/**
* @var \Foolz\Profiler\Profiler
*/
protected $profiler;
/**
* @var DoctrineConnection
*/
protected $dc;
/**
* @var Notices
*/
protected $notices;
/**
* @var Security
*/
protected $security;
/**
* @var array
*/
protected $modules = [];
/**
* @var array
*/
protected $preferences = [];
/**
* @var bool
*/
protected $loaded = false;
/**
* @param Context $context
*/
public function __construct(Context $context)
{
parent::__construct($context);
$this->config = $context->getService('config');
$this->profiler = $context->getService('profiler');
$this->dc = $context->getService('doctrine');
$this->security = $this->getContext()->getService('security');
}
/**
* @param bool $reload
* @return array|mixed
*/
public function load($reload = false)
{
$this->profiler->log('Preferences::load Start');
if ($reload === true) {
Cache::item('foolframe.model.preferences.settings')->delete();
}
$this->modules = $this->config->get('foolz/foolframe', 'config', 'modules.installed');
try {
$this->preferences = Cache::item('foolframe.model.preferences.settings')->get();
} catch (\OutOfBoundsException $e) {
$preferences = $this->dc->qb()
->select('*')
->from($this->dc->p('preferences'), 'p')
->execute()
->fetchAll();
foreach($preferences as $pref) {
// fix the PHP issue where . is changed to _ in the $_POST array
$this->preferences[$pref['name']] = $pref['value'];
}
Cache::item('foolframe.model.preferences.settings')->set($this->preferences, 3600);
}
$this->preferences = Hook::forge('Foolz\FoolFrame\Model\Preferences::load#var.preferences')
->setObject($this)
->setParam('preferences', $this->preferences)
->execute()
->get($this->preferences);
$this->profiler->logMem('Preferences $preferences', $this->preferences);
$this->profiler->log('Preferences::load End');
$this->loaded = true;
return $this->preferences;
}
public function get($setting, $fallback = null, $show_empty_string = false)
{
if (!$this->loaded) {
$this->load();
}
if (isset($this->preferences[$setting]) && ($show_empty_string || $this->preferences[$setting] !== '')) {
return $this->preferences[$setting];
}
if ($fallback !== null) {
return $fallback;
}
$segments = explode('.', $setting);
$identifier = array_shift($segments);
$query = implode('.', $segments);
return $this->config->get($this->modules[$identifier]['namespace'], 'package', 'preferences.'.$query);
}
public function set($setting, $value, $reload = true)
{
// if array, serialize value
if (is_array($value)) {
$value = serialize($value);
}
$count = $this->dc->qb()
->select('COUNT(*) as count')
->from($this->dc->p('preferences'), 'p')
->where('p.name = :name')
->setParameter(':name', $setting)
->execute()
->fetch()['count'];
if ($count > 0) {
$this->dc->qb()
->update($this->dc->p('preferences'))
->set('value', ':value')
->where('name = :name')
->setParameters([':value' => $value, ':name' => $setting])
->execute();
} else {
$this->dc->getConnection()->insert($this->dc->p('preferences'), ['name' => $setting, 'value' => $value]);
}
if ($reload) {
return $this->load(true);
}
return $this->preferences;
}
/**
* Save in the preferences table the name/value pairs
*
* @param array $data name => value
*/
public function submit($data)
{
foreach ($data as $name => $value) {
// in case it's an array of values from name="thename[]"
if(is_array($value)) {
// remove also empty values with array_filter
// but we want to keep 0s
$value = serialize(array_filter($value, function($var) {
if($var === 0) {
return true;
}
return $var;
}));
}
$this->set($name, $value, false);
}
// reload those preferences
$this->load(true);
}
/**
* A lazy way to submit the preference panel input, saves some code in controller
*
* This function runs the custom validation function that uses the $form array
* to first run the original FuelPHP validation and then the anonymous
* functions included in the $form array. It sets a proper notice for the
* admin interface on conclusion.
*
* @param Request $request
* @param array $form
* @param bool|array $input If it evaluates to false, content won't be submitted
*/
public function submit_auto(Request $request, $form, $input = false)
{
if ($input) {
$this->notices = $this->getContext()->getService('notices');
if (!$this->security->checkCsrfToken($request)) {
$this->notices->set('warning', _i('The security token wasn\'t found. Try resubmitting.'));
return;
}
$post = [];
foreach ($input as $key => $item) {
// PHP doesn't allow periods in POST array
$post[str_replace(',', '.', $key)] = $item;
}
$result = Validator::formValidate($form, $post);
if (isset($result['error'])) {
$this->notices->set('warning', $result['error']);
} else {
if (isset($result['warning'])) {
$this->notices->set('warning', $result['warning']);
}
$this->notices->set('success', _i('Preferences updated.'));
$this->submit($result['success']);
}
}
}
}