From 0d6b44aa44377cbabbc7a538acb29a74092088b8 Mon Sep 17 00:00:00 2001 From: Franco Springveldt Date: Sat, 17 Jun 2017 19:20:29 +1200 Subject: [PATCH 1/2] FIX mysql >= 5.7 sql_mode=only_full_group_by error --- code/widgets/BlogArchiveWidget.php | 69 ++++++++++++++---------------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/code/widgets/BlogArchiveWidget.php b/code/widgets/BlogArchiveWidget.php index 66cdd45..2755664 100644 --- a/code/widgets/BlogArchiveWidget.php +++ b/code/widgets/BlogArchiveWidget.php @@ -84,52 +84,47 @@ class BlogArchiveWidget extends Widget /** * Returns a list of months where blog posts are present. * - * @return DataList + * @return ArrayList */ public function getArchive() { - $query = $this->Blog()->getBlogPosts()->dataQuery(); + $format = ($this->ArchiveType == 'Yearly') ? '%Y' : '%Y-%m'; + $publishDate = DB::get_conn()->formattedDatetimeClause('"PublishDate"', $format); + $fields = array( + 'PublishDate' => $publishDate, + 'Total' => "Count('PublishDate')" + ); - if ($this->ArchiveType == 'Yearly') { - $query->groupBy('DATE_FORMAT("PublishDate", \'%Y\')'); - } else { - $query->groupBy('DATE_FORMAT("PublishDate", \'%Y-%M\')'); - } + $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())); - $posts = $this->Blog()->getBlogPosts()->setDataQuery($query); + $posts = $query->execute(); + $result = new ArrayList(); + while ($next = $posts->next()) { + $date = Date::create(); + $date->setValue(strtotime($next['PublishDate'])); + $year = $date->Format('Y'); - 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) - ))); + 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; } } From 236bf6d8fc86466a953019319202440f071c8133 Mon Sep 17 00:00:00 2001 From: Franco Springveldt Date: Wed, 9 Aug 2017 09:49:58 +1200 Subject: [PATCH 2/2] FIX added date formatting --- code/widgets/BlogArchiveWidget.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/widgets/BlogArchiveWidget.php b/code/widgets/BlogArchiveWidget.php index 2755664..e6b7b8f 100644 --- a/code/widgets/BlogArchiveWidget.php +++ b/code/widgets/BlogArchiveWidget.php @@ -100,7 +100,7 @@ class BlogArchiveWidget extends Widget $query = SQLSelect::create($fields, "BlogPost{$suffix}") ->addGroupBy($publishDate) ->addOrderBy('PublishDate Desc') - ->addWhere(array('PublishDate < ?' => SS_Datetime::now())); + ->addWhere(array('PublishDate < ?' => SS_Datetime::now()->Format('Y-m-d'))); $posts = $query->execute(); $result = new ArrayList();