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

ResourceFilter::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 15
rs 9.9
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\TextField;
11
use SilverStripe\i18n\i18n;
12
use SilverStripe\ORM\DataObject;
13
use SilverStripe\ORM\ValidationResult;
14
15
class ResourceFilter extends DataObject
16
{
17
    private static $filter_types = [
0 ignored issues
show
introduced by
The private property $filter_types is not used, and could be removed.
Loading history...
18
        TextField::class => 'Text',
19
        DropdownField::class => 'Select one from list',
20
    ];
21
22
    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...
23
24
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
25
        'Name' => 'Varchar',
26
        'Type' => 'Varchar',
27
        'AllFields' => 'Boolean',
28
        'TypeOptions' => 'Text',
29
    ];
30
31
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
32
        'FilterFor' => Resource::class,
33
    ];
34
35
    public function getCMSFields()
36
    {
37
        $fields = parent::getCMSFields();
38
        $typeTitle = $fields->dataFieldByName('Type')->Title();
39
        $fields->replaceField('Type', DropdownField::create(
40
            'Type',
41
            $typeTitle,
42
            $this->config()->get('filter_types')
43
        ));
44
        $fields->replaceField('TypeOptions', HiddenField::create('TypeOptions'));
45
        $fields->push(
46
            DropdownField::create('Fields', 'Columns', $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

46
            DropdownField::create('Fields', 'Columns', $this->/** @scrutinizer ignore-call */ FilterFor()->Fields()->map('Name', 'ReadableName'))
Loading history...
47
        );
48
        $fields->removeByName('FilterForID');
49
        return $fields;
50
    }
51
52
    public function forTemplate()
53
    {
54
        $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...
55
        $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...
56
        if ($field instanceof FormField) {
57
            throw new InvalidArgumentException("$this->Type is not a FormField");
58
        }
59
        return $field;
60
    }
61
}
62