-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcategories.php
executable file
·116 lines (100 loc) · 5.88 KB
/
categories.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
<?php
require_once 'login.php';
require_once 'support/urlBase.php';
$smarty->assign('urlBase', $urlBase);
require_once './support/dba.php';
if ($usePrettyURLs) {
$smarty->assign('urlPostFix', '');
} else {
$smarty->assign('urlPostFix', '.php');
}
$alert = "";
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
if (isset($_GET['setCategoryId']) && !empty($_GET['setCategoryId']) && isset($_GET['to']) && !empty($_GET['to'])) {
$newCategory = DB::queryFirstRow('SELECT `id`, `amount` FROM `headCategories` WHERE `id`=%d', (int)$_GET['to']);
$subCategory = DB::queryFirstRow('SELECT `id`, `amount`, `headcategory` FROM `subCategories` WHERE id=%d', (int)$_GET['setCategoryId']);
$previousCategory = DB::queryFirstRow('SELECT `id`, `amount` FROM `headCategories` WHERE `id`=%d', $subCategory['headcategory']);
if ($previousCategory['id'] !== $newCategory['id']) {
DB::update('headCategories', array('amount' => (int)$previousCategory['amount'] - (int)$subCategory['amount']), 'id=%d', $previousCategory['id']);
if ($newCategory !== null) {
DB::update('headCategories', array('amount' => (int)$newCategory['amount'] + (int)$subCategory['amount']), 'id=%d', $newCategory['id']);
}
DB::update('subCategories', array('headcategory' => $newCategory['id']), 'id=%d', (int)$_GET['setCategoryId']);
}
if ($usePrettyURLs) {
header("location: $urlBase/categories");
} else {
header("location: $urlBase/categories.php");
}
die();
} else if (isset($_GET['resetSubcategoryId']) && !empty($_GET['resetSubcategoryId'])) {
$subCategory = DB::queryFirstRow('SELECT `id`, `amount`, `headcategory` FROM `subCategories` WHERE id=%d', (int)$_GET['resetSubcategoryId']);
$previousCategory = DB::queryFirstRow('SELECT `id`, `amount` FROM `headCategories` WHERE `id`=%d', $subCategory['headcategory']);
if ($previousCategory !== null) {
DB::update('headCategories', array('amount' => $previousCategory['amount'] - $subCategory['amount']), 'id=%d', $subCategory['headcategory']);
}
DB::update('subCategories', array('headcategory' => null), 'id=%d', $subCategory['id']);
if ($usePrettyURLs) {
header("location: $urlBase/categories");
} else {
header("location: $urlBase/categories.php");
}
die();
} else if (isset($_GET['to']) && !empty($_GET['to']) && (isset($_GET['headCategory']) || isset($_GET['subCategory']))) {
if (isset($_GET['headCategory']) && !empty($_GET['headCategory'])) {
DB::update('headCategories', array('name' => $_GET['to']), 'id=%d', (int)$_GET['headCategory']);
if (DB::affectedRows() === 1) {
$alert = '<div class="alert alert-info" role="alert"><p>' . gettext('Kategorie umbenannt.') . '</p></div>';
}
} else {
DB::update('subCategories', array('name' => $_GET['to']), 'id=%d', (int)$_GET['subCategory']);
if (DB::affectedRows() === 1) {
$alert = '<div class="alert alert-info" role="alert"><p>' . gettext('Unterkategorie umbenannt.') . '</p></div>';
}
}
} else if (isset($_GET['removeCategory']) && !empty($_GET['removeCategory'])) {
DB::delete('headCategories', "id=%d", (int)$_GET['removeCategory']);
if (DB::affectedRows() === 1) {
$alert = '<div class="alert alert-info" role="alert"><p>' . gettext('Kategorie entfernt.') . '</p></div>';
}
DB::query('UPDATE items SET headcategory = 0 WHERE headcategory = %i', $_GET['removeCategory']);
DB::query('UPDATE subCategories SET headcategory = 0 WHERE headcategory = %i', $_GET['removeCategory']);
} else if (isset($_GET['removeSubcategory']) && !empty($_GET['removeSubcategory'])) {
$subCategory = DB::queryFirstRow('SELECT `id`, `amount`, `headcategory` FROM `subCategories` WHERE `id`=%d', (int)$_GET['removeSubcategory']);
$previousCategory = DB::queryFirstRow('SELECT `id`, `amount` FROM `headCategories` WHERE `id`=%d', $subCategory['headcategory']);
if ($subCategory !== null) {
if ($previousCategory !== null) {
DB::update('headCategories', array('amount' => (int)$previousCategory['amount'] - $subCategory['amount']), 'id=%d', $previousCategory['id']);
}
DB::delete('subCategories', "id=%d", (int)$_GET['removeSubcategory']);
if (DB::affectedRows() === 1) {
$alert = '<div class="alert alert-info" role="alert"><p>' . gettext('Unterkategorie entfernt.') . '</p></div>';
}
DB::query('UPDATE items SET subcategories=REPLACE(subcategories,",' . (int)$_GET['removeSubcategory'] . ',","," ) ');
}
}
}
$headCategories = DB::query('SELECT `id`, `name`, `amount` FROM `headCategories` ORDER BY name ASC');
foreach ($headCategories as $key => $category) {
if ((int) $category['id'] === 0) {
unset($headCategories[$key]);
break;
}
}
foreach ($headCategories as $key => $category) {
$positions = DB::queryFirstField('SELECT COUNT(*) FROM `items` WHERE `headcategory`=%d', $category['id']);
$headCategories[$key]['positions'] = $positions;
}
$subCategories = DB::query('SELECT `id`, `name`, `amount`, `headcategory` FROM `subCategories` ORDER BY name ASC');
foreach ($subCategories as $key => $category) {
$positions = DB::queryFirstField('SELECT COUNT(*) FROM `items` WHERE `subcategories` LIKE %ss', ',' . $category['id'] . ',');
$subCategories[$key]['positions'] = $positions;
}
$smarty->assign('alert', $alert);
$smarty->assign("headCategories", $headCategories);
$smarty->assign("subCategories", $subCategories);
$smarty->assign('SESSION', $_SESSION);
$smarty->assign('REQUEST', $_SERVER['REQUEST_URI']);
$smarty->assign('_POST', $_POST);
$smarty->assign('_GET', $_GET);
$smarty->display('categories.tpl');