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 = [ |
|
|
|
|
32
|
|
|
TextField::class => 'Text', |
33
|
|
|
DropdownField::class => 'Select one from list', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
private static $table_name = 'CKANFilter'; |
|
|
|
|
37
|
|
|
|
38
|
|
|
private static $db = [ |
|
|
|
|
39
|
|
|
'Name' => 'Varchar', |
40
|
|
|
'Type' => 'Varchar', |
41
|
|
|
'AllFields' => 'Boolean', |
42
|
|
|
'TypeOptions' => 'Text', |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
private static $has_one = [ |
|
|
|
|
46
|
|
|
'FilterFor' => Resource::class, |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
private static $many_many = [ |
|
|
|
|
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') |
|
|
|
|
67
|
|
|
)); |
68
|
|
|
$fields->removeByName('FilterForID'); |
69
|
|
|
return $fields; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function forTemplate() |
73
|
|
|
{ |
74
|
|
|
$options = json_decode($this->TypeOptions, true); |
|
|
|
|
75
|
|
|
$field = Injector::inst()->createWithArgs($this->Type, $options); |
|
|
|
|
76
|
|
|
if ($field instanceof FormField) { |
77
|
|
|
throw new InvalidArgumentException("$this->Type is not a FormField"); |
78
|
|
|
} |
79
|
|
|
return $field; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|