1 | <?php |
||||
2 | |||||
3 | use SilverStripe\ORM\DB; |
||||
4 | use Symfony\Component\Yaml\Yaml; |
||||
5 | use SilverStripe\Control\Director; |
||||
6 | use SilverStripe\Dev\MigrationTask; |
||||
7 | use SilverStripe\ORM\DatabaseAdmin; |
||||
8 | use SilverStripe\Control\Controller; |
||||
9 | use SilverStripe\Core\Config\Config; |
||||
10 | use SilverCommerce\GeoZones\Model\Zone; |
||||
11 | use SilverCommerce\GeoZones\Model\Region; |
||||
12 | use SilverStripe\Core\Manifest\ModuleManifest; |
||||
13 | use SilverCommerce\GeoZones\Helpers\GeoZonesHelper; |
||||
14 | use SilverStripe\i18n\Data\Intl\IntlLocales; |
||||
15 | |||||
16 | class RegionMigrationTask extends MigrationTask |
||||
17 | { |
||||
18 | private static $run_during_dev_build = true; |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
19 | |||||
20 | private static $segment = 'RegionMigrationTask'; |
||||
0 ignored issues
–
show
|
|||||
21 | |||||
22 | protected $description = "Migrate additional regions from the DB to an additional YML config file"; |
||||
23 | |||||
24 | /** |
||||
25 | * Run this task |
||||
26 | * |
||||
27 | * @param HTTPRequest $request The current request |
||||
28 | * |
||||
29 | * @return void |
||||
30 | */ |
||||
31 | public function run($request) { |
||||
32 | if ($request->getVar('direction') == 'down') { |
||||
0 ignored issues
–
show
The method
getVar() does not exist on HttpRequest .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
33 | $this->down(); |
||||
34 | } else { |
||||
35 | $this->up(); |
||||
36 | } |
||||
37 | } |
||||
38 | |||||
39 | /** |
||||
40 | * {@inheritdoc} |
||||
41 | */ |
||||
42 | public function up() |
||||
43 | { |
||||
44 | $project = Config::inst()->get(ModuleManifest::class, 'project'); |
||||
45 | $config_path = Controller::join_links( |
||||
46 | BASE_PATH, |
||||
47 | $project, |
||||
48 | "_config", |
||||
49 | "regions.yml" |
||||
50 | ); |
||||
51 | |||||
52 | $legacy = Region::get(); |
||||
53 | $helper = GeoZonesHelper::create(); |
||||
54 | $data = [ |
||||
55 | IntlLocales::class => [ |
||||
56 | 'countries' => [] |
||||
57 | ], |
||||
58 | GeoZonesHelper::class => [ |
||||
59 | 'iso_3166_regions' => [] |
||||
60 | ] |
||||
61 | ]; |
||||
62 | |||||
63 | $this->log('Migrating Regions to YML'); |
||||
64 | $this->log('(This may take some time)'); |
||||
65 | |||||
66 | $i = 0; |
||||
67 | $j = 0; |
||||
68 | $k = 0; |
||||
69 | |||||
70 | foreach ($legacy as $region) { |
||||
71 | $name = $region->Name; |
||||
72 | $country = $region->CountryCode; |
||||
73 | $code = $region->Code; |
||||
74 | $type = $region->Type; |
||||
75 | |||||
76 | // If country is not in system, add to countries |
||||
77 | try { |
||||
78 | $helper->setCountriesList([$region->CountryCode]); |
||||
79 | $helper->setLimitRegionCodes([$region->Code]); |
||||
80 | $region->delete(); |
||||
81 | $j++; |
||||
82 | } catch (LogicException $e) { |
||||
83 | // If either country or region throw an exception, |
||||
84 | // add them to the new map |
||||
85 | if (!in_array(strtolower($country), $data[IntlLocales::class]['countries'])) { |
||||
86 | $data[IntlLocales::class]['countries'][] = strtolower($country); |
||||
87 | $k++; |
||||
88 | } |
||||
89 | |||||
90 | $data[GeoZonesHelper::class]['iso_3166_regions'][] = [ |
||||
91 | 'code' => strtoupper($country) . "-" . $code, |
||||
92 | 'name' => $name, |
||||
93 | 'type' => $type |
||||
94 | ]; |
||||
95 | |||||
96 | $region->delete(); |
||||
97 | $i++; |
||||
98 | } |
||||
99 | } |
||||
100 | |||||
101 | $yml = <<<YAML |
||||
102 | --- |
||||
103 | Name: customregions |
||||
104 | --- |
||||
105 | |||||
106 | YAML; |
||||
107 | |||||
108 | $yml .= Yaml::dump($data, 4); |
||||
109 | |||||
110 | $this->log("Migrated {$k} Countries"); |
||||
111 | $this->log("Migrated {$i} Regions"); |
||||
112 | $this->log("Deleted {$j} Regions"); |
||||
113 | |||||
114 | if ($i > 0) { |
||||
115 | file_put_contents($config_path, $yml); |
||||
116 | $this->log("Created regions.yml"); |
||||
117 | } |
||||
118 | } |
||||
119 | |||||
120 | /** |
||||
121 | * {@inheritdoc} |
||||
122 | */ |
||||
123 | public function down() { |
||||
124 | $regions = Config::inst()->get( |
||||
125 | GeoZonesHelper::class, |
||||
126 | 'iso_3166_regions' |
||||
127 | ); |
||||
128 | |||||
129 | $this->log('Downgrading Regions'); |
||||
130 | $this->log('(This might take some time)'); |
||||
131 | $i = 0; |
||||
132 | |||||
133 | foreach ($regions as $region) { |
||||
134 | $code = explode('-', $region['code']); |
||||
135 | |||||
136 | $new_region = Region::create(); |
||||
137 | $new_region->Name = $region['name']; |
||||
138 | $new_region->Type = $region['type']; |
||||
139 | $new_region->CountryCode = $code[0]; |
||||
140 | $new_region->Code = $code[1]; |
||||
141 | $new_region->write(); |
||||
142 | |||||
143 | $i++; |
||||
144 | } |
||||
145 | |||||
146 | $this->log("Downgraded {$i} Regions"); |
||||
147 | } |
||||
148 | |||||
149 | /** |
||||
150 | * @param string $text |
||||
151 | */ |
||||
152 | protected function log($text) { |
||||
153 | if(Controller::curr() instanceof DatabaseAdmin) { |
||||
154 | DB::alteration_message($text, 'obsolete'); |
||||
155 | } elseif (Director::is_cli()) { |
||||
156 | echo $text . "\n"; |
||||
157 | } else { |
||||
158 | echo $text . "<br/>"; |
||||
159 | } |
||||
160 | } |
||||
161 | } |
||||
162 |