Passed
Push — master ( 3abeb3...edd76b )
by Jason
03:04 queued 01:02
created

ElementPromos::fieldLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 ElementPromos
17
 * @package Dynamic\Elements\Promos\Elements
18
 *
19
 * @property string $Content
20
 *
21
 * @method \SilverStripe\ORM\ManyManyList Promos()
22
 */
23
class ElementPromos extends BaseElement
24
{
25
    /**
26
     * @var string
27
     */
28
    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...
29
30
    /**
31
     * @return string
32
     */
33
    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...
34
35
    /**
36
     * @return string
37
     */
38
    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...
39
40
    /**
41
     * @var string
42
     */
43
    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...
44
45
    /**
46
     * @var array
47
     */
48
    private static $styles = array();
0 ignored issues
show
introduced by
The private property $styles is not used, and could be removed.
Loading history...
49
50
    /**
51
     * @var array
52
     */
53
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
54
        'Content' => DBHTMLText::class,
55
    ];
56
57
    /**
58
     * @var array
59
     */
60
    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...
61
        'Promos' => PromoObject::class,
62
    );
63
64
    /**
65
     * @var array
66
     */
67
    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...
68
        'Promos' => array(
69
            'SortOrder' => 'Int',
70
        ),
71
    );
72
73
    /**
74
     * @var array
75
     */
76
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
77
        'Promos',
78
    ];
79
80
    /**
81
     * Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be
82
     * clickable and a GridFieldDetailForm will be used.
83
     *
84
     * @config
85
     * @var bool
86
     */
87 1
    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...
88 1
89 1
    /**
90 1
     * @param bool $includerelations
91
     * @return array
92 1
     */
93
    public function fieldLabels($includerelations = true)
94 1
    {
95 1
        $labels = parent::fieldLabels($includerelations);
96 1
97 1
        $labels['Content'] = _t(__CLASS__.'.ContentLabel', 'Intro');
98 1
        $labels['Promos'] = _t(__CLASS__ . '.PromosLabel', 'Promos');
99
100
        return $labels;
101 1
    }
102 1
103 1
    /**
104
     * @return FieldList
105
     */
106 1
    public function getCMSFields()
107 1
    {
108
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
109
            $fields->dataFieldByName('Content')
110 1
                ->setRows(5);
111
112 1
            if ($this->ID) {
113
                /** @var \SilverStripe\Forms\GridField\GridField $promoField */
114
                $promoField = $fields->dataFieldByName('Promos');
115
                $fields->removeByName('Promos');
116
                $config = $promoField->getConfig();
117
                $config->removeComponentsByType([
118 1
                    GridFieldAddExistingAutocompleter::class,
119
                    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...
120 1
                    GridFieldArchiveAction::class,
121
                ])->addComponents(
122
                    new GridFieldOrderableRows('SortOrder'),
123
                    new GridFieldAddExistingSearchButton()
124
                );
125
126 1
                $fields->addFieldsToTab('Root.Main', array(
127
                    $promoField,
128 1
                ));
129
            }
130
        });
131 1
132
        return parent::getCMSFields();
133 1
    }
134
135
    /**
136
     * @return mixed
137
     */
138
    public function getPromoList()
139
    {
140
        return $this->Promos()->sort('SortOrder');
141
    }
142
143
    /**
144
     * @return DBHTMLText
145
     */
146
    public function getSummary()
147
    {
148
        if ($this->Promos()->count() == 1) {
149 1
            $label = ' promo';
150
        } else {
151 1
            $label = ' promos';
152
        }
153
        return DBField::create_field('HTMLText', $this->Promos()->count() . $label)->Summary(20);
154
    }
155
156
    /**
157
     * @return array
158
     */
159
    protected function provideBlockSchema()
160
    {
161
        $blockSchema = parent::provideBlockSchema();
162
        $blockSchema['content'] = $this->getSummary();
163
        return $blockSchema;
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function getType()
170
    {
171
        return _t(__CLASS__.'.BlockType', 'Promos');
172
    }
173
}
174