diff --git a/public/site/config/config.php b/public/site/config/config.php index 00a6aa9..aab999b 100644 --- a/public/site/config/config.php +++ b/public/site/config/config.php @@ -2,6 +2,12 @@ return [ 'debug' => true, + 'cache' => [ + 'pages' => [ + 'active' => true + ], + 'api' => true + ], 'smartypants' => true, 'locale' => 'fr_FR.utf-8', 'date' => [ @@ -34,5 +40,6 @@ return [ 'hooks' => [ 'page.create:after' => require_once(__DIR__ . '/hooks/create-steps.php'), 'page.delete:before' => require_once(__DIR__ . '/hooks/delete-steps.php'), + 'page.update:after' => require_once(__DIR__ . '/hooks/regenerate-project-steps-cache.php') ], ]; diff --git a/public/site/config/hooks/regenerate-project-steps-cache.php b/public/site/config/hooks/regenerate-project-steps-cache.php new file mode 100644 index 0000000..34c3b57 --- /dev/null +++ b/public/site/config/hooks/regenerate-project-steps-cache.php @@ -0,0 +1,7 @@ +template() == 'project' || $newPage->parents()->findBy('template', 'project')) { + $newPage->getSteps(); + } +}; \ No newline at end of file diff --git a/public/site/models/project.php b/public/site/models/project.php index ea2fea8..7e74452 100644 --- a/public/site/models/project.php +++ b/public/site/models/project.php @@ -4,19 +4,27 @@ use adrienpayet\notifications\NotificationsPage; class ProjectPage extends NotificationsPage { public function getSteps() { - $steps = []; + $apiCache = kirby()->cache('api'); + $stepsData = $apiCache->get($this->slug() . '_' . 'steps'); - foreach ($this->children() as $child) { - try { - $steps[] = $this->createStep($child); - } catch (\Throwable $th) { - //throw $th; + if ($stepsData === null) { + $steps = []; + + foreach ($this->children() as $child) { + try { + $steps[] = $this->createStep($child); + } catch (\Throwable $th) { + //throw $th; + } } + + usort($steps, fn($a, $b) => $a['index'] <=> $b['index']); + + $apiCache->set($this->slug() . '_' . 'steps', $steps); + return $steps; + } else { + return $stepsData; } - - usort($steps, fn($a, $b) => $a['index'] <=> $b['index']); - - return $steps; } private function createStep($child) {