Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
colintucker committed May 14, 2018
2 parents 10c60a7 + 1bbdacf commit e7e6c7c
Showing 1 changed file with 144 additions and 43 deletions.
187 changes: 144 additions & 43 deletions src/Pages/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use SilverStripe\ORM\FieldType\DBDate;
use SilverStripe\Security\Member;
use SilverWare\Blog\Model\BlogTag;
use SilverWare\Crumbs\Breadcrumb;
use PageController;

/**
Expand Down Expand Up @@ -141,14 +142,9 @@ public function archive(HTTPRequest $request)
return $this->httpError(404);
}

// Define Date Value:
// Define Date Message:

$dateMessage = $year;

if ($month) {
$date = DBDate::create()->setValue(strtotime(sprintf('%d-%02d', $year, $month)));
$dateMessage = $date->format('LLLL y');
}
$dateMessage = $month ? $this->getFormattedMonthAndYear($month, $year) : $year;

// Define Filter Message:

Expand Down Expand Up @@ -193,27 +189,21 @@ public function archive(HTTPRequest $request)
*/
public function author(HTTPRequest $request)
{
// Obtain Author Segment:
// Obtain Author Object:

if ($segment = $request->param('Author')) {
if ($author = $this->getCurrentAuthor()) {

// Obtain Tag Object:
// Filter Posts by Author Post IDs:

if ($author = Member::get()->find('URLSegment', $segment)) {

// Filter Posts by Author Post IDs:

$this->data()->addListFilter(['ID' => ($author->BlogPosts()->column('ID') ?: null)]);

// Add Filter Alert to List:

$this->data()->addListAlert(
sprintf(_t(__CLASS__ . '.SHOWINGPOSTSBYAUTHOR', 'Showing posts by author "%s"'), $author->Name)
);

return [];

}
$this->data()->addListFilter(['ID' => ($author->BlogPosts()->column('ID') ?: null)]);

// Add Filter Alert to List:

$this->data()->addListAlert(
sprintf(_t(__CLASS__ . '.SHOWINGPOSTSBYAUTHOR', 'Showing posts by author "%s"'), $author->Name)
);

return [];

}

Expand All @@ -231,27 +221,21 @@ public function author(HTTPRequest $request)
*/
public function tag(HTTPRequest $request)
{
// Obtain Tag Segment:
// Obtain Tag Object:

if ($segment = $request->param('Tag')) {
if ($tag = $this->getCurrentTag()) {

// Obtain Tag Object:
// Filter Posts by Tagged Post IDs:

if ($tag = BlogTag::get()->find('URLSegment', $segment)) {

// Filter Posts by Tagged Post IDs:

$this->data()->addListFilter(['ID' => $tag->Posts()->column('ID')]);

// Add Filter Alert to List:

$this->data()->addListAlert(
sprintf(_t(__CLASS__ . '.SHOWINGPOSTSTAGGEDWITH', 'Showing posts tagged with "%s"'), $tag->Title)
);

return [];

}
$this->data()->addListFilter(['ID' => $tag->Posts()->column('ID')]);

// Add Filter Alert to List:

$this->data()->addListAlert(
sprintf(_t(__CLASS__ . '.SHOWINGPOSTSTAGGEDWITH', 'Showing posts tagged with "%s"'), $tag->Title)
);

return [];

}

Expand All @@ -270,6 +254,110 @@ public function getBlog()
return $this->data();
}

/**
* Answers the author associated with the current request.
*
* @return Member
*/
public function getCurrentAuthor()
{
if ($segment = $this->getRequest()->param('Author')) {
return Member::get()->find('URLSegment', $segment);
}
}

/**
* Answers the blog tag associated with the current request.
*
* @return BlogTag
*/
public function getCurrentTag()
{
if ($segment = $this->getRequest()->param('Tag')) {
return BlogTag::get()->find('URLSegment', $segment);
}
}

/**
* Answers a list of extra breadcrumb items for the template.
*
* @return ArrayList
*/
public function getExtraBreadcrumbItems()
{
$items = parent::getExtraBreadcrumbItems();

if ($crumb = $this->getExtraBreadcrumb()) {
$items->push($crumb);
}

return $items;
}

/**
* Answers an extra breadcrumb object for the current request.
*
* @return Breadcrumb
*/
public function getExtraBreadcrumb()
{
// Answer Tag Breadcrumb:

if ($tag = $this->getCurrentTag()) {

return Breadcrumb::create(
$tag->Link,
sprintf(
_t(
__CLASS__ . '.POSTSTAGGEDWITH',
'Posts tagged with "%s"'
),
$tag->Title
)
);

}

// Answer Author Breadcrumb:

if ($author = $this->getCurrentAuthor()) {

return Breadcrumb::create(
$this->data()->getAuthorLink($author),
sprintf(
_t(
__CLASS__ . '.POSTSBYAUTHOR',
'Posts by author "%s"'
),
$author->Name
)
);

}

// Answer Archive Breadcrumb:

if ($year = $this->getRequest()->param('Year')) {

$year = (integer) $year;
$month = (integer) $this->getRequest()->param('Month');

$label = $month ? $this->getFormattedMonthAndYear($month, $year) : $year;

return Breadcrumb::create(
$this->data()->getArchiveLink($year, $month),
sprintf(
_t(
__CLASS__ . '.POSTSFOR',
'Posts for %s'
),
$label
)
);

}
}

/**
* Performs initialisation before any action is called on the receiver.
*
Expand All @@ -287,4 +375,17 @@ protected function init()
RSSFeed::linkToFeed($this->Link('rss'), $this->FeedTitle);
}
}

/**
* Answers a formatted string for the given month and year.
*
* @param integer $month
* @param integer $year
*
* @return string
*/
protected function getFormattedMonthAndYear($month, $year)
{
return DBDate::create()->setValue(strtotime(sprintf('%d-%02d', $year, $month)))->format('LLLL y');
}
}

0 comments on commit e7e6c7c

Please sign in to comment.