Completed
Pull Request — master (#8)
by Matthew
12:24
created

ElementBlogPosts::ElementSummary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

93
                    /** @scrutinizer ignore-type */ 'Limit'
Loading history...
94
                );
95
            }
96
        });
97
98
        return parent::getCMSFields();
99
    }
100
101
    /**
102
     * @return ValidationResult
103
     */
104
    public function validate()
105
    {
106
        $result = parent::validate();
107
        if (!$this->BlogID) {
0 ignored issues
show
Bug Best Practice introduced by
The property BlogID does not exist on Dynamic\Elements\Blog\Elements\ElementBlogPosts. Since you implemented __get, consider adding a @property annotation.
Loading history...
108
            $result->addError('Featured Blog is required before you can save');
109
        }
110
        return $result;
111
    }
112
113
    /**
114
     * @return mixed
115
     */
116
    public function getPostsList()
117
    {
118
        if ($this->BlogID) {
0 ignored issues
show
Bug Best Practice introduced by
The property BlogID does not exist on Dynamic\Elements\Blog\Elements\ElementBlogPosts. Since you implemented __get, consider adding a @property annotation.
Loading history...
119
            return Blog::get()->byID($this->BlogID)->getBlogPosts()->Limit($this->Limit);
0 ignored issues
show
Bug Best Practice introduced by
The property Limit does not exist on Dynamic\Elements\Blog\Elements\ElementBlogPosts. Since you implemented __get, consider adding a @property annotation.
Loading history...
120
        }
121
        return null;
122
    }
123
}
124