Newer
Older
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
<?php
// Set defaults
$mode = "list";
$admin = false;
$admin_group="staff";
$admin = isset($session->groups[$admin_group]);
// If your an admin and the path ends in Edit/ then a number put it into edit mode
// create canedit rules
$canedit=isset($pathlist[($component[depth]/2)+1]);
$canedit=$canedit && isset($pathlist[($component[depth]/2)+2]);
$canedit=$canedit && $pathlist[($component[depth]/2)+1]=='Edit';
$canedit=$canedit && is_numeric($pathlist[($component[depth]/2)+2]);
$canedit=$canedit && $admin;
// Apply canedit rules
if ($canedit){
$id = $pathlist[($component[depth]/2)+2];
$items = $DB->GetAll("SELECT id, title, category, description, donated_by, status FROM inventory WHERE id=? AND requested_by IS NULL",$id);
if(sizeof($items) === 1){
$item = $items[0];
$smarty->assign("item", $item);
$mode = "edit";
}
}
// If your an admin and the path ends in Add put it into add mode
// create canadd rules
$canadd=isset($pathlist[($component[depth]/2)+1]);
$canadd=$canadd && $pathlist[($component[depth]/2)+1]=='Add';
$canadd=$canadd && $admin;
// Apply canadd rules
if ($canadd){$mode = "add";}
// Process actions before retriving the data
// List request data
if ($session->loggedin && isset($_REQUEST['action'])) {
// Junk Requests
if ($_REQUEST['action'] == "Request") {
// Request Item if its available
$query = "UPDATE inventory SET requested_by=?, requested_on=now() WHERE id=? AND requested_by IS NULL";
$DB->Query($query, array($session->username, $_REQUEST['item']));
}
elseif ($_REQUEST['action'] == "Un-Request") {
// Un-Request Item
if ($admin){
// if your admin just do it
$query = "UPDATE inventory SET requested_by=null, requested_on=null WHERE id=?";
$array = array($_REQUEST['item']);
}
else{
// if not admin check if you requested it first
$query = "UPDATE inventory SET requested_by=null, requested_on=null WHERE id=? AND requested_by=?";
$array = array($_REQUEST['item'], $session->username);
}
$DB->Query($query, $array);
}
elseif ($_REQUEST['action'] == "Take" && $admin){
// Take item, if your admin
$query = "UPDATE inventory SET taken_on=now() WHERE id=? AND requested_by IS NOT NULL";
$DB->Query($query, $_REQUEST['item']);
}
elseif ($_REQUEST['action'] == "Remove" && $admin){
// Remove item, if your admin and its been taken
$query = "DELETE FROM inventory WHERE id=? AND taken_on IS NOT NULL";
$DB->Query($query, $_REQUEST['item']);
}
elseif ($_REQUEST['action'] == "Not Junk" && $admin){
// Mark item as not junk if its not been requested and your admin
$query = "UPDATE inventory SET status='unknown' WHERE id=? AND requested_by IS NULL";
$DB->Query($query, $_REQUEST['item']);
}
elseif ($_REQUEST['action'] =="Junk" && $admin){
// Mark item as junk, if your admin
$query = "UPDATE inventory SET status='junk' WHERE id=?";
$DB->Query($query, $_REQUEST['item']);
}
}
// Update/Add item
if ($session->loggedin && $admin && (isset($_REQUEST['update']) || isset($_REQUEST['add']))) {
// Update/Add item if title and category are filled in else error
if ($_REQUEST['title'] != "" && $_REQUEST['category'] != "") {
// if the description is blank, return null
if ($_REQUEST['description'] == ""){
$description = null;
}
else{
$description = $_REQUEST['description'];
}
// if the donated_by is blank, return null
if ($_REQUEST['donated_by'] == ""){
$donated_by = null;
}
else{
$donated_by = $_REQUEST['donated_by'];
}
// run the query
if(isset($_REQUEST['update'])){
$query = "UPDATE inventory SET title=?, category=?, description=?, donated_by=?, status=? WHERE id=?";
$array = array($_REQUEST['title'], $_REQUEST['category'], $description, $donated_by, $_REQUEST['status'], $_REQUEST['id']);
message_flash("Item Updated");
}
elseif(isset($_REQUEST['add'])){
$query = "INSERT INTO inventory (title, category, description, donated_by, status) VALUES (?, ?, ?, ?, ?)";
$array = array($_REQUEST['title'], $_REQUEST['category'], $description, $donated_by, $_REQUEST['status']);
message_flash("Item Added");
}
$DB->Query($query, $array);
}
else{
trigger_error("Required field(s) missing", E_USER_WARNING);
}
}
if ($mode == 'list'){
// Get junk from database, and give admin the full list
if ($admin == true){
$junk = $DB->GetAll("SELECT * FROM inventory ORDER BY category");
}
else{
$junk = $DB->GetAll("SELECT * FROM inventory WHERE status = 'junk' ORDER BY category");
}
$smarty->assign("junk", $junk);
// Check there is some junk
if (sizeof($junk) < 1) {
$mode = "nojunk";
}
}
// Generate output
$smarty->assign("statuses",array("unknown", "in use", "wanted", "junk"));
$smarty->assign("componentpath", $baseurl . $component[path]);
$smarty->assign("mode", $mode);
$smarty->assign("admin", $admin);
$output = $smarty->fetch("junk.tpl");
$smarty->assign("title", "Junk List");
$smarty->assign("body", $output);
?>