447 lines
12 KiB
PHP
447 lines
12 KiB
PHP
<?php
|
||
|
||
namespace manager\repo;
|
||
use \manager\Database;
|
||
use \manager\ManagerError;
|
||
use \manager\Repo;
|
||
use \manager\ORM\Table;
|
||
use \manager\ORM\Rows;
|
||
|
||
class machine_cluster extends parentRepo{
|
||
|
||
protected static function table_name(){ static $table_name = 'machine_cluster'; return $table_name; }
|
||
|
||
/* CREATION D'UN GROUPE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @name<String> Nom du groupe
|
||
*
|
||
* @return id_cluster<int> Renvoie l'UID du groupe cree
|
||
* Renvoie FALSE si une erreur occure
|
||
*
|
||
*/
|
||
public static function create($id_warehouse, $name){
|
||
/* [1] Verification de l'unicite de name
|
||
=========================================================*/
|
||
$check_unique = ( self::getByName($id_warehouse, $name) === false );
|
||
|
||
// Si un groupe a deja ce nom, on renvoie une erreur
|
||
if( !$check_unique )
|
||
return false;
|
||
|
||
|
||
/* [2] Creation du groupe
|
||
=========================================================*/
|
||
$insert_cluster = Database::getPDO()->prepare("INSERT INTO machine_cluster(id_machine_cluster, id_warehouse, name)
|
||
VALUES(DEFAULT, :id_warehouse, :name)");
|
||
$insert_cluster->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':name' => $name
|
||
]);
|
||
|
||
/* [3] On retourne l'id_group ou FALSE si erreur
|
||
=========================================================*/
|
||
$check_group = self::getByName($id_warehouse, $name);
|
||
|
||
// Si n'existe pas on retourne FALSE
|
||
if( $check_group === false )
|
||
return false;
|
||
|
||
// Sinon, on retourne son id
|
||
return $check_group['id_machine_cluster'];
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* ASSOCIATION D'UNE MACHINE A UN GROUPE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @id_machine_cluster<int> UID du groupe
|
||
* @id_machine<int> UID de la machine
|
||
*
|
||
* @return error<int> Renvoie le code 'ManagerError'
|
||
*
|
||
*/
|
||
public static function link($id_warehouse, $id_machine_cluster, $id_machine){
|
||
/* [1] Verification de l'existence du groupe
|
||
=========================================================*/
|
||
$group_check = self::getById($id_warehouse, $id_machine_cluster);
|
||
|
||
// Si le groupe n'existe pas, on retourne ue erreur
|
||
if( $group_check === false )
|
||
return ManagerError::NoMatchFound;
|
||
|
||
|
||
|
||
/* [2] Verification de l'existente de la machine
|
||
=========================================================*/
|
||
$machine_checkReq = new Repo('machine/getById', [$id_warehouse, $id_machine]);
|
||
$machine_check = $machine_checkReq->answer();
|
||
|
||
|
||
|
||
// Si aucune machine existante, on retourne une erreur
|
||
if( $machine_check === false )
|
||
return ManagerError::NoMatchFound;
|
||
|
||
|
||
/* [3] Verification que l'existente n'existe pas deja
|
||
=========================================================*/
|
||
$already_machine_cluster_merge = self::getMerge($id_warehouse, $id_machine_cluster, $id_machine);
|
||
|
||
// Si l'association existe deja, on ne la cree donc pas
|
||
if( $already_machine_cluster_merge !== false )
|
||
return ManagerError::Success;
|
||
|
||
|
||
/* [4] On cree l'association
|
||
=========================================================*/
|
||
$link_entity = Database::getPDO()->prepare("INSERT INTO machine_cluster_merge(id_machine_cluster_merge, id_machine_cluster, id_machine)
|
||
VALUES(DEFAULT, :id_machine_cluster, :id_machine)");
|
||
$link_entity->execute([
|
||
':id_machine_cluster' => $id_machine_cluster,
|
||
':id_machine' => $id_machine
|
||
]);
|
||
|
||
/* [5] On retourne l'id_machine_cluster_merge ou FALSE si erreur
|
||
=========================================================*/
|
||
$check_machine_cluster_merge = self::getMerge($id_warehouse, $id_machine_cluster, $id_machine);
|
||
|
||
// Si n'existe pas on retourne FALSE
|
||
if( !is_array($check_machine_cluster_merge) )
|
||
return ManagerError::RepoError;
|
||
|
||
// Sinon, on retourne son id
|
||
return ManagerError::Success;
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* SUPPRIME UNE ASSOCIATION D'UNE MACHINE A UN GROUPE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @id_machine_cluster<int> UID du groupe
|
||
* @id_machine<int> UID de la machine
|
||
*
|
||
* @return error<int> Renvoie le code 'ManagerError'
|
||
*
|
||
*/
|
||
public static function unlink($id_warehouse, $id_machine_cluster, $id_machine){
|
||
/* [1] On récupère l'UID du lien
|
||
=========================================================*/
|
||
/* (1) On récupère le lien */
|
||
$get_merge_id = self::getMerge($id_warehouse, $id_machine_cluster, $id_machine);
|
||
|
||
/* (2) Si on ne trouve rien */
|
||
if( $get_merge_id == false )
|
||
return ManagerError::NoMatchFound;
|
||
|
||
|
||
|
||
/* [2] On supprime le lien
|
||
=========================================================*/
|
||
$delete_machine_cluster_merge = Database::getPDO()->prepare("DELETE FROM machine_cluster_merge
|
||
WHERE id_machine_cluster_merge = :uid");
|
||
$delete_machine_cluster_merge->execute([
|
||
':uid' => $get_merge_id['id_machine_cluster_merge']
|
||
]);
|
||
|
||
|
||
/* [2] On verifie que l'association n'existe plus
|
||
=========================================================*/
|
||
if( self::getMerge($id_warehouse, $id_machine_cluster, $id_machine) !== false )
|
||
return ManagerError::ModuleError;
|
||
|
||
|
||
return ManagerError::Success;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* RENVOIE L'ID D'UNE ASSOCIATION D'UNE MACHINE A UN GROUPE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @id_machine_cluster<int> UID du groupe
|
||
* @id_machine<int> UID de la machine
|
||
*
|
||
* @return association<int> Renvoie l'UID de l'association cree
|
||
* Renvoie FALSE si une erreur occure
|
||
*
|
||
*/
|
||
public static function getMerge($id_warehouse, $id_machine_cluster, $id_machine){
|
||
// On ecrit la requete
|
||
$request = Database::getPDO()->prepare("SELECT cm.*
|
||
FROM machine_cluster_merge as cm, machine_cluster as c
|
||
WHERE cm.id_machine_cluster = c.id_machine_cluster
|
||
AND c.id_warehouse = :id_warehouse
|
||
AND cm.id_machine_cluster = :id_machine_cluster
|
||
AND cm.id_machine = :id_machine");
|
||
|
||
// On execute la requete
|
||
$request->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':id_machine_cluster' => $id_machine_cluster,
|
||
':id_machine' => $id_machine
|
||
]);
|
||
|
||
// On recupere 1 seule relation
|
||
$answer = $request->fetch();
|
||
|
||
// Gestion d'erreur -> aucun resultat
|
||
if( $answer === false )
|
||
return false;
|
||
|
||
// On retourne en supprimant les indices numeriques
|
||
return Database::delNumeric( $answer, true );
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* RENVOIE LA LISTE DES GROUPES D'UN ENTREPOT
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
*
|
||
* @return machine_clusters<Array> Renvoie la liste des groupes
|
||
* Renvoie FALSE si une erreur occure
|
||
*
|
||
*/
|
||
public static function getAll($id_warehouse){
|
||
// On ecrit la requete
|
||
$request = Database::getPDO()->prepare("SELECT c.id_machine_cluster, c.name
|
||
FROM machine_cluster as c
|
||
WHERE c.id_warehouse = :id_warehouse
|
||
ORDER BY c.name ASC");
|
||
|
||
// On execute la requete
|
||
$request->execute([
|
||
':id_warehouse' => $id_warehouse
|
||
]);
|
||
|
||
// On retourne en supprimant les indices numeriques
|
||
return Database::delNumeric( $request->fetchAll() );
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* RENVOIE UN GROUPE SPECIFIQUE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @id<int> UID du groupe
|
||
*
|
||
* @return machine_cluster<Array> Renvoie le groupe correspondant
|
||
* Renvoie FALSE si une erreur occure
|
||
*
|
||
*/
|
||
public static function getById($id_warehouse, $id){
|
||
// On ecrit la requete
|
||
$request = Database::getPDO()->prepare("SELECT c.*
|
||
FROM machine_cluster as c
|
||
WHERE c.id_warehouse = :id_warehouse
|
||
AND c.id_machine_cluster = :id");
|
||
|
||
// On execute la requete
|
||
$request->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':id' => $id
|
||
]);
|
||
|
||
$fetch = $request->fetch();
|
||
|
||
// Si aucun résultat
|
||
if( $fetch == false )
|
||
return false;
|
||
|
||
// On retourne en supprimant les indices numeriques
|
||
return Database::delNumeric( $fetch, true );
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* RENVOIE UN GROUPE SPECIFIQUE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @name<String> Nom du groupe
|
||
*
|
||
* @return machine_cluster<Array> Renvoie le groupe correspondant
|
||
* Renvoie FALSE si une erreur occure
|
||
*
|
||
*/
|
||
public static function getByName($id_warehouse, $name){
|
||
// On ecrit la requete
|
||
$request = Database::getPDO()->prepare("SELECT c.*
|
||
FROM machine_cluster as c
|
||
WHERE c.id_warehouse = :id_warehouse
|
||
AND c.name = :name");
|
||
|
||
// On execute la requete
|
||
$request->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':name' => $name
|
||
]);
|
||
|
||
$fetch = $request->fetch();
|
||
|
||
// Si aucun résultat
|
||
if( $fetch == false )
|
||
return false;
|
||
|
||
// On retourne en supprimant les indices numeriques
|
||
return Database::delNumeric( $fetch, true );
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* RENVOIE LES MACHINES D'UN GROUPE DONNE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @id_machine_cluster<int> UID du groupe en question
|
||
*
|
||
* @return members<Array> Retourne la liste des machines du groupe
|
||
*
|
||
*/
|
||
public static function getMembers($id_warehouse, $id_machine_cluster){
|
||
/* [1] On recupere la liste des associations
|
||
=========================================================*/
|
||
$get_machines = Database::getPDO()->prepare("SELECT m.id_machine, m.name
|
||
FROM machine_cluster_merge as cm, machine_cluster as c, machine as m
|
||
WHERE cm.id_machine_cluster = c.id_machine_cluster
|
||
AND c.id_machine_cluster = :id_machine_cluster
|
||
AND c.id_warehouse = :id_warehouse
|
||
AND cm.id_machine = m.id_machine
|
||
ORDER BY m.name");
|
||
$get_machines->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':id_machine_cluster' => $id_machine_cluster
|
||
]);
|
||
|
||
|
||
/* [2] On verifie que le groupe n'existe plus
|
||
=========================================================*/
|
||
return Database::delNumeric( $get_machines->fetchAll() );;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
/* RENVOIE UNE LISTE DE GROUPES EN FONCTION D'UN MOT CLE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @keyword<String> Element de recherche
|
||
*
|
||
* @return machine_clusters<Array> Retourne les groupes trouves
|
||
*
|
||
*/
|
||
public static function search($id_warehouse, $keyword){
|
||
/* [1] On rédige/exécute la requête
|
||
=========================================================*/
|
||
$search = Table::get('machine_cluster')
|
||
->whereIdWarehouse($id_warehouse)
|
||
->whereName(["%$keyword%", Rows::COND_LIKE])
|
||
->select('*');
|
||
|
||
/* [2] On retourne le résultat
|
||
=========================================================*/
|
||
return $search->fetch();
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* MODIFIE LE NOM D'UN GROUPE DONNE
|
||
*
|
||
* @id_machine_cluster<int> UID du gorupe
|
||
* @name<String> Nouveau nom du groupe
|
||
*
|
||
* @return status<Boolean> TRUE si tout s'est bien passé, sinon FALSE
|
||
*
|
||
*/
|
||
public static function edit($id_warehouse, $id_machine_cluster, $name){
|
||
/* [1] On rédige la requête
|
||
=========================================================*/
|
||
$edit_group = Database::getPDO()->prepare("UPDATE machine_cluster
|
||
SET name = :name
|
||
WHERE id_machine_cluster = :id_machine_cluster");
|
||
$edited = $edit_group->execute([
|
||
':name' => $name,
|
||
':id_machine_cluster' => $id_machine_cluster
|
||
]);
|
||
|
||
|
||
return $edited;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* SUPPRIME UN GROUPE DONNE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @id_machine_cluster<int> UID du groupe en question
|
||
*
|
||
* @return status<Boolean> Retourne si oui ou non le groupe a bien ete supprime
|
||
*
|
||
*/
|
||
public static function delete($id_warehouse, $id_machine_cluster){
|
||
/* [1] On redige/execute la requete
|
||
=========================================================*/
|
||
$delete_group = Database::getPDO()->prepare("DELETE FROM machine_cluster
|
||
WHERE id_warehouse = :id_warehouse
|
||
AND id_machine_cluster = :id_machine_cluster");
|
||
$delete_group->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':id_machine_cluster' => $id_machine_cluster
|
||
]);
|
||
|
||
|
||
/* [2] On verifie que le groupe n'existe plus
|
||
=========================================================*/
|
||
return ( self::getById($id_warehouse, $id_machine_cluster) === false );
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
?>
|