|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Blog\Widgets; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Blog\Model\Blog; |
|
6
|
|
|
use SilverStripe\Control\Controller; |
|
7
|
|
|
use SilverStripe\Forms\DropdownField; |
|
8
|
|
|
use SilverStripe\Forms\FieldList; |
|
9
|
|
|
use SilverStripe\Forms\NumericField; |
|
10
|
|
|
use SilverStripe\ORM\ArrayList; |
|
11
|
|
|
use SilverStripe\ORM\DB; |
|
12
|
|
|
use SilverStripe\ORM\FieldType\DBDate; |
|
13
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
|
14
|
|
|
use SilverStripe\ORM\FieldType\DBEnum; |
|
15
|
|
|
use SilverStripe\ORM\Queries\SQLSelect; |
|
16
|
|
|
use SilverStripe\Versioned\Versioned; |
|
17
|
|
|
use SilverStripe\View\ArrayData; |
|
18
|
|
|
use SilverStripe\Widgets\Model\Widget; |
|
19
|
|
|
|
|
20
|
|
|
if (!class_exists(Widget::class)) { |
|
21
|
|
|
return; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @method Blog Blog() |
|
26
|
|
|
* |
|
27
|
|
|
* @property string $ArchiveType |
|
28
|
|
|
* @property int $NumberToDisplay |
|
29
|
|
|
*/ |
|
30
|
|
|
class BlogArchiveWidget extends Widget |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private static $title = 'Archive'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private static $cmsTitle = 'Archive'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private static $description = 'Displays an archive list of posts.'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
private static $db = [ |
|
51
|
|
|
'NumberToDisplay' => 'Int', |
|
52
|
|
|
'ArchiveType' => 'Enum(\'Monthly,Yearly\', \'Monthly\')', |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
private static $defaults = [ |
|
59
|
|
|
'NumberOfMonths' => 12, |
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var array |
|
64
|
|
|
*/ |
|
65
|
|
|
private static $has_one = [ |
|
66
|
|
|
'Blog' => Blog::class, |
|
67
|
|
|
]; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
private static $table_name = 'BlogArchiveWidget'; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getCMSFields() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->beforeUpdateCMSFields(function ($fields) { |
|
80
|
|
|
/** |
|
81
|
|
|
* @var DBEnum $archiveType |
|
82
|
|
|
*/ |
|
83
|
|
|
$archiveType = $this->dbObject('ArchiveType'); |
|
84
|
|
|
|
|
85
|
|
|
$type = $archiveType->enumValues(); |
|
86
|
|
|
|
|
87
|
|
|
foreach ($type as $k => $v) { |
|
88
|
|
|
$type[$k] = _t(__CLASS__ .'.' . ucfirst(strtolower($v)), $v); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @var FieldList $fields |
|
93
|
|
|
*/ |
|
94
|
|
|
$fields->merge([ |
|
95
|
|
|
DropdownField::create( |
|
96
|
|
|
'BlogID', |
|
97
|
|
|
_t(__CLASS__ . '.Blog', 'Blog'), |
|
98
|
|
|
Blog::get()->map() |
|
99
|
|
|
), |
|
100
|
|
|
DropdownField::create('ArchiveType', _t(__CLASS__ . '.ArchiveType', 'ArchiveType'), $type), |
|
101
|
|
|
NumericField::create('NumberToDisplay', _t(__CLASS__ . '.NumberToDisplay', 'No. to Display')) |
|
102
|
|
|
]); |
|
103
|
|
|
}); |
|
104
|
|
|
|
|
105
|
|
|
return parent::getCMSFields(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns a list of months where blog posts are present. |
|
110
|
|
|
* |
|
111
|
|
|
* @return ArrayList |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getArchive() |
|
114
|
|
|
{ |
|
115
|
|
|
$format = ($this->ArchiveType == 'Yearly') ? '%Y' : '%Y-%m'; |
|
116
|
|
|
$publishDate = DB::get_conn()->formattedDatetimeClause('"PublishDate"', $format); |
|
117
|
|
|
$fields = [ |
|
118
|
|
|
'PublishDate' => $publishDate, |
|
119
|
|
|
'Total' => "COUNT('\"PublishDate\"')" |
|
120
|
|
|
]; |
|
121
|
|
|
|
|
122
|
|
|
$stage = Versioned::get_stage(); |
|
123
|
|
|
$suffix = ($stage === Versioned::LIVE) ? '_' . Versioned::LIVE : ''; |
|
124
|
|
|
$query = SQLSelect::create($fields, '"BlogPost' . $suffix . '"') |
|
125
|
|
|
->addGroupBy($publishDate) |
|
126
|
|
|
->addOrderBy('"PublishDate" DESC') |
|
127
|
|
|
->addLeftJoin('SiteTree' . $suffix, '"SiteTree' . $suffix . '"."ID" = "BlogPost' . $suffix . '"."ID"') |
|
128
|
|
|
->addWhere([ |
|
129
|
|
|
'"PublishDate" <= ?' => DBDatetime::now()->Format(DBDatetime::ISO_DATETIME), |
|
130
|
|
|
'"SiteTree' . $suffix . '"."ParentID"' => $this->BlogID, |
|
131
|
|
|
]); |
|
132
|
|
|
|
|
133
|
|
|
$posts = $query->execute(); |
|
134
|
|
|
$result = ArrayList::create(); |
|
135
|
|
|
foreach ($posts as $post) { |
|
136
|
|
|
if ($this->ArchiveType == 'Yearly') { |
|
137
|
|
|
$year = $post['PublishDate']; |
|
138
|
|
|
$month = null; |
|
139
|
|
|
$title = $year; |
|
140
|
|
|
} else { |
|
141
|
|
|
$date = DBDate::create(); |
|
142
|
|
|
$date->setValue(strtotime($post['PublishDate'])); |
|
143
|
|
|
|
|
144
|
|
|
$year = $date->Format('y'); |
|
145
|
|
|
$month = $date->Format('MM'); |
|
146
|
|
|
$title = $date->Format('MMMM y'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$result->push(ArrayData::create([ |
|
150
|
|
|
'Title' => $title, |
|
151
|
|
|
'Link' => Controller::join_links($this->Blog()->Link('archive'), $year, $month) |
|
152
|
|
|
])); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$this->extend('updateGetArchive', $result); |
|
156
|
|
|
|
|
157
|
|
|
return $result; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|