Skip to content

Commit

Permalink
Merge pull request #446 from creative-commoners/issue/ss-widgets/144
Browse files Browse the repository at this point in the history
FIX select list not in group-by clause error
  • Loading branch information
dhensby committed Aug 17, 2017
2 parents 58a4693 + 236bf6d commit 619df86
Showing 1 changed file with 35 additions and 40 deletions.
75 changes: 35 additions & 40 deletions code/widgets/BlogArchiveWidget.php
Expand Up @@ -84,52 +84,47 @@ public function getCMSFields()
/**
* Returns a list of months where blog posts are present.
*
* @return DataList
* @return ArrayList
*/
public function getArchive()
{
$query = $this->Blog()->getBlogPosts()->dataQuery();

if ($this->ArchiveType == 'Yearly') {
$query->groupBy('DATE_FORMAT("PublishDate", \'%Y\')');
} else {
$query->groupBy('DATE_FORMAT("PublishDate", \'%Y-%M\')');
}

$posts = $this->Blog()->getBlogPosts()->setDataQuery($query);

if ($this->NumberToDisplay > 0) {
$posts = $posts->limit($this->NumberToDisplay);
}

$archive = new ArrayList();

if ($posts->count() > 0) {
foreach ($posts as $post) {
/**
* @var BlogPost $post
*/
$date = Date::create();
$date->setValue($post->PublishDate);

if ($this->ArchiveType == 'Yearly') {
$year = $date->Format("Y");
$month = null;
$title = $year;
} else {
$year = $date->Format("Y");
$month = $date->Format("m");
$title = $date->FormatI18N("%B %Y");
}

$archive->push(new ArrayData(array(
'Title' => $title,
'Link' => Controller::join_links($this->Blog()->Link('archive'), $year, $month)
)));
$format = ($this->ArchiveType == 'Yearly') ? '%Y' : '%Y-%m';
$publishDate = DB::get_conn()->formattedDatetimeClause('"PublishDate"', $format);
$fields = array(
'PublishDate' => $publishDate,
'Total' => "Count('PublishDate')"
);

$stage = Versioned::current_stage();
$suffix = ($stage == 'Stage') ? '' : "_{$stage}";
$query = SQLSelect::create($fields, "BlogPost{$suffix}")
->addGroupBy($publishDate)
->addOrderBy('PublishDate Desc')
->addWhere(array('PublishDate < ?' => SS_Datetime::now()->Format('Y-m-d')));

$posts = $query->execute();
$result = new ArrayList();
while ($next = $posts->next()) {
$date = Date::create();
$date->setValue(strtotime($next['PublishDate']));
$year = $date->Format('Y');

if ($this->ArchiveType == 'Yearly') {
$month = null;
$title = $year;
} else {
$month = $date->Format('m');
$title = $date->FormatI18N('%B %Y');
}

$result->push(new ArrayData(array(
'Title' => $title,
'Link' => Controller::join_links($this->Blog()->Link('archive'), $year, $month)
)));
}

return $archive;
$this->extend('updateGetArchive', $result);
return $result;
}
}

Expand Down

0 comments on commit 619df86

Please sign in to comment.