Files
citrus-cms/app/Http/Controllers/Api/DashboardController.php
T
2026-04-28 21:14:25 +03:00

72 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\JsonResponse;
class DashboardController extends Controller
{
/**
* Get dashboard summary data extracted from the cached blade file.
*
* @return JsonResponse
*/
public function summary(): JsonResponse
{
try {
// Get the cached dashboard content from the 'documents' disk
$cachedContent = Storage::disk('local')->get('cache/dashboard.blade.php');
if (!$cachedContent) {
return response()->json([
'status' => 'error',
'message' => 'Cached dashboard content not found.'
], 404);
}
// Regex pattern to extract icon, value, and label
// Matches block-content, img src, font-size-h3 (value), and font-size-sm (label)
$pattern = '/<div class="block-content[^>]*>.*?<img src="([^"]+)"[^>]*>.*?<div class="font-size-h3[^>]*>(.*?)<\/div>\s*<div class="font-size-sm[^>]*>(.*?)<\/div>/s';
preg_match_all($pattern, $cachedContent, $matches, PREG_SET_ORDER);
$data = [];
$baseUrl = url('/');
foreach ($matches as $match) {
$iconPath = $match[1];
$value = trim(strip_tags($match[2]));
$label = trim(strip_tags($match[3]));
// Clean up excessive whitespace and newlines
$value = preg_replace('/\s+/', ' ', $value);
$label = preg_replace('/\s+/', ' ', $label);
// Make the icon URL absolute
$iconUrl = str_starts_with($iconPath, 'http') ? $iconPath : rtrim($baseUrl, '/') . '/' . ltrim($iconPath, '/');
if ($label) {
$data[] = [
'label' => $label,
'value' => $value,
'icon' => $iconUrl
];
}
}
return response()->json([
'status' => 'success',
'data' => $data
]);
} catch (\Exception $e) {
return response()->json([
'status' => 'error',
'message' => 'An error occurred while extracting dashboard data: ' . $e->getMessage()
], 500);
}
}
}