Moved some free functions into the Page class.
Updated all uses of 'action' to 'page' in GET parameters and related logic. Added a ProductPage subclass. Minor tweaks.
This commit is contained in:
parent
8f570392ff
commit
3e1213976b
229
include/view.php
229
include/view.php
@ -4,22 +4,22 @@ require_once('./include/db.php');
|
||||
require_once('./include/ldap.php');
|
||||
require_once('./include/functions.php');
|
||||
|
||||
$fragments = get_fragments('./html/fragments.html');
|
||||
|
||||
function make_page($action) {
|
||||
switch($action) {
|
||||
function make_page($page) {
|
||||
switch($page) {
|
||||
case 'home':
|
||||
default:
|
||||
return new StartPage();
|
||||
case 'search':
|
||||
return new SearchPage();
|
||||
case 'products':
|
||||
return new ProductPage();
|
||||
}
|
||||
}
|
||||
|
||||
abstract class Page {
|
||||
protected abstract function render_body();
|
||||
|
||||
protected $action = 'home';
|
||||
protected $page = 'home';
|
||||
protected $title = "Boka2";
|
||||
protected $subtitle = '';
|
||||
protected $menuitems = array('home' => 'Start',
|
||||
@ -27,14 +27,17 @@ abstract class Page {
|
||||
'users' => 'Användare',
|
||||
'checkout' => 'Låna ut');
|
||||
private $template_parts = array();
|
||||
protected $fragments = array();
|
||||
|
||||
public function __construct() {
|
||||
$this->template_parts = get_fragments('./html/base.html');
|
||||
if(isset($_GET['action'])) {
|
||||
$this->action = $_GET['action'];
|
||||
$this->fragments = get_fragments('./html/fragments.html');
|
||||
|
||||
if(isset($_GET['page'])) {
|
||||
$this->page = $_GET['page'];
|
||||
}
|
||||
if(isset($this->menuitems[$this->action])) {
|
||||
$this->subtitle = $this->menuitems[$this->action];
|
||||
if(isset($this->menuitems[$this->page])) {
|
||||
$this->subtitle = $this->menuitems[$this->page];
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,34 +48,35 @@ abstract class Page {
|
||||
}
|
||||
|
||||
final private function render_head() {
|
||||
global $fragments;
|
||||
|
||||
$headtitle = $this->title;
|
||||
$pagetitle = $this->title;
|
||||
if($this->subtitle) {
|
||||
$headtitle .= ' - '. $this->subtitle;
|
||||
$pagetitle = $this->subtitle;
|
||||
}
|
||||
$query = '';
|
||||
if(isset($_GET['q'])) {
|
||||
$query = $_GET['q'];
|
||||
}
|
||||
print(replace(
|
||||
array('title' => $headtitle,
|
||||
'menu' => $this->build_menu()),
|
||||
'menu' => $this->build_menu(),
|
||||
'query'=> $query),
|
||||
$this->template_parts['head']
|
||||
));
|
||||
|
||||
print(replace(array('title' => $pagetitle),
|
||||
$fragments['title']));
|
||||
|
||||
$this->fragments['title']));
|
||||
}
|
||||
|
||||
private function build_menu() {
|
||||
$menu = '';
|
||||
foreach($this->menuitems as $action => $title) {
|
||||
foreach($this->menuitems as $page => $title) {
|
||||
$active = '';
|
||||
if($this->action == $action) {
|
||||
if($this->page == $page) {
|
||||
$active = 'active';
|
||||
}
|
||||
$menu .= replace(array('title' => $title,
|
||||
'action' => $action,
|
||||
'page' => $page,
|
||||
'active' => $active),
|
||||
$this->template_parts['menuitem']);
|
||||
}
|
||||
@ -82,6 +86,67 @@ abstract class Page {
|
||||
final private function render_foot() {
|
||||
print($this->template_parts['foot']);
|
||||
}
|
||||
|
||||
final protected function build_user_table($users) {
|
||||
$rows = '';
|
||||
$replacements = array('id' => 0,
|
||||
'name' => '',
|
||||
'displayname' => '',
|
||||
'loan' => '');
|
||||
foreach($users as $user) {
|
||||
$replacements['id'] = $user->get_id();
|
||||
$replacements['name'] = $user->get_name();
|
||||
$replacements['displayname'] = $user->get_displayname();
|
||||
$loans = $user->get_loans('active');
|
||||
$loan_str = '';
|
||||
$count = count($loans);
|
||||
switch($count) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
$product = new Product($loans[0]->get_product());
|
||||
$loan_str = $product->get_name();
|
||||
break;
|
||||
default:
|
||||
$loan_str = $count .' produkter';
|
||||
break;
|
||||
}
|
||||
$replacements['loan'] = $loan_str;
|
||||
$rows .= replace($replacements, $this->fragments['user_row']);
|
||||
}
|
||||
return replace(array('title' => 'Användare',
|
||||
'rows' => $rows),
|
||||
$this->fragments['user_table']);
|
||||
}
|
||||
|
||||
final protected function build_product_table($products) {
|
||||
$rows = '';
|
||||
$replacements = array('id' => 0,
|
||||
'name' => '',
|
||||
'available' => '');
|
||||
foreach($products as $product) {
|
||||
$replacements['id'] = $product->get_id();
|
||||
$replacements['name'] = $product->get_name();
|
||||
$available = '';
|
||||
$loan = $product->get_active_loan();
|
||||
if($loan) {
|
||||
if($loan->is_overdue()) {
|
||||
$available = 'Försenad';
|
||||
} else {
|
||||
$end = date('Y/m/d', $loan->get_duration()['end']);
|
||||
$available = 'Utlånad till '.$end;
|
||||
}
|
||||
} else {
|
||||
$available = 'Tillgänglig';
|
||||
}
|
||||
$replacements['available'] = $available;
|
||||
$rows .= replace($replacements, $this->fragments['product_row']);
|
||||
}
|
||||
return replace(array('title' => 'Produkter',
|
||||
'rows' => $rows),
|
||||
$this->fragments['product_table']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class StartPage extends Page {
|
||||
@ -144,16 +209,16 @@ class SearchPage extends Page {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->subtitle = 'Sökresultat';
|
||||
$this->subtitle = 'Sökresultat för ';
|
||||
if(isset($_GET['q'])) {
|
||||
$this->query = $_GET['q'];
|
||||
$this->subtitle .= "'$this->query'";
|
||||
}
|
||||
}
|
||||
|
||||
private function do_search() {
|
||||
$out = array('users' => array(),
|
||||
'products' => array(),
|
||||
'loans' => array());
|
||||
'products' => array());
|
||||
if(!$this->query) {
|
||||
return $out;
|
||||
}
|
||||
@ -164,75 +229,87 @@ class SearchPage extends Page {
|
||||
|
||||
protected function render_body() {
|
||||
$hits = $this->do_search();
|
||||
$nohits = true;
|
||||
if($hits['users']) {
|
||||
print(build_user_table($hits['users']));
|
||||
print($this->build_user_table($hits['users']));
|
||||
$nohits = false;
|
||||
}
|
||||
if($hits['products']) {
|
||||
print(build_product_table($hits['products']));
|
||||
print($this->build_product_table($hits['products']));
|
||||
$nohits = false;
|
||||
}
|
||||
if($nohits) {
|
||||
print('Inga träffar.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function build_user_table($users) {
|
||||
global $fragments;
|
||||
class ProductPage extends Page {
|
||||
private $action = 'list';
|
||||
private $product = null;
|
||||
|
||||
$rows = '';
|
||||
$replacements = array('id' => 0,
|
||||
'name' => '',
|
||||
'displayname' => '',
|
||||
'loan' => '');
|
||||
foreach($users as $user) {
|
||||
$replacements['id'] = $user->get_id();
|
||||
$replacements['name'] = $user->get_name();
|
||||
$replacements['displayname'] = $user->get_displayname();
|
||||
$loans = $user->get_loans('active');
|
||||
$loan_str = '';
|
||||
$count = count($loans);
|
||||
switch($count) {
|
||||
case 0:
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
if(isset($_GET['action'])) {
|
||||
$this->action = $_GET['action'];
|
||||
}
|
||||
if(isset($_GET['id'])) {
|
||||
$this->product = new Product($_GET['id']);
|
||||
}
|
||||
switch($this->action) {
|
||||
case 'show':
|
||||
$this->subtitle = 'Produktdetaljer';
|
||||
break;
|
||||
case 1:
|
||||
$product = new Product($loans[0]->get_product());
|
||||
$loan_str = $product->get_name();
|
||||
break;
|
||||
default:
|
||||
$loan_str = $count .' produkter';
|
||||
case 'new':
|
||||
$this->subtitle = 'Ny produkt';
|
||||
break;
|
||||
}
|
||||
$replacements['loan'] = $loan_str;
|
||||
$rows .= replace($replacements, $fragments['user_row']);
|
||||
}
|
||||
return replace(array('title' => 'Användare',
|
||||
'rows' => $rows),
|
||||
$fragments['user_table']);
|
||||
}
|
||||
|
||||
function build_product_table($products) {
|
||||
global $fragments;
|
||||
|
||||
$rows = '';
|
||||
$replacements = array('id' => 0,
|
||||
'name' => '',
|
||||
'available' => '');
|
||||
foreach($products as $product) {
|
||||
$replacements['id'] = $product->get_id();
|
||||
$replacements['name'] = $product->get_name();
|
||||
$available = '';
|
||||
$loan = $product->get_active_loan();
|
||||
if($loan) {
|
||||
if($loan->is_overdue()) {
|
||||
$available = 'Försenad';
|
||||
} else {
|
||||
$end = date('Y/m/d', $loan->get_duration()['end']);
|
||||
$available = 'Utlånad till '.$end;
|
||||
}
|
||||
|
||||
protected function render_body() {
|
||||
switch($this->action) {
|
||||
case 'list':
|
||||
print($this->fragments['create_product']);
|
||||
print($this->build_product_table(get_items('product')));
|
||||
break;
|
||||
case 'show':
|
||||
print($this->build_product_details());
|
||||
break;
|
||||
case 'new':
|
||||
print($this->build_new_page());
|
||||
break;
|
||||
}
|
||||
$replacements['available'] = $available;
|
||||
$rows .= replace($replacements, $fragments['product_row']);
|
||||
}
|
||||
return replace(array('title' => 'Produkter',
|
||||
'rows' => $rows),
|
||||
$fragments['product_table']);
|
||||
|
||||
private function build_product_details() {
|
||||
$info = '';
|
||||
foreach($this->product->get_info() as $key => $value) {
|
||||
$info .= replace(array('key' => $key,
|
||||
'value' => $value
|
||||
), $this->fragments['info_item']);
|
||||
}
|
||||
$tags = '';
|
||||
foreach($this->product->get_tags() as $tag) {
|
||||
$tags .= replace(array('tag' => $tag), $this->fragments['tag']);
|
||||
}
|
||||
return replace(array('id' => $this->product->get_id(),
|
||||
'name' => $this->product->get_name(),
|
||||
'serial' => $this->product->get_serial(),
|
||||
'invoice' => $this->product->get_invoice(),
|
||||
'tags' => $tags,
|
||||
'info' => $info
|
||||
), $this->fragments['product_details']);
|
||||
}
|
||||
|
||||
private function build_new_page() {
|
||||
return replace(array('id' => '',
|
||||
'name' => '',
|
||||
'serial' => '',
|
||||
'invoice' => '',
|
||||
'tags' => '',
|
||||
'info' => ''
|
||||
), $this->fragments['product_details']);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
Loading…
x
Reference in New Issue
Block a user