Issues (25)

src/Model/Region.php (5 issues)

1
<?php
2
3
namespace SilverCommerce\GeoZones\Model;
4
5
use SilverStripe\ORM\DB;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\Control\Controller;
8
9
/**
10
 * Subdivisions of a country, based on ISO-3166-2 standards.
11
 * 
12
 * Thanks to debian (https://salsa.debian.org/iso-codes-team/iso-codes/blob/master/data/iso_3166-2.json)
13
 * for the base data set
14
 */
15
class Region extends DataObject
16
{
17
    /**
18
     * Syncronise codes on dev/build
19
     */
20
    private static $create_on_build = true;
21
22
    private static $table_name = "GeoZoneRegion";
0 ignored issues
show
The private property $table_name is not used, and could be removed.
Loading history...
23
24
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
25
        "Name" => "Varchar",
26
        "Type" => "Varchar",
27
        "Code" => "Varchar(3)",
28
        "CountryCode" => "Varchar(2)"
29
    ];
30
31
    private static $belongs_many_many = [
0 ignored issues
show
The private property $belongs_many_many is not used, and could be removed.
Loading history...
32
        'Zones' => Zone::class
33
    ];
34
35
    private static $summary_fields = [
0 ignored issues
show
The private property $summary_fields is not used, and could be removed.
Loading history...
36
        "CountryCode",
37
        "Name",
38
        "Type",
39
        "Code"
40
    ];
41
42
    private static $default_sort = [
0 ignored issues
show
The private property $default_sort is not used, and could be removed.
Loading history...
43
        "CountryCode" => "ASC",
44
        "Name" => "ASC"
45
    ];
46
47
    public function requireDefaultRecords()
48
    {
49
        parent::requireDefaultRecords();
50
        $existing = self::get();
51
        $create = $this->config()->create_on_build;
52
53
        if (!$existing->exists() && $create) {
54
            DB::alteration_message(
55
                "Setting up regions (this could take some time)",
56
                "created"
57
            );
58
59
            $data_loc = Controller::join_links(
60
                dirname(dirname(dirname(__FILE__))),
61
                "data",
62
                "iso_3166-2.json"
63
            );
64
65
            $data = file_get_contents($data_loc);
66
            $data_json = json_decode($data, true);
67
            $data_json = $data_json["3166-2"];
68
            $i = 0;
69
70
            foreach ($data_json as $item) {
71
                if (array_key_exists("code", $item) && array_key_exists("name", $item)) {
72
                    $codes = explode("-", $item["code"]);
73
                    $type = array_key_exists("type", $item) ? $item["type"] : "";
74
75
                    $region = Region::create([
76
                        "Name" => $item["name"],
77
                        "Type" => $type,
78
                        "Code" => $codes[1],
79
                        "CountryCode" => $codes[0],
80
                    ]);
81
                    $region->write();
82
                    $i++;
83
                }
84
            }
85
86
            DB::alteration_message(
87
                "Added {$i} regions",
88
                "created"
89
            );
90
        }
91
    }
92
}