blue-twilight/app/Http/Controllers/Gallery/StatisticsController.php

70 lines
1.9 KiB
PHP

<?php
namespace App\Http\Controllers\Gallery;
use App\Facade\Theme;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class StatisticsController extends Controller
{
public function camerasChart(Request $request)
{
$stats = DB::table('photos')
->where([
['camera_make', '!=', ''],
['camera_model', '!=', '']
])
->groupBy('camera_make', 'camera_model')
->select('camera_make', 'camera_model', DB::raw('count(*) as photo_count'))
->orderBy('photo_count', 'desc')
->get();
$labels = [];
$data = [];
foreach ($stats as $stat)
{
// Remove the model from the make if it starts with it
// E.g. CANON - CANON EOS 1200D becomes just CANON EOS 1200D
if (substr($stat->camera_model, 0, strlen($stat->camera_make)) == $stat->camera_make)
{
$stat->camera_make = substr($stat->camera_make, strlen($stat->camera_make));
}
$labels[] = sprintf('%s %s', $stat->camera_make, $stat->camera_model);
$data[] = $stat->photo_count;
}
return response()->json([
'labels' => $labels,
'backgrounds' => $this->rotateColoursForData($data),
'data' => $data
]);
}
public function index(Request $request)
{
return Theme::render('gallery.statistics');
}
private function rotateColoursForData(array $data = [])
{
$colours = ['#0F2240', '#174E79', '#287598', '#46BBB5', '#35DCAD'];
$result = [];
$lastIndex = 0;
for ($i = 0; $i < count($data); $i++)
{
$result[] = $colours[$lastIndex];
$lastIndex++;
if ($lastIndex >= count($colours))
{
$lastIndex = 0;
}
}
return $result;
}
}