Passed
Pull Request — master (#78)
by Robbie
02:37
created

ResourceFilter::getColumns()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 0
dl 0
loc 24
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\CKANRegistry\Model;
4
5
use InvalidArgumentException;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Forms\CompositeField;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\FormField;
10
use SilverStripe\Forms\ListboxField;
11
use SilverStripe\Forms\TextField;
12
use SilverStripe\ORM\DataObject;
13
use SilverStripe\ORM\FieldType\DBField;
14
use SilverStripe\ORM\ManyManyList;
15
16
/**
17
 * Represents a filter for a data resource, which accepts user inputted data and generates a query string (?key=value)
18
 * to use in a request against a CKAN API for that particular resource in order to filter the results shown in a
19
 * representation of that data.
20
 *
21
 * @property string FilterLabel
22
 * @property bool AllColumns
23
 * @property int Order
24
 * @method Resource FilterFor
25
 * @method ManyManyList FilterFields
26
 */
27
class ResourceFilter extends DataObject
28
{
29
    private static $table_name = 'CKANFilter_Text';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
30
31
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
32
        'FilterLabel' => 'Varchar',
33
        'AllColumns' => 'Boolean',
34
        'Order' => 'Int',
35
    ];
36
37
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
38
        'FilterFor' => Resource::class,
39
    ];
40
41
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
42
        'FilterFields' => ResourceField::class,
43
    ];
44
45
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
46
        'FilterLabel',
47
        'Type',
48
        'Columns',
49
    ];
50
51
    private static $singular_name = 'Text Filter';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
52
53
    /**
54
     * Defines the type of {@link FormField} that will be used to render the filter in the CMS. This is defined
55
     * in subclasses. Filters will render as TextFields by default.
56
     *
57
     * @var FormField
58
     */
59
    protected $fieldType = TextField::class;
60
61
    public function getCMSFields()
62
    {
63
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
64
            $allColumnsField = $fields->dataFieldByName('AllColumns');
65
            $allColumnsField->addExtraClass('ckan-columns__all-columns');
66
            // See https://github.com/silverstripe/silverstripe-framework/issues/8696
67
            $allColumnsField->setTitle(ucfirst(strtolower($allColumnsField->Title())));
68
69
            // Remove the scaffolded Filter Fields tab and the AllColumns field
70
            $fields->removeByName(['FilterFields', 'AllColumns']);
71
72
            // Add a composite field containing the "All columns" checkbox and the "Columns source(s)" checkbox
73
            $filterFields = ListboxField::create(
74
                'FilterFields',
75
                '',
76
                $this->FilterFor()->Fields()->map('ID', 'ReadableLabel')
77
            );
78
            $filterFields->addExtraClass('ckan-columns__filter-fields');
79
80
            $columnSources = CompositeField::create($allColumnsField, $filterFields);
81
            $columnSources
82
                ->setTitle(_t(__CLASS__ . '.COLUMNS_SOURCES', 'Columns source(s)'))
83
                ->addExtraClass('ckan-columns__sources');
84
            $fields->push($columnSources);
85
86
            // See https://github.com/silverstripe/silverstripe-framework/issues/8696
87
            $filterLabel = $fields->dataFieldByName('FilterLabel');
88
            $filterLabel->setTitle(ucfirst(strtolower($filterLabel->Title())));
89
90
            $fields->removeByName('FilterForID');
91
        });
92
93
        return parent::getCMSFields();
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     *
99
     * @throws InvalidArgumentException If the provided Type is not an instance of FormField
100
     */
101
    public function forTemplate()
102
    {
103
        $field = Injector::inst()->createWithArgs($this->fieldType, [$this->Name]);
0 ignored issues
show
Bug Best Practice introduced by
The property Name does not exist on SilverStripe\CKANRegistry\Model\ResourceFilter. Since you implemented __get, consider adding a @property annotation.
Loading history...
104
        if (!$field instanceof FormField) {
105
            throw new InvalidArgumentException("$this->fieldType is not a FormField");
106
        }
107
        return $field;
108
    }
109
110
    /**
111
     * Returns the type of the filter, used for summary fields
112
     *
113
     * @return string
114
     */
115
    public function getType()
116
    {
117
        return $this->singular_name();
118
    }
119
120
    /**
121
     * Returns either the selected column's readable label value, or a fixed string representing multiple columns
122
     * having been selected.
123
     *
124
     * @return string|DBField
125
     */
126
    public function getColumns()
127
    {
128
        if ($this->AllColumns) {
129
            return DBField::create_Field(
130
                'HTMLFragment',
131
                '<span class="ckan-columns--all-columns">'
132
                . _t(__CLASS__ . '.ALL_COLUMNS', 'All columns')
133
                . '</span>'
134
            );
135
        }
136
137
        if (!$this->FilterFields()->count()) {
138
            return '';
139
        }
140
141
        if ($this->FilterFields()->count() === 1) {
142
            return (string) $this->FilterFields()->first()->ReadableLabel;
143
        }
144
145
        return DBField::create_Field(
146
            'HTMLFragment',
147
            '<span class="ckan-columns--multiple">'
148
            . _t(__CLASS__ . '.MULTIPLE_COLUMNS', 'Multiple columns')
149
            . '</span>'
150
        );
151
    }
152
}
153