<?php
namespace admin\controls;

class Users 
{  
  public static function user_delete()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'users_administration', $user['id'] ) )
      return \S::alert('Nie masz uprawnień');
    
    if ( \admin\factory\Users::user_delete( \S::get( 'id' ) ) )
      \S::alert( 'Użytkownik został usunięty.' );
    
    header( 'Location: /admin/users/view_list/' );
    exit;    
  }
  
  public static function user_save()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'users_administration', $user['id'] ) )
      return \S::alert('Nie masz uprawnień');
    
    $values   = \S::json_to_array( \S::get( 'values' ) );
    $response = \admin\factory\Users::user_save( 
                  $values['id'], $values['login'], $values['status'], $values['active_to'], $values['password'], $values['password_re'], $values['admin'], $values['privileges']
                );
    echo json_encode( $response );
    exit;
  }
  
  public static function user_edit()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'users_administration', $user['id'] ) )
      return \S::alert('Nie masz uprawnień');
    
    return \admin\view\Users::user_edit( 
              \admin\factory\Users::user_details( 
              \S::get( 'id' ) ),
              \admin\factory\Users::user_privileges( 
              \S::get( 'id' ) )
            ); 
  }
  
  public static function view_list()
  {
    global $user;
    if( !\admin\factory\Users::check_privileges( 'users_administration', $user['id']))
             return \S::alert('Nie masz uprawnień');
    
    return \admin\view\Users::users_list();
  }
}
?>
