204 lines
5.4 KiB
PHP
Executable File
204 lines
5.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace manager\repo;
|
|
use \manager\Database;
|
|
use \manager\repo\cluster as clusterRepo;
|
|
|
|
class machine extends parentRepo{
|
|
|
|
protected static function table_name(){ static $table_name = 'machine'; return $table_name; }
|
|
|
|
/* CREATION D'UNE MACHINE
|
|
*
|
|
* @code<String> Code de la machine
|
|
* @name<String> Nom de la machine
|
|
*
|
|
* @return id_machine<int> Renvoie l'UID de la machine cree
|
|
* Renvoie FALSE si une erreur occure
|
|
*
|
|
*/
|
|
public static function create($code, $name){
|
|
/* [1] Verification de l'unicite de code+name
|
|
=========================================================*/
|
|
$check_unique = ( self::getByCode($code) === false );
|
|
$check_unique = $check_unique && ( self::getByName($name) === false );
|
|
|
|
// Si une machine a deja ce code/nom, on renvoie une erreur
|
|
if( !$check_unique )
|
|
return false;
|
|
|
|
|
|
/* [2] Creation de la machine
|
|
=========================================================*/
|
|
$insert_machine = Database::getPDO()->prepare("INSERT INTO machine(id_machine, code, name)
|
|
VALUES(DEFAULT, :code, :name)");
|
|
$insert_machine->execute(array(
|
|
':code' => $code,
|
|
':name' => $name
|
|
));
|
|
|
|
/* [3] On retourne l'id_machine ou FALSE si erreur
|
|
=========================================================*/
|
|
$check_machine = self::getByCode($code);
|
|
|
|
// Si n'existe pas on retourne FALSE
|
|
if( $check_machine === false )
|
|
return false;
|
|
|
|
// Sinon, on retourne son id
|
|
return $check_machine['id_machine'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE UNE LISTE DE MACHINE EN FONCTION D'UN MOT CLE
|
|
*
|
|
* @keyword<String> Element de recherche
|
|
*
|
|
* @return machines<Array> Retourne les machines trouvees
|
|
*
|
|
*/
|
|
public static function search($keyword){
|
|
// On recupere les donnees
|
|
$searchmachines = Database::getPDO()->query("SELECT * FROM machine
|
|
WHERE code LIKE '%".$keyword."%'
|
|
OR name LIKE '%".$keyword."%'
|
|
");
|
|
|
|
return Database::delNumeric( $searchmachines->fetchAll() );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE LES GROUPES AUQUEL APPARTIENT UNE MACHINE DONNEE
|
|
*
|
|
* @id_machine<int> UID de la machine en question
|
|
*
|
|
* @return clusters<Array> Retourne la liste des groupes auquel appartient la machine
|
|
*
|
|
*/
|
|
public static function getClusters($id_machine){
|
|
/* [1] On redige/execute la requete
|
|
=========================================================*/
|
|
$get_clusters = Database::getPDO()->prepare("SELECT c.* FROM cluster as c, cluster_merge as cm
|
|
WHERE cm.id_cluster = c.id_cluster
|
|
AND cm.id_entity = :id_machine
|
|
AND cm.class = :class
|
|
ORDER BY c.id_cluster");
|
|
$get_clusters->execute(array(
|
|
':id_machine' => $id_machine,
|
|
':class' => clusterRepo::MACHINE_CLASS
|
|
));
|
|
|
|
/* [2] On retourne la liste des groupes
|
|
=========================================================*/
|
|
return Database::delNumeric( $get_clusters->fetchAll() );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* MODIFICATION D'UNE MACHINE DONNEE
|
|
*
|
|
* @id_machine<int> UID de la machine
|
|
* @code<String> Code RFID de la machine
|
|
* @name<String> Identifiant l'utilisateur
|
|
*
|
|
* @return status<Boolean> Renvoie si oui ou non tout s'est bien passe
|
|
*
|
|
*/
|
|
public static function edit($id_machine=null, $code=null, $name=null){
|
|
/* [1] Verification de l'unicite de code+name (si differents)
|
|
=========================================================*/
|
|
// On recupere les machine ayant le meme code ou name (si existent)
|
|
$getbycode = self::getByCode($code);
|
|
$getbyname = self::getByName($name);
|
|
|
|
$check_unique = is_array($getbycode) && $getbycode['id_machine'] == $id_machine || !is_array($getbycode);
|
|
$check_unique = $check_unique && is_array($getbyname) && $getbyname['id_machine'] == $id_machine || !is_array($getbyname);
|
|
|
|
// Si un utilisateur a deja ce code ou ce name (sauf lui), on renvoie une erreur
|
|
if( !$check_unique )
|
|
return false;
|
|
|
|
/* [2] Modification de la machine
|
|
=========================================================*/
|
|
$edit_machine = Database::getPDO()->prepare("UPDATE machine
|
|
SET code = :code,
|
|
name = :name
|
|
WHERE id_machine = :id_machine");
|
|
$edit_machine->execute(array(
|
|
':code' => $code,
|
|
':name' => $name,
|
|
':id_machine' => $id_machine
|
|
));
|
|
|
|
|
|
|
|
/* [3] On retourne si tout a ete modifie, si erreur
|
|
=========================================================*/
|
|
$check_machine = self::getById($id_machine);
|
|
|
|
$checker = $check_machine['id_machine'] == $id_machine;
|
|
$checker = $checker && $check_machine['code'] == $code;
|
|
$checker = $checker && $check_machine['name'] == $name;
|
|
|
|
// On retourne l'etat de la modification
|
|
return $checker;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SUPPRIME UNE MACHINE DONNE
|
|
*
|
|
* @id_machine<int> UID de la machine en question
|
|
*
|
|
* @return status<Boolean> Retourne si oui ou non la machine a bien ete supprime
|
|
*
|
|
*/
|
|
public static function delete($id_machine){
|
|
/* [1] On redige/execute la requete
|
|
=========================================================*/
|
|
$delete_machine = Database::getPDO()->prepare("DELETE FROM machine WHERE id_machine = :id_machine");
|
|
$delete_machine->execute(array(
|
|
':id_machine' => $id_machine
|
|
));
|
|
|
|
|
|
/* [2] On verifie que la machine n'existe plus
|
|
=========================================================*/
|
|
return ( self::getById($id_machine) === false );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
?>
|