Reverted the last two commits; back to eager product init.

Made product creation a separate page and added a menu item for it
This commit is contained in:
Erik Thuning 2020-01-20 14:47:50 +01:00
parent 2e7ac9fe71
commit 9d346296bc
6 changed files with 80 additions and 101 deletions

@ -64,20 +64,7 @@
¤¤ product_page ¤¤
<div id="product-table">
<form id="product-create"
action="./"
method="get">
<input type="hidden"
name="page"
value="products" />
<input type="hidden"
name="action"
value="new" />
<button>
Ny artikel
</button>
¤product_table¤
</form>
</div>
¤¤ product_table ¤¤
@ -125,9 +112,6 @@
<datalist id="template_suggest"></datalist>
<input type="hidden"
name="page"
value="products" />
<input type="hidden"
name="action"
value="new" />
<input onFocus="JavaScript:suggest(this, 'template')"
list="template_suggest"

56
include/NewPage.php Normal file

@ -0,0 +1,56 @@
<?php
class NewPage extends Page {
private $template = null;
public function __construct() {
parent::__construct();
if(isset($_GET['template'])) {
$template = $_GET['template'];
if($template) {
try {
$this->template = new Template($template, 'name');
} catch(Exception $e) {
$this->template = null;
$this->error = 'Det finns ingen mall med det namnet.';
}
}
}
}
protected function render_body() {
print($this->build_new_page());
}
private function build_new_page() {
$template = '';
$fields = '';
$tags = '';
if($this->template) {
$template = $this->template->get_name();
foreach($this->template->get_fields() as $field) {
$fields .= replace(array('name' => ucfirst($field),
'key' => $field,
'value' => ''),
$this->fragments['info_item']);
}
foreach($this->template->get_tags() as $tag) {
$tags .= replace(array('tag' => ucfirst($tag)),
$this->fragments['tag']);
}
}
$out = replace(array('template' => $template),
$this->fragments['template_management']);
$out .= replace(array('id' => '',
'name' => '',
'brand' => '',
'serial' => '',
'invoice' => '',
'tags' => $tags,
'info' => $fields,
'label' => '',
'hidden' => 'hidden'),
$this->fragments['product_details']);
return $out;
}
}
?>

@ -9,6 +9,7 @@ abstract class Page extends Responder {
protected $menuitems = array('checkout' => 'Låna',
'return' => 'Lämna',
'products' => 'Artiklar',
'new' => 'Ny artikel',
'users' => 'Låntagare',
'inventory' => 'Inventera',
'history' => 'Historik',
@ -167,7 +168,7 @@ abstract class Page extends Responder {
$rows .= replace(array('status' => $status,
'item_link' => $prodlink,
'serial' => $product->get_serial(),
'note' => $note,),
'note' => $note),
$this->fragments['product_row']);
}
return replace(array('rows' => $rows),

@ -1,22 +1,22 @@
<?php
class Product {
private $id;
private $brand;
private $name;
private $invoice;
private $serial;
private $createtime;
private $discardtime;
private $info;
private $tags;
private $id = 0;
private $brand = '';
private $name = '';
private $invoice = '';
private $serial = '';
private $createtime = null;
private $discardtime = null;
private $info = array();
private $tags = array();
public static function create_product(
$brand,
$name,
$invoice,
$serial,
$info,
$tags
$info = array(),
$tags = array()
) {
$now = time();
begin_trans();
@ -28,15 +28,11 @@ class Product {
bind($ins_prod, 'ssssi', $brand, $name, $invoice, $serial, $now);
execute($ins_prod);
$product = new Product($serial, 'serial');
if(is_array($info)) {
foreach($info as $field => $value) {
$product->set_info($field, $value);
}
foreach($info as $field => $value) {
$product->set_info($field, $value);
}
if(is_array($tags)) {
foreach($tags as $tag) {
$product->add_tag($tag);
}
foreach($tags as $tag) {
$product->add_tag($tag);
}
commit_trans();
return $product;
@ -50,12 +46,12 @@ class Product {
$search = null;
switch($type) {
case 'id':
$search = prepare('select * from `product`
$search = prepare('select `id` from `product`
where `id`=?');
bind($search, 'i', $clue);
break;
case 'serial':
$search = prepare('select * from `product`
$search = prepare('select `id` from `product`
where `serial`=?');
bind($search, 's', $clue);
break;
@ -68,13 +64,9 @@ class Product {
throw new Exception('Product does not exist.');
}
$this->id = $result['id'];
$this->brand = $result['brand'];
$this->name = $result['name'];
$this->invoice = $result['invoice'];
$this->serial = $result['serial'];
#$this->update_fields();
#$this->update_info();
#$this->update_tags();
$this->update_fields();
$this->update_info();
$this->update_tags();
}
private function update_fields() {
@ -250,9 +242,6 @@ class Product {
}
public function get_info() {
if($this->info === null) {
$this->update_info();
}
return $this->info;
}
@ -295,9 +284,6 @@ class Product {
}
public function get_tags() {
if($this->tags === null) {
$this->update_tags();
}
return $this->tags;
}

@ -1,7 +1,6 @@
<?php
class ProductPage extends Page {
private $action = 'list';
private $template = null;
private $product = null;
public function __construct() {
@ -9,17 +8,6 @@ class ProductPage extends Page {
if(isset($_GET['action'])) {
$this->action = $_GET['action'];
}
if(isset($_GET['template'])) {
$template = $_GET['template'];
if($template) {
try {
$this->template = new Template($template, 'name');
} catch(Exception $e) {
$this->template = null;
$this->error = 'Det finns ingen mall med det namnet.';
}
}
}
if(isset($_GET['id'])) {
$id = $_GET['id'];
if($id) {
@ -36,9 +24,6 @@ class ProductPage extends Page {
case 'show':
$this->subtitle = 'Artikeldetaljer';
break;
case 'new':
$this->subtitle = 'Ny artikel';
break;
case 'list':
$this->subtitle = 'Artikellista';
break;
@ -55,9 +40,6 @@ class ProductPage extends Page {
case 'show':
print($this->build_product_details());
break;
case 'new':
print($this->build_new_page());
break;
}
}
@ -156,37 +138,5 @@ class ProductPage extends Page {
return replace(array('attachments' => $items),
$this->fragments['attachment_list']);
}
private function build_new_page() {
$template = '';
$fields = '';
$tags = '';
if($this->template) {
$template = $this->template->get_name();
foreach($this->template->get_fields() as $field) {
$fields .= replace(array('name' => ucfirst($field),
'key' => $field,
'value' => ''),
$this->fragments['info_item']);
}
foreach($this->template->get_tags() as $tag) {
$tags .= replace(array('tag' => ucfirst($tag)),
$this->fragments['tag']);
}
}
$out = replace(array('template' => $template),
$this->fragments['template_management']);
$out .= replace(array('id' => '',
'name' => '',
'brand' => '',
'serial' => '',
'invoice' => '',
'tags' => $tags,
'info' => $fields,
'label' => '',
'hidden' => 'hidden'),
$this->fragments['product_details']);
return $out;
}
}
?>

@ -85,6 +85,8 @@ function make_page($page) {
return new SearchPage();
case 'products':
return new ProductPage();
case 'new':
return new NewPage();
case 'users':
return new UserPage();
case 'inventory':