SMMP/build/database/repo/module.php

244 lines
5.7 KiB
PHP
Raw Normal View History

<?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 module extends parentRepo{
protected static function table_name(){ static $table_name = 'module'; return $table_name; }
/* [1] Fetch all modules
*
* @return modules<Array> The list of modules
* FALSE on error
*
=========================================================*/
public static function getAll(){
return Table::get('module')->select('*')->fetch();
}
/* [2] Fetch all modules for the given warehouse
*
* @id_warehouse<int> UID of the given warehouse
*
* @return modules<Array> The list of modules
* FALSE on error
*
=========================================================*/
public static function getForWarehouse($id_warehouse){
/* (1) Build request */
$modules = Table::get('module')->select('*');
$merge = Table::get('module_availability')->whereIdWarehouse($id_warehouse);
$merge->join('id_module', $modules);
return $merge->fetch();
}
/* [3] Fetch all modules for the given machine_cluster
*
* @id_warehouse<int> UID of the given warehouse
* @id_machine_cluster<int> UID of the given machine_cluster
*
* @return modules<Array> The list of modules
* 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')
->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;
}
}
?>