Completed
Push — master ( fc234b...50b50a )
by Matthew
02:02
created

ElementBlogPosts::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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 SilverStripe\Blog\Model\Blog;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\RequiredFields;
9
use SilverStripe\ORM\FieldType\DBField;
10
use SilverStripe\ORM\FieldType\DBHTMLText;
11
use SilverStripe\ORM\ValidationResult;
12
13
/**
14
 * Class ElementBlogPosts
15
 * @package Dynamic\Elements\Elements
16
 *
17
 * @property int $Limit
18
 * @property string $Content
19
 *
20
 * @property int $BlogID
21
 * @property Blog Blog()
22
 */
23
class ElementBlogPosts extends BaseElement
24
{
25
    /**
26
     * @var string
27
     */
28
    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...
29
30
    /**
31
     * @var string
32
     */
33
    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...
34
35
    /**
36
     * @var string
37
     */
38
    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...
39
40
    /**
41
     * @var string
42
     */
43
    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...
44
45
    /**
46
     * @var array
47
     */
48
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
49
        'Limit' => 'Int',
50
        'Content' => 'HTMLText',
51
    );
52
53
    /**
54
     * @var array
55
     */
56
    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...
57
        'Blog' => Blog::class,
58
    );
59
60
    /**
61
     * @var array
62
     */
63
    private static $defaults = array(
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
64
        'Limit' => 3,
65
    );
66
67
    /**
68
     * @return DBHTMLText
69
     */
70 1
    public function ElementSummary()
71
    {
72 1
        return DBField::create_field('HTMLText', $this->Content)->Summary(20);
73
    }
74
75
    /**
76
     * @return string
77
     */
78 1
    public function getType()
79
    {
80 1
        return _t(__CLASS__ . '.BlockType', 'Blog Posts');
81
    }
82
83
    /**
84
     * @return FieldList
85
     */
86
    public function getCMSFields()
87
    {
88 1
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
89 1
            $fields->dataFieldByName('Content')
90 1
                ->setRows(8);
91
92 1
            $fields->dataFieldByName('Limit')
93 1
                ->setTitle('Articles to show');
94
95 1
            if (class_exists(Blog::class)) {
96 1
                $fields->insertBefore(
97 1
                    $fields->dataFieldByName('BlogID')
98 1
                        ->setTitle('Featured Blog'),
99 1
                    '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

99
                    /** @scrutinizer ignore-type */ 'Limit'
Loading history...
100
                );
101
            }
102 1
        });
103
104 1
        return parent::getCMSFields();
105
    }
106
107
    /**
108
     * @return ValidationResult
109
     */
110 1
    public function validate()
111
    {
112 1
        $result = parent::validate();
113 1
        if (!$this->BlogID) {
114 1
            $result->addError('Featured Blog is required before you can save');
115
        }
116 1
        return $result;
117
    }
118
119
    /**
120
     * @return mixed
121
     */
122 1
    public function getPostsList()
123
    {
124 1
        if ($this->BlogID) {
125 1
            return Blog::get()->byID($this->BlogID)->getBlogPosts()->Limit($this->Limit);
126
        }
127
        return null;
128
    }
129
}
130