<?php
class CheckoutPage extends Page {
    private $userstr = '';
    private $emailstr = '';
    private $user = null;

    public function __construct() {
        parent::__construct();
        if(isset($_GET['user'])) {
            $this->userstr = trim(strtolower($_GET['user']));
        }
        if(isset($_GET['email'])) {
            $this->emailstr = trim(strtolower($_GET['email']));
        }
        try {
            $this->user = $this->user_init($this->userstr,
                                           $this->emailstr);
        } catch(Exception $e) {
            $this->error = $e->getMessage();
        }
    }

    protected function user_init($name, $email) {
        $nameuser = null;
        $emailuser = null;
        if($name) {
            try {
                $nameuser = new User($name, 'name');
            } catch(Exception $ue) {
                # The user wasn't found locally
                try {
                    $this->ldap->get_user($name);
                    $nameuser = User::create_user($name);
                } catch(Exception $le) {
                    $err = "Användarnamnet '$name' kunde inte hittas.";
                    throw new Exception($err);
                }
            }
        }
        if($email) {
            try {
                $search = $email;
                if(strpos($email, '@') === false) {
                    $search = $email .'@dsv.su.se';
                }
                # Lookup email directly in ldap since we don't store it
                $emailuser = new User($this->ldap->search_email($search),
                                      'name');
            } catch(Exception $ue) {
                $err = "E-postadressen '$search' kunde inte hittas.";
                throw new Exception($err);
            }
        }
        if($nameuser && $emailuser) {
            if($nameuser != $emailuser) {
                $err = "Användarnamn och e-post matchar olika användare.";
                throw new Exception($err);
            }
            return $nameuser;
        }
        if($nameuser) {
            return $nameuser;
        }
        return $emailuser;
    }

    protected function render_body() {
        $username = $this->userstr;
        $email = $this->emailstr;
        $displayname = '';
        $notes = '';
        $loan_table = '';
        $subhead = '';
        $enddate = '';
        $disabled = 'disabled';
        if($this->user !== null) {
            $username = $this->user->get_name();
            $email = $this->user->get_email($this->ldap);
            $displayname = $this->user->get_displayname($this->ldap);
            $notes = $this->user->get_notes();
            $enddate = format_date(default_loan_end(time()));
            $disabled = '';
            $loans = $this->user->get_loans('active');
            $loan_table = 'Inga pågående lån.';
            if($loans) {
                $loan_table = $this->build_user_loan_table($loans);
            }
            $subhead = replace(array('title' => 'Lånade artiklar'),
                               $this->fragments['subtitle']);
        }
        print(replace(array('user' => $username,
                            'email' => $email,
                            'displayname' => $displayname,
                            'notes' => $notes,
                            'end' => $enddate,
                            'subtitle' => $subhead,
                            'disabled' => $disabled,
                            'loan_table' => $loan_table),
                      $this->fragments['checkout_page']));
    }
}
?>