silvercommerce /
geozones
| 1 | <?php |
||||
| 2 | |||||
| 3 | use SilverStripe\ORM\DB; |
||||
| 4 | use SilverStripe\Dev\MigrationTask; |
||||
| 5 | use SilverStripe\ORM\DatabaseAdmin; |
||||
| 6 | use SilverStripe\Control\Controller; |
||||
| 7 | use SilverCommerce\GeoZones\Model\Zone; |
||||
| 8 | use SilverStripe\Dev\Debug; |
||||
| 9 | use SilverStripe\Control\Director; |
||||
| 10 | |||||
| 11 | class ZoneMigrationTask extends MigrationTask { |
||||
| 12 | /** |
||||
| 13 | * Should this task be invoked automatically via dev/build? |
||||
| 14 | * |
||||
| 15 | * @config |
||||
| 16 | * |
||||
| 17 | * @var bool |
||||
| 18 | */ |
||||
| 19 | private static $run_during_dev_build = true; |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 20 | |||||
| 21 | private static $segment = 'ZoneMigrationTask'; |
||||
|
0 ignored issues
–
show
|
|||||
| 22 | |||||
| 23 | protected $description = "Upgrade zones to allowing multiple countries"; |
||||
| 24 | |||||
| 25 | |||||
| 26 | /** |
||||
| 27 | * Run this task |
||||
| 28 | * |
||||
| 29 | * @param HTTPRequest $request The current request |
||||
| 30 | * |
||||
| 31 | * @return void |
||||
| 32 | */ |
||||
| 33 | public function run($request) { |
||||
| 34 | 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. Loading history...
|
|||||
| 35 | $this->down(); |
||||
| 36 | } else { |
||||
| 37 | $this->up(); |
||||
| 38 | } |
||||
| 39 | } |
||||
| 40 | |||||
| 41 | /** |
||||
| 42 | * {@inheritdoc} |
||||
| 43 | */ |
||||
| 44 | public function up() { |
||||
| 45 | $zones = Zone::get(); |
||||
| 46 | |||||
| 47 | $this->log('Migrating Zones'); |
||||
| 48 | $i = 0; |
||||
| 49 | |||||
| 50 | foreach ($zones as $zone) { |
||||
| 51 | $countries = json_decode($zone->Country); |
||||
| 52 | |||||
| 53 | if (empty($countries) && isset($zone->Country)) { |
||||
| 54 | $countries = [$zone->Country]; |
||||
| 55 | $zone->Country = json_encode($countries); |
||||
| 56 | $zone->write(); |
||||
| 57 | $i++; |
||||
| 58 | } |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | $this->log("Migrated {$i} Zones"); |
||||
| 62 | } |
||||
| 63 | |||||
| 64 | /** |
||||
| 65 | * {@inheritdoc} |
||||
| 66 | */ |
||||
| 67 | public function down() { |
||||
| 68 | $zones = Zone::get(); |
||||
| 69 | |||||
| 70 | $this->log('Downgrading Zones'); |
||||
| 71 | $i = 0; |
||||
| 72 | |||||
| 73 | foreach ($zones as $zone) { |
||||
| 74 | $countries = json_decode($zone->Country); |
||||
| 75 | |||||
| 76 | if (is_array($countries)) { |
||||
| 77 | $zone->Country = $countries[0]; |
||||
| 78 | $zone->write(); |
||||
| 79 | $i++; |
||||
| 80 | } |
||||
| 81 | } |
||||
| 82 | |||||
| 83 | $this->log("Downgraded {$i} Zones"); |
||||
| 84 | } |
||||
| 85 | |||||
| 86 | /** |
||||
| 87 | * @param string $text |
||||
| 88 | */ |
||||
| 89 | protected function log($text) { |
||||
| 90 | if(Controller::curr() instanceof DatabaseAdmin) { |
||||
| 91 | DB::alteration_message($text, 'obsolete'); |
||||
| 92 | } elseif (Director::is_cli()) { |
||||
| 93 | echo $text . "\n"; |
||||
| 94 | } else { |
||||
| 95 | echo $text . "<br/>"; |
||||
| 96 | } |
||||
| 97 | } |
||||
| 98 | } |
||||
| 99 |