Passed
Push — master ( a78c29...10ae60 )
by Jason
01:29
created

FoxyCategory::getCMSFields()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 1
nop 0
dl 0
loc 19
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Foxy\Model;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\ReadonlyField;
7
use SilverStripe\ORM\DataObject;
8
9
class FoxyCategory extends DataObject
10
{
11
    /**
12
     * @var array
13
     */
14
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
15
        'Title' => 'Varchar(255)',
16
        'Code' => 'Varchar(50)',
17
    ];
18
19
    /**
20
     * @var array
21
     */
22
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
23
        'Title' => 'Name',
24
        'Code' => 'Code',
25
    ];
26
    /**
27
     * @var array
28
     */
29
    private static $indexes = [
0 ignored issues
show
introduced by
The private property $indexes is not used, and could be removed.
Loading history...
30
        'Code' => [
31
            'type' => 'unique',
32
            'columns' => ['Code'],
33
        ],
34
    ];
35
    /**
36
     * @var string
37
     */
38
    private static $table_name = 'FoxyCategory';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
39
40
    /**
41
     * @param bool $includerelations
42
     * @return array
43
     */
44
    public function fieldLabels($includerelations = true)
45
    {
46
        $labels = parent::fieldLabels($includerelations);
47
48
        $labels['Title'] = _t(__CLASS__ . '.TitleLabel', 'Title');
49
        $labels['Code'] = _t(__CLASS__ . '.CodeLabel', 'Code');
50
51
        return $labels;
52
    }
53
54
    /**
55
     * @return FieldList|void
56
     */
57
    public function getCMSFields()
58
    {
59
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
60
            if ($this->ID) {
61
                if ($this->Code == 'DEFAULT') {
62
                    $fields->replaceField(
63
                        'Title',
64
                        ReadonlyField::create('Title')
65
                    );
66
67
                    $fields->replaceField(
68
                        'Code',
69
                        ReadonlyField::create('Code')
70
                    );
71
                }
72
            }
73
        });
74
75
        return parent::getCMSFields();
76
    }
77
78
    /**
79
     * @return \SilverStripe\ORM\ValidationResult
80
     */
81
    public function validate()
82
    {
83
        $result = parent::validate();
84
85
        if (FoxyCategory::get()->filter('Code', $this->Code)->exclude('ID', $this->ID)->first()) {
86
            $result->addError('Code must be unique for each category.');
87
        }
88
89
        return $result;
90
    }
91
92
    /**
93
     * @throws \SilverStripe\ORM\ValidationException
94
     */
95
    public function requireDefaultRecords()
96
    {
97
        parent::requireDefaultRecords();
98
99
        $allCats = self::get();
100
        if (!$allCats->count()) {
101
            $cat = new self();
102
            $cat->Title = 'Default';
103
            $cat->Code = 'DEFAULT';
0 ignored issues
show
Bug Best Practice introduced by
The property Code does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
104
            $cat->write();
105
        }
106
    }
107
}
108