Passed
Push — master ( 3d7f15...de16b5 )
by Thomas
02:45
created

TabulatorExportButton::forTemplate()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 27
rs 8.8333
cc 7
nc 9
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
            'ButtonClasses' => 'btn-secondary font-icon-export',
46
            'Icon' => $this->isAdmini() ? 'list_alt' : '',
47
        ]);
48
        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

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