Passed
Pull Request — master (#15)
by Jason
11:59
created

ElementBlogPosts::getSummary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Elements\Blog\Elements;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use Sheadawson\DependentDropdown\Forms\DependentDropdownField;
7
use SilverStripe\Blog\Model\Blog;
8
use SilverStripe\Blog\Model\BlogCategory;
9
use SilverStripe\Blog\Model\BlogPost;
10
use SilverStripe\Forms\DropdownField;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\Forms\RequiredFields;
13
use SilverStripe\ORM\FieldType\DBField;
14
use SilverStripe\ORM\FieldType\DBHTMLText;
15
use SilverStripe\ORM\ValidationResult;
16
17
/**
18
 * Class ElementBlogPosts
19
 * @package Dynamic\Elements\Elements
20
 *
21
 * @property int $Limit
22
 * @property string $Content
23
 *
24
 * @property int $BlogID
25
 * @property int $CategoryID
26
 * @method Blog Blog()
27
 * @method BlogCategory Category()
28
 */
29
class ElementBlogPosts extends BaseElement
30
{
31
    /**
32
     * @var string
33
     */
34
    private static $icon = 'vendor/dnadesign/silverstripe-elemental/images/base.svg';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
35
36
    /**
37
     * @var string
38
     */
39
    private static $singular_name = 'Blog Posts Element';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
40
41
    /**
42
     * @var string
43
     */
44
    private static $plural_name = 'Blog Posts Elements';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
45
46
    /**
47
     * @var string
48
     */
49
    private static $description = 'Show recent blog posts from a featured blog.';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
50
51
    /**
52
     * @var string
53
     */
54
    private static $table_name = 'ElementBlogPosts';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
55
56
    /**
57
     * @var array
58
     */
59
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
60
        'Limit' => 'Int',
61
        'Content' => 'HTMLText',
62
    );
63
64
    /**
65
     * @var array
66
     */
67
    private static $has_one = array(
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
68
        'Blog' => Blog::class,
69
        'Category' => BlogCategory::class,
70
    );
71
72
    /**
73
     * @var array
74
     */
75
    private static $defaults = array(
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
76 1
        'Limit' => 3,
77
    );
78 1
79
    /**
80
     * @return DBHTMLText
81
     */
82
    public function getSummary()
83
    {
84 1
        return DBField::create_field('HTMLText', $this->Content)->Summary(20);
85
    }
86 1
87
    /**
88
     * @return array
89
     */
90
    protected function provideBlockSchema()
91
    {
92
        $blockSchema = parent::provideBlockSchema();
93
        $blockSchema['content'] = $this->getSummary();
94 1
        return $blockSchema;
95 1
    }
96 1
97
    /**
98 1
     * @return string
99 1
     */
100
    public function getType()
101 1
    {
102 1
        return _t(__CLASS__ . '.BlockType', 'Blog Posts');
103 1
    }
104 1
105 1
    /**
106
     * @return FieldList
107
     */
108 1
    public function getCMSFields()
109
    {
110
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
111
            $fields->dataFieldByName('Content')
112
                ->setRows(8);
113 1
114
            $fields->dataFieldByName('Limit')
115 1
                ->setTitle('Articles to show');
116 1
117 1
            if (class_exists(Blog::class)) {
118 1
                $fields->insertBefore(
119 1
                    $fields->dataFieldByName('BlogID')
120 1
                        ->setTitle('Featured Blog'),
121
                    'Limit'
0 ignored issues
show
Bug introduced by
'Limit' of type string is incompatible with the type SilverStripe\Forms\FormField expected by parameter $item of SilverStripe\Forms\FieldList::insertBefore(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
                    /** @scrutinizer ignore-type */ 'Limit'
Loading history...
122
                );
123 1
124
                $dataSource = function ($val) {
125 1
                    if ($val) {
126
                        return Blog::get()->byID($val)->Categories()->map('ID', 'Title');
127
                    }
128
                    return [];
129
                };
130
131 1
                $fields->insertAfter(
132
                    'BlogID',
133 1
                    DependentDropdownField::create('CategoryID', 'Category', $dataSource)
134 1
                        ->setDepends($fields->dataFieldByName('BlogID'))
135 1
                        ->setHasEmptyDefault(true)
136
                        ->setEmptyString(' -- Category --')
137 1
                );
138
            }
139
        });
140
141
        return parent::getCMSFields();
142
    }
143 1
144
    /**
145 1
     * @return mixed
146 1
     */
147
    public function getPostsList()
148
    {
149 1
        if ($this->BlogID) {
150
            if ($this->CategoryID) {
151
                return BlogCategory::get()->byID($this->CategoryID)->BlogPosts()->Limit($this->Limit);
152
            }
153
            return Blog::get()->byID($this->BlogID)->getBlogPosts()->Limit($this->Limit);
154
        } else {
155
            return BlogPost::get()->sort('PublishDate DESC')->limit($this->Limit);
156
        }
157
    }
158
}
159