Passed
Push — master ( 85839c...7d586d )
by Jason
03:54 queued 02:00
created

ElementPromos::provideBlockSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Dynamic\Elements\Promos\Elements;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use Dynamic\Elements\Promos\Model\PromoObject;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
9
use SilverStripe\ORM\FieldType\DBField;
10
use SilverStripe\ORM\FieldType\DBHTMLText;
11
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
12
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
13
14
/**
15
 * Class PromosElement.
16
 */
17
class ElementPromos extends BaseElement
18
{
19
    /**
20
     * @var string
21
     */
22
    private static $icon = 'font-icon-block-banner';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
23
24
    /**
25
     * @return string
26
     */
27
    private static $singular_name = 'Promos Element';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
28
29
    /**
30
     * @return string
31
     */
32
    private static $plural_name = 'Promos 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 = 'ElementPromos';
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 $styles = array();
0 ignored issues
show
introduced by
The private property $styles is not used, and could be removed.
Loading history...
43
44
    /**
45
     * @var array
46
     */
47
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
48
        'Content' => DBHTMLText::class,
49
    ];
50
51
    /**
52
     * @var array
53
     */
54
    private static $many_many = array(
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
55
        'Promos' => PromoObject::class,
56
    );
57
58
    /**
59
     * @var array
60
     */
61
    private static $many_many_extraFields = array(
0 ignored issues
show
introduced by
The private property $many_many_extraFields is not used, and could be removed.
Loading history...
62
        'Promos' => array(
63
            'SortOrder' => 'Int',
64
        ),
65
    );
66
67
    /**
68
     * Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be
69
     * clickable and a GridFieldDetailForm will be used.
70
     *
71
     * @config
72
     * @var bool
73
     */
74
    private static $inline_editable = false;
0 ignored issues
show
introduced by
The private property $inline_editable is not used, and could be removed.
Loading history...
75
76
    /**
77
     * @return FieldList
78
     */
79
    public function getCMSFields()
80
    {
81 1
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
82 1
            $fields->dataFieldByName('Content')
83 1
                ->setRows(8)
84
            ;
85
86 1
            if ($this->ID) {
87 1
                $promoField = $fields->dataFieldByName('Promos');
88 1
                $config = $promoField->getConfig();
89 1
                $config->addComponent(new GridFieldOrderableRows('SortOrder'));
90 1
                $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
91 1
                $config->addComponent(new GridFieldAddExistingSearchButton());
92 1
                $config->removeComponentsByType(GridFieldDeleteAction::class);
0 ignored issues
show
Bug introduced by
The type Dynamic\Elements\Promos\...s\GridFieldDeleteAction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
93 1
                $fields->addFieldsToTab('Root.Promos', array(
94 1
                    $promoField,
95
                ));
96
            }
97 1
        });
98
99 1
        return parent::getCMSFields();
100
    }
101
102
    /**
103
     * @return mixed
104
     */
105 1
    public function getPromoList()
106
    {
107 1
        return $this->Promos()->sort('SortOrder');
0 ignored issues
show
Bug introduced by
The method Promos() does not exist on Dynamic\Elements\Promos\Elements\ElementPromos. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

107
        return $this->/** @scrutinizer ignore-call */ Promos()->sort('SortOrder');
Loading history...
108
    }
109
110
    /**
111
     * @return DBHTMLText
112
     */
113 1
    public function getSummary()
114
    {
115 1
        if ($this->Promos()->count() == 1) {
116
            $label = ' promo';
117
        } else {
118 1
            $label = ' promos';
119
        }
120 1
        return DBField::create_field('HTMLText', $this->Promos()->count() . $label)->Summary(20);
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    protected function provideBlockSchema()
127
    {
128
        $blockSchema = parent::provideBlockSchema();
129
        $blockSchema['content'] = $this->getSummary();
130
        return $blockSchema;
131
    }
132
133
    /**
134
     * @return string
135
     */
136 1
    public function getType()
137
    {
138 1
        return _t(__CLASS__.'.BlockType', 'Promos');
139
    }
140
}
141