2017-12-10 19:34:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace api\module;
|
|
|
|
|
|
|
|
use \error\core\Error;
|
2017-12-10 21:42:09 +00:00
|
|
|
use \error\core\Err;
|
2017-12-10 21:33:23 +00:00
|
|
|
use \database\core\Repo;
|
2017-12-10 19:34:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class admin{
|
|
|
|
|
|
|
|
|
2017-12-10 21:33:23 +00:00
|
|
|
/* (1) Return an admin data
|
|
|
|
*
|
|
|
|
* @id_admin<id> [OPT] UID de l'administrateur
|
|
|
|
*
|
|
|
|
* @return data<Array> Administrateurs correspondants
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
public static function get($args){
|
2017-12-10 19:34:49 +00:00
|
|
|
extract($args);
|
|
|
|
|
2017-12-10 21:33:23 +00:00
|
|
|
/* (1) If @id_admin is set -> get by id
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( is_numeric($id_admin) ){
|
|
|
|
|
|
|
|
/* (1) Search admin by id */
|
|
|
|
$fetch_admin = Repo::request('admin', 'getById', $id_admin);
|
|
|
|
|
|
|
|
/* (2) If not found -> return empty data */
|
|
|
|
if( !$fetch_admin )
|
|
|
|
return [ 'data' => [] ];
|
|
|
|
|
|
|
|
/* (3) Return fetched admin */
|
|
|
|
return [ 'data' => [$fetch_admin] ];
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Else -> get all
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
}else
|
|
|
|
return [ 'data' => Repo::request('admin', 'getAll') ];
|
2017-12-10 21:42:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Creates a new administrator
|
|
|
|
*
|
|
|
|
* @username<string> Identifiant de l'administrateur
|
|
|
|
* @mail<string> Adresse mail de l'administrateur
|
|
|
|
* @password<string> Mot de passe de l'administrateur
|
|
|
|
*
|
|
|
|
* @return id_admin<id> UID de l'administrateur crée
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
public static function post($args){
|
|
|
|
extract($args);
|
|
|
|
|
|
|
|
/* (1) Création admin */
|
|
|
|
$id_created = Repo::request('admin', 'create', $username, $mail, $password);
|
|
|
|
|
|
|
|
/* (2) Gestion erreur */
|
|
|
|
if( $id_created === false )
|
|
|
|
return [ 'error' => new Error(Err::RepoError) ];
|
|
|
|
|
|
|
|
/* (3) Renvoi @id_admin */
|
|
|
|
return [ 'id_admin' => $id_created ];
|
|
|
|
|
2017-12-10 19:34:49 +00:00
|
|
|
}
|
|
|
|
|
2017-12-10 21:57:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (3) Updates an existing new administrator
|
|
|
|
*
|
|
|
|
* @id_admin<id> UID de l'administrateur
|
|
|
|
* @mail<string> [OPT] Adresse mail de l'administrateur
|
|
|
|
* @password<string> [OPT] Mot de passe de l'administrateur
|
|
|
|
*
|
|
|
|
* @return admin<array> The new administrator data
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
public static function put($args){
|
|
|
|
extract($args);
|
|
|
|
debug();
|
|
|
|
|
|
|
|
/* (1) If @mail given
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( !is_null($mail) ){
|
|
|
|
|
|
|
|
/* (1) Update mail address */
|
|
|
|
$updated = Repo::request('admin', 'setMail', $id_admin, $mail);
|
|
|
|
|
|
|
|
/* (2) Gestion erreur */
|
|
|
|
if( $updated === false )
|
|
|
|
return [ 'error' => new Error(Err::RepoError) ];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) If @password given
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( !is_null($password) ){
|
|
|
|
|
|
|
|
/* (1) Update password */
|
|
|
|
$updated = Repo::request('admin', 'setPassword', $id_admin, $password);
|
|
|
|
|
|
|
|
/* (2) Gestion erreur */
|
|
|
|
if( $updated === false )
|
|
|
|
return [ 'error' => new Error(Err::RepoError) ];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Renvoi @id_admin */
|
|
|
|
return [ 'id_admin' => Repo::request('admin', 'getById', $id_admin) ];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-10 19:34:49 +00:00
|
|
|
}
|