<?php
class UserPage extends Page {
    private $action = 'list';
    private $user = null;

    public function __construct() {
        parent::__construct();
        if(isset($_GET['action'])) {
            $this->action = $_GET['action'];
        }
        if(isset($_GET['id'])) {
            $id = $_GET['id'];
            if($id) {
                try {
                    $this->user = new User($_GET['id']);
                } catch(Exception $e) {
                    $this->user = null;
                    $this->action = 'list';
                    $this->error = i18n('There is no user with that ID.');
                }
            }
        }
        switch($this->action) {
            case 'show':
                $this->subtitle = i18n('Borrower details');
                break;
            case 'list':
                $this->subtitle = i18n('Borrower list');
                break;
        }
    }

    protected function render_body() {
        switch($this->action) {
            case 'list':
                print($this->build_user_table(get_items('user')));
                break;
            case 'show':
                print($this->build_user_details());
                break;
        }
    }

    private function build_user_details() {
        $active_loans = $this->user->get_loans('active');
        $table_active = i18n('No active loans.');
        if($active_loans) {
            $table_active = $this->build_user_loan_table($active_loans);
        }
        $inactive_loans = $this->user->get_loans('inactive');
        $table_inactive = i18n('No past loans.');
        if($inactive_loans) {
            $table_inactive = $this->build_user_loan_table($inactive_loans);
        }
        return replace(array('active_loans' => $table_active,
                             'inactive_loans' => $table_inactive,
                             'id' => $this->user->get_id(),
                             'name' => $this->user->get_name(),
                             'displayname' => $this->user->get_displayname($this->ldap),
                             'notes' => $this->user->get_notes()),
                       $this->fragments['user_details']);
    }
}
?>