Completed
Push — master ( a8d5f1...edd0cb )
by Jason
02: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 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 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 = 'font-icon-block-content';
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
        'Limit' => 3,
77
    );
78
79
    /**
80
     * @return DBHTMLText
81
     */
82 1
    public function getSummary()
83
    {
84 1
        return DBField::create_field('HTMLText', $this->Content)->Summary(20);
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    protected function provideBlockSchema()
91
    {
92
        $blockSchema = parent::provideBlockSchema();
93
        $blockSchema['content'] = $this->getSummary();
94
        return $blockSchema;
95
    }
96
97
    /**
98
     * @return string
99
     */
100 1
    public function getType()
101
    {
102 1
        return _t(__CLASS__ . '.BlockType', 'Blog Posts');
103
    }
104
105
    /**
106
     * @return FieldList
107
     */
108
    public function getCMSFields()
109
    {
110 1
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
111 1
            $fields->dataFieldByName('Content')
112 1
                ->setRows(8);
113
114 1
            $fields->dataFieldByName('Limit')
115 1
                ->setTitle('Articles to show');
116
117 1
            if (class_exists(Blog::class)) {
118 1
                $fields->insertBefore(
119 1
                    $fields->dataFieldByName('BlogID')
120 1
                        ->setTitle('Featured Blog')
121 1
                        ->setEmptyString('-- select --'),
122 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

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