Completed
Pull Request — master (#421)
by Robbie
02:31
created

BlogArchiveWidget::getArchive()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 45
rs 8.439
cc 6
eloc 26
nc 8
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 21 and the first side effect is on line 6.

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.

Loading history...
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';
0 ignored issues
show
Unused Code introduced by
The property $title is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
27
28
    /**
29
     * @var string
30
     */
31
    private static $cmsTitle = 'Archive';
0 ignored issues
show
Unused Code introduced by
The property $cmsTitle is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
32
33
    /**
34
     * @var string
35
     */
36
    private static $description = 'Displays an archive list of posts.';
0 ignored issues
show
Unused Code introduced by
The property $description is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
37
38
    /**
39
     * @var array
40
     */
41
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
42
        'NumberToDisplay' => 'Int',
43
        'ArchiveType' => 'Enum(\'Monthly,Yearly\', \'Monthly\')',
44
    );
45
46
    /**
47
     * @var array
48
     */
49
    private static $defaults = array(
0 ignored issues
show
Unused Code introduced by
The property $defaults is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
50
        'NumberOfMonths' => 12,
51
    );
52
53
    /**
54
     * @var array
55
     */
56
    private static $has_one = array(
0 ignored issues
show
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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