Passed
Push — master ( bb21bb...10576c )
by Thomas
23:04 queued 11:25
created

TabulatorAddExistingAutocompleter::autocomplete()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
c 1
b 0
f 1
dl 0
loc 16
rs 9.9332
cc 2
nc 4
nop 1
1
<?php
2
3
namespace LeKoala\Tabulator;
4
5
use Exception;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\View\ArrayData;
8
use SilverStripe\ORM\RelationList;
9
use SilverStripe\Control\Controller;
10
use SilverStripe\Control\HTTPRequest;
11
use SilverStripe\Control\HTTPResponse;
12
use LeKoala\FormElements\BsAutocompleteField;
13
14
/**
15
 * This component provides a autocomplete field to link element to the grid
16
 */
17
class TabulatorAddExistingAutocompleter extends AbstractTabulatorTool
18
{
19
    /**
20
     * @config
21
     */
22
    private static array $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
23
        'add',
24
        'autocomplete',
25
    ];
26
27
28
    protected string $name = 'add_existing';
29
    protected ?BsAutocompleteField $autocompleteField = null;
30
31
    public function forTemplate()
32
    {
33
        $grid = $this->tabulatorGrid;
34
        $singleton = singleton($grid->getModelClass());
35
        $context = [];
36
        if ($grid->getList() instanceof RelationList) {
37
            $record = $grid->getForm()->getRecord();
38
            if ($record && $record instanceof DataObject) {
0 ignored issues
show
introduced by
$record is always a sub-type of SilverStripe\ORM\DataObject.
Loading history...
39
                $context['Parent'] = $record;
40
            }
41
        }
42
43
        if (!$singleton->canCreate(null, $context)) {
44
            return false;
45
        }
46
47
        if (!$this->buttonName) {
48
            // provide a default button name, can be changed by calling {@link setButtonName()} on this component
49
            $this->buttonName = _t('SilverStripe\\Forms\\GridField\\GridField.LinkExisting', "Link Existing");
50
        }
51
52
        $data = new ArrayData([
53
            'ButtonName' => $this->buttonName,
54
            'ButtonClasses' => 'btn-outline-secondary font-icon-link',
55
            'Icon' => $this->isAdmini() ? 'link' : '',
56
            'AutocompletField' => $this->getAutocompleteField(),
57
        ]);
58
        return $this->renderWith($this->getViewerTemplates(), $data);
0 ignored issues
show
Bug introduced by
$data of type SilverStripe\View\ArrayData is incompatible with the type array expected by parameter $customFields of SilverStripe\View\ViewableData::renderWith(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        return $this->renderWith($this->getViewerTemplates(), /** @scrutinizer ignore-type */ $data);
Loading history...
59
    }
60
61
    public function getAutocompleteField(): BsAutocompleteField
62
    {
63
        if (!$this->autocompleteField) {
64
            $this->autocompleteField = $this->createAutocompleteField();
65
        }
66
        return $this->autocompleteField;
67
    }
68
69
    protected function createAutocompleteField(): BsAutocompleteField
70
    {
71
        $grid = $this->tabulatorGrid;
72
73
        $field = new BsAutocompleteField('autocomplete', _t('SilverStripe\\Forms\\GridField\\GridField.Find', "Find"));
74
        $field->setForm($grid->getForm());
75
        $field->setPlaceholder(_t('SilverStripe\\Forms\\GridField\\GridField.Find', "Find"));
76
        $field->setAjax(Controller::join_links($grid->Link('tool'), $this->name, 'autocomplete'));
77
        $field->setAjaxWizard($grid->getModelClass());
78
        return $field;
79
    }
80
81
    public function add(HTTPRequest $request): HTTPResponse
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

81
    public function add(/** @scrutinizer ignore-unused */ HTTPRequest $request): HTTPResponse

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        $response = new HTTPResponse();
84
85
        try {
86
            $body = json_encode([
87
                'success' => true,
88
            ]);
89
            $response->setBody($body);
90
            $response->addHeader('Content-Type', 'application/json');
91
        } catch (Exception $ex) {
92
            $response->setStatusCode(500);
93
            $response->setBody($ex->getMessage());
94
        }
95
96
        return $response;
97
    }
98
99
    public function autocomplete(HTTPRequest $request): HTTPResponse
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
    public function autocomplete(/** @scrutinizer ignore-unused */ HTTPRequest $request): HTTPResponse

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        $response = new HTTPResponse();
102
103
        try {
104
            $body = json_encode([
105
                'success' => true,
106
            ]);
107
            $response->setBody($body);
108
            $response->addHeader('Content-Type', 'application/json');
109
        } catch (Exception $ex) {
110
            $response->setStatusCode(500);
111
            $response->setBody($ex->getMessage());
112
        }
113
114
        return $response;
115
    }
116
}
117