Completed
Push — master ( 784d5e...baa2d2 )
by Jason
02:37
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 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 $table_name = 'ElementBlogPosts';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
40
41
    /**
42
     * @var array
43
     */
44
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
45
        'Limit' => 'Int',
46
        'Content' => 'HTMLText',
47
    );
48
49
    /**
50
     * @var array
51
     */
52
    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...
53
        'Blog' => Blog::class,
54
        'Category' => BlogCategory::class,
55
    );
56
57
    /**
58
     * @var array
59
     */
60
    private static $defaults = array(
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
61
        'Limit' => 3,
62
    );
63
64
    /**
65
     * @return FieldList
66
     */
67
    public function getCMSFields()
68
    {
69 1
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
70 1
            $fields->dataFieldByName('Content')
71 1
                ->setRows(8);
72
73 1
            $fields->dataFieldByName('Limit')
74 1
                ->setTitle(_t(__CLASS__ . 'LimitLabel', 'Posts to show'));
75
76 1
            if (class_exists(Blog::class)) {
77 1
                $fields->insertBefore(
78 1
                    $fields->dataFieldByName('BlogID')
79 1
                        ->setTitle(_t(__CLASS__ . 'BlogLabel', 'Featured Blog'))
80 1
                        ->setEmptyString(''),
81 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

81
                    /** @scrutinizer ignore-type */ 'Limit'
Loading history...
82
                );
83
84 1
                $dataSource = function ($val) {
85
                    if ($val) {
86
                        return Blog::get()->byID($val)->Categories()->map('ID', 'Title');
87
                    }
88
                    return [];
89 1
                };
90
91 1
                $fields->insertAfter(
92 1
                    'BlogID',
93 1
                    DependentDropdownField::create('CategoryID', _t(
94 1
                        __CLASS__ . 'CategoryLabel',
95 1
                        'Category'
96
                    ), $dataSource)
97 1
                        ->setDepends($fields->dataFieldByName('BlogID'))
98 1
                        ->setHasEmptyDefault(true)
99 1
                        ->setEmptyString('')
100
                );
101
            }
102 1
        });
103
104 1
        return parent::getCMSFields();
105
    }
106
107
    /**
108
     * @return mixed
109
     */
110 2
    public function getPostsList()
111
    {
112 2
        if ($this->BlogID) {
113 2
            if ($this->CategoryID) {
114
                return BlogCategory::get()->byID($this->CategoryID)->BlogPosts()->Limit($this->Limit);
115
            }
116 2
            return Blog::get()->byID($this->BlogID)->getBlogPosts()->Limit($this->Limit);
117
        } else {
118
            return BlogPost::get()->sort('PublishDate DESC')->limit($this->Limit);
119
        }
120
    }
121
122
    /**
123
     * @return DBHTMLText
124
     */
125 1
    public function getSummary()
126
    {
127 1
        $count = $this->getPostsList()->count();
128 1
        $label = _t(
129 1
            BlogPost::class . '.PLURALS',
130 1
            'A Blog Post|{count} Blog Posts',
131 1
            [ 'count' => $count ]
132
        );
133 1
        return DBField::create_field('HTMLText', $label)->Summary(20);
134
    }
135
136
    /**
137
     * @return array
138
     */
139
    protected function provideBlockSchema()
140
    {
141
        $blockSchema = parent::provideBlockSchema();
142
        $blockSchema['content'] = $this->getSummary();
143
        return $blockSchema;
144
    }
145
146
    /**
147
     * @return string
148
     */
149 1
    public function getType()
150
    {
151 1
        return _t(__CLASS__ . '.BlockType', 'Blog Posts');
152
    }
153
}
154