Completed
Pull Request — master (#6354)
by Will
08:07
created

GridFieldImportButton   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 66
rs 10
c 1
b 0
f 0
wmc 6
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getHTMLFragments() 0 27 4
A getActions() 0 4 1
1
<?php
2
3
namespace SilverStripe\Forms\GridField;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Control\HTTPResponse;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\Forms\Form;
9
use SilverStripe\Forms\CompositeField;
10
11
12
class GridFieldImportButton implements GridField_HTMLProvider
13
{
14
    /**
15
     * Fragment to write the button to
16
     */
17
    protected $targetFragment;
18
19
    /**
20
     * @var CompositeField
21
     */
22
    protected $importFormField;
23
24
    /**
25
     * @param string $targetFragment The HTML fragment to write the button into
26
     */
27
    public function __construct($targetFragment = "after", $importFormField = null)
28
    {
29
        $this->targetFragment = $targetFragment;
30
        $this->importFormField = $importFormField;
31
    }
32
33
    /**
34
     * Place the export button in a <p> tag below the field
35
     *
36
     * @param GridField $gridField
37
     * @return array
38
     */
39
    public function getHTMLFragments($gridField)
40
    {
41
        $button = new GridField_FormAction(
42
            $gridField,
43
            'import',
44
            _t('TableListField.CSVIMPORT', 'Import CSV'),
45
            'import',
46
            null
47
        );
48
        $button->addExtraClass('btn btn-secondary no-ajax font-icon-upload action_import');
49
        $button
50
            ->setAttribute('data-toggle', "modal")
51
            ->setAttribute('data-target', "#". $gridField->ID() . ' .grid-field-import');
52
53
        $button->setForm($gridField->getForm());
54
        $extra = null;
55
56
        if($this->importFormField) {
57
            $extra = ($this->importFormField instanceof Form)
58
                ? $this->importFormField->forTemplate()
59
                : $this->importFormField->Field();
60
        }
61
62
        return array(
63
            $this->targetFragment => ($extra) ? '<p class="grid-csv-button">' . $button->Field() . '</p>'.$extra : '',
64
        );
65
    }
66
67
    /**
68
     * export is an action button
69
     *
70
     * @param GridField $gridField
71
     * @return array
72
     */
73
    public function getActions($gridField)
0 ignored issues
show
Unused Code introduced by
The parameter $gridField is not used and could be removed.

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

Loading history...
74
    {
75
        return [];
76
    }
77
}
78