SMMP/build/viewer/view/history/view.php

130 lines
3.6 KiB
PHP
Raw Normal View History

<?php
namespace viewer\view\history;
2017-01-30 17:39:21 +00:00
use \api\core\Request;
use \error\core\Error;
2017-01-30 17:39:21 +00:00
use \error\core\Err;
class view{
public static function render(){
/* [1] Init Twig
=========================================================*/
$loader = new \Twig_Loader_Filesystem(__BUILD__.'/viewer/view');
$twig = new \Twig_Environment($loader, []);
2017-07-24 16:52:19 +00:00
$twig->getExtension('Twig_Extension_Core')->setTimezone('Europe/Paris');
/* [2] Store variables
=========================================================*/
$variables = [
'p_theme' => $_SESSION['WAREHOUSE']['theme']
];
/* [3] Store functions
=========================================================*/
$twig->addFunction(new \Twig_Function('f_history', function(){
2017-01-30 17:39:21 +00:00
$req = new Request('historyDefault/getAll', []);
$res = $req->dispatch();
// si erreur, on retourne rien par défaut
2017-01-30 17:39:21 +00:00
if( $res->error->get() != Err::Success )
return [];
return $res->get('history');
}));
$twig->addFilter(new \Twig_Filter('f_tsformat', function($ts){
return date('d/m/Y H:i:s', $ts);
}));
$twig->addFilter(new \Twig_Filter('f_tsrelative', function($ts){
$r = self::relativetime($ts);
// if only one
$o = $r[0] <= 1;
switch($r[1]){
case 'Y': return $r[0].' an'.($o?'':'s'); break;
case 'm': return $r[0].' mois'; break;
case 'd': return $r[0].' jour'.($o?'':'s'); break;
case 'H': return $r[0].' heure'.($o?'':'s'); break;
case 'i': return $r[0].' minute'.($o?'':'s'); break;
case 's': return $r[0].' seconde'.($o?'':'s'); break;
default: return 'peu de temps'; break;
}
}));
/* [4] Build the whole stuff
=========================================================*/
return $twig->render('history/view.twig', [
'p_theme' => $variables['p_theme']
]);
}
2017-07-24 16:52:19 +00:00
/* RETURNS A RELATIVE HUMAN-READABLE TIME
*
* @ts<int> Timestamp to process
*
* @return relative<Array> human-readable relative time [value, unit]
*
*/
private static function relativetime($ts){
/* [1] Explode time into human-readable time units
=========================================================*/
$units = [];
/* (1) Date units */
$units['year'] = (int) date('Y', $ts);
$units['month'] = (int) date('m', $ts);
$units['day'] = (int) date('d', $ts);
/* (2) Time units */
$units['hour'] = (int) date('H', $ts);
$units['minute'] = (int) date('i', $ts);
$units['second'] = (int) date('s', $ts);
/* [2] Calculate relative time for each unit
=========================================================*/
/* (1) Date units */
$units['year'] = intval(date('Y')) - $units['year'];
$units['month'] = intval(date('m')) - $units['month'];
$units['day'] = intval(date('d')) - $units['day'];
/* (2) Time units */
$units['hour'] = intval(date('H')) - $units['hour'];
$units['minute'] = intval(date('i')) - $units['minute'];
$units['second'] = intval(date('s')) - $units['second'];
2017-07-24 16:52:19 +00:00
/* [3] Return significative relative time
=========================================================*/
/* (1) Date units */
2017-07-24 16:52:19 +00:00
if( $units['year'] > 0 ) return [ $units['year'], 'Y' ];
if( $units['month'] > 0 ) return [ $units['month'], 'm' ];
if( $units['day'] > 0 ) return [ $units['day'], 'd' ];
/* (2) Time units */
2017-07-24 16:52:19 +00:00
if( $units['hour'] > 0 ) return [ $units['hour'], 'H' ];
if( $units['minute'] > 0 ) return [ $units['minute'], 'i' ];
if( $units['second'] > 0 ) return [ $units['second'], 's' ];
/* (3) Default value */
return [0, '.'];
}
}