'Int', 'ArchiveType' => 'Enum(\'Monthly,Yearly\', \'Monthly\')', ]; /** * @var array */ private static $defaults = [ 'NumberOfMonths' => 12, ]; /** * @var array */ private static $has_one = [ 'Blog' => Blog::class, ]; /** * {@inheritdoc} */ public function getCMSFields() { $this->beforeUpdateCMSFields(function ($fields) { /** * @var Enum $archiveType */ $archiveType = $this->dbObject('ArchiveType'); $type = $archiveType->enumValues(); foreach ($type as $k => $v) { $type[$k] = _t(__CLASS__ .'.' . ucfirst(strtolower($v)), $v); } /** * @var FieldList $fields */ $fields->merge([ DropdownField::create( 'BlogID', _t(__CLASS__ . '.Blog', 'Blog'), Blog::get()->map() ), DropdownField::create('ArchiveType', _t(__CLASS__ . '.ArchiveType', 'ArchiveType'), $type), NumericField::create('NumberToDisplay', _t(__CLASS__ . '.NumberToDisplay', 'No. to Display')) ]); }); return parent::getCMSFields(); } /** * Returns a list of months where blog posts are present. * * @return ArrayList */ public function getArchive() { $format = ($this->ArchiveType == 'Yearly') ? '%Y' : '%Y-%m'; $publishDate = DB::get_conn()->formattedDatetimeClause('"PublishDate"', $format); $fields = [ 'PublishDate' => $publishDate, 'Total' => "Count('PublishDate')" ]; $stage = Versioned::get_stage(); $suffix = ($stage == 'Stage') ? '' : "_{$stage}"; $query = SQLSelect::create($fields, "BlogPost{$suffix}") ->addGroupBy($publishDate) ->addOrderBy('PublishDate Desc') ->addWhere(['PublishDate < ?' => DBDatetime::now()->Format('Y-m-d')]); $posts = $query->execute(); $result = ArrayList::create(); while ($next = $posts->next()) { $date = DBDate::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(ArrayData::create([ 'Title' => $title, 'Link' => Controller::join_links($this->Blog()->Link('archive'), $year, $month) ])); } $this->extend('updateGetArchive', $result); return $result; } }