SMMP/build/database/repo/etree.php

203 lines
4.9 KiB
PHP

<?php
namespace database\repo;
use \database\core\DatabaseDriver;
use \error\core\Err;
use \database\core\Repo;
use \orm\core\Table;
use \orm\core\Rows;
class etree extends parentRepo{
protected static function table_name(){ static $table_name = 'etree'; return $table_name; }
/* [1] Fetch all etrees for the given machine_cluster
*
* @id_warehouse<int> UID of the given warehouse
* @id_machine_cluster<int> UID of the given machine_cluster
*
* @return etrees<Array> The list of etrees
* FALSE on error
*
=========================================================*/
public static function getForMachineCluster($id_warehouse, $id_machine_cluster){
/* (1) On récupère le ETREE de la machine
---------------------------------------------------------*/
$module = Table::get('module')
->select('id_module')
->select('name');
$etree = Table::get('etree')
->select('id_etree')
->select('daemon')
->join('id_module', $module);
$merge = Table::get('module_merge')
->join('id_etree', $etree)
->whereIdWarehouse($id_warehouse)
->whereIdMachineCluster($id_machine_cluster);
return $merge->fetch();
}
/* [4] Adds a module to machine cluster of a warehouse
*
* @id_warehouse<int> UID of the given warehouse
* @id_machine_cluster<int> UID of the given machine_cluster
* @id_module UID of the given module
*
* @return status<bool> TRUE on success
*
=========================================================*/
public static function link($id_warehouse, $id_machine_cluster, $id_module){
/* (1) Check module
---------------------------------------------------------*/
/* (1) Check if module exists (by its id) */
$module = Table::get('module')
->select('*')
->unique()
->whereId($id_module);
if( !$module->fetch() )
return false;
/* (2) Check if the module is allowed in the warehouse */
$module_m = Table::get('module_availability')
->select('*')
->unique()
->whereIdWarehouse($id_warehouse)
->whereIdModule($id_module);
// if not allowed in the warehouse -> error
if( !$module->fetch() )
return false;
/* (2) Check machine_cluster
---------------------------------------------------------*/
/* (1) Check if machine_cluster exists (by its id) */
$machine_cluster = Table::get('machine_cluster')
->select('*')
->unique()
->whereIdWarehouse($id_warehouse)
->whereId($id_machine_cluster);
if( !$machine_cluster->fetch() )
return false;
/* (2) Check link does not already exists */
$exists = Table::get('module_merge')
->select('*')
->unique()
->whereIdWarehouse($id_warehouse)
->whereIdMachineCluster($id_machine_cluster)
->whereIdModule($id_module);
if( !!$exists->fetch() )
return true; // returns all right (even if nothing done)
/* (3) Create link
---------------------------------------------------------*/
$inserted = Table::get('module_merge')->insert([
'id_warehouse' => $id_warehouse,
'id_module' => $id_module,
'id_machine_cluster' => $id_machine_cluster
]);
// return TRUE only if PDO insert successful
return $inserted;
}
/* [5] Removes a module from machine cluster of a warehouse
*
* @id_warehouse<int> UID of the given warehouse
* @id_machine_cluster<int> UID of the given machine_cluster
* @id_module UID of the given module
*
* @return status<bool> TRUE on success
*
=========================================================*/
public static function unlink($id_warehouse, $id_machine_cluster, $id_module){
/* (1) Check module
---------------------------------------------------------*/
/* (1) Check if module exists (by its id) */
$module = Table::get('module')
->select('*')
->unique()
->whereId($id_module);
if( !$module->fetch() )
return false;
/* (2) Check machine_cluster
---------------------------------------------------------*/
/* (1) Check if machine_cluster exists (by its id) */
$machine_cluster = Table::get('machine_cluster')
->select('*')
->unique()
->whereIdWarehouse($id_warehouse)
->whereId($id_machine_cluster);
if( !$machine_cluster->fetch() )
return false;
/* (2) Check if link already exists */
$exists = Table::get('module_merge')
->select('*')
->unique()
->whereIdWarehouse($id_warehouse)
->whereIdMachineCluster($id_machine_cluster)
->whereIdModule($id_module);
if( !$exists->fetch() )
return false; // returns all right (even if nothing done)
/* (3) Remove link
---------------------------------------------------------*/
$deleted = Table::get('module_merge')
->whereIdWarehouse($id_warehouse)
->whereIdModule($id_module)
->whereIdMachineCluster($id_machine_cluster)
->delete();
// return TRUE only if PDO delete successful
return $deleted;
}
}
?>