SMMP/build/api/module/motheure.php

141 lines
3.2 KiB
PHP

<?php
namespace api\module;
use \database\core\DatabaseDriver;
use \manager\sessionManager;
use \error\core\Error;
use \error\core\Err;
use \database\core\Repo;
use \api\core\Request;
use \orm\core\Rows;
use \orm\core\Table;
class motheure{
public function __construct(){}
public function __destruct(){}
/* (1) RETURNS THE TOTAL COUNT FOR A MACHINE
*
* @id_machine<id> Identifiant de la machine
*
* @return count<int> Retourne le compte horaire
*
---------------------------------------------------------*/
public function getCount($params){
extract($params);
/* (1) Check if machine have motheure etree activated
---------------------------------------------------------*/
$mod = Table::get('module')
->whereName('motheure');
$etree = Table::get('etree')
->whereDaemon('simple')
->join('id_module', $mod);
$mc = Table::get('module_merge')
->join('id_etree', $etree);
$m = Table::get('machine')
->select('id_machine')
->whereId($id_machine);
$mc_m = Table::get('machine_cluster_merge')
->join('id_machine', $m)
->join('id_machine_cluster', $mc);
/* (1) If not the etree -> exit */
if( count($mc_m->fetch()) == 0 )
return ['error' => new Error(Err::NoMatchFound)];
/* (2) Get the motor count
---------------------------------------------------------*/
/* (1) Fetch count */
$count = Table::get('motheure')
->unique()
->select('count')
->whereIdMachine($id_machine)
->fetch();
/* (2) If no result -> return 0 */
if( !is_array($count) || !isset($count['count']) )
return ['count' => 0];
/* (3) Else -> return the count */
return [ 'count' => $count['count'] ];
}
/* (2) Increment the motor count
*
* @count<int> Count to add to motor count of the machine
*
---------------------------------------------------------*/
public function increment($params){
extract($params);
/* (1) Check if entry exists
---------------------------------------------------------*/
/* (1) Check if there is an entry for the current machine */
$already_exists = is_array(Table::get('motheure')
->unique()
->select('id_motheure')
->select('id_machine')
->whereIdMachine($_SESSION['SATS']['id'])
->fetch());
/* (2) If does not exist, create entry
---------------------------------------------------------*/
if( !$already_exists ){
/* (1) Insert new entry */
$inserted = Table::get('motheure')
->insert([
'id_motheure' => Rows::INSERT_DEFAULT,
'id_machine' => $_SESSION['SATS']['id'],
'count' => $count
]);
/* (2) Propagate error */
return [ 'error' => ($inserted) ? new Error(Err::Success) : new Error(Err::RepoError) ];
/* (3) If already exists, update entry
---------------------------------------------------------*/
}else{
/* (1) Update entry */
$updated = Table::get('motheure')
->unique()
->whereIdMachine($_SESSION['SATS']['id'])
->edit([
'count' => $count
]);
/* (2) Propagate error */
return [ 'error' => ($updated) ? new Error(Err::Success) : new Error(Err::RepoError) ];
}
/* (x) Default error */
return [ 'error' => new Error(Err::Success) ];
}
}
?>