Completed
Push — master ( 9526e1...79bc18 )
by Loz
03:00
created

code/widgets/BlogArchiveWidget.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 13 and the first side effect is on line 4.

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
if (!class_exists('Widget')) {
4
    return;
5
}
6
7
/**
8
 * @method Blog Blog()
9
 *
10
 * @property string $ArchiveType
11
 * @property int $NumberToDisplay
12
 */
13
class BlogArchiveWidget extends Widget
14
{
15
    /**
16
     * @var string
17
     */
18
    private static $title = 'Archive';
19
20
    /**
21
     * @var string
22
     */
23
    private static $cmsTitle = 'Archive';
24
25
    /**
26
     * @var string
27
     */
28
    private static $description = 'Displays an archive list of posts.';
29
30
    /**
31
     * @var array
32
     */
33
    private static $db = array(
34
        'NumberToDisplay' => 'Int',
35
        'ArchiveType' => 'Enum(\'Monthly,Yearly\', \'Monthly\')',
36
    );
37
38
    /**
39
     * @var array
40
     */
41
    private static $defaults = array(
42
        'NumberOfMonths' => 12,
43
    );
44
45
    /**
46
     * @var array
47
     */
48
    private static $has_one = array(
49
        'Blog' => 'Blog',
50
    );
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getCMSFields()
56
    {
57
        $self =& $this;
58
59
        $this->beforeUpdateCMSFields(function ($fields) use ($self) {
60
            /**
61
             * @var Enum $archiveType
62
             */
63
            $archiveType = $self->dbObject('ArchiveType');
64
65
            $type = $archiveType->enumValues();
66
67
            foreach ($type as $k => $v) {
68
                $type[$k] = _t('BlogArchiveWidget.' . ucfirst(strtolower($v)), $v);
69
            }
70
71
            /**
72
             * @var FieldList $fields
73
             */
74
            $fields->merge(array(
75
                DropdownField::create('BlogID', _t('BlogArchiveWidget.Blog', 'Blog'), Blog::get()->map()),
76
                DropdownField::create('ArchiveType', _t('BlogArchiveWidget.ArchiveType', 'ArchiveType'), $type),
77
                NumericField::create('NumberToDisplay', _t('BlogArchiveWidget.NumberToDisplay', 'No. to Display'))
78
            ));
79
        });
80
81
        return parent::getCMSFields();
82
    }
83
84
    /**
85
     * Returns a list of months where blog posts are present.
86
     *
87
     * @return DataList
88
     */
89
    public function getArchive()
90
    {
91
        $query = $this->Blog()->getBlogPosts()->dataQuery();
92
93
        if ($this->ArchiveType == 'Yearly') {
94
            $query->groupBy('DATE_FORMAT("PublishDate", \'%Y\')');
95
        } else {
96
            $query->groupBy('DATE_FORMAT("PublishDate", \'%Y-%M\')');
97
        }
98
99
        $posts = $this->Blog()->getBlogPosts()->setDataQuery($query);
100
101
        if ($this->NumberToDisplay > 0) {
102
            $posts = $posts->limit($this->NumberToDisplay);
103
        }
104
105
        $archive = new ArrayList();
106
107
        if ($posts->count() > 0) {
108
            foreach ($posts as $post) {
109
                /**
110
                 * @var BlogPost $post
111
                 */
112
                $date = Date::create();
113
                $date->setValue($post->PublishDate);
114
115
                if ($this->ArchiveType == 'Yearly') {
116
                    $year = $date->FormatI18N("%Y");
117
                    $month = null;
118
                    $title = $year;
119
                } else {
120
                    $year = $date->FormatI18N("%Y");
121
                    $month = $date->FormatI18N("%m");
122
                    $title = $date->FormatI18N("%B %Y");
123
                }
124
125
                $archive->push(new ArrayData(array(
126
                    'Title' => $title,
127
                    'Link' => Controller::join_links($this->Blog()->Link('archive'), $year, $month)
128
                )));
129
            }
130
        }
131
132
        return $archive;
133
    }
134
}
135
136
class BlogArchiveWidget_Controller extends Widget_Controller
137
{
138
}
139