forked from cfinke/anyInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategory_class.php
executable file
·366 lines (288 loc) · 13.7 KB
/
category_class.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
<?php
class category {
var $id; // The id in the database anyInventory_categories for this category
var $name; // The name of this category
var $parent_id; // The id in the database anyInventory_categories for this category's parent
var $children_ids = array(); // An array of category ids that are children of this category
var $num_children = 0; // The number of children this category has. (Counts children only throught the first generation.)
var $all_children_ids = array();
var $breadcrumbs = array(); // An array of the parents of this category, starting at 0, the top level.
var $breadcrumb_names; // A breadcrumb style string of $this->breadcrumbs, ie. Top > Books > Fiction
var $field_ids = array(); // An array of all of the ids (found in anyInventory_fields) of the fields that this category uses.
var $field_names = array(); // An array of the field names that matches up with the ids found in $this->field_ids.
var $auto_inc_field = false;
function category($cat_id){
global $db;
// Set the id of this category.
$this->id = $cat_id;
// Separate the actions for the Top Level category and the other categories
if ($this->id != 0){
// Not the Top Level
// Get all of the information about this category from the categories table.
$query = "SELECT " . $db->quoteIdentifier('name') . "," . $db->quoteIdentifier('parent') . "," . $db->quoteIdentifier('auto_inc_field') . " FROM " . $db->quoteIdentifier('anyInventory_categories') . " WHERE " . $db->quoteIdentifier('id') . "='".$this->id."'";
$result = $db->query($query);
if (DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
$row = $result->fetchRow();
// Set the name and parent id
$this->name = $row["name"];
$this->parent_id = $row["parent"];
$this->auto_inc_field = $row["auto_inc_field"];
// Begin setting the breadcrumbs of this category.
// Set the parent id to this category's id
$parent_id = $this->id;
// As long as the parent is not the Top Level, set another breadcrumb
while ($parent_id != 0){
// Add the parent id to the breadcrumbs
$this->breadcrumbs[] = $parent_id;
// Set the parent id to the parent of the current parent id
$parent_id = $this->find_parent_id($parent_id);
}
// Add the top level to the breadcrumbs.
$this->breadcrumbs[] = 0;
// Reverse the breadcrumbs so that the Top Level is at the beginning
$this->breadcrumbs = array_reverse($this->breadcrumbs);
// Set the breadcrumb names from $this->breadcrumbs
if (is_array($this->breadcrumbs)){
foreach($this->breadcrumbs as $crumb){
if ($crumb == 0){
// The Top Level category
$this->breadcrumb_names .= TOP_LEVEL_CATEGORY.' > ';
}
else{
// Find the name of the current category
$query = "SELECT " . $db->quoteIdentifier('name') . " FROM " . $db->quoteIdentifier('anyInventory_categories') . " WHERE " . $db->quoteIdentifier('id') . "='".$crumb."'";
$result = $db->query($query);
if (DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
$this->breadcrumb_names .= $row["name"] . ' > ';
}
}
}
$this->breadcrumb_names = substr($this->breadcrumb_names, 0, strlen($this->breadcrumb_names) - 3);
// Get all of the fields that this category uses.
$query = "SELECT " . $db->quoteIdentifier('id') . "," . $db->quoteIdentifier('name') . " FROM " . $db->quoteIdentifier('anyInventory_fields') . " WHERE " . $db->quoteIdentifier('categories') . " LIKE '%\"".$this->id."\"%' OR " . $db->quoteIdentifier('input_type') . "='divider' ORDER BY " . $db->quoteIdentifier('importance') . " ASC";
$result = $db->query($query);
if (DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
// Add each field's id and name to the appropriate arrays.
while ($row = $result->fetchRow()){
$this->field_ids[] = $row["id"];
$this->field_names[] = $row["name"];
}
}
else{
// Top Level category
$this->name = TOP_LEVEL_CATEGORY;
$this->parent_id = 0;
// Set the breadcrumbs and breadcrumbs names
$this->breadcrumbs[] = 0;
$this->breadcrumb_names = TOP_LEVEL_CATEGORY;
// Get the fields that the Top Level uses.
$query = "SELECT " . $db->quoteIdentifier('id') . "," . $db->quoteIdentifier('name') . " FROM " . $db->quoteIdentifier('anyInventory_fields') . " WHERE " . $db->quoteIdentifier('categories') . " LIKE '%0%' OR " . $db->quoteIdentifier('input_type') . "='divider' ORDER BY " . $db->quoteIdentifier('importance') . "";
$result = $db->query($query);
if(DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
// Add each field id and name to the arrays.
while ($row = $result->fetchRow()){
$this->field_ids[] = $row["id"];
$this->field_names[] = $row["name"];
}
}
// Find the children of the current category
$query = "SELECT " . $db->quoteIdentifier('id') . " FROM " . $db->quoteIdentifier('anyInventory_categories') . " WHERE " . $db->quoteIdentifier('parent') . " = '".$this->id."' ORDER BY " . $db->quoteIdentifier('name') . " ASC";
$result = $db->query($query);
if(DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
while($row = $result->fetchRow()){
$this->children_ids[] = $row["id"];
$this->num_children++;
}
$this->get_all_subcats($this->all_children_ids);
}
// This function returns the number of items that are inventoried in this category.
function num_items(){
global $db;
$query = "SELECT " . $db->quoteIdentifier('id') . " FROM " . $db->quoteIdentifier('anyInventory_items') . " WHERE " . $db->quoteIdentifier('item_category') . "='".$this->id."'";
$result = $db->query($query);
if(DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
return $result->numRows();
}
// This function returns the number of items that are inventoried in this category AND its subcategories.
function num_items_r(){
// First, start with the number of items in this category.
$total = $this->num_items();
if (is_array($this->children_ids)){
// For each child, call this function on it.
foreach($this->children_ids as $child_id){
$child = new category($child_id);
$total += $child->num_items_r();
}
}
return $total;
}
function get_all_subcats(&$subcats){
if ($this->num_children > 0){
foreach($this->children_ids as $child_id){
$subcats[] = $child_id;
$child = new category($child_id);
$child->get_all_subcats($subcats);
}
}
sort($subcats);
}
// This function returns the parent id of a given category id.
function find_parent_id($cat_id){
global $db;
if ($cat_id == 0){
// The Top Level is its own category.
return 0;
}
else{
// Get the parent from the categories table.
$query = "SELECT " . $db->quoteIdentifier('parent') . " FROM " . $db->quoteIdentifier('anyInventory_categories') . " WHERE " . $db->quoteIdentifier('id') . "='".$cat_id."'";
$result = $db->query($query);
if(DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
// If there is no parent, then, the parent is the Top Level.
if ($result->numRows() == 0){
return 0;
}
else{
$row = $result->fetchRow();
return $row['parent'];
}
}
}
// This function returns the breadcrumbs_names string with links for each category.
function get_breadcrumb_links(){
global $DIR_PREFIX;
// For each id in the breadcrumbs, add the link and separator.
if (is_array($this->breadcrumbs)){
foreach($this->breadcrumbs as $id){
if($id == 0){
$breadcrumbs .= '<a href="'.$_SERVER["PHP_SELF"].'?c=0">'.TOP_LEVEL_CATEGORY.'</a> > ';
}
else{
$crumb = new category($id);
if ($crumb->id)
$breadcrumbs .= '<a href="'.$DIR_PREFIX.'index.php?c='.$crumb->id.'">'.$crumb->name.'</a> > ';
}
}
}
// Remove the last 6 characters from the string.
$breadcrumbs = substr($breadcrumbs, 0, strlen($breadcrumbs) - 6);
return $breadcrumbs;
}
function get_breadcrumb_admin_links(){
global $DIR_PREFIX;
// For each id in the breadcrumbs, add the link and separator.
if (is_array($this->breadcrumbs)){
foreach($this->breadcrumbs as $id){
if($id == 0){
$breadcrumbs .= TOP_LEVEL_CATEGORY .' > ';
}
else{
$crumb = new category($id);
if ($crumb->id)
$breadcrumbs .= '<a href="'.$DIR_PREFIX.'admin/edit_category.php?id='.$crumb->id.'">'.$crumb->name.'</a> > ';
}
}
}
// Remove the last 6 characters from the string.
$breadcrumbs = substr($breadcrumbs, 0, strlen($breadcrumbs) - 6);
return $breadcrumbs;
}
function export_table_header($sortfid = 0, $sort_dir = "DESC"){
$output .= '<tr class="tableHeader">';
if ($this->auto_inc_field){
$output .= '<td> </td>';
}
$output .= '<td style="white-space: nowrap;"><nobr>'.NAME_FIELD_NAME.'</nobr></td>';
if (is_array($this->field_ids)){
foreach($this->field_ids as $fid){
$field = new field($fid);
if (($field->input_type != 'divider') && ($field->input_type != 'file') && ($field->input_type != 'item')){
if ($sortfid == $field->id){
$dir = ($sort_dir == "DESC") ? "ASC" : "DESC";
}
else{
$dir = "DESC";
}
$output .= '<td style="white-space: nowrap;"><nobr><a href="'.$_SERVER["PHP_SELF"].'?c='.$this->id.'&fid='.$field->id.'&dir='.$dir.'">'.$field->name.'</a></nobr></td>';
}
}
}
$output .= '</tr>';
return $output;
}
function move_subcats($parent_id = 0){
$query = "UPDATE " . $db->quoteIdentifier('anyInventory_categories') . " SET " . $db->quoteIdentifier('parent') . "='".$parent_id."' WHERE " . $db->quoteIdentifier('parent') . "='".$this->id."'";
$result = $db->query($query);
if(DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
}
function move_items($cat_id = null){
global $db;
if ($cat_id == null){
$query = "SELECT " . $db->quoteIdentifier('id') . " FROM " . $db->quoteIdentifier('anyInventory_items') . " WHERE " . $db->quoteIdentifier('item_category') . "='".$this->id."'";
$result = $db->query($query);
if(DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
while ($row = $result->fetchRow()){
$item = new item($row["id"]);
$item->delete_self();
}
}
else{
$query = "SELECT " . $db->quoteIdentifier('id') . " FROM " . $db->quoteIdentifier('anyInventory_items') . " WHERE " . $db->quoteIdentifier('item_category') . "='".$category->id."'";
$result = $db->query($query);
if(DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
while($row = $result->fetchRow()){
$newquery = "SELECT " . $db->quoteIdentifier('id') . " FROM " . $db->quoteIdentifier('anyInventory_alerts') . " WHERE " . $db->quoteIdentifier('item_ids') . " LIKE '%\"".$row["id"]."\"%' AND " . $db->quoteIdentifier('category_ids') . " NOT LIKE '%\"".$cat_id."\"%' GROUP BY " . $db->quoteIdentifier('id') . "";
$newresult = $db->query($newquery);
if(DB::isError($newresult)) die($newresult->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $newquery);
while ($newrow = $newresult->fetchRow()){
$alert = new alert($newrow["id"]);
$alert->remove_item($row["id"]);
}
}
$query = "UPDATE " . $db->quoteIdentifier('anyInventory_items') . " SET " . $db->quoteIdentifier('item_category') . "='".$cat_id."' WHERE " . $db->quoteIdentifier('item_category') . "='".$this->id."'";
$result = $db->query($query);
if(DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
}
}
function delete_self(){
global $admin_user;
global $db;
if (!$admin_user->can_admin($_POST["id"])){
header("Location: ../error_handler.php?eid=13");
exit;
}
else{
// Delete the category
$query = "DELETE FROM " . $db->quoteIdentifier('anyInventory_categories') . " WHERE " . $db->quoteIdentifier('id') . "='".$this->id."'";
$result = $db->query($query);
if(DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
// Remove all of the fields from this category.
if (is_array($this->field_ids)){
foreach($this->field_ids as $field_id){
$field = new field($field_id);
$field->remove_category($this->id);
}
}
$query = "SELECT " . $db->quoteIdentifier('id') . " FROM " . $db->quoteIdentifier('anyInventory_alerts') . " WHERE " . $db->quoteIdentifier('category_ids') . " LIKE '%\"".$this->id."\"%'";
$result = $db->query($query);
if(DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
while ($row = $result->fetchRow()){
$alert = new alert($row["id"]);
$alert->remove_category($this->id);
}
}
}
function delete_subcats(){
if ($this->num_children > 0){
foreach($this->children_ids as $child_id){
$child = new category($child_id);
$child->delete_subcats();
}
}
else{
$this->move_items();
$this->delete_self();
}
}
}
?>