Issues (13)

src/Elements/ElementFeatures.php (7 issues)

1
<?php
2
3
namespace Dynamic\Elements\Features\Elements;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use Dynamic\Elements\Features\Model\FeatureObject;
7
use SilverStripe\Forms\CompositeField;
8
use SilverStripe\Forms\FieldGroup;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
11
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
12
use SilverStripe\ORM\FieldType\DBField;
13
use SilverStripe\ORM\FieldType\DBHTMLText;
14
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
15
16
/**
17
 * Class PageSectionBlock.
18
 *
19
 * @method HasManyList $Features
20
 */
21
class ElementFeatures extends BaseElement
22
{
23
    /**
24
     * @var string
25
     */
26
    private static $icon = 'font-icon-block-banner';
0 ignored issues
show
The private property $icon is not used, and could be removed.
Loading history...
27
28
    /**
29
     * @var string
30
     */
31
    private static $table_name = 'ElementFeatures';
0 ignored issues
show
The private property $table_name is not used, and could be removed.
Loading history...
32
33
    /**
34
     * @var array
35
     */
36
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
37
        'Content' => 'HTMLText',
38
        'Alternate' => 'Boolean',
39
    ];
40
41
    /**
42
     * @var array
43
     */
44
    private static $has_many = [
0 ignored issues
show
The private property $has_many is not used, and could be removed.
Loading history...
45
        'Features' => FeatureObject::class,
46
    ];
47
48
    /**
49
     * @var array
50
     */
51
    private static $owns = [
0 ignored issues
show
The private property $owns is not used, and could be removed.
Loading history...
52
        'Features',
53
    ];
54
55
    /**
56
     * Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be
57
     * clickable and a GridFieldDetailForm will be used.
58
     *
59 1
     * @config
60
     * @var bool
61 1
     */
62
    private static $inline_editable = false;
0 ignored issues
show
The private property $inline_editable is not used, and could be removed.
Loading history...
63 1
64 1
    /**
65 1
     * @param bool $includerelations
66
     * @return array
67 1
     */
68
    public function fieldLabels($includerelations = true)
69
    {
70
        $labels = parent::fieldLabels($includerelations);
71
72
        $labels['Content'] = _t(__CLASS__.'.ContentLabel', 'Intro');
73
        $labels['Alternate'] = _t(__CLASS__ . '.AlternateLabel', 'Alternate Layout');
74
        $labels['Features'] = _t(__CLASS__ . '.FeaturesLabel', 'Features');
75 1
76 1
        return $labels;
77 1
    }
78 1
79 1
    /**
80 1
     * @return \SilverStripe\Forms\FieldList
81 1
     */
82
    public function getCMSFields()
83
    {
84 1
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
85
            $fields->dataFieldByName('Content')
86 1
                ->setTitle($this->fieldLabel('Content'))
87 1
                ->setRows(5);
88
89 1
            $alternate = $fields->dataFieldByName('Alternate');
90
            $fields->removeByName('Alternate');
91 1
92 1
            $fields->addFieldToTab(
93 1
                'Root.Main',
94
                CompositeField::create(
95
                    $alternate
96
                        ->setTitle($this->fieldLabel('Alternate'))
97 1
                        ->setDescription(_t(
98
                            __CLASS__ . '.AlternateDescription',
99 1
                            'alternate image and text alignment - first feature: image left, copy right; 
100
                                second feature: image right, copy left;'
101 1
                        ))
102
                )->setTitle(_t(
103
                    __CLASS__ . '.LayoutLabel',
104
                    'Layout'
105
                ))
106
            );
107 1
108
            if ($this->ID) {
109 1
                // Features
110
                $features = $fields->dataFieldByName('Features');
111
                $fields->removeByName('Features');
112
113
                $config = $features->getConfig();
114
                $config
115 1
                    ->addComponent(new GridFieldOrderableRows())
116
                    ->removeComponentsByType([
117 1
                        GridFieldAddExistingAutocompleter::class,
118 1
                        GridFieldDeleteAction::class
119 1
                    ]);
120 1
121 1
                $fields->addFieldToTab('Root.Main', $features);
122
            }
123 1
        });
124
125
        return parent::getCMSFields();
126
    }
127
128
    /**
129
     * @return mixed
130
     */
131
    public function getFeaturesList()
132
    {
133
        return $this->Features()->sort('Sort');
0 ignored issues
show
The method Features() does not exist on Dynamic\Elements\Features\Elements\ElementFeatures. 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

133
        return $this->/** @scrutinizer ignore-call */ Features()->sort('Sort');
Loading history...
134
    }
135
136
    /**
137
     * @return DBHTMLText
138
     */
139 1
    public function getSummary()
140
    {
141 1
        $count = $this->Features()->count();
142
        $label = _t(
143
            FeatureObject::class . '.PLURALS',
144
            'A Feature|{count} Features',
145
            [ 'count' => $count ]
146
        );
147
        return DBField::create_field('HTMLText', $label)->Summary(20);
148
    }
149
150
    /**
151
     * @return array
152
     */
153
    protected function provideBlockSchema()
154
    {
155
        $blockSchema = parent::provideBlockSchema();
156
        $blockSchema['content'] = $this->getSummary();
157
        return $blockSchema;
158
    }
159
160
    /**
161
     * @return string
162
     */
163
    public function getType()
164
    {
165
        return _t(__CLASS__.'.BlockType', 'Features');
166
    }
167
}
168