<?php
namespace admin\factory;

class Users 
{
  public static function user_delete( $user_id )
  {
    global $mdb;
    return $mdb -> delete( 'pp_users', [ 'id' => (int)$user_id ] );
  }
  
  public static function user_details( $user_id )
  {
    global $mdb;
    return $mdb -> get( 'pp_users', '*', [ 'id' => (int)$user_id ] );
  }
  
  public static function user_save( $user_id = '', $login, $status, $password, $password_re, $admin )
  {
    global $mdb, $lang, $config;
    
    if ( !$user_id )
    {      
      if ( strlen( $password ) < 5 )
        return $response = [ 'status' => 'error', 'msg' => 'Podane hasło jest zbyt krótkie.' ];
      
      if ( $password != $password_re )
        return $response = [ 'status' => 'error', 'msg' => 'Podane hasła są różne' ];
      
      if ( $mdb -> insert( 'pp_users', [
              'login' => $login,
              'status' => $status == 'on' ? 1 : 0,
              'admin' => $admin,
              'password' => md5( $password )
            ] ) ) 
      {  
        return $response = [ 'status' => 'ok', 'msg' => 'Użytkownik został zapisany.' ];
      }
    }
    else
    {
      if ( $password and strlen( $password ) < 5 )
        return $response = [ 'status' => 'error', 'msg' => 'Podane hasło jest zbyt krótkie.' ];
      
      if ( $password and $password != $password_re )
        return $response = [ 'status' => 'error', 'msg' => 'Podane hasła są różne' ];
      
      if ( $password )
        $mdb -> update( 'pp_users', [
              'password' => md5( $password )
            ], [
              'id' => (int)$user_id
            ] );
      
      $mdb -> update( 'pp_users', [
              'login' => $login,
              'admin' => $admin,
              'status' => $status == 'on' ? 1 : 0
            ], [
              'id' => (int)$user_id
            ] );
      
      return $response = [ 'status' => 'ok', 'msg' => 'Uzytkownik został zapisany.' ]; 
    }
  }
  
  public static function check_login( $login, $user_id )
  {
    global $mdb;
    
    if ( $mdb -> get( 'pp_users', 'login', [ 'AND' => [ 'login' => $login, 'id[!]' => (int)$user_id ] ] ) )
      return $response = [ 'status' => 'error', 'msg' => 'Podany login jest już zajęty.' ];
    
    return $response = [ 'status' => 'ok' ];
  }
  
  public static function logon( $login, $password )
  {
    global $mdb;
    
    if ( !$mdb -> get( 'pp_users', '*', [ 'login' => $login ] ) )
      return 0;
    
    if ( !$mdb -> get( 'pp_users', '*', [ 'AND' => [ 'login' => $login, 'status' => 1, 'error_logged_count[<]' => 5 ] ] ) )
      return -1;
    
    if ( $mdb -> get( 'pp_users', '*', [ 'AND' => [ 'login' => $login, 'status' => 1, 'password' => md5( $password ) ] ] ) )
    {
      $mdb -> update( 'pp_users', [ 'last_logged' => date( 'Y-m-d H:i:s' ), 'error_logged_count' => 0 ], [ 'login' => $login ] );
      return 1;
    }
    else
    {
      $mdb -> update( 'pp_users', [ 'last_error_logged' => date( 'Y-m-d H:i:s' ), 'error_logged_count[+]' => 1 ], [ 'login' => $login ] );
      if ( $mdb -> get( 'pp_users', 'error_logged_count', [ 'login' => $login ] ) >= 5 )
      {
        $mdb -> update( 'pp_users', [ 'status' => 0 ], [ 'login' => $login ] );
        return -1;
      }
    }
    return 0;
  }
  
  public static function details( $login )
  {
    global $mdb;
    return $mdb -> get( 'pp_users', '*', [ 'login' => $login ] );
  }
}
?>
