insert_id); } public function __construct($clue, $type = 'id') { parent::__construct(); $find = null; switch($type) { case 'id': $find = prepare('select `id` from `user` where `id`=?'); bind($find, 'i', $clue); break; case 'name': $find = prepare('select `id` from `user` where `name`=?'); bind($find, 's', $clue); break; default: throw new Exception('Invalid type'); } execute($find); $id = result_single($find)['id']; if($id === null) { throw new Exception("Invalid username '$clue'"); } $this->id = $id; $this->update_fields(); } private function update_fields() { $get = prepare('select * from `user` where `id`=?'); bind($get, 'i', $this->id); execute($get); $user = result_single($get); $this->name = $user['name']; $this->notes = $user['notes']; return true; } public function matches($terms, $ldap, $matchAll=false) { $terms = $this->specify_search($terms, array('name', 'email', 'notes')); $matches = array(); foreach($terms as $field => $values) { switch($field) { case 'name': if(match($values, $this->name)) { $matches['name'] = $this->name; } $dname = $this->get_displayname($ldap); if(match($values, $dname)) { $matches['displayname'] = $dname; } break; case 'email': $email_raw = $this->get_email($ldap, false); $email_fmt = $this->get_email($ldap); if($email_raw && match($values, $email_fmt)) { $matches['email'] = $email_fmt; } break; case 'notes': if(match($values, $this->notes)) { $matches['notes'] = $this->notes; } break; } } if($matchAll && array_diff_assoc($terms, $matches)) { return array(); } return $matches; } public function get_displayname($ldap) { try { return $ldap->get_user($this->name); } catch(Exception $e) { return 'Ej i SUKAT'; } } public function get_email($ldap, $format = true) { try { return $ldap->get_user_email($this->name); } catch(Exception $e) { if($format) { return 'Mailadress saknas'; } return false; } } public function get_id() { return $this->id; } public function get_name() { return $this->name; } public function set_name($newname) { $update = prepare('update `user` set `name`=? where `id`=?'); bind($update, 'si', $newname, $this->id); execute($update); $this->name = $newname; return true; } public function get_notes() { return $this->notes; } public function set_notes($newnotes) { $update = prepare('update `user` set `notes`=? where `id`=?'); bind($update, 'si', $newnotes, $this->id); execute($update); $this->notes = $newnotes; return true; } public function get_loans($type = 'both') { $statement = "select `id` from `event` left join `loan` on `event`.`id` = `loan`.`event` where `type`='loan' and `user`=?"; switch($type) { case 'active': $statement .= ' and `returntime` is null'; break; case 'inactive': $statement .= ' and `returntime` is not null'; break; case 'both': break; default: $err = "$type is not a valid argument. Valid arguments are active, inactive, both."; throw new Exception($err); break; } $statement .= ' order by `starttime` desc'; $get = prepare($statement); bind($get, 'i', $this->id); execute($get); $loans = array(); foreach(result_list($get) as $row) { $loans[] = new Loan($row['id']); } return $loans; } public function get_overdue_loans() { $overdue = array(); foreach($this->get_loans('active') as $loan) { if($loan->is_overdue()) { $overdue[] = $loan; } } return $overdue; } } ?>