Passed
Push — master ( a409c4...ad0a33 )
by Thomas
11:42
created

TabulatorExportButton::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace LeKoala\Tabulator;
4
5
use LeKoala\ExcelImportExport\ExcelGridFieldExportButton;
0 ignored issues
show
Bug introduced by
The type LeKoala\ExcelImportExpor...elGridFieldExportButton 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...
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\ORM\RelationList;
8
use SilverStripe\View\ArrayData;
9
10
/**
11
 * This component provides a button for exporting data
12
 * Depends on excel-import-export module
13
 */
14
class TabulatorExportButton extends AbstractTabulatorTool
15
{
16
    public function __construct()
17
    {
18
        parent::__construct();
19
        $this->btn = new ExcelGridFieldExportButton();
0 ignored issues
show
Bug Best Practice introduced by
The property btn does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
    }
21
22
    public function forTemplate()
23
    {
24
        $grid = $this->tabulatorGrid;
25
        $singleton = singleton($grid->getModelClass());
26
        $context = [];
27
        if ($grid->getList() instanceof RelationList) {
28
            $record = $grid->getForm()->getRecord();
29
            if ($record && $record instanceof DataObject) {
0 ignored issues
show
introduced by
$record is always a sub-type of SilverStripe\ORM\DataObject.
Loading history...
30
                $context['Parent'] = $record;
31
            }
32
        }
33
34
        if (!$singleton->canCreate(null, $context)) {
35
            return false;
36
        }
37
38
        if (!$this->buttonName) {
39
            // provide a default button name, can be changed by calling {@link setButtonName()} on this component
40
            $this->buttonName = _t('Tabulator.ExportRecords', 'Export');
0 ignored issues
show
Bug Best Practice introduced by
The property buttonName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41
        }
42
43
        $data = new ArrayData([
44
            'ButtonName' => $this->buttonName,
45
        ]);
46
        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

46
        return $this->renderWith($this->getViewerTemplates(), /** @scrutinizer ignore-type */ $data);
Loading history...
47
    }
48
49
    public function getButton(): ExcelGridFieldExportButton
50
    {
51
        return $this->btn;
52
    }
53
54
    public function index()
55
    {
56
        return $this->btn->handleExport($this->tabulatorGrid);
57
    }
58
}
59