Starting to get the view bits in order

This commit is contained in:
Erik Thuning 2018-07-18 16:36:58 +02:00
parent e4faabc42e
commit 8b134e3183
2 changed files with 116 additions and 28 deletions

@ -1,22 +1,117 @@
<?php
require_once('./include/db.php');
require_once('./include/ldap.php');
require_once('./include/functions.php');
class StartPage {
public $title = '';
public $content = '';
private $menu = array();
public function get_contents() {
$menu_html = '';
foreach($this->menu as $item) {
$menu_html .= '<div class="menuitem">'.$item.'</div>';
function make_page($action) {
switch($action) {
case 'home':
default:
return new StartPage();
}
}
abstract class Page {
protected abstract function render_body();
protected $action = 'home';
protected $title = "Boka2";
protected $menuitems = array('home' => 'Start',
'products' => 'Produkter',
'users' => 'Användare',
'checkout' => 'Låna ut');
private $template_parts = array();
public function __construct() {
$this->template_parts = get_fragments('./html/base.html');
if(isset($_GET['action'])) {
$this->action = $_GET['action'];
}
return replace(array('¤pagetitle¤' => $this->title,
'¤contents¤' => $this->content,
'¤menu¤' => $menu_html),
file_get_contents('./html/base.html'));
}
public function render() {
$this->render_head();
$this->render_body();
$this->render_foot();
}
final private function render_head() {
print(replace(
array('¤title¤' => $this->title,
'¤menu¤' => $this->build_menu()),
$this->template_parts['head']
));
}
private function build_menu() {
$menu = '';
foreach($this->menuitems as $action => $title) {
$active = '';
if($this->action == $action) {
$active = 'active';
}
$menu .= replace(array('¤title¤' => $title,
'¤action¤' => $action,
'¤active¤' => $active),
$this->template_parts['menuitem']);
}
return $menu;
}
final private function render_foot() {
print($this->template_parts['foot']);
}
}
class StartPage extends Page {
protected function render_body() {
foreach(get_ids('user') as $userid) {
$user = new User($userid);
$users[] = $user;
}
foreach(get_ids('product') as $prodid) {
$product = new Product($prodid);
$products[] = $product;
}
$ldap = new Ldap();
foreach($users as $user) {
echo "User: ".$ldap->find_user($user->get_name())."<br/>";
foreach($user->get_loans() as $loan) {
$product = new Product($loan->get_product());
$active = $loan->is_active();
if($active) {
echo "Borrowed product: ".$product->get_name()."<br/>";
if($loan->is_overdue()) {
echo "Loan is overdue.";
} else {
$end = $loan->get_duration()['end'];
echo "Loan expires on $end.";
}
} else {
echo "Returned product: ".$product->get_name();
}
echo "<br/>";
}
echo "<br/>";
}
echo "<br/>";
foreach($products as $product) {
echo "Product name: ".$product->get_name()."<br/>";
echo "Available: ";
if($product->is_available()) {
echo "yes";
} else {
echo "no";
}
echo "<br/>";
}
}
}

@ -3,24 +3,17 @@ require_once('./include/view.php');
header('Content-Type: text/html; charset=UTF-8');
$action = 'start';
$action = null;
if(isset($_GET['action'])) {
$action = $_GET['action'];
}
$page = null;
switch($action) {
case 'start':
default:
$page = new StartPage();
$page->title = "Boka2";
$page->content = "Här är det tomt just nu.";
break;
case 'do':
print "hej";
break;
}
if($page) {
print($page->get_contents());
if($action === 'do') {
print('ajax endpoint');
exit(0);
}
$page = make_page($action);
$page->render();
?>