Completed
Push — master ( 7d586d...ccbe7d )
by Matthew
04:48
created

ElementPromos   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 82.14%

Importance

Changes 0
Metric Value
wmc 7
eloc 40
dl 0
loc 127
ccs 23
cts 28
cp 0.8214
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPromoList() 0 3 1
A getType() 0 3 1
A getCMSFields() 0 26 2
A getSummary() 0 8 2
A provideBlockSchema() 0 5 1
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 SilverStripe\Versioned\GridFieldArchiveAction;
12
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
13
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
14
15
/**
16
 * Class PromosElement.
17
 */
18
class ElementPromos extends BaseElement
19
{
20
    /**
21
     * @var string
22
     */
23
    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...
24
25
    /**
26
     * @return string
27
     */
28
    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...
29
30
    /**
31
     * @return string
32
     */
33
    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...
34
35
    /**
36
     * @var string
37
     */
38
    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...
39
40
    /**
41
     * @var array
42
     */
43
    private static $styles = array();
0 ignored issues
show
introduced by
The private property $styles is not used, and could be removed.
Loading history...
44
45
    /**
46
     * @var array
47
     */
48
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
49
        'Content' => DBHTMLText::class,
50
    ];
51
52
    /**
53
     * @var array
54
     */
55
    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...
56
        'Promos' => PromoObject::class,
57
    );
58
59
    /**
60
     * @var array
61
     */
62
    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...
63
        'Promos' => array(
64
            'SortOrder' => 'Int',
65
        ),
66
    );
67
68
    /**
69
     * Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be
70
     * clickable and a GridFieldDetailForm will be used.
71
     *
72
     * @config
73
     * @var bool
74
     */
75
    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...
76
77
    /**
78
     * @return FieldList
79
     */
80
    public function getCMSFields()
81
    {
82 1
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
83 1
            $fields->dataFieldByName('Content')
84 1
                ->setRows(8);
85
86 1
            if ($this->ID) {
87
                /** @var \SilverStripe\Forms\GridField\GridField $promoField */
88 1
                $promoField = $fields->dataFieldByName('Promos');
89 1
                $config = $promoField->getConfig();
90 1
                $config->removeComponentsByType([
91 1
                    GridFieldAddExistingAutocompleter::class,
92
                    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
                    GridFieldArchiveAction::class,
94 1
                ])->addComponents(
95 1
                    new GridFieldOrderableRows('SortOrder'),
96 1
                    new GridFieldAddExistingSearchButton()
97
                );
98
99 1
                $fields->addFieldsToTab('Root.Promos', array(
100 1
                    $promoField,
101
                ));
102
            }
103 1
        });
104
105 1
        return parent::getCMSFields();
106
    }
107
108
    /**
109
     * @return mixed
110
     */
111 1
    public function getPromoList()
112
    {
113 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

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