ResultConditionsField::Type()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SilverStripe\CKANRegistry\Forms;
4
5
use SilverStripe\Forms\Form;
6
use SilverStripe\Forms\TextField;
7
8
/**
9
 * A ResultConditionsField renders a list of "must have" and "must not have" text conditions which are then used
10
 * to determine whether a column is displayed to frontend users.
11
 */
12
class ResultConditionsField extends TextField
13
{
14
    /**
15
     * @var int
16
     */
17
    const MATCH_TYPE_MUST = 1;
18
19
    /**
20
     * @var int
21
     */
22
    const MATCH_TYPE_MUST_NOT = 0;
23
24
    /**
25
     * @var int
26
     */
27
    const MATCH_TYPE_DEFAULT = self::MATCH_TYPE_MUST;
28
29
    protected $schemaComponent = 'ResultConditions';
30
31
    public function __construct($name, $title = null, $value = '', $maxLength = null, Form $form = null)
32
    {
33
        parent::__construct($name, $title, $value, $maxLength, $form);
34
35
        $this->addExtraClass('ckan-result-conditions__container');
36
    }
37
38
    public function Type()
39
    {
40
        return 'ckan-result-conditions';
41
    }
42
43
    public function performReadonlyTransformation()
44
    {
45
        // If we have no value then defer to the parent that renders a "none" field
46
        $value = trim($this->Value());
47
        if (empty($value)) {
48
            return parent::performReadonlyTransformation();
49
        }
50
51
        return clone $this->setReadonly(true);
52
    }
53
54
    public function getSchemaDataDefaults()
55
    {
56
        $data = parent::getSchemaDataDefaults();
57
        $data['data']['source'] = static::getMatchOptions();
58
        $data['data']['matchTypeDefault'] = self::MATCH_TYPE_DEFAULT;
59
        return $data;
60
    }
61
62
    /**
63
     * Get a list of options for filtering with a human readable (translated) label
64
     *
65
     * @return array[]
66
     */
67
    public static function getMatchOptions()
68
    {
69
        return [
70
            ['value' => self::MATCH_TYPE_MUST, 'title' => _t(__CLASS__ . '.MUST_MATCH', 'Must match')],
71
            ['value' => self::MATCH_TYPE_MUST_NOT, 'title' => _t(__CLASS__ . '.MUST_NOT_MATCH', 'Must not match')],
72
        ];
73
    }
74
}
75