designtopack/public/site/models/project.php

178 lines
5.4 KiB
PHP
Raw Normal View History

2024-09-26 15:46:13 +02:00
<?php
use adrienpayet\notifications\NotificationsPage;
class ProjectPage extends NotificationsPage {
2025-05-27 11:39:20 +02:00
public function getSteps() {
2025-04-30 14:43:38 +02:00
$apiCache = kirby()->cache('api');
2025-05-27 11:39:20 +02:00
$stepsData = $apiCache?->get($this->slug() . '_' . 'steps');
2025-05-26 14:14:52 +02:00
2025-05-27 12:19:17 +02:00
if ($stepsData === null || count($stepsData) === 0) {
2025-05-27 11:39:20 +02:00
$this->rebuildStepsCache();
2025-05-26 13:48:04 +02:00
};
2025-05-27 11:39:20 +02:00
$stepsData = $apiCache->get($this->slug() . '_' . 'steps');
2025-05-26 14:14:52 +02:00
2025-05-26 13:48:04 +02:00
return $stepsData;
}
2025-05-27 11:39:20 +02:00
public function rebuildStepsCache() {
2025-05-26 13:48:04 +02:00
// Create steps
$steps = [];
foreach ($this->children() as $child) {
try {
$steps[] = $this->createStep($child);
} catch (\Throwable $th) {
throw new Exception("Can't create step. File " . $th->getFile() . " line " . $th->getLine(), 1);
}
2025-04-30 14:43:38 +02:00
}
2025-05-26 13:48:04 +02:00
// Sort steps by their index
usort($steps, fn($a, $b) => $a['index'] <=> $b['index']);
2025-05-27 11:39:20 +02:00
$apiCache = kirby()->cache('api');
$apiCache->set($this->slug() . '_' . 'steps', $steps);
}
private function createStep($child) {
$files = [];
$uri = null;
2024-12-11 14:37:06 +01:00
if (str_contains($child->stepName()->value(), 'Brief') || $child->stepName()->value() === "industrialIdeation") {
2024-11-19 13:59:06 +01:00
$this->handleBriefStep($child, $files, $uri);
}
2024-11-18 15:27:41 +01:00
if ($child->stepName() == 'proposal') {
$this->handleProposalStep($child, $files, $uri);
}
if ($child->stepName() == 'virtualSample') {
$this->handleVirtualSampleStep($child, $files, $uri);
}
2024-12-11 14:37:06 +01:00
if ($child->stepName() == 'physicalSample') {
$this->handlePhysicalSampleStep($child, $files, $uri);
}
2024-10-16 17:32:15 +02:00
return [
'label' => $child->title()->value(),
'id' => $child->stepName()->value(),
'slug' => $child->slug(),
'index' => intval($child->stepIndex()->value()),
'modified' => $child->modified('Y-MM-dd'),
'isValidated' => $child->isValidated() == 'true' ? true : false,
2024-12-11 14:37:06 +01:00
'description' => $child->description()->isNotempty() ? $child->description()->value() : null,
2024-12-11 15:05:40 +01:00
'title' => $child->headline()->isNotempty() ? $child->headline()->value() : null,
'date' => $child->date()->isNotempty() ? $child->date()->value() : null,
2024-12-11 14:37:06 +01:00
'cover' => $child->cover()->isNotempty() ? $child->cover()->toFile()->url() : null,
'uri' => $uri,
'files' => $files,
];
}
2024-11-19 13:59:06 +01:00
private function handleBriefStep($child, &$files, &$uri) {
$uri = $child->uri();
if ($child->moodboard()->isNotEmpty()) {
2024-10-16 17:32:15 +02:00
foreach ($child->moodboard()->toFiles() as $file) {
$files[] = getFileData($file);
}
2024-09-26 19:14:20 +02:00
}
2024-09-26 15:46:13 +02:00
if ($child->pdf()->isNotEmpty()) {
$files[] = getFileData($child->pdf()->toFile());
}
}
2024-11-18 15:27:41 +01:00
private function handleProposalStep($child, &$files, &$uri) {
$uri = $child->parent()->uri() . '?dialog=' . $child->slug();
if ($child->pdf()->isNotEmpty()) {
foreach ($child->pdf()->toFiles() as $file) {
$files[] = getFileData($file);
}
}
}
private function handleVirtualSampleStep($child, &$files, &$uri) {
$uri = $child->parent()->uri() . '?dialog=' . $child->slug();
if ($child->hasChildren()) {
$files['dynamic'] = [];
2024-11-20 08:26:12 +01:00
foreach ($child->children() as $key => $track) {
$trackData = [
'title' => (string) $track->title(),
'slug' => (string) $track->slug(),
2025-06-06 12:22:59 +02:00
'backgroundColor' => (string) $track->backgroundColor(),
'files' => [],
];
foreach ($track->views()->toFiles() as $view) {
$trackData['files'][] = getFileData($view, true);
}
if ($track->group()->isNotEmpty()) {
$files['dynamic'][$track->group()->value()][] = $trackData;
} else {
2025-10-02 12:42:15 +02:00
$files['dynamic']['Autres pistes'][] = $trackData;
}
2024-11-20 08:26:12 +01:00
}
2025-10-02 12:42:15 +02:00
if (isset($files['dynamic']['Autres pistes'])) {
$others = $files['dynamic']['Autres pistes'];
unset($files['dynamic']['Autres pistes']);
$files['dynamic']['Autres pistes'] = $others;
}
}
2024-11-20 08:26:12 +01:00
if ($child->rawGlass()->isNotEmpty() || $child->finishedGlass()->isNotEmpty()) {
$files['static'] = [];
}
2024-11-20 08:26:12 +01:00
if ($child->rawGlass()->isNotEmpty()) {
$files['static']['rawGlass'] = getFileData($child->rawGlass()->toFile());
}
if ($child->finishedGlass()->isNotEmpty()) {
$files['static']['finishedGlass'] = getFileData($child->finishedGlass()->toFile());
}
2024-09-26 15:46:13 +02:00
}
2024-12-11 14:37:06 +01:00
private function handlePhysicalSampleStep($child, &$files, &$uri) {
$uri = $child->parent()->uri() . '?dialog=' . $child->slug();
if ($child->media()->isNotEmpty()) {
foreach ($child->media()->toFiles() as $file) {
$files[] = getFileData($file);
}
}
}
public function getStepLabel() {
$stepsLabel = [
"clientBrief" => "brief",
"proposal" => "offre commerciale",
"extendedBrief" => "brief enrichi",
"industrialIdeation" => "idéation industrielle",
"virtualSample" => "échantillon virtuel",
"physicalSample" => "échantillon physique",
];
return $stepsLabel[$this->currentStep()->value()];
}
2024-11-22 07:53:43 +01:00
// public function printManagers() {
// return A::implode($this->managers()->toUsers()->pluck('name'), ', ');
// }
public function managers() {
return kirby()->users()->filter(function($user) {
2024-12-04 15:09:22 +01:00
if ($user->role() != 'admin' && $user->projects()->isEmpty()) {
2024-11-22 07:53:43 +01:00
return false;
}
2024-12-04 15:09:22 +01:00
return $user->role() == 'admin' || $user->projects()->toPages()->has($this);
2024-11-22 07:53:43 +01:00
});
}
2024-09-26 15:46:13 +02:00
}