Passed
Pull Request — master (#36)
by
unknown
02:37
created

ResourceFilter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 11 1
A forTemplate() 0 8 2
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\TextField;
10
use SilverStripe\i18n\i18n;
11
use SilverStripe\ORM\DataObject;
12
use SilverStripe\ORM\ValidationResult;
13
14
class ResourceFilter extends DataObject
15
{
16
    private static $filter_types = [
0 ignored issues
show
introduced by
The private property $filter_types is not used, and could be removed.
Loading history...
17
        TextField::class => 'Text',
18
        DropdownField::class => 'Select one from list',
19
    ];
20
21
    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...
22
23
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
24
        'Name' => 'Varchar',
25
        'Type' => 'Varchar',
26
        'TypeOptions' => 'Text',
27
    ];
28
29
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
30
        'FilterFor' => Resource::class,
31
    ];
32
33
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
34
        'FilterFields' => ResourceField::class,
35
    ];
36
37
    public function getCMSFields()
38
    {
39
        $fields = parent::getCMSFields();
40
        $typeTitle = $fields->dataFieldByName('Type')->Title();
41
        $fields->replaceField('Type', DropdownField::create(
42
            'Type',
43
            $typeTitle,
44
            $this->config()->get('filter_types')
45
        ));
46
        $fields->removeByName('FilterForID');
47
        return $fields;
48
    }
49
50
    public function forTemplate()
51
    {
52
        $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...
53
        $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...
54
        if ($field instanceof FormField) {
55
            throw new InvalidArgumentException("$this->Type is not a FormField");
56
        }
57
        return $field;
58
    }
59
}