- Changed the location field of products to a serial field in accordance with the db model
 - removed unnecessary if clauses
 - significantly expanded available functions
This commit is contained in:
Erik Thuning 2018-07-11 15:17:00 +02:00
parent 49fb4935cc
commit 60f04041c2

@ -1,11 +1,62 @@
<?php
require_once('./include/sql.php');
function get_ids($type) {
switch($type) {
case 'user':
case 'product':
case 'loan':
break;
default:
$err = "$type is not a valid argument. Valid arguments are user, product, loan.";
throw new Exception($err);
break;
}
$get = prepare("select `id` from `$type`");
execute($get);
$ids = array();
foreach(result_list($get) as $row) {
$ids[] = $row['id'];
}
return $ids;
}
function create_product(
$name = '',
$invoice = '',
$serial = '',
$info = array(),
$tags = array()
) {
$ins_prod = prepare('insert into `product`(`name`, `invoice`, `serial`) values (?, ?, ?)');
bind($ins_prod, 'sss', $name, $invoice, $serial);
execute($ins_prod);
$prodid = $ins_prod->insert_id;
$ins_info = prepare('insert into `product_info`(`product`, `field`, `data`) values (?, ?, ?)');
bind($ins_info, 'iss', $prodid, $key, $value);
foreach($ins_info as $key => $value) {
execute($ins_info);
}
$ins_tag = prepare('insert into `product_tag`(`product`, `tag`) values (?, ?)');
bind($ins_tag, 'is', $prodid, $tag);
foreach($tags as $tag) {
execute($ins_tag);
}
return new Product($prodid);
}
function create_user($name = '') {
$ins_user = prepare('insert into `user`(`name`) values (?)');
bind($ins_user, 's', $name);
execute($ins_user);
return new User($ins_user->insert_id);
}
class Product {
private $id = 0;
private $name = '';
private $invoice = '';
private $location = '';
private $serial = '';
private $info = array();
private $tags = array();
@ -23,7 +74,7 @@ class Product {
$product = result_single($get);
$this->name = $product['name'];
$this->invoice = $product['invoice'];
$this->location = $product['location'];
$this->serial = $product['serial'];
return true;
}
@ -62,11 +113,9 @@ class Product {
public function set_name($newname) {
$update = prepare('update `product` set `name`=? where `id`=?');
bind($update, 'si', $newname, $this->id);
if(execute($update)) {
$this->name = $newname;
return true;
}
return false;
execute($update);
$this->name = $newname;
return true;
}
public function get_invoice() {
@ -76,25 +125,21 @@ class Product {
public function set_invoice($newinvoice) {
$update = prepare('update `product` set `invoice`=? where `id`=?');
bind($update, 'si', $newinvoice, $this->id);
if(execute($update)) {
$this->invoice = $newinvoice;
return true;
}
return false;
execute($update);
$this->invoice = $newinvoice;
return true;
}
public function get_location() {
return $this->location;
public function get_serial() {
return $this->serial;
}
public function set_location($newlocation) {
$update = prepare('update `product` set `location`=? where `id`=?');
bind($update, 'si', $newlocation, $this->id);
if(execute($update)) {
$this->location = $newlocation;
return true;
}
return false;
public function set_serial($newserial) {
$update = prepare('update `product` set `serial`=? where `id`=?');
bind($update, 'si', $newserial, $this->id);
execute($update);
$this->serial = $newserial;
return true;
}
public function get_info() {
@ -111,11 +156,9 @@ class Product {
$update = prepare('update `product_info` set `data`=? where `product`=? and `field`=?');
}
bind($update, 'sis', $value, $this->id, $field);
if(execute($update)) {
$this->update_info();
return true;
}
return false;
execute($update);
$this->update_info();
return true;
}
public function remove_info($field) {
@ -127,11 +170,9 @@ class Product {
}
$update = prepare('delete from `product_info` where `field`=? and `product`=?');
bind($update, 'si', $field, $this->id);
if(execute($update)) {
$this->update_info();
return true;
}
return false;
execute($update);
$this->update_info();
return true;
}
public function get_tags() {
@ -148,11 +189,9 @@ class Product {
$update = prepare('update `product_tag` set `tag`=? where `product`=?');
}
bind($update, 'si', $tag, $this->id);
if(execute($update)) {
$this->update_tags();
return true;
}
return false;
execute($update);
$this->update_tags();
return true;
}
public function remove_tag($tag) {
@ -164,8 +203,16 @@ class Product {
}
$update = prepare('delete from `product_tag` where `tag`=? and `product`=?');
bind($update, 'si', $tag, $this->id);
if(execute($update)) {
$this->update_tags();
execute($update);
$this->update_tags();
return true;
}
public function is_available() {
$find = prepare('select `user` from `loan` where `active`=1 and product=?');
bind($find, 'i', $this->id);
execute($find);
if(result_single($find) === null) {
return true;
}
return false;
@ -199,13 +246,11 @@ class User {
public function set_name($newname) {
$update = prepare('update `user` set `name`=? where `id`=?');
bind($update, 'si', $newname, $this->id);
if(execute($update)) {
$this->name = $newname;
return true;
}
return false;
execute($update);
$this->name = $newname;
return true;
}
public function get_notes() {
return $this->notes;
}
@ -213,11 +258,53 @@ class User {
public function set_notes($newnotes) {
$update = prepare('update `user` set `notes`=? where `id`=?');
bind($update, 'si', $newnotes, $this->id);
if(execute($update)) {
$this->notes = $newnotes;
return true;
execute($update);
$this->notes = $newnotes;
return true;
}
public function get_loans($type = 'both') {
$statement = 'select `id` from `loan` where `user`=?';
switch($type) {
case 'active':
$statement .= ' and `active`=1';
break;
case 'inactive':
$statement .= ' and `active`=0';
break;
case 'both':
break;
default:
$err = "$type is not a valid argument. Valid arguments are active, inactive, both.";
throw new Exception($err);
break;
}
return false;
$get = prepare($statement);
bind($get, 'i', $this->id);
execute($get);
$loans = array();
foreach(result_list($get) as $row) {
$loans[] = new Loan($row['id']);
}
return $loans;
}
public function create_loan($product, $endtime) {
$find = prepare('select * from `loan` where `product`=? and `active`=1');
$prod_id = $product->get_id();
bind($find, 'i', $prod_id);
execute($find);
$loan = result_single($find);
if($loan !== null) {
$loan_id = $loan['id'];
throw new Exception("Product $prod_id has an active loan (id $loan_id) already.");
}
$now = time();
$insert = prepare('insert into `loan`(`user`, `product`, `starttime`, `endtime`) values (?, ?, ?, ?)');
bind($insert, 'iiii', $this->id, $prod_id, $now, $endtime);
execute($insert);
$loan_id = $statement->insert_id;
return new Loan($id);
}
}
@ -227,12 +314,11 @@ class Loan {
private $product = 0;
private $starttime = 0;
private $endtime = 0;
private $active = true;
private $active = 1;
public function __construct($id) {
$this->id = $id;
$this->update_fields();
$this->update_active();
}
private function update_fields() {
@ -240,17 +326,48 @@ class Loan {
bind($get, 'i', $this->id);
execute($get);
$loan = result_single($get);
$this->user = $get['user'];
$this->product = $get['product'];
$this->starttime = get['starttime'];
$this->endtime = get['endtime'];
$this->user = $loan['user'];
$this->product = $loan['product'];
$this->starttime = $loan['starttime'];
$this->endtime = $loan['endtime'];
$this->active = $loan['active'];
}
public function get_id() {
return $this->id;
}
public function get_user() {
return $this->user;
}
public function get_product() {
return $this->product;
}
public function get_duration() {
return array('start' => $this->starttime,
'end' => $this->endtime);
}
public function is_active() {
return $this->active;
}
public function end_loan() {
$end = prepare('update `loan` set `active`=0 where `id`=?');
bind($end, 'i', $this->id);
if(execute($end)) {
$this->active = false;
execute($end);
$this->active = false;
return true;
}
public function is_overdue() {
if($this->active === 0) {
return false;
}
$now = time();
if($now > $this->endtime) {
return true;
}
return false;
@ -288,11 +405,9 @@ class Kvs {
$update = prepare('update `kvs` set `value`=? where `key`=?');
}
bind($update, 'ss', $value, $key);
if(execute($update)) {
$this->items[$key] = $value;
return true;
}
return false;
execute($update);
$this->items[$key] = $value;
return true;
}
public function remove_key($key) {
@ -304,11 +419,9 @@ class Kvs {
}
$update = prepare('delete from `kvs` where `key`=?');
bind($update, 's', $key);
if(execute($update)) {
unset($this->items[$key]);
return true;
}
return false;
execute($update);
unset($this->items[$key]);
return true;
}
}