silverstripe-blog/widgets/BlogArchiveWidget.php

86 lines
2.3 KiB
PHP
Raw Normal View History

2013-08-04 18:38:26 +02:00
<?php
2013-10-10 00:09:28 +02:00
if(class_exists("Widget")) {
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
class BlogArchiveWidget extends Widget {
private static $title = "Archive";
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
private static $cmsTitle = "Archive";
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
private static $description = "Displays an archive list of posts.";
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
private static $db = array(
"NumberToDisplay" => "Int",
"Type" => "Enum('Monthly, Yearly', 'Monthly')"
);
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
private static $defaults = array(
"NumberOfMonths" => 12
);
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
private static $has_one = array(
"Blog" => "Blog",
);
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
public function getCMSFields() {
$fields = parent::getCMSFields();
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
$type = $this->dbObject("Type")->enumValues();
foreach($type as $k => $v) {
$type[$k] = _t("BlogArchiveWidget." . ucfirst(strtolower($v)), $v);
}
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
$fields->push(DropdownField::create("BlogID", _t("BlogArchiveWidget.Blog", "Blog"), Blog::get()->map()));
$fields->push(DropdownField::create("Type", _t("BlogArchiveWidget.Type", "Type"), $type));
$fields->push(NumericField::create("NumberToDisplay", _t("BlogArchiveWidget.NumberToDisplay", "No. to Display")));
return $fields;
}
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
/**
* Returns a list of months where blog posts are present.
*
* @return DataList
**/
public function getArchive() {
$query = $this->Blog()->getBlogPosts()->dataQuery();
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
if($this->Type == "Yearly") {
$query->groupBy("DATE_FORMAT(PublishDate, '%Y')");
} else {
$query->groupBy("DATE_FORMAT(PublishDate, '%Y-%M')");
}
$articles = $this->Blog()->getBlogPosts()->setDataQuery($query);
if($this->NumberToDisplay > 0) $articles = $articles->limit($this->NumberToDisplay);
$archive = new ArrayList();
if($articles->count() > 0) {
foreach($articles as $article) {
if($this->Type == "Yearly") {
$year = date('Y', strtotime($article->PublishDate));
$month = null;
$title = $year;
} else {
$year = date('Y', strtotime($article->PublishDate));
$month = date('m', strtotime($article->PublishDate));
$title = date('F Y', strtotime($article->PublishDate));
}
$archive->push(new ArrayData(array(
"Title" => $title,
"Link" => Controller::join_links($this->Blog()->Link("archive"), $year, $month)
)));
2013-08-04 18:38:26 +02:00
}
}
2013-10-10 00:09:28 +02:00
return $archive;
2013-08-04 18:38:26 +02:00
}
2013-10-10 00:09:28 +02:00
2013-08-04 18:38:26 +02:00
}
2013-10-10 00:09:28 +02:00
class BlogArchiveWidget_Controller extends Widget_Controller {
}
2013-08-04 18:38:26 +02:00
2013-10-10 00:09:28 +02:00
}