diff --git a/controller/action/ContentActions.class.php b/controller/action/ContentActions.class.php index 8e12e0a3..fe1f6d3a 100644 --- a/controller/action/ContentActions.class.php +++ b/controller/action/ContentActions.class.php @@ -76,7 +76,7 @@ class ContentActions extends Actions try { $post = Post::load(static::SLUG_NEWS . '/' . ltrim($slug, '/')); - } catch (PostNotFoundException $e) { + } catch (PostException $e) { return NavActions::execute404(); } @@ -130,7 +130,7 @@ class ContentActions extends Actions try { $post = Post::load(static::SLUG_FAQ . '/' . ltrim($slug, '/')); - } catch (PostNotFoundException $e) { + } catch (PostException $e) { return Controller::redirect('/' . static::SLUG_FAQ); } return ['content/faq-post', ['post' => $post]]; @@ -154,7 +154,7 @@ class ContentActions extends Actions try { $post = Post::load(static::SLUG_CREDIT_REPORTS . '/' . $year . '-Q' . $quarter); - } catch (PostNotFoundException $e) { + } catch (PostException $e) { return Controller::redirect('/' . static::SLUG_CREDIT_REPORTS); } $metadata = $post->getMetadata(); diff --git a/model/Post.class.php b/model/Post.class.php index a39a8acb..f6f17afe 100644 --- a/model/Post.class.php +++ b/model/Post.class.php @@ -1,10 +1,12 @@ getDate()->format('Y-m-d'), $a->getDate()->format('Y-m-d')); - }); - break; + case static::SORT_DATE_DESC: + usort($posts, function (Post $a, Post $b) { + return strcasecmp($b->getDate()->format('Y-m-d'), $a->getDate()->format('Y-m-d')); + }); + break; - case static::SORT_ORD_ASC: - usort($posts, function (Post $a, Post $b) { - $aMeta = $a->getMetadata(); - $bMeta = $b->getMetadata(); - if (!isset($aMeta['order']) && !isset($bMeta['order'])) { - return $a->getTitle() < $b->getTitle() ? -1 : 1; - } - if (isset($aMeta['order']) && isset($bMeta['order'])) { - return $aMeta['order'] < $bMeta['order'] ? -1 : 1; - } - return isset($aMeta['order']) ? -1 : 1; - }); - break; - } + case static::SORT_ORD_ASC: + usort($posts, function (Post $a, Post $b) { + $aMeta = $a->getMetadata(); + $bMeta = $b->getMetadata(); + if (!isset($aMeta['order']) && !isset($bMeta['order'])) { + return $a->getTitle() < $b->getTitle() ? -1 : 1; + } + if (isset($aMeta['order']) && isset($bMeta['order'])) { + return $aMeta['order'] < $bMeta['order'] ? -1 : 1; + } + return isset($aMeta['order']) ? -1 : 1; + }); + break; + } } return $posts; } @@ -185,7 +193,7 @@ class Post public function getDate() { - return $this->date; + return $this->date ?? new DateTime(); } public function getCover() @@ -230,14 +238,28 @@ class Post { $slugs = array_keys(Post::getSlugMap($this->postType)); $postNum = $this->getPostNum(); - return $postNum === false || $postNum === 0 ? null : Post::load($this->postType . '/' . $slugs[$postNum-1]); + if ($postNum === false || $postNum === 0) { + return null; + } + try { + return Post::load($this->postType . '/' . $slugs[$postNum-1]); + } catch (PostException $e) { + return null; + } } public function getNextPost() { $slugs = array_keys(Post::getSlugMap($this->postType)); $postNum = $this->getPostNum(); - return $postNum === false || $postNum >= count($slugs)-1 ? null : Post::load($this->postType . '/' . $slugs[$postNum+1]); + if ($postNum === false || $postNum >= count($slugs)-1) { + return null; + } + try { + return Post::load($this->postType . '/' . $slugs[$postNum+1]); + } catch (PostException $e) { + return null; + } } public function hasAuthor()