kuvia/app/Http/Controllers/ReportController.php

68 lines
2.8 KiB
PHP
Raw Normal View History

2021-01-21 14:36:33 +00:00
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;
class ReportController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function showReportList()
{
2021-01-21 17:56:30 +00:00
$oldesDate = DB::select("SELECT `year`, `month` FROM storage WHERE tenant = " . (int)session("current_tenant_id") . " ORDER BY `date` ASC LIMIT 1;");
if(count($oldesDate) == 0) {
return view("dashboard.reports", ["avalibleReports" => []]);
}
$oldesDate = $oldesDate[0];
2021-01-21 14:36:33 +00:00
$avalibleReports = [];
for ($i = $oldesDate->year; $i <= date("Y"); $i++) {
for ($m = 1; $m <= 12; $m++) {
if($i == $oldesDate->year && $m < $oldesDate->month) {
continue;
}
if($i == date("Y") && $m > date("m")) {
continue;
}
$avalibleReports[] = ["year" => $i, "month" => $m];
}
}
return view("dashboard.reports", ["avalibleReports" => $avalibleReports]);
}
public function spaceReport($year, $month) {
$reportData = DB::select("SELECT * FROM storage WHERE tenant = ".(int)session("current_tenant_id")." AND `year` = ".(int)$year." AND `month` = ".(int)$month.";");
2021-01-21 14:39:20 +00:00
$report = "year;month;day;hour;gallery;size\n";
2021-01-21 14:36:33 +00:00
foreach ($reportData as $reportLine) {
2021-01-21 14:51:49 +00:00
$report .= $reportLine->year.";".$reportLine->month.";".$reportLine->day.";".$reportLine->hour.";".$reportLine->gallery.";".$reportLine->size."\n";
2021-01-21 14:36:33 +00:00
}
$res = new Response();
$res->header("Content-type", "text/csv");
$res->header("Content-Disposition", "attachment; filename=".$year."_".$month."_space.csv");
$res->setContent($report);
return $res;
}
2021-01-21 15:05:26 +00:00
public function trafficReport($year, $month) {
2021-01-21 14:36:33 +00:00
$reportData = DB::select("SELECT * FROM traffic WHERE tenant = ".(int)session("current_tenant_id")." AND `year` = ".(int)$year." AND `month` = ".(int)$month.";");
2021-01-21 15:05:26 +00:00
$report = "year;month;day;hour;gallery;typ;traffic\n";
2021-01-21 14:36:33 +00:00
foreach ($reportData as $reportLine) {
2021-01-21 15:05:26 +00:00
$report .= $reportLine->year.";".$reportLine->month.";".$reportLine->day.";".$reportLine->hour.";".$reportLine->gallery.";".$reportLine->typ.";".$reportLine->traffic."\n";
2021-01-21 14:36:33 +00:00
}
$res = new Response();
$res->header("Content-type", "text/csv");
$res->header("Content-Disposition", "attachment; filename=".$year."_".$month."_trafficg.csv");
$res->setContent($report);
return $res;
}
}