| 
									
										
										
										
											2017-05-27 20:02:27 -06:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 13:32:58 -07:00
										 |  |  | /* This Source Code Form is subject to the terms of the Mozilla Public | 
					
						
							|  |  |  |  * License, v. 2.0. If a copy of the MPL was not distributed with this | 
					
						
							|  |  |  |  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-27 20:02:27 -06:00
										 |  |  | require_once __DIR__ . '/../required.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | dieifnotloggedin(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | header("Content-Type: application/json"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-07 18:29:25 -06:00
										 |  |  | $showwant = (isset($VARS['show_want']) && $VARS['show_want'] == 1); | 
					
						
							| 
									
										
										
										
											2017-09-05 22:35:35 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-27 20:02:27 -06:00
										 |  |  | $out = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $out['draw'] = intval($VARS['draw']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-05 22:35:35 -06:00
										 |  |  | if ($showwant) { | 
					
						
							|  |  |  |     $out['recordsTotal'] = $database->count('items', ["AND" => ["qty[<]want", "want[>]" => 0]]); | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  |     $out['recordsTotal'] = $database->count('items'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-27 20:02:27 -06:00
										 |  |  | $filter = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // sort
 | 
					
						
							|  |  |  | $order = null; | 
					
						
							|  |  |  | $sortby = "DESC"; | 
					
						
							|  |  |  | if ($VARS['order'][0]['dir'] == 'asc') { | 
					
						
							|  |  |  |     $sortby = "ASC"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | switch ($VARS['order'][0]['column']) { | 
					
						
							|  |  |  |     case 2: | 
					
						
							|  |  |  |         $order = ["name" => $sortby]; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case 3: | 
					
						
							|  |  |  |         $order = ["catname" => $sortby]; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case 4: | 
					
						
							|  |  |  |         $order = ["locname" => $sortby]; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case 5: | 
					
						
							|  |  |  |         $order = ["code1" => $sortby]; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case 6: | 
					
						
							|  |  |  |         $order = ["code2" => $sortby]; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case 7: | 
					
						
							|  |  |  |         $order = ["qty" => $sortby]; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2017-09-05 22:35:35 -06:00
										 |  |  |     case 8: | 
					
						
							|  |  |  |         $order = ["want" => $sortby]; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2017-05-27 20:02:27 -06:00
										 |  |  |     // Note: We're not going to sort by assigned user.  It's too hard.  Maybe later.
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // search
 | 
					
						
							|  |  |  | if (!is_empty($VARS['search']['value'])) { | 
					
						
							|  |  |  |     $filter = true; | 
					
						
							| 
									
										
										
										
											2017-09-05 22:35:35 -06:00
										 |  |  |     $wherenolimit = []; | 
					
						
							|  |  |  |     if ($showwant) { | 
					
						
							|  |  |  |         $wherenolimit["AND"] = ["qty[<]want", "want[>]" => 0]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     $wherenolimit["AND"]["OR"] = [ | 
					
						
							|  |  |  |         "name[~]" => $VARS['search']['value'], | 
					
						
							|  |  |  |         "catname[~]" => $VARS['search']['value'], | 
					
						
							|  |  |  |         "locname[~]" => $VARS['search']['value'], | 
					
						
							|  |  |  |         "code1[~]" => $VARS['search']['value'], | 
					
						
							| 
									
										
										
										
											2018-04-10 01:25:07 -06:00
										 |  |  |         "code2[~]" => $VARS['search']['value'], | 
					
						
							|  |  |  |         "cost[~]" => $VARS['search']['value'], | 
					
						
							|  |  |  |         "price[~]" => $VARS['search']['value'], | 
					
						
							| 
									
										
										
										
											2017-05-27 20:02:27 -06:00
										 |  |  |     ]; | 
					
						
							|  |  |  |     $where = $wherenolimit; | 
					
						
							|  |  |  |     $where["LIMIT"] = [$VARS['start'], $VARS['length']]; | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  |     $where = ["LIMIT" => [$VARS['start'], $VARS['length']]]; | 
					
						
							| 
									
										
										
										
											2017-09-05 22:35:35 -06:00
										 |  |  |     if ($showwant) { | 
					
						
							|  |  |  |         $where["AND"] = ["qty[<]want", "want[>]" => 0]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-05-27 20:02:27 -06:00
										 |  |  | } | 
					
						
							|  |  |  | if (!is_null($order)) { | 
					
						
							|  |  |  |     $where["ORDER"] = $order; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $items = $database->select('items', [ | 
					
						
							|  |  |  |     '[>]categories' => ['catid' => 'catid'], | 
					
						
							|  |  |  |     '[>]locations' => ['locid' => 'locid'] | 
					
						
							|  |  |  |         ], [ | 
					
						
							|  |  |  |     'itemid', | 
					
						
							|  |  |  |     'name', | 
					
						
							|  |  |  |     'catname', | 
					
						
							|  |  |  |     'locname', | 
					
						
							|  |  |  |     'loccode', | 
					
						
							|  |  |  |     'code1', | 
					
						
							|  |  |  |     'code2', | 
					
						
							|  |  |  |     'qty', | 
					
						
							| 
									
										
										
										
											2017-09-05 22:35:35 -06:00
										 |  |  |     'want', | 
					
						
							| 
									
										
										
										
											2017-05-27 20:02:27 -06:00
										 |  |  |     'userid' | 
					
						
							|  |  |  |         ], $where); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $out['status'] = "OK"; | 
					
						
							|  |  |  | if ($filter) { | 
					
						
							|  |  |  |     $recordsFiltered = $database->count('items', [ | 
					
						
							|  |  |  |         '[>]categories' => ['catid' => 'catid'], | 
					
						
							|  |  |  |         '[>]locations' => ['locid' => 'locid'] | 
					
						
							|  |  |  |             ], 'itemid', $wherenolimit); | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  |     $recordsFiltered = $out['recordsTotal']; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | $out['recordsFiltered'] = $recordsFiltered; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $usercache = []; | 
					
						
							|  |  |  | for ($i = 0; $i < count($items); $i++) { | 
					
						
							| 
									
										
										
										
											2018-09-07 18:29:25 -06:00
										 |  |  |     $items[$i]["editbtn"] = '<a class="btn btn-primary" href="app.php?page=edititem&id=' . $items[$i]['itemid'] . '"><i class="fas fa-edit"></i> ' . $Strings->get("edit", false) . '</a>'; | 
					
						
							|  |  |  |     $items[$i]["clonebtn"] = '<a class="btn btn-success" href="app.php?page=edititem&id=' . $items[$i]['itemid'] . '&clone=1"><i class="fas fa-clone"></i> ' . $Strings->get("clone", false) . '</a>'; | 
					
						
							| 
									
										
										
										
											2017-05-27 20:02:27 -06:00
										 |  |  |     if (is_null($items[$i]['userid'])) { | 
					
						
							|  |  |  |         $items[$i]["username"] = ""; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         if (!isset($usercache[$items[$i]['userid']])) { | 
					
						
							| 
									
										
										
										
											2018-09-07 18:29:25 -06:00
										 |  |  |             $usercache[$items[$i]['userid']] = new User($items[$i]['userid']); | 
					
						
							| 
									
										
										
										
											2017-05-27 20:02:27 -06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-07 18:29:25 -06:00
										 |  |  |         $items[$i]["username"] = $usercache[$items[$i]['userid']]->getName() . " (" . $usercache[$items[$i]['userid']]->getUsername() . ")"; | 
					
						
							| 
									
										
										
										
											2017-05-27 20:02:27 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | $out['items'] = $items; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | echo json_encode($out); |