-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathshow_part_label.php
376 lines (317 loc) · 16.9 KB
/
show_part_label.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/*
part-db version 0.4
Copyright (C) 2016 Jan B�hmer
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
include_once __DIR__ . '/start_session.php';
use PartDB\Database;
use PartDB\HTML;
use PartDB\Label\BaseLabel;
use PartDB\Log;
use PartDB\Part;
use PartDB\Permissions\LabelPermission;
use PartDB\Permissions\PermissionManager;
use PartDB\Tools\JSONStorage;
use PartDB\User;
$messages = array();
$fatal_error = false; // if a fatal error occurs, only the $messages will be printed, but not the site content
/********************************************************************************
*
* Evaluate $_REQUEST
*
*********************************************************************************/
// We save every setting in this array, so we can serialize and deserialize it later easily.
$profile = array();
$element_id = isset($_REQUEST['id']) ? (int)$_REQUEST['id'] : 0;
$generator_type = isset($_REQUEST['generator']) ? (string)$_REQUEST['generator'] : 'part';
$profile_name = !empty($_REQUEST['profile']) ? (string)$_REQUEST['profile'] : 'default';
$selected_profile = isset($_REQUEST['selected_profile']) ? (string)$_REQUEST['selected_profile'] : '';
//Create JSON storage object, so we can load the profile the user has selected.
$json_storage = new JSONStorage(BASE_DATA . '/label_profiles.json');
//If Json storage default profile is empty, then create one.
if (!$json_storage->itemExists($generator_type . '@' . $profile_name)) {
if ($generator_type == 'part') {
$profile = array('label_size' => '',
'label_preset' => '',
'label_type' => BaseLabel::TYPE_BARCODE,
'text_bold' => false,
'text_italic' => false,
'text_underline' => false,
'text_size' => 8,
'output_mode' => 'html',
'barcode_alignment' => 'center',
'custom_rows' => '',
'custom_height' => '',
'custom_width' => '',
'text_alignment' => 'left',
'logo_path' => '',
'use_footprint_image' => false);
$json_storage->addItem($generator_type . '@' . $profile_name, $profile );
} elseif ($generator_type == 'location') {
$profile = array('label_size' => BaseLabel::SIZE_50X30,
'label_preset' => 'Preset A',
'label_type' => BaseLabel::TYPE_C39,
'text_bold' => false,
'text_italic' => false,
'text_underline' => false,
'text_size' => 8,
'output_mode' => 'html',
'barcode_alignment' => 'center',
'custom_rows' => '',
'custom_height' => '',
'custom_width' => '',
'text_alignment' => 'left',
'logo_path' => '',
'use_footprint_image' => false);
$json_storage->addItem($generator_type . '@' . $profile_name, $profile );
}
/*if ($profile_name == "default") {
$json_storage->addItem($generator_type . "@default", $profile);
}*/
} else {
$profile = $json_storage->getItem($generator_type . '@' . $profile_name);
}
$profile['label_size'] = isset($_REQUEST['size']) ? (string)$_REQUEST['size'] : $profile['label_size'];
$profile['label_preset'] = isset($_REQUEST['preset']) ? (string)$_REQUEST['preset'] : $profile['label_preset'];
$profile['label_type'] = isset($_REQUEST['type']) ? (int)$_REQUEST['type'] : $profile['label_type'] ;
//Advanced settings
$profile['text_bold'] = isset($_REQUEST['text_bold']) ? true : $profile['text_bold'] ;
$profile['text_italic'] = isset($_REQUEST['text_italic']) ? true : $profile['text_italic'];
$profile['text_underline'] = isset($_REQUEST['text_underline']) ? true : $profile['text_underline'];
$profile['text_size'] = isset($_REQUEST['text_size']) ? (int) $_REQUEST['text_size'] : $profile['text_size'] ;
$profile['text_alignment'] = isset($_REQUEST['text_alignment']) ? (string)$_REQUEST['text_alignment'] : $profile['text_alignment'];
$profile['logo_path'] = isset($_REQUEST['logo_path']) ? (string)$_REQUEST['logo_path'] : $profile['logo_path'];
$profile['custom_width'] = isset($_REQUEST['custom_width']) ? (string) $_REQUEST['custom_width'] : $profile['custom_width'] ;
$profile['custom_height'] = isset($_REQUEST['custom_height']) ? (string) $_REQUEST['custom_height'] : $profile['custom_height'] ;
$profile['output_mode'] = isset($_REQUEST['radio_output']) ? (string)$_REQUEST['radio_output'] : $profile['output_mode'];
$profile['barcode_alignment'] = isset($_REQUEST['barcode_alignment']) ? (string)$_REQUEST['barcode_alignment'] : $profile['barcode_alignment'];
$profile['custom_rows'] = isset($_REQUEST['custom_rows']) ? (string)$_REQUEST['custom_rows'] : $profile['custom_rows'];
$profile['use_footprint_image'] = isset($_REQUEST['use_footprint_image']) ? true : $profile['use_footprint_image'] ;
$action = 'default';
if (isset($_REQUEST['label_generate'])) {
$action = 'generate';
}
if (isset($_REQUEST['download'])) {
$action = 'download';
}
if (isset($_REQUEST['view'])) {
$action = 'view';
}
if (isset($_REQUEST['save_profile'])) {
$action = 'save_profile';
}
if (isset($_REQUEST['load_profile'])) {
$action = 'load_profile';
}
if (isset($_REQUEST['delete_profile'])) {
$action = 'delete_profile';
}
if (isset($_REQUEST['delete_confirmed'])) {
$action = 'delete_confirmed';
}
/********************************************************************************
*
* Initialize Objects
*
*********************************************************************************/
$html = new HTML($config['html']['theme'], $user_config['theme'], _('Labels'));
try {
$database = new Database();
$log = new Log($database);
$current_user = User::getLoggedInUser($database, $log);
$current_user->tryDo(PermissionManager::LABELS, LabelPermission::CREATE_LABELS);
switch ($generator_type) {
case 'part':
/* @var $generator_class BaseLabel */
$generator_class = \PartDB\Label\PartLabel::class;
if ($element_id > 0) {
$element = Part::getInstance($database, $current_user, $log, $element_id);
}
break;
case 'location':
/* @var $generator_class BaseLabel */
$generator_class = \PartDB\Label\StorelocationLabel::class;
if ($element_id > 0) {
$element = \PartDB\Storelocation::getInstance($database, $current_user, $log, $element_id);
}
break;
}
} catch (Exception $e) {
$messages[] = array('text' => nl2br($e->getMessage()), 'strong' => true, 'color' => 'red');
$fatal_error = true;
}
/********************************************************************************
*
* Execute Actions
*
*********************************************************************************/
if (!$fatal_error) {
try {
//Check if a file was uploaded, then move it to correct place and use it as logo.
$filepath = BASE . '/data/media/labels/';
if (isset($_FILES['logo_file']) && strlen($_FILES['logo_file']['name']) > 0) {
$uploaded_file = uploadFile($_FILES['logo_file'], $filepath);
$profile['logo_path'] = str_replace(BASE . '/', '', $uploaded_file);
}
//Build config array
$options = array();
$options['text_bold'] = $profile['text_bold'];
$options['text_italic'] = $profile['text_italic'];
$options['text_underline'] = $profile['text_underline'];
$options['text_size'] = $profile['text_size'];
$options['custom_width'] = $profile['custom_width'];
$options['custom_height'] = $profile['custom_height'];
$options['text_alignment'] = $profile['text_alignment'];
$options['logo_path'] = $profile['logo_path'];
//Override $options['logo_path'] if use_footprint_image option is set
if ($profile['use_footprint_image'] && $generator_type == 'part') {
$options['logo_path'] = $element->getFootprint()->getFilename(false);
}
if ($profile['output_mode'] == 'text') {
$options['force_text_output'] = true;
}
$options['barcode_alignment'] = $profile['barcode_alignment'];
$options['custom_rows'] = $profile['custom_rows'];
//If selected preset is not "custom", than show the preset lines in custom_rows
if ($profile['label_preset'] != 'custom') {
foreach ($generator_class::getLinePresets() as $preset) {
if ($preset['name'] == $profile['label_preset']) {
$profile['custom_rows'] = implode("\n", $preset['lines']);
}
}
}
//Show size preset in custom size inputs
if ($profile['label_size'] != 'custom' && !empty($profile['label_size'])) {
$exploded = explode('x', $profile['label_size']);
$profile['custom_width'] = $exploded[0];
$profile['custom_height'] = $exploded[1];
}
switch ($action) {
case 'generate':
$html->setVariable('preview_src', 'show_part_label.php?' . http_build_query($_REQUEST) . '&view', 'string');
$html->setVariable('download_link', 'show_part_label.php?' . http_build_query($_REQUEST) . '&download', 'string');
break;
case 'view':
/* @var BaseLabel $generator */
if (isset($element)) {
$generator = new $generator_class($element, $profile['label_type'], $profile['label_size'], $profile['label_preset'], $options);
$generator->generate();
}
break;
case 'download':
/* @var BaseLabel $generator */
if (isset($element)) {
$generator = new $generator_class($element, $profile['label_type'], $profile['label_size'], $profile['label_preset'], $options);
$generator->download();
}
break;
case 'save_profile':
$current_user->tryDo(PermissionManager::LABELS, LabelPermission::EDIT_PROFILES);
$new_name = $_REQUEST['save_name'];
if ($new_name == '') {
throw new Exception(_('Der Profilname darf nicht leer sein!'));
}
$json_storage->editItem($generator_type . '@' . $new_name, $profile, true, true);
$messages[] = array('text' => _('Das Profil wurde erfolgreich gespeichert!'), 'strong' => true, 'color' => 'green');
break;
case 'load_profile':
if ($selected_profile == '') {
throw new Exception(_('Sie müssen ein Profil zum Laden auswählen!'));
}
$new_request = $_GET;
$new_request['profile'] = $selected_profile;
$html->redirect('show_part_label.php?' . http_build_query($new_request));
break;
case 'delete_profile':
$current_user->tryDo(PermissionManager::LABELS, LabelPermission::DELETE_PROFILES);
if ($selected_profile == '') {
throw new Exception(_('Sie müssen ein Profil zum Löschen auswählen!'));
}
$messages[] = array('text' => sprintf(_('Soll das Profil "%s' .
'" wirklich unwiederruflich gelöscht werden?'), $selected_profile), 'strong' => true, 'color' => 'red');
$messages[] = array('html' => '<input type="hidden" name="generator" value="' . $generator_type . '">');
$messages[] = array('html' => '<input type="hidden" name="selected_profile" value="' . $selected_profile . '">');
$messages[] = array('html' => '<input type="submit" class="btn btn-secondary" name="" value="' . _('Nein, nicht löschen') . '">', 'no_linebreak' => true);
$messages[] = array('html' => '<input type="submit" class="btn btn-danger" name="delete_confirmed" value="' . _('Ja, Profil löschen') . '">');
break;
case 'delete_confirmed':
$current_user->tryDo(PermissionManager::LABELS, LabelPermission::DELETE_PROFILES);
if ($selected_profile == '') {
throw new Exception(_('Sie müssen ein Profil zum Löschen auswählen!'));
}
$messages[] = array('text' => _('Das Profil wurde erfolgreich gelöscht!'), 'strong' => true, 'color' => 'green');
$json_storage->deleteItem($generator_type . '@' . $selected_profile);
}
} catch (Exception $e) {
$messages[] = array('text' => nl2br($e->getMessage()), 'strong' => true, 'color' => 'red');
//$fatal_error = true;
}
}
/********************************************************************************
*
* Set the rest of the HTML variables
*
*********************************************************************************/
if (! $fatal_error) {
try {
$html->setVariable('id', $element_id, 'integer');
$html->setVariable('selected_size', $profile['label_size'], 'string');
$html->setVariable('selected_preset', $profile['label_preset'], 'string');
$html->setVariable('type', $profile['label_type'], 'integer');
$html->setVariable('generator', $generator_type, 'string');
//Show which label sizes are supported.
$html->setVariable('supported_sizes', $generator_class::getSupportedSizes());
$html->setVariable('supported_types', $generator_class::getSupportedTypes());
$html->setVariable('available_presets', $generator_class::getLinePresets());
//Advanced settings
$html->setVariable('text_bold', $profile['text_bold'], 'bool');
$html->setVariable('text_italic', $profile['text_italic'], 'bool');
$html->setVariable('text_underline', $profile['text_underline'], 'bool');
$html->setVariable('text_size', $profile['text_size'], 'int');
$html->setVariable('radio_output', $profile['output_mode'], 'string');
$html->setVariable('barcode_alignment', $profile['barcode_alignment'], 'string');
$html->setVariable('text_alignment', $profile['text_alignment'], 'string');
$html->setVariable('custom_rows', $profile['custom_rows'], 'string');
$html->setVariable('custom_width', $profile['custom_width'], 'int');
$html->setVariable('custom_height', $profile['custom_height'], 'int');
$html->setVariable('logo_path', $profile['logo_path'], 'string');
$html->setVariable('use_footprint_image', $profile['use_footprint_image'], 'bool');
//Profile tabs
$html->setVariable('save_name', $profile_name != 'default' ? $profile_name : '', 'string');
$html->setVariable('selected_profile', $profile_name, 'string');
$html->setVariable('profiles', buildLabelProfilesDropdown($generator_type, true));
//Permission variables
$html->setVariable('can_save_profile', $current_user->canDo(PermissionManager::LABELS, LabelPermission::EDIT_PROFILES));
$html->setVariable('can_edit_option', $current_user->canDo(PermissionManager::LABELS, LabelPermission::EDIT_OPTIONS));
$html->setVariable('can_delete_profile', $current_user->canDo(PermissionManager::LABELS, LabelPermission::DELETE_PROFILES));
} catch (Exception $e) {
$messages[] = array('text' => nl2br($e->getMessage()), 'strong' => true, 'color' => 'red');
$fatal_error = true;
}
}
/********************************************************************************
*
* Generate HTML Output
*
*********************************************************************************/
//If a ajax version is requested, say this the template engine.
if (isset($_REQUEST['ajax'])) {
$html->setVariable('ajax_request', true);
}
$reload_link = $fatal_error ? 'show_part_label.php?pid='.$part_id : ''; // an empty string means that the...
$html->printHeader($messages, $reload_link); // ...reload-button won't be visible
if (! $fatal_error) {
$html->printTemplate('show_part_label');
}
$html->printFooter();