action = $_GET['action']; } } public function render() { $out = ''; switch($this->action) { default: $out = new Success('ajax endpoint'); break; case 'getfragment': $out = $this->get_fragment(); break; case 'checkout': $out = $this->checkout_product(); break; case 'return': $out = $this->return_product(); break; case 'extend': $out = $this->extend_loan(); break; case 'startinventory': $out = $this->start_inventory(); break; case 'endinventory': $out = $this->end_inventory(); break; case 'inventoryproduct': $out = $this->inventory_product(); break; case 'updateproduct': $out = $this->update_product(); break; case 'updateuser': $out = $this->update_user(); break; case 'savetemplate': $out = $this->save_template(); break; case 'deletetemplate': $out = $this->delete_template(); break; case 'suggest': $out = $this->suggest(); break; case 'suggestcontent': $out = $this->suggest_content(); break; case 'discardproduct': $out = $this->discard_product(); break; case 'toggleservice': $out = $this->toggle_service(); break; case 'addattachment': $out = $this->add_attachment(); break; case 'deleteattachment': $out = $this->delete_attachment(); break; } print($out->toJson()); } private function get_fragment() { $fragment = $_POST['fragment']; if(isset($this->fragments[$fragment])) { return new Success($this->fragments[$fragment]); } return new Failure(i18n('Invalid fragment {fragment}', $fragment)); } private function checkout_product() { $user = null; try { $user = new User($_POST['user'], 'name'); } catch(Exception $e) { return new Failure(i18n('Invalid user ID.')); } $product = null; try { $product = new Product(trim($_POST['product']), 'serial'); } catch(Exception $e) { return new Failure(i18n('Invalid serial number.')); } try { Loan::create_loan($user, $product, $_POST['end']); return new Success(i18n('{product} checked out.', $product)); } catch(Exception $e) { return new Failure(i18n('The product is already on loan.')); } } private function return_product() { $product = null; try { $product = new Product(trim($_POST['serial']), 'serial'); } catch(Exception $e) { return new Failure(i18n('Invalid serial number.')); } $loan = $product->get_active_loan(); if($loan) { $loan->end(); $user = $loan->get_user(); $userlink = replace(array('page' => 'users', 'id' => $user->get_id(), 'name' => $user->get_displayname($this->ldap)), $this->fragments['item_link']); $productlink = replace(array('page' => 'products', 'id' => $product->get_id(), 'name' => $product->get_name()), $this->fragments['item_link']); $user = $loan->get_user(); return new Success(i18n('{product} returned from {user}', $productlink, $userlink)); } return new Failure(i18n('The product is not on loan.')); } private function extend_loan() { $product = null; try { $product = new Product($_POST['product']); } catch(Exception $e) { return new Failure(i18n('Invalid serial number.')); } $loan = $product->get_active_loan(); if($loan) { $loan->extend($_POST['end']); return new Success(i18n('Loan extended.')); } return new Failure(i18n('The product is not on loan.')); } private function start_inventory() { try { Inventory::begin(); return new Success(i18n('Inventory started.')); } catch(Exception $e) { return new Failure(i18n('Inventory already in progress.')); } } private function end_inventory() { $inventory = Inventory::get_active(); if($inventory === null) { return new Failure(i18n('No inventory in progress.')); } $inventory->end(); return new Success(i18n('Inventory ended.')); } private function inventory_product() { $inventory = Inventory::get_active(); if($inventory === null) { return new Failure(i18n('No inventory in progress.')); } $product = null; try { $product = new Product(trim($_POST['serial']), 'serial'); } catch(Exception $e) { return new Failure(i18n('Invalid serial number.')); } $result = $inventory->add_product($product); if(!$result) { return new Failure(i18n('Product already registered.')); } return new Success(i18n('Product registered.')); } private function update_product() { $info = $_POST; $id = $info['id']; $name = $info['name']; $brand = $info['brand']; $serial = $info['serial']; $invoice = $info['invoice']; $tags = array(); if(isset($info['tag'])) { $tags = $this->unescape_tags($info['tag']); } foreach(array('id', 'name', 'brand', 'serial', 'invoice', 'tag') as $key) { unset($info[$key]); } if(!$name) { return new Failure(i18n('The product must have a name.')); } if(!$serial) { return new Failure( i18n('The product must have a serial number.')); } if(!$invoice) { return new Failure( i18n('The product must have an invoice number.')); } $product = null; if(!$id) { try { $temp = new Product($serial, 'serial'); return new Failure(i18n('The given serial number is taken.')); } catch(Exception $e) {} try { $product = Product::create_product($brand, $name, $invoice, $serial, $info, $tags); $prodlink = replace(array('page' => 'products', 'id' => $product->get_id(), 'name' => $product->get_name()), $this->fragments['item_link']); return new Success(i18n("Product {link} saved.", $prodlink)); } catch(Exception $e) { return new Failure($e->getMessage()); } } $product = new Product($id); if($product->get_discardtime()) { return new Failure( i18n('Discarded products may not be modified.')); } if($serial != $product->get_serial()) { try { $product->set_serial($serial); } catch(Exception $e) { return new Failure(i18n('The given serial number is taken.')); } } if($brand != $product->get_brand()) { $product->set_brand($brand); } if($name != $product->get_name()) { $product->set_name($name); } if($invoice != $product->get_invoice()) { $product->set_invoice($invoice); } foreach($product->get_info() as $key => $prodvalue) { if(!isset($info[$key]) || !$info[$key]) { $product->remove_info($key); continue; } if($prodvalue != $info[$key]) { $product->set_info($key, $info[$key]); } unset($info[$key]); } foreach($info as $key => $invalue) { if($invalue) { $product->set_info($key, $invalue); } } foreach($product->get_tags() as $tag) { if(!in_array($tag, $tags)) { $product->remove_tag($tag); continue; } unset($tags[array_search($tag, $tags)]); } foreach($tags as $tag) { $product->add_tag($tag); } return new Success(i18n('Changes saved.')); } private function update_user() { $id = $_POST['id']; $name = $_POST['name']; $notes = $_POST['notes']; if(!$name) { return new Failure(i18n('Username may not be empty.')); } $user = new User($id); if($user->get_name() != $name) { $user->set_name($name); } if($user->get_notes() != $notes) { $user->set_notes($notes); } return new Success(i18n('Changes saved.')); } private function save_template() { $info = $_POST; $name = $info['template']; $tags = array(); if(isset($info['tag'])) { $tags = $this->unescape_tags($info['tag']); } foreach(array('template', 'id', 'name', 'serial', 'invoice', 'brand', 'tag') as $key) { unset($info[$key]); } if(!$name) { return new Failure(i18n('The template must have a name.')); } $template = null; try { $template = new Template($name, 'name'); } catch(Exception $e) { $template = Template::create_template($name, $info, $tags); return new Success(i18n("Template {name} saved.", $template->get_name())); } foreach($template->get_fields() as $field) { if(!isset($info[$field])) { $template->remove_field($field); } } $existingfields = $template->get_fields(); foreach($info as $field) { if(!in_array($field, $existingfields)) { $template->add_field($field); } } foreach($template->get_tags() as $tag) { if(!in_array($tag, $tags)) { $template->remove_tag($tag); } } $existingtags = $template->get_tags(); foreach($tags as $tag) { if(!in_array($tag, $existingtags)) { $template->add_tag($tag); } } return new Success(i18n("Template {name} updated.", $template->get_name())); } private function delete_template() { try { $request_name = $_POST['template']; $template = new Template($request_name, 'name'); $template_name = $template->get_name(); Template::delete_template($template_name); return new Success(i18n("Template {name} deleted.", $template_name)); } catch(Exception $e) { return new Failure(i18n('There is no template by that name.')); } } private function suggest() { return new Success(suggest($_POST['type'])); } private function suggest_content() { return new Success(suggest_content($_POST['fieldname'])); } private function discard_product() { $product = new Product($_POST['id']); if(!$product->get_discardtime()) { if($product->get_active_loan()) { return new Failure(i18n('Product on loan.')); } $product->discard(); return new Success(i18n('Product scrapped.')); } else { return new Failure(i18n('Product has been scrapped already.')); } } private function toggle_service() { $product = new Product($_POST['id']); try { $product->toggle_service(); return new Success(i18n('Product service status updated.')); } catch(Exception $e) { return new Failure(i18n('Cannot register service.')); } } private function add_attachment() { try { $product = new Product($_POST['id']); $uploadfile = $_FILES['uploadfile']; $attach = Attachment::create($uploadfile, $product->get_id()); $date = format_date($attach->get_uploadtime()); $fragment = replace(array('name' => $attach->get_filename(), 'id' => $attach->get_id(), 'date' => $date), $this->fragments['attachment']); return new Success($fragment); } catch(Exception $e) { return new Failure($e->getMessage()); } } private function delete_attachment() { $attach = new Attachment($_POST['id']); try { $attach->delete(); return new Success(''); } catch(Exception $e) { return new Failure($e->getMessage()); } } } ?>