This repository has been archived by the owner on Dec 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.php
253 lines (206 loc) · 6.21 KB
/
admin.php
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/**
* @package ThumbnailsAddon
*/
class addon_thumbnails_admin extends addon_thumbnails_info
{
public function init_pages ($menuName)
{
menu_page::addonAddPage('addon_thumbnails_status','','Status', $this->name);
menu_page::addonAddPage('addon_thumbnails_settings','','Settings', $this->name);
menu_page::addonAddPage('addon_thumbnails_thumbnails','','Sizes and Tags', $this->name);
}
public function display_addon_thumbnails_status ()
{
$util = geoAddon::getUtil($this->name);
$db = true;
include(GEO_BASE_DIR . 'get_common_vars.php');
$util->db = $db;
$tpl_vars = array('jobs' => $util->getJobs());
geoView::getInstance()->setBodyTpl('admin/status.tpl', $this->name)->setBodyVar($tpl_vars);
}
public function update_addon_thumbnails_status ()
{
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else {
return false;
}
$util = geoAddon::getUtil($this->name);
$db = true;
include(GEO_BASE_DIR . 'get_common_vars.php');
$util->db = $db;
switch ($action) {
case 'prune_successful':
$util->removeJobs('success');
break;
case 'prune_failed':
$util->removeJobs('failed');
break;
}
}
public function display_addon_thumbnails_settings ()
{
$reg = geoAddon::getRegistry($this->name);
// load existing settings or submitted settings on error
if ($reg->settings_with_error) {
$settings = $reg->settings_with_error;
} elseif ($reg->settings) {
$settings = $reg->settings;
}
// unset settings_with_error to avoid loop
$reg->settings_with_error = null;
$reg->save();
$tpl_vars = array();
$tpl_vars['admin_messages'] = geoAdmin::m();
$tpl_vars['settings'] = $settings;
geoView::getInstance()->setBodyTpl('admin/settings.tpl', $this->name)->setBodyVar($tpl_vars);
}
public function update_addon_thumbnails_settings ()
{
if (isset($_POST['settings'])) {
$settings = $_POST['settings'];
} else {
return false;
}
$admin = geoAdmin::getInstance();
$reg = geoAddon::getRegistry($this->name);
$errors = $this->checkSettingsInput($settings);
// if there was an error add each error to the list of admin messages
if ($errors) {
foreach ($errors as $error) {
$admin->userError($error);
}
$admin->userError("Settings not saved.");
$reg->settings_with_error = $settings;
$reg->save();
return false;
} else {
$reg->settings = $settings;
$reg->save();
return true;
}
}
public function display_addon_thumbnails_thumbnails ()
{
$reg = geoAddon::getRegistry($this->name);
if (!$reg->next_size_id) {
$reg->next_size_id = 1;
$reg->save();
}
// load existing settings or submitted settings on error
if ($reg->tags_with_error) {
$tags = $reg->tags_with_error;
} elseif ($reg->tags) {
$tags = $reg->tags;
}
// unset settings_with_error to avoid loop
$reg->tags_with_error = null;
$reg->save();
$tpl_vars = array();
$tpl_vars['admin_messages'] = geoAdmin::m();
$tpl_vars['next_size_id'] = $reg->next_size_id;
if ($reg->sizes) {
$tpl_vars['sizes'] = $reg->sizes;
}
if ($tags) {
$tpl_vars['tags'] = $tags;
}
geoView::getInstance()->setBodyTpl('admin/thumbnails.tpl', $this->name)->setBodyVar($tpl_vars);
}
public function update_addon_thumbnails_thumbnails ()
{
if (isset($_POST['sizes'])) {
$new_sizes = $_POST['sizes'];
} elseif (isset($_POST['tags'])) {
$new_tags = $_POST['tags'];
} else {
return false;
}
$admin = geoAdmin::getInstance();
$reg = geoAddon::getRegistry($this->name);
if ($new_tags) {
$tags = array();
foreach ($new_tags as $key => $tag) {
$cleaned_result = $this->cleanTagInput($key, $tag);
$tag = $cleaned_result[0];
$errors = $cleaned_result[1];
// if there was an error add each error to the list of admin messages
if ($errors) {
$error = true;
foreach ($errors as $error) {
$admin->userError($error);
}
}
if (!(is_null($tag) || $tag['remove'])) {
array_push($tags, $tag);
}
}
array_multisort($tags);
// give each tag an id field
foreach ($tags as $key => &$tag) {
$tag['id'] = $key;
}
if ($error) {
$admin->userError("Tag settings not saved.");
$reg->tags_with_error = $tags;
$reg->save();
return false;
} else {
$reg->tags = $tags;
$reg->save();
return true;
}
} elseif ($new_sizes) {
$util = geoAddon::getUtil($this->name);
$db = true;
include(GEO_BASE_DIR . 'get_common_vars.php');
$util->db = $db;
$sizes = array();
$jobs = array();
foreach ($new_sizes as $key => $size) {
if ($key == 'new' && $size['width'] && $size['height'] && $size['quality']) {
$size['id'] = $reg->next_size_id;
$reg->next_size_id = $size['id'] + 1;
array_push($jobs, array('job' => 'add_size', 'target' => $size['id']));
array_push($sizes, $size);
} elseif ($size['remove'] == 1) {
array_push($jobs, array('job' => 'remove_size', 'target' => $size['id']));
} elseif ($key != 'new') {
array_push($sizes, $size);
}
}
$util->queueJobs($jobs);
$reg->sizes = $sizes;
$reg->save();
}
return true;
}
private function checkSettingsInput ($input)
{
$errors = array();
return $errors;
}
private function checkSizeInput ($input)
{
$errors = array();
// fail if empty field
if (!$input['width'] || !$input['height'] || !$input['quality']) {
array_push($errors, 'Empty field.');
}
return $errors;
}
private function cleanTagInput ($id, $input)
{
$errors = array();
// ignore a new group if field is empty
if ($id == 'new' && (!$input['name'] && !$input['size'])) {
return array(null, $errors);
}
// fail if empty field
if (!$input['name'] || !$input['size']) {
array_push($errors, 'Empty field.');
}
return array($input, $errors);
}
}