Passed
Push — master ( dfa610...bac577 )
by Jason
14:40 queued 12:56
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 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
     * Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be
75
     * clickable and a GridFieldDetailForm will be used.
76
     *
77
     * @config
78
     * @var bool
79
     */
80
    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...
81
82
    /**
83
     * @return FieldList
84
     */
85
    public function getCMSFields()
86
    {
87 1
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
88 1
            $fields->dataFieldByName('Content')
89 1
                ->setTitle('Description')
90 1
                ->setRows(5);
91
92 1
            if ($this->ID) {
93
                /** @var \SilverStripe\Forms\GridField\GridField $promoField */
94 1
                $promoField = $fields->dataFieldByName('Promos');
95 1
                $fields->removeByName('Promos');
96 1
                $config = $promoField->getConfig();
97 1
                $config->removeComponentsByType([
98 1
                    GridFieldAddExistingAutocompleter::class,
99
                    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...
100
                    GridFieldArchiveAction::class,
101 1
                ])->addComponents(
102 1
                    new GridFieldOrderableRows('SortOrder'),
103 1
                    new GridFieldAddExistingSearchButton()
104
                );
105
106 1
                $fields->addFieldsToTab('Root.Main', array(
107 1
                    $promoField,
108
                ));
109
            }
110 1
        });
111
112 1
        return parent::getCMSFields();
113
    }
114
115
    /**
116
     * @return mixed
117
     */
118 1
    public function getPromoList()
119
    {
120 1
        return $this->Promos()->sort('SortOrder');
121
    }
122
123
    /**
124
     * @return DBHTMLText
125
     */
126 1
    public function getSummary()
127
    {
128 1
        if ($this->Promos()->count() == 1) {
129
            $label = ' promo';
130
        } else {
131 1
            $label = ' promos';
132
        }
133 1
        return DBField::create_field('HTMLText', $this->Promos()->count() . $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', 'Promos');
152
    }
153
}
154