Completed
Push — master ( 442c30...3b833c )
by
unknown
12s
created

ResourceFilter::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 17
rs 9.8333
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\DropdownField;
8
use SilverStripe\Forms\FormField;
9
use SilverStripe\Forms\HiddenField;
10
use SilverStripe\Forms\ListboxField;
11
use SilverStripe\Forms\TextField;
12
use SilverStripe\i18n\i18n;
13
use SilverStripe\ORM\DataObject;
14
use SilverStripe\ORM\ValidationResult;
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
class ResourceFilter extends DataObject
22
{
23
    /**
24
     * Types of FormFields that a filter could display as on the user facing side. Generally a list of
25
     * {@see SilverStripe\Forms\FormField} mapped to human readable identifiers such as would be passed to a
26
     * {@see DropDownField} as the source constructor parameter
27
     *
28
     * @config
29
     * @var array
30
     */
31
    private static $filter_types = [
0 ignored issues
show
introduced by
The private property $filter_types is not used, and could be removed.
Loading history...
32
        TextField::class => 'Text',
33
        DropdownField::class => 'Select one from list',
34
    ];
35
36
    private static $table_name = 'CKANFilter';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
37
38
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
39
        'Name' => 'Varchar',
40
        'Type' => 'Varchar',
41
        'AllFields' => 'Boolean',
42
        'TypeOptions' => 'Text',
43
    ];
44
45
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
46
        'FilterFor' => Resource::class,
47
    ];
48
49
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
50
        'FilterFields' => ResourceField::class,
51
    ];
52
53
    public function getCMSFields()
54
    {
55
        $fields = parent::getCMSFields();
56
        $typeTitle = $fields->dataFieldByName('Type')->Title();
57
        $fields->replaceField('Type', DropdownField::create(
58
            'Type',
59
            $typeTitle,
60
            $this->config()->get('filter_types')
61
        ));
62
        $fields->replaceField('TypeOptions', HiddenField::create('TypeOptions'));
63
        $fields->push(ListboxField::create(
64
            'FilterFields',
65
            'Columns to search',
66
            $this->FilterFor()->Fields()->map('ID', 'ReadableName')
0 ignored issues
show
Bug introduced by
The method FilterFor() does not exist on SilverStripe\CKANRegistry\Model\ResourceFilter. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
            $this->/** @scrutinizer ignore-call */ 
67
                   FilterFor()->Fields()->map('ID', 'ReadableName')
Loading history...
67
        ));
68
        $fields->removeByName('FilterForID');
69
        return $fields;
70
    }
71
72
    public function forTemplate()
73
    {
74
        $options = json_decode($this->TypeOptions, true);
0 ignored issues
show
Bug Best Practice introduced by
The property TypeOptions does not exist on SilverStripe\CKANRegistry\Model\ResourceFilter. Since you implemented __get, consider adding a @property annotation.
Loading history...
75
        $field = Injector::inst()->createWithArgs($this->Type, $options);
0 ignored issues
show
Bug Best Practice introduced by
The property Type does not exist on SilverStripe\CKANRegistry\Model\ResourceFilter. Since you implemented __get, consider adding a @property annotation.
Loading history...
76
        if ($field instanceof FormField) {
77
            throw new InvalidArgumentException("$this->Type is not a FormField");
78
        }
79
        return $field;
80
    }
81
}
82