Passed
Pull Request — master (#36)
by
unknown
02:09
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\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
    public function getCMSFields()
37
    {
38
        $fields = parent::getCMSFields();
39
        $typeTitle = $fields->dataFieldByName('Type')->Title();
40
        $fields->replaceField('Type', DropdownField::create(
41
            'Type',
42
            $typeTitle,
43
            $this->config()->get('filter_types')
44
        ));
45
        $fields->replaceField('TypeOptions', HiddenField::create('TypeOptions'));
46
        $fields->push(
47
            ListboxField::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

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