GridFieldTogglePaginator   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 154
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A updateState() 0 21 4
A getHTMLFragments() 0 18 4
A getActions() 0 4 1
A handleAction() 0 17 2
A getManipulatedData() 0 9 2
1
<?php
2
3
/**
4
 * A GridField component that allows to temporary disable the
5
 * pagination.
6
 *
7
 * It should be included before the GridFieldPaginator component that
8
 * must be present (otherwise this class would be pretty useless).
9
 *
10
 * @package silverstripe-togglepaginator
11
 */
12
class GridFieldTogglePaginator implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
{
14
    /**
15
     * The icon to use on the enable pagination button. Can be null.
16
     * @config
17
     */
18
    private static $enable_icon;
0 ignored issues
show
Unused Code introduced by
The property $enable_icon is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
20
    /**
21
     * The icon to use on the disable pagination button. Can be null.
22
     * @config
23
     */
24
    private static $disable_icon;
0 ignored issues
show
Unused Code introduced by
The property $disable_icon is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
25
26
    /**
27
     * Fragment to write the button to.
28
     */
29
    protected $target;
30
31
    /**
32
     * ID of the session data.
33
     */
34
    protected $state_id;
35
36
    /**
37
     * Session data (an associative array).
38
     */
39
    protected $state;
40
41
42
    public function __construct($target = 'buttons-before-right')
43
    {
44
        $this->target = $target;
45
    }
46
47
    /**
48
     * Update $state and $state_id properties.
49
     *
50
     * If the $state array does not exist in the session, create it.
51
     *
52
     * The current implementation is borrowed directly from
53
     * GridField_FormAction::getAttributes() 3.2.0-beta2.
54
     */
55
    protected function updateState($grid)
56
    {
57
        if (isset($this->state_id)) {
58
            return;
59
        }
60
61
        $state = array(
62
            'grid'       => $grid->getName(),
63
            'actionName' => 'toggle',
64
            'active'     => true,
65
        );
66
67
        // Ensure the id doesn't contain only numeric characters
68
        $this->state_id = 'gf_' . substr(md5(serialize($state)), 0, 8);
69
70
        $this->state = Session::get($this->state_id);
71
        if (! is_array($this->state) || ! array_key_exists('active', $this->state)) {
72
            $this->state = $state;
73
            Session::set($this->state_id, $state);
0 ignored issues
show
Documentation introduced by
$state is of type array<string,?,{"grid":"...g","active":"boolean"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
        }
75
    }
76
77
    /**
78
     * Implements the GridField_HTMLProvider interface.
79
     *
80
     * @param  GridField $grid
81
     * @return array
82
     */
83
    public function getHTMLFragments($grid)
84
    {
85
        $this->updateState($grid);
86
87
        $active = $this->state['active'];
88
        $params = http_build_query(array('StateID' => $this->state_id));
89
        $data = new ArrayData(array(
90
            'state' => $active ? 'ui-state-default' : 'ss-ui-alternate ui-state-highlight',
91
            'icon'  => Config::inst()->get(__CLASS__, $active ? 'enable_icon' : 'disable_icon'),
92
            'label' => $active ? _t(__CLASS__ . '.DISABLE', 'Disable') : _t(__CLASS__ . '.ENABLE', 'Enable'),
93
            'name'  => 'action_gridFieldAlterAction?' . $params,
94
            'url'   => $grid->Link(),
95
        ));
96
97
        return array(
98
            $this->target => $data->renderWith(__CLASS__),
99
        );
100
    }
101
102
    /**
103
     * Required by the GridField_ActionProvider interface.
104
     *
105
     * @param  GridField $grid
106
     * @return array
107
     */
108
    public function getActions($grid)
109
    {
110
        return array('toggle');
111
    }
112
113
    /**
114
     * Handle the action.
115
     *
116
     * It swithces the "state" flag and saves the data into a session
117
     * variable for later reuse.
118
     *
119
     * @param  GridField $grid      The subject GridField instance
120
     * @param  string    $action    The action name (lowercase!)
121
     * @param  mixed     $arguments Optional argument(s) for the action
122
     * @param  array     $data      Form data, if relevant
123
     *
124
     * @throws InvalidArgumentException
125
     */
126
    public function handleAction(GridField $grid, $action, $arguments, $data)
127
    {
128
        switch ($action) {
129
130
        case 'toggle':
131
            $this->updateState($grid);
132
            $this->state['active'] = ! $this->state['active'];
133
            Session::set($this->state_id, $this->state);
0 ignored issues
show
Documentation introduced by
$this->state is of type array<string,?,{"active":"boolean"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
134
            break;
135
136
        default:
137
            throw new InvalidArgumentException(sprintf(
138
                'Action "%s" not understood',
139
                $action
140
            ));
141
        }
142
    }
143
144
    /**
145
     * Required by the GridField_DataManipulator interface.
146
     *
147
     * Disable pagination on the GridFieldPaginator component, if
148
     * required by the current "state" flag. It must be called before
149
     * GridFieldPaginator::getManipulatedData() to take effect, hence
150
     * the requirement that GridFieldTogglePaginator must be added
151
     * before GridFieldPaginator.
152
     *
153
     * @param  GridField $grid
154
     * @return SS_List
155
     */
156
    public function getManipulatedData(GridField $grid, SS_List $list)
157
    {
158
        $this->updateState($grid);
159
        if (! $this->state['active']) {
160
            $grid->getConfig()->getComponentByType('GridFieldPaginator')
161
                ->setItemsPerPage(PHP_INT_MAX);
162
        }
163
        return $list;
164
    }
165
}
166