SilverstripeAdminCompat::getFormActions()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 50
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 50
rs 8.7697
cc 6
nc 6
nop 1
1
<?php
2
3
namespace LeKoala\Tabulator;
4
5
use SilverStripe\View\HTML;
6
use SilverStripe\Forms\Form;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\FormAction;
10
use SilverStripe\Control\Controller;
11
use SilverStripe\Forms\LiteralField;
12
use SilverStripe\Forms\CompositeField;
13
14
/**
15
 * Abstract logic found from GridFieldDetailForm_ItemRequest
16
 */
17
class SilverstripeAdminCompat implements CompatLayerInterface
18
{
19
    public function adjustItemEditForm(TabulatorGrid_ItemRequest $itemRequest, Form $form)
20
    {
21
        // Always show with base template (full width, no other panels),
22
        // regardless of overloaded CMS controller templates.
23
        $form->setTemplate([
24
            'type' => 'Includes',
25
            'SilverStripe\\Admin\\LeftAndMain_EditForm',
26
        ]);
27
        $form->addExtraClass('cms-content cms-edit-form center fill-height flexbox-area-grow');
28
        $form->setAttribute('data-pjax-fragment', 'CurrentForm Content');
29
        if ($form->Fields()->hasTabSet()) {
30
            $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet');
31
            $form->addExtraClass('cms-tabset');
32
        }
33
34
        $form->Backlink = $itemRequest->getBackLink();
35
    }
36
37
    public function getFormActions(TabulatorGrid_ItemRequest $itemRequest): FieldList
38
    {
39
        $actions = FieldList::create();
40
        $majorActions = CompositeField::create()->setName('MajorActions');
41
        $majorActions->setFieldHolderTemplate(get_class($majorActions) . '_holder_buttongroup');
42
        $actions->push($majorActions);
43
44
        $record = $itemRequest->getRecord();
45
46
        if ($record->ID !== 0) { // existing record
47
            if ($record->canEdit()) {
48
                $noChangesClasses = 'btn-outline-primary font-icon-tick';
49
                $majorActions->push(FormAction::create('doSave', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Save', 'Save'))
50
                    ->addExtraClass($noChangesClasses)
51
                    ->setAttribute('data-btn-alternate-add', 'btn-primary font-icon-save')
52
                    ->setAttribute('data-btn-alternate-remove', $noChangesClasses)
53
                    ->setUseButtonTag(true)
54
                    ->setAttribute('data-text-alternate', _t('SilverStripe\\CMS\\Controllers\\CMSMain.SAVEDRAFT', 'Save')));
55
            }
56
57
            if ($record->canDelete()) {
58
                $actions->insertAfter('MajorActions', FormAction::create('doDelete', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Delete', 'Delete'))
59
                    ->setUseButtonTag(true)
60
                    ->addExtraClass('btn-outline-danger btn-hide-outline font-icon-trash-bin action--delete'));
61
            }
62
63
            $actions->push($this->getRightGroupField($itemRequest));
64
        } else { // adding new record
65
            //Change the Save label to 'Create'
66
            $majorActions->push(FormAction::create('doSave', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Create', 'Create'))
67
                ->setUseButtonTag(true)
68
                ->addExtraClass('btn-primary font-icon-plus-thin'));
69
70
            // Add a Cancel link which is a button-like link and link back to one level up.
71
            $crumbs = $itemRequest->Breadcrumbs();
72
            if ($crumbs && $crumbs->count() >= 2) {
73
                $oneLevelUp = $crumbs->offsetGet($crumbs->count() - 2);
74
                $text = sprintf(
75
                    "<a class=\"%s\" href=\"%s\">%s</a>",
76
                    "crumb btn btn-secondary cms-panel-link", // CSS classes
77
                    $oneLevelUp->Link, // url
78
                    _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.CancelBtn', 'Cancel') // label
79
                );
80
                $actions->insertAfter('MajorActions', new LiteralField('cancelbutton', $text));
81
            }
82
        }
83
84
85
86
        return $actions;
87
    }
88
89
    /**
90
     * @return CompositeField Returns the right aligned toolbar group field along with its FormAction's
91
     */
92
    protected function getRightGroupField(TabulatorGrid_ItemRequest $itemRequest)
93
    {
94
        $rightGroup = CompositeField::create()->setName('RightGroup');
95
        $rightGroup->addExtraClass('ml-auto');
96
        $rightGroup->setFieldHolderTemplate(get_class($rightGroup) . '_holder_buttongroup');
97
98
        $previousAndNextGroup = CompositeField::create()->setName('PreviousAndNextGroup');
99
        $previousAndNextGroup->addExtraClass('btn-group--circular mr-2');
100
        $previousAndNextGroup->setFieldHolderTemplate(CompositeField::class . '_holder_buttongroup');
101
102
        $grid = $itemRequest->getTabulatorGrid();
103
        $record = $itemRequest->getRecord();
104
        $pagination = $grid->getOption("pagination");
105
        if ($pagination) {
106
            $previousIsDisabled = !$itemRequest->getPreviousRecordID();
107
            $nextIsDisabled = !$itemRequest->getNextRecordID();
108
109
            $previousAndNextGroup->push(
110
                LiteralField::create(
111
                    'previous-record',
112
                    HTML::createTag($previousIsDisabled ? 'span' : 'a', [
113
                        'href' => $previousIsDisabled ? '#' : $itemRequest->getEditLink($itemRequest->getPreviousRecordID()),
114
                        'title' => _t(__CLASS__ . '.PREVIOUS', 'Go to previous record'),
115
                        'aria-label' => _t(__CLASS__ . '.PREVIOUS', 'Go to previous record'),
116
                        'class' => 'btn btn-secondary font-icon-left-open action--previous discard-confirmation'
117
                            . ($previousIsDisabled ? ' disabled' : ''),
118
                    ])
119
                )
120
            );
121
122
            $previousAndNextGroup->push(
123
                LiteralField::create(
124
                    'next-record',
125
                    HTML::createTag($nextIsDisabled ? 'span' : 'a', [
126
                        'href' => $nextIsDisabled ? '#' : $itemRequest->getEditLink($itemRequest->getNextRecordID()),
127
                        'title' => _t(__CLASS__ . '.NEXT', 'Go to next record'),
128
                        'aria-label' => _t(__CLASS__ . '.NEXT', 'Go to next record'),
129
                        'class' => 'btn btn-secondary font-icon-right-open action--next discard-confirmation'
130
                            . ($nextIsDisabled ? ' disabled' : ''),
131
                    ])
132
                )
133
            );
134
        }
135
136
        $rightGroup->push($previousAndNextGroup);
137
138
        if ($record->canCreate()) {
139
            $rightGroup->push(
140
                LiteralField::create(
141
                    'new-record',
142
                    HTML::createTag('a', [
143
                        'href' => Controller::join_links($grid->Link('item'), 'new'),
144
                        'title' => _t(__CLASS__ . '.NEW', 'Add new record'),
145
                        'aria-label' => _t(__CLASS__ . '.NEW', 'Add new record'),
146
                        'class' => 'btn btn-primary font-icon-plus-thin btn--circular action--new discard-confirmation',
147
                    ])
148
                )
149
            );
150
        }
151
152
        return $rightGroup;
153
    }
154
155
    /**
156
     * Add Save&Close if not using cms-actions
157
     *
158
     * @param FieldList $actions
159
     * @param DataObject $record
160
     * @return void
161
     */
162
    public function addSaveAndClose(FieldList $actions, DataObject $record)
163
    {
164
        if (!$record->canEdit()) {
165
            return;
166
        }
167
        if (!$record->ID && !$record->canCreate()) {
168
            return;
169
        }
170
171
        $MajorActions = $actions->fieldByName('MajorActions');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $MajorActions is correct as $actions->fieldByName('MajorActions') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
172
173
        // If it doesn't exist, push to default group
174
        if (!$MajorActions) {
0 ignored issues
show
introduced by
$MajorActions is of type null, thus it always evaluated to false.
Loading history...
175
            $MajorActions = $actions;
176
        }
177
178
        if ($record->ID) {
179
            $label = _t(__CLASS__ . '.SAVEANDCLOSE', 'Save and Close');
180
        } else {
181
            $label = _t(__CLASS__ . '.CREATEANDCLOSE', 'Create and Close');
182
        }
183
        $saveAndClose = new FormAction('doSaveAndClose', $label);
184
        $saveAndClose->addExtraClass($this->getBtnClassForRecord($record));
185
        $saveAndClose->setAttribute('data-text-alternate', $label);
186
        if ($record->ID) {
187
            $saveAndClose->setAttribute('data-btn-alternate-add', 'btn-primary');
188
            $saveAndClose->setAttribute('data-btn-alternate-remove', 'btn-outline-primary');
189
        }
190
        $saveAndClose->addExtraClass('font-icon-level-up');
191
        $saveAndClose->setUseButtonTag(true);
192
        $MajorActions->push($saveAndClose);
193
    }
194
195
    /**
196
     * New and existing records have different classes
197
     *
198
     * @param DataObject $record
199
     * @return string
200
     */
201
    protected function getBtnClassForRecord(DataObject $record)
202
    {
203
        if ($record->ID) {
204
            return 'btn-outline-primary';
205
        }
206
        return 'btn-primary';
207
    }
208
}
209