loadLinkedContent($pageHasContent, 'page', $id), (array)$this->loadLinkedContent($sectionHasContent, 'section', $page['section_id'])); // Exit if nothing was found if (!_exists($contents)) { parent::throw404(); } $sideContent = in_array('2', array_column($contents, 'type')); $this->router->service()->contents = $contents; $this->router->service()->sideContent = $sideContent; $this->view('content', $title, $metaDescription); } //-------------------------------------// /** * Load all content blocks linked to the provided Model. * * @param Model $model * @param string $column * @param int $id * * @return null|array */ protected function loadLinkedContent(Model $model, string $column, int $id): ?array { // Load all the Model <-> Content link data $hasContent = $model::selectAll('*', " WHERE {$column}_id = :id ORDER BY {$model->getSort()} ASC", [ [':id', $id, \PDO::PARAM_INT], ] ); // Exit if nothing was found if (!_exists($hasContent)) { return null; } // Get all the content $contentIds = array_column($hasContent, 'content_id'); $contents = ContentModel::findAll($contentIds); // Exit if nothing was found if (!_exists($contents)) { return null; } // Remove inactive content foreach ($contents as $key => $content) { if ($content['active'] == "0") { unset($contents[$key]); } } $contents = array_values($contents); return $contents; } /** * Render page view with title and meta description. * * @param string $view * @param string $pageTitle * @param string $metaDescription * * @return void */ protected function view( string $view = '', string $pageTitle = '', string $metaDescription = ''): void { if ($view != '') { $view = $this->fileExists($this->views . $view . '.php'); } if ($this->page == null) { if ($view == '' && $this->section == '') { // / $view = $this->fileExists($this->views . 'home.php'); } if ($view == '') { // /example.php $view = $this->fileExists($this->views . $this->section . '.php'); } if ($view == '') { // /example/index.php $view = $this->fileExists($this->views . $this->section . '/index.php'); } } else if ($view == '') { // /example/my-page.php $view = $this->fileExists($this->views . $this->section . '/' . $this->page . '.php'); } if ($view != '') { $pageTitle != '' ? $this->router->service()->pageTitle = $pageTitle : $this->router->service()->pageTitle = ucfirst(str_replace('-', ' ', $this->page)); $this->router->service()->metaDescription = $metaDescription; $this->router->service()->render($view); } else { parent::throw404(); } } //-------------------------------------// /** * Loop back filename if it exists, empty string otherwise. * * @param string $file * * @return string */ private function fileExists(string $file): string { return file_exists($file) ? $file : ''; } } // @Todo // - Fix line 32, breaks if no DB content! // - Implement page.description (meta) // - Use page.title instead of content.title (?)