<?php namespace models ;

class Users extends \core\model

{

/** LOAD DATA FROM ACTIVE USER VIA PASSWD */

public function getLogin ( $email , $pass )

{

return $this -> _db -> select ( "SELECT * FROM users WHERE email = :email AND pass = :pass LIMIT 1" , array ( ":email" => $email , ":pass" => hash ( HASH , $pass ) ) ) [ 0 ] ;

}

/** LOAD ALL THE USER DATA VIA ID */

public function get ( $id )

{

return $this -> _db -> select ( "SELECT users.*, groups.name as groupName FROM users, groups WHERE users.id = :id AND groups.id = users.group LIMIT 1" , array ( ":id" => $id ) ) [ 0 ] ;

}

/** LOAD ALL THE USER DATA VIA ID */

public function getToken ( $id , $email )

{

return $this -> _db -> select ( "SELECT access_token,active FROM users WHERE id = :id AND email = :email AND token_timeout > NOW()" , array ( ":id" => $id , ":email" => $email ) ) [ 0 ] ;

}

/** LOADS ALL THE DATA USERS */

public function getUsers ( )

{

return $this -> _db -> select ( "SELECT * FROM users" ) ;

}

/** LOADS ALL THE DATA USERS */

public function getUsersFirstname ( )

{

return $this -> _db -> select ( "SELECT DISTINCT(firstname)FROM users" ) ;

}

/** LOADS ALL THE DATA USERS */

public function getUsersLastname ( )

{

return $this -> _db -> select ( "SELECT DISTINCT(lastname)FROM users" ) ;

}

/** LOADS ALL THE DATA USERS */

public function getUsersEmail ( )

{

return $this -> _db -> select ( "SELECT DISTINCT(email)FROM users" ) ;

}

/** LOADS ALL THE COLLECTED DATA FROM USERS and OTHER TABLES */

public function getUsersInfo ( )

{

return $this -> _db -> select ( "SELECT users.*, groups.name as group_name FROM users, groups WHERE groups.id = users.group" ) ;

}

/** LOADS USERDATA VIA EMAIL */

public function getbymail ( $email )

{

return $this -> _db -> select ( "SELECT * FROM users WHERE email = :email AND active = 1 LIMIT 1" , array ( ":email" => $email ) ) [ 0 ] ;

}

/** ADDS AN USER IF HE DOESN'T EXISTS */

public function add ( $data )

{

if ( $this -> getbymail ( $data [ "email" ] ) -> id > 0 ) {

// User exisiert schon

return false ;

}

$data [ "firstlogin" ] = date ( "Y-m-d H:i:s" ) ;

$this -> _db -> insert ( "users" , $data ) ;

return true ;

}

/** UPDATE THE USER DATA */

public function update ( $data , $where )

{

if ( is_numeric ( $where ) ) {

$type = "id" ;

} else {

$type = "email" ;

}

$this -> _db -> update ( "users" , $data , array ( $type => $where ) ) ;

}

/** DELTE AN USER VIA EMAIL OR ID */

public function delete ( $param )

{

if ( is_numeric ( $param ) ) {

// Löschen mit id

$this -> _db -> delete ( "users" , array ( "id" => $param ) ) ;

} else {

// Löschen mit mail

$this -> _db -> delete ( "users" , array ( "email" => $param ) ) ;

}

}