TabulatorExportButton::forTemplate()   B
last analyzed

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 InvalidArgumentException;
6
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...
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\RelationList;
9
use SilverStripe\View\ArrayData;
10
11
/**
12
 * This component provides a button for exporting data
13
 * Depends on excel-import-export module
14
 */
15
class TabulatorExportButton extends AbstractTabulatorTool
16
{
17
    protected $exportFormat = 'xlsx';
18
    protected $btn;
19
20
    public function __construct()
21
    {
22
        parent::__construct();
23
        $this->btn = new ExcelGridFieldExportButton();
24
    }
25
26
    public function forTemplate()
27
    {
28
        $grid = $this->tabulatorGrid;
29
        $singleton = singleton($grid->getModelClass());
30
        $context = [];
31
        if ($grid->getList() instanceof RelationList) {
32
            $record = $grid->getForm()->getRecord();
33
            if ($record && $record instanceof DataObject) {
0 ignored issues
show
introduced by
$record is always a sub-type of SilverStripe\ORM\DataObject.
Loading history...
34
                $context['Parent'] = $record;
35
            }
36
        }
37
38
        if (!$singleton->canCreate(null, $context)) {
39
            return false;
40
        }
41
42
        if (!$this->buttonName) {
43
            // provide a default button name, can be changed by calling {@link setButtonName()} on this component
44
            $this->buttonName = _t('Tabulator.ExportRecordsIn', 'Export in {format}', ['format' => $this->exportFormat]);
45
        }
46
47
        $data = new ArrayData([
48
            'ButtonName' => $this->buttonName,
49
            'ButtonClasses' => 'btn-secondary font-icon-export no-ajax',
50
            'Icon' => $this->isAdmini() ? 'list_alt' : '',
51
        ]);
52
        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

52
        return $this->renderWith($this->getViewerTemplates(), /** @scrutinizer ignore-type */ $data);
Loading history...
53
    }
54
55
    public function getButton(): ExcelGridFieldExportButton
56
    {
57
        return $this->btn;
58
    }
59
60
    public function index()
61
    {
62
        $this->btn->setIsLimited(false);
63
        $this->btn->setExportType($this->exportFormat);
64
65
        return $this->btn->handleExport($this->tabulatorGrid);
66
    }
67
68
    /**
69
     * Get the value of exportFormat
70
     */
71
    public function getExportFormat(): mixed
72
    {
73
        return $this->exportFormat;
74
    }
75
76
    /**
77
     * Set the value of exportFormat
78
     *
79
     * @param mixed $exportFormat csv,xlsx
80
     */
81
    public function setExportFormat($exportFormat): self
82
    {
83
        if (!in_array($exportFormat, ['csv', 'xslx'])) {
84
            throw new InvalidArgumentException("Format must be csv or xlsx");
85
        }
86
        $this->exportFormat = $exportFormat;
87
        return $this;
88
    }
89
}
90