<?php

namespace FOP;

class Figures {
  
  

  
  

    // --- Statische Methoden ---
  
  public static function getFigures($timespans) { 
    
    
    // --- BLOCK: main ---
    
	$sql = 'SELECT '.qi('ID').', title, '.qi('startTime').', '.qi('endTime');
	
		if ((!\FOP\User::getIsGuest())) {
			$sql .= ' , COALESCE((SELECT COUNT(*) FROM '.qi('aufgabe').' WHERE creation BETWEEN '.qi('startTime').' AND '.qi('endTime').'), 0) AS '.qi('Aufgaben');
		}
	

	$sql .= ' FROM (';

	$fromblocks = array();
	foreach($timespans as $title => $timespan){
		array_push($fromblocks, 'SELECT ' . \FOP\Database::quote($title) . ' AS '.qi('ID').', ' . \FOP\Database::quote($title) . ' AS title, ' . $timespan['from'] . ' AS startTime, ' . $timespan['to'] . ' AS endTime');
	}

	$sql .= implode(' UNION ', $fromblocks);
	$sql .= ') AS history';

	$result = \FOP\Database::query($sql)->fetchAll(\PDO::FETCH_ASSOC);

	return $result;

    
  }

  public static function getFiguresDefault() { 
    
    
    // --- BLOCK: main ---
    
	$timespans = ['last24hours' => ['title' => _('last24hours'), 'from' => 'UNIX_TIMESTAMP( NOW( ) ) - 24 *60 *60', 'to' => 'UNIX_TIMESTAMP( NOW( ) )'], 'last7days' => ['title' => _('last7days'), 'from' => 'UNIX_TIMESTAMP( NOW( ) ) - 7 *24 *60 *60', 'to' => 'UNIX_TIMESTAMP( NOW( ) )'], 'prelast7days' => ['title' => _('prelast7days'), 'from' => 'UNIX_TIMESTAMP( NOW( ) ) - 14 *24 *60 *60', 'to' => 'UNIX_TIMESTAMP( NOW( ) ) - 7 *24 *60 *60'], 'last30days' => ['title' => _('last30days'), 'from' => 'UNIX_TIMESTAMP( NOW( ) ) - 30 *24 *60 *60', 'to' => 'UNIX_TIMESTAMP( NOW( ) )'], 'prelast30days' => ['title' => _('prelast30days'), 'from' => 'UNIX_TIMESTAMP( NOW( ) ) - 60 *24 *60 *60', 'to' => 'UNIX_TIMESTAMP( NOW( ) ) - 30 *24 *60 *60'], 'runningyear' => ['title' => _('runningyear'), 'from' => 'UNIX_TIMESTAMP( CONCAT(YEAR(NOW()), \'-01-01\') )', 'to' => 'UNIX_TIMESTAMP( NOW( ) )'], 'lastyear' => ['title' => _('lastyear'), 'from' => 'UNIX_TIMESTAMP( CONCAT(YEAR(NOW()) - 1, \'-01-01\') )', 'to' => 'UNIX_TIMESTAMP( CONCAT(YEAR(NOW()), \'-01-01\') ) - 1'], 'prelastyear' => ['title' => _('prelastyear'), 'from' => 'UNIX_TIMESTAMP( CONCAT(YEAR(NOW()) - 2, \'-01-01\') )', 'to' => 'UNIX_TIMESTAMP( CONCAT(YEAR(NOW()) - 1, \'-01-01\') ) - 1']];
	return self::getFigures($timespans);

    
  }

  public static function getFiguresGraphAction($monthoffset = 0 , $monthoffset2 = 0 ) { 
    
    
    // --- BLOCK: access ---
    if(!(((\FOP\User::getIsLoggedIn())))) throw new \FOP\AccessDeniedException();
    // --- BLOCK: checktypemonthoffset ---
    
    // --- BLOCK: checktypemonthoffset2 ---
    
    // --- BLOCK: main ---
    
	$timespans = array();
	for($i=1;$i<=31;$i++){
		$timespans[($i-1) . 'day'] = ['from' => 'UNIX_TIMESTAMP(DATE_ADD(DATE_ADD(subdate(curdate(), (day(curdate()))), INTERVAL ' . intval($monthoffset) . ' MONTH), INTERVAL ' . $i . ' DAY))', 'to' => 'UNIX_TIMESTAMP(DATE_ADD(DATE_ADD(subdate(curdate(), (day(curdate()))), INTERVAL ' . intval($monthoffset) . ' MONTH), INTERVAL ' . ($i+1) . ' DAY))'];
	}

	for($i=0;$i<24;$i++){
		$timespans[$i . 'month'] = ['from' => 'UNIX_TIMESTAMP(DATE_ADD(DATE_ADD(MAKEDATE(year(now()),1), INTERVAL ' . intval($monthoffset2) . ' MONTH), INTERVAL ' . $i . ' MONTH))+60*60*6', 'to' => 'UNIX_TIMESTAMP(DATE_ADD(DATE_ADD(MAKEDATE(year(now()),1), INTERVAL ' . intval($monthoffset2) . ' MONTH), INTERVAL ' . ($i+1) . ' MONTH))+60*60*6'];
	}
	return self::getFigures($timespans);

    
  }


  
  

  
  
}



