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

ResourceFilter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 17 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\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
class ResourceFilter extends DataObject
17
{
18
    private static $filter_types = [
0 ignored issues
show
introduced by
The private property $filter_types is not used, and could be removed.
Loading history...
19
        TextField::class => 'Text',
20
        DropdownField::class => 'Select one from list',
21
    ];
22
23
    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...
24
25
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
26
        'Name' => 'Varchar',
27
        'Type' => 'Varchar',
28
        'AllFields' => 'Boolean',
29
        'TypeOptions' => 'Text',
30
    ];
31
32
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
33
        'FilterFor' => Resource::class,
34
    ];
35
36
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
37
        'FilterFields' => ResourceField::class,
38
    ];
39
40
    public function getCMSFields()
41
    {
42
        $fields = parent::getCMSFields();
43
        $typeTitle = $fields->dataFieldByName('Type')->Title();
44
        $fields->replaceField('Type', DropdownField::create(
45
            'Type',
46
            $typeTitle,
47
            $this->config()->get('filter_types')
48
        ));
49
        $fields->replaceField('TypeOptions', HiddenField::create('TypeOptions'));
50
        $fields->push(ListboxField::create(
51
            'FilterFields',
52
            'Columns to search',
53
            $this->FilterFor()->Fields()->map('Name', '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

53
            $this->/** @scrutinizer ignore-call */ 
54
                   FilterFor()->Fields()->map('Name', 'ReadableName')
Loading history...
54
        ));
55
        $fields->removeByName('FilterForID');
56
        return $fields;
57
    }
58
59
    public function forTemplate()
60
    {
61
        $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...
62
        $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...
63
        if ($field instanceof FormField) {
64
            throw new InvalidArgumentException("$this->Type is not a FormField");
65
        }
66
        return $field;
67
    }
68
}
69