Issues (78)

src/AdminiCompat.php (2 issues)

Labels
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
 * Compat layer with admini module
16
 * Maybe migrate this to admini module at some point
17
 */
18
class AdminiCompat implements CompatLayerInterface
19
{
20
    public function adjustItemEditForm(TabulatorGrid_ItemRequest $itemRequest, Form $form)
21
    {
22
        $form->setTemplate([
23
            'type' => 'Includes',
24
            'LeKoala\\Admini\\LeftAndMain_EditForm',
25
        ]);
26
        if ($form->Fields()->hasTabSet()) {
27
            $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet');
28
        }
29
        $form->addExtraClass('needs-validation'); // client side form validation
30
        $form->Backlink = $itemRequest->getBackLink();
31
    }
32
33
    public function getFormActions(TabulatorGrid_ItemRequest $itemRequest): FieldList
34
    {
35
        $actions = FieldList::create();
36
        $majorActions = CompositeField::create()->setName('MajorActions');
37
        $majorActions->setFieldHolderTemplate(get_class($majorActions) . '_holder_buttongroup');
38
        $actions->push($majorActions);
39
40
        $record = $itemRequest->getRecord();
41
42
        if ($record->ID !== 0) { // existing record
43
            if ($record->canEdit()) {
44
                $majorActions->push(
45
                    FormAction::create('doSave', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Save', 'Save'))
46
                        ->setIcon('done')
47
                        ->addExtraClass('btn-outline-success')
48
                        ->setUseButtonTag(true)
49
                );
50
            }
51
52
            if ($record->canDelete()) {
53
                $actions->insertAfter(
54
                    'MajorActions',
55
                    FormAction::create('doDelete', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Delete', 'Delete'))
56
                        ->setUseButtonTag(true)
57
                        ->setIcon('delete')
58
                        ->addExtraClass('btn-danger')
59
                );
60
            }
61
62
            $actions->push($this->getRightGroupField($itemRequest));
63
        } else { // adding new record
64
            //Change the Save label to 'Create'
65
            $majorActions->push(FormAction::create('doSave', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Create', 'Create'))
66
                ->setUseButtonTag(true)
67
                ->setIcon('add')
68
                ->addExtraClass('btn-success'));
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
                    "ms-auto btn btn-default", // 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
        return $actions;
85
    }
86
87
    /**
88
     * @return CompositeField Returns the right aligned toolbar group field along with its FormAction's
89
     */
90
    protected function getRightGroupField(TabulatorGrid_ItemRequest $itemRequest)
91
    {
92
        $rightGroup = CompositeField::create()->setName('RightGroup');
93
        $rightGroup->addExtraClass('ms-auto');
94
        $rightGroup->setFieldHolderTemplate(get_class($rightGroup) . '_holder_buttongroup');
95
96
        $previousAndNextGroup = CompositeField::create()->setName('PreviousAndNextGroup');
97
        $previousAndNextGroup->setFieldHolderTemplate(CompositeField::class . '_holder_buttongroup');
98
99
        $grid = $itemRequest->getTabulatorGrid();
100
        $record = $itemRequest->getRecord();
101
        $pagination = $grid->getOption("pagination");
102
        if ($pagination) {
103
            $previousIsDisabled = !$itemRequest->getPreviousRecordID();
104
            $nextIsDisabled = !$itemRequest->getNextRecordID();
105
106
            $previousAndNextGroup->push(
107
                LiteralField::create(
108
                    'previous-record',
109
                    HTML::createTag($previousIsDisabled ? 'span' : 'a', [
110
                        'href' => $previousIsDisabled ? '#' : $itemRequest->getEditLink($itemRequest->getPreviousRecordID()),
111
                        'title' => _t(__CLASS__ . '.PREVIOUS', 'Go to previous record'),
112
                        'aria-label' => _t(__CLASS__ . '.PREVIOUS', 'Go to previous record'),
113
                        'class' => 'btn btn-light action--previous discard-confirmation'
114
                            . ($previousIsDisabled ? ' disabled' : ''),
115
                    ], '<l-i name="navigate_before"></l-i>')
116
                )
117
            );
118
119
            $previousAndNextGroup->push(
120
                LiteralField::create(
121
                    'next-record',
122
                    HTML::createTag($nextIsDisabled ? 'span' : 'a', [
123
                        'href' => $nextIsDisabled ? '#' : $itemRequest->getEditLink($itemRequest->getNextRecordID()),
124
                        'title' => _t(__CLASS__ . '.NEXT', 'Go to next record'),
125
                        'aria-label' => _t(__CLASS__ . '.NEXT', 'Go to next record'),
126
                        'class' => 'btn btn-light action--next discard-confirmation'
127
                            . ($nextIsDisabled ? ' disabled' : ''),
128
                    ], '<l-i name="navigate_next"></l-i>')
129
                )
130
            );
131
        }
132
133
        $rightGroup->push($previousAndNextGroup);
134
135
        if ($record->canCreate()) {
136
            $rightGroup->push(
137
                LiteralField::create(
138
                    'new-record',
139
                    HTML::createTag('a', [
140
                        'href' => Controller::join_links($grid->Link('item'), 'new'),
141
                        'title' => _t(__CLASS__ . '.NEW', 'Add new record'),
142
                        'aria-label' => _t(__CLASS__ . '.NEW', 'Add new record'),
143
                        'class' => 'btn btn-success action--new discard-confirmation',
144
                    ], '<l-i name="add"></l-i>')
145
                )
146
            );
147
        }
148
149
        return $rightGroup;
150
    }
151
152
    /**
153
     * Add Save&Close if not using cms-actions
154
     *
155
     * @param FieldList $actions
156
     * @param DataObject $record
157
     * @return void
158
     */
159
    public function addSaveAndClose(FieldList $actions, DataObject $record)
160
    {
161
        if (!$record->canEdit()) {
162
            return;
163
        }
164
        if (!$record->ID && !$record->canCreate()) {
165
            return;
166
        }
167
168
        $MajorActions = $actions->fieldByName('MajorActions');
0 ignored issues
show
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...
169
170
        // If it doesn't exist, push to default group
171
        if (!$MajorActions) {
0 ignored issues
show
$MajorActions is of type null, thus it always evaluated to false.
Loading history...
172
            $MajorActions = $actions;
173
        }
174
175
        if ($record->ID) {
176
            $label = _t(__CLASS__ . '.SAVEANDCLOSE', 'Save and Close');
177
        } else {
178
            $label = _t(__CLASS__ . '.CREATEANDCLOSE', 'Create and Close');
179
        }
180
181
        $saveAndClose = new FormAction('doSaveAndClose', $label);
182
        $saveAndClose->addExtraClass('btn btn-outline-primary');
183
        if ($record->ID) {
184
            $saveAndClose->setIcon("done");
185
        } else {
186
            $saveAndClose->setIcon("add");
187
        }
188
189
        $saveAndClose->setUseButtonTag(true);
190
        $MajorActions->push($saveAndClose);
191
    }
192
}
193