1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Blog\Widgets; |
4
|
|
|
|
5
|
|
|
if (!class_exists('\\SilverStripe\\Widgets\\Model\\Widget')) { |
6
|
|
|
return; |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
use SilverStripe\Blog\Model\Blog; |
10
|
|
|
use SilverStripe\Control\Controller; |
11
|
|
|
use SilverStripe\Forms\DropdownField; |
12
|
|
|
use SilverStripe\Forms\NumericField; |
13
|
|
|
use SilverStripe\Widgets\Model\Widget; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @method Blog Blog() |
17
|
|
|
* |
18
|
|
|
* @property string $ArchiveType |
19
|
|
|
* @property int $NumberToDisplay |
20
|
|
|
*/ |
21
|
|
|
class BlogArchiveWidget extends Widget |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private static $title = 'Archive'; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private static $cmsTitle = 'Archive'; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private static $description = 'Displays an archive list of posts.'; |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private static $db = array( |
|
|
|
|
42
|
|
|
'NumberToDisplay' => 'Int', |
43
|
|
|
'ArchiveType' => 'Enum(\'Monthly,Yearly\', \'Monthly\')', |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private static $defaults = array( |
|
|
|
|
50
|
|
|
'NumberOfMonths' => 12, |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private static $has_one = array( |
|
|
|
|
57
|
|
|
'Blog' => Blog::class, |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function getCMSFields() |
64
|
|
|
{ |
65
|
|
|
$this->beforeUpdateCMSFields(function ($fields) { |
66
|
|
|
/** |
67
|
|
|
* @var Enum $archiveType |
68
|
|
|
*/ |
69
|
|
|
$archiveType = $this->dbObject('ArchiveType'); |
70
|
|
|
|
71
|
|
|
$type = $archiveType->enumValues(); |
72
|
|
|
|
73
|
|
|
foreach ($type as $k => $v) { |
74
|
|
|
$type[$k] = _t('BlogArchiveWidget.' . ucfirst(strtolower($v)), $v); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var FieldList $fields |
79
|
|
|
*/ |
80
|
|
|
$fields->merge(array( |
81
|
|
|
DropdownField::create( |
82
|
|
|
'BlogID', |
83
|
|
|
_t('BlogArchiveWidget.Blog', 'Blog'), |
84
|
|
|
Blog::get()->map() |
85
|
|
|
), |
86
|
|
|
DropdownField::create('ArchiveType', _t('BlogArchiveWidget.ArchiveType', 'ArchiveType'), $type), |
87
|
|
|
NumericField::create('NumberToDisplay', _t('BlogArchiveWidget.NumberToDisplay', 'No. to Display')) |
88
|
|
|
)); |
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
return parent::getCMSFields(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns a list of months where blog posts are present. |
96
|
|
|
* |
97
|
|
|
* @return DataList |
98
|
|
|
*/ |
99
|
|
|
public function getArchive() |
100
|
|
|
{ |
101
|
|
|
$query = $this->Blog()->getBlogPosts()->dataQuery(); |
102
|
|
|
|
103
|
|
|
if ($this->ArchiveType == 'Yearly') { |
104
|
|
|
$query->groupBy('DATE_FORMAT("PublishDate", \'%Y\')'); |
105
|
|
|
} else { |
106
|
|
|
$query->groupBy('DATE_FORMAT("PublishDate", \'%Y-%M\')'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$posts = $this->Blog()->getBlogPosts()->setDataQuery($query); |
110
|
|
|
|
111
|
|
|
if ($this->NumberToDisplay > 0) { |
112
|
|
|
$posts = $posts->limit($this->NumberToDisplay); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$archive = new ArrayList(); |
116
|
|
|
|
117
|
|
|
if ($posts->count() > 0) { |
118
|
|
|
foreach ($posts as $post) { |
119
|
|
|
/** |
120
|
|
|
* @var BlogPost $post |
121
|
|
|
*/ |
122
|
|
|
$date = Date::create(); |
123
|
|
|
$date->setValue($post->PublishDate); |
124
|
|
|
|
125
|
|
|
if ($this->ArchiveType == 'Yearly') { |
126
|
|
|
$year = $date->Format("Y"); |
127
|
|
|
$month = null; |
128
|
|
|
$title = $year; |
129
|
|
|
} else { |
130
|
|
|
$year = $date->Format("Y"); |
131
|
|
|
$month = $date->Format("m"); |
132
|
|
|
$title = $date->FormatI18N("%B %Y"); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$archive->push(new ArrayData(array( |
136
|
|
|
'Title' => $title, |
137
|
|
|
'Link' => Controller::join_links($this->Blog()->Link('archive'), $year, $month) |
138
|
|
|
))); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $archive; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.