Passed
Pull Request — 1.0 (#1)
by Chris
13:30
created

Zone::getCountriesArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
namespace SilverCommerce\GeoZones\Model;
4
5
use Locale;
6
use SilverCommerce\GeoZones\Helpers\GeoZonesHelper;
7
use SilverCommerce\GeoZones\Tasks\ZoneMigrationTask;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\Forms\ListboxField;
10
use SilverStripe\SiteConfig\SiteConfig;
11
12
/**
13
 * A container of multiple regions
14
 * @property string Name
15
 * @property string Country
16
 * @property string RegionsCode
17
 * @property bool AllRegions
18
 */
19
class Zone extends DataObject
20
{
21
    private static $table_name = 'GeoZoneZone';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
22
23
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
24
        "Name" => "Varchar",
25
        "Country" => "Varchar",
26
        "RegionCodes" => "Text",
27
        "AllRegions" => "Boolean",
28
        "Enabled" => "Boolean"
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
        "Site" => SiteConfig::class
33
    ];
34
35
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
36
        "Regions" => Region::class // Remaining for legacy support
37
    ];
38
39
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
40
        "CountriesList" => "Varchar"
41
    ];
42
43
    private static $field_labels = [
0 ignored issues
show
introduced by
The private property $field_labels is not used, and could be removed.
Loading history...
44
        "RegionsCount" => "No. of regions"
45
    ];
46
47
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
48
        "Name",
49
        "CountriesList",
50
        "RegionsCount",
51
        "Enabled"
52
    ];
53
54
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
55
        "Name",
56
        "Country",
57
        "RegionCodes",
58
        "Enabled"
59
    ];
60
61
    /**
62
     * Return an array of all associated countries
63
     *
64
     * @return array
65
     */
66
    public function getCountriesArray()
67
    {
68
        $return = json_decode($this->Country);
69
70
        if (empty($return) && isset($this->Country)) {
71
            $return = [$this->Country];
72
        }
73
74
        return $return;
75
    }
76
77
    /**
78
     * Get an array of region codes saved against this object
79
     *
80
     * @return array
81
     */
82
    public function getRegionCodesArray()
83
    {
84
        $return = json_decode($this->RegionCodes);
85
86
        if (empty($return) && isset($this->RegionCodes)) {
87
            $return = [$this->RegionCodes];
88
        }
89
90
        return $return;
91
    }
92
93
    /**
94
     * Return a simple, comma seperated list of associated countries
95
     *
96
     * @return string
97
     */
98
    public function getCountriesList()
99
    {
100
        return implode(",", $this->getCountriesArray());
101
    }
102
103
    /**
104
     * Get an array of regions for the current zone, or an empty
105
     * array if no countries selected
106
     *
107
     * @return array
108
     */
109
    public function getRegionsArray()
110
    {
111
        $region_codes = $this->getRegionCodesArray();
112
        $helper = GeoZonesHelper::create();
113
114
        if (count($region_codes) > 0) {
115
            $helper->setLimitRegionCodes($region_codes);
116
        }
117
118
        return $helper->getRegionArray();
119
    }
120
121
    /**
122
     * Get an array of regions for the current country, or an empty
123
     * array if no countries selected
124
     *
125
     * @return array
126
     */
127
    public function getRegionsCount()
128
    {
129
        return count($this->getRegionsArray());
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function getCMSFields()
136
    {
137
        $this->beforeUpdateCMSFields(function ($fields) {
138
            $fields->removeByName("Regions");
139
140
            $helper = GeoZonesHelper::create($this->getCountriesArray());
141
142
            $fields->replaceField(
143
                "Country",
144
                ListboxField::create(
145
                    'Country',
146
                    $this->fieldLabel("Country"),
147
                    $helper->getISOCountries()
148
                )
149
            );
150
151
            $fields->replaceField(
152
                "RegionCodes",
153
                ListboxField::create(
154
                    'RegionCodes',
155
                    $this->fieldLabel("RegionCodes"),
156
                    $helper->getRegionsAsObjects()->map('RegionCode', 'Name')
157
                )
158
            );
159
        });
160
161
        return parent::getCMSFields();
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function requireDefaultRecords()
168
    {
169
        parent::requireDefaultRecords();
170
171
        if (ZoneMigrationTask::config()->run_during_dev_build) {
172
            $task = new ZoneMigrationTask();
173
            $task->up();
174
        }
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function onBeforeWrite()
181
    {
182
        parent::onBeforeWrite();
183
184
        // If this applies to all regions in the country,
185
        // then add them all on save
186
        if ($this->AllRegions && isset($this->Country)) {
187
            $helper = GeoZonesHelper::create($this->getCountriesArray());
188
            $codes = [];
189
            foreach ($helper->getRegionArray() as $region) {
190
                $codes[] = $region['region_code'];
191
            }
192
193
            if (count($codes) > 0) {
194
                $this->RegionCodes = json_encode($codes);
0 ignored issues
show
Bug Best Practice introduced by
The property RegionCodes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
195
            }
196
        }
197
    }
198
}
199