137 lines
3.3 KiB
PHP
137 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* Inclut un template avec des données
|
|
* @param string $name Nom du template (sans .php)
|
|
* @param array $data Variables à passer au template
|
|
*/
|
|
function include_template(string $name, array $data = []): void
|
|
{
|
|
extract($data, EXTR_SKIP);
|
|
include __DIR__ . "/../templates/{$name}.php";
|
|
}
|
|
|
|
/**
|
|
* Charge et parse un fichier JSON
|
|
*/
|
|
function loadJsonData(string $filename): array
|
|
{
|
|
$path = __DIR__ . "/../data/{$filename}";
|
|
|
|
if (!file_exists($path)) {
|
|
error_log("JSON file not found: {$filename}");
|
|
return [];
|
|
}
|
|
|
|
$content = file_get_contents($path);
|
|
$data = json_decode($content, true);
|
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
error_log("JSON parse error in {$filename}: " . json_last_error_msg());
|
|
return [];
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* Récupère tous les projets
|
|
*/
|
|
function getProjects(): array
|
|
{
|
|
$data = loadJsonData('projects.json');
|
|
return $data['projects'] ?? [];
|
|
}
|
|
|
|
/**
|
|
* Récupère les projets par catégorie
|
|
*/
|
|
function getProjectsByCategory(string $category): array
|
|
{
|
|
return array_values(array_filter(getProjects(), fn($p) => ($p['category'] ?? '') === $category));
|
|
}
|
|
|
|
/**
|
|
* Récupère un projet par son slug
|
|
*/
|
|
function getProjectBySlug(string $slug): ?array
|
|
{
|
|
foreach (getProjects() as $project) {
|
|
if (($project['slug'] ?? '') === $slug) {
|
|
return $project;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Récupère les technologies uniques de tous les projets
|
|
*/
|
|
function getAllTechnologies(): array
|
|
{
|
|
$technologies = [];
|
|
foreach (getProjects() as $project) {
|
|
foreach ($project['technologies'] ?? [] as $tech) {
|
|
if (!in_array($tech, $technologies, true)) {
|
|
$technologies[] = $tech;
|
|
}
|
|
}
|
|
}
|
|
sort($technologies);
|
|
return $technologies;
|
|
}
|
|
|
|
/**
|
|
* Helper pour afficher une image projet optimisée
|
|
*/
|
|
function projectImage(string $filename, string $alt, int $width, int $height, bool $lazy = true): string
|
|
{
|
|
$webp = $filename;
|
|
$fallback = str_replace('.webp', '.jpg', $filename);
|
|
$lazyAttr = $lazy ? 'loading="lazy"' : '';
|
|
|
|
$altEsc = htmlspecialchars($alt, ENT_QUOTES, 'UTF-8');
|
|
$webpEsc = htmlspecialchars($webp, ENT_QUOTES, 'UTF-8');
|
|
$fallbackEsc = htmlspecialchars($fallback, ENT_QUOTES, 'UTF-8');
|
|
|
|
return <<<HTML
|
|
<picture>
|
|
<source srcset="/assets/img/projects/{$webpEsc}" type="image/webp">
|
|
<img
|
|
src="/assets/img/projects/{$fallbackEsc}"
|
|
alt="{$altEsc}"
|
|
width="{$width}"
|
|
height="{$height}"
|
|
{$lazyAttr}
|
|
class="w-full h-full object-cover"
|
|
onerror="this.onerror=null;this.src='/assets/img/projects/default-project.svg';"
|
|
>
|
|
</picture>
|
|
HTML;
|
|
}
|
|
|
|
/**
|
|
* Compte les projets par technologie
|
|
*/
|
|
function getProjectCountByTech(): array
|
|
{
|
|
$projects = getProjects();
|
|
$count = [];
|
|
|
|
foreach ($projects as $project) {
|
|
foreach ($project['technologies'] ?? [] as $tech) {
|
|
$count[$tech] = ($count[$tech] ?? 0) + 1;
|
|
}
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
|
|
/**
|
|
* Récupère les projets utilisant une technologie
|
|
*/
|
|
function getProjectsByTech(string $tech): array
|
|
{
|
|
return array_values(array_filter(getProjects(), function ($project) use ($tech) {
|
|
return in_array($tech, $project['technologies'] ?? [], true);
|
|
}));
|
|
} |