|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\Locator\Tasks; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\Locator\Location; |
|
6
|
|
|
use Dynamic\Locator\Page\LocationPage; |
|
7
|
|
|
use Dynamic\Locator\Page\Locator; |
|
8
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
9
|
|
|
use SilverStripe\Dev\BuildTask; |
|
10
|
|
|
use SilverStripe\Versioned\Versioned; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class LocationToPageTask |
|
14
|
|
|
* @package Dynamic\Locator\Tasks |
|
15
|
|
|
*/ |
|
16
|
|
|
class LocationToPageTask extends BuildTask |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $title = 'Locator - Location to Page Task'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private static $segment = 'locator-location-to-page-task'; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var bool |
|
30
|
|
|
*/ |
|
31
|
|
|
private static $auto_publish_location = false; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param \SilverStripe\Control\HTTPRequest $request |
|
35
|
|
|
*/ |
|
36
|
|
|
public function run($request) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->migrateLocations(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function migrateLocations() |
|
45
|
|
|
{ |
|
46
|
|
|
if (!$parent = Locator::get()->first()) { |
|
47
|
|
|
$parent = Locator::create(); |
|
48
|
|
|
$parent->Title = "Locator"; |
|
49
|
|
|
$parent->writeToStage(Versioned::DRAFT); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** @var Location $location */ |
|
53
|
|
|
foreach ($this->getLocations() as $location) { |
|
54
|
|
|
if (!LocationPage::get()->filter('LegacyObjectID', $location->ID)->first()) { |
|
55
|
|
|
if ($location->hasMethod('isPublished')) { |
|
56
|
|
|
$published = $location->isPublished(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** @var LocationPage $newLocation */ |
|
60
|
|
|
$newLocation = Injector::inst()->create(LocationPage::class, $location->toMap(), false); |
|
61
|
|
|
$newLocation->setClassName(LocationPage::class); |
|
62
|
|
|
$newLocation->ID = null; |
|
63
|
|
|
$newLocation->ParentID = $parent->ID; |
|
|
|
|
|
|
64
|
|
|
$newLocation->LegacyObjectID = $location->ID; |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$this->extend('preLocationMigrationUpdate', $location, $newLocation); |
|
67
|
|
|
|
|
68
|
|
|
$newLocation->writeToStage(Versioned::DRAFT); |
|
69
|
|
|
|
|
70
|
|
|
if ((isset($published) && $published) || $this->config()->get('auto_publish_location')) { |
|
71
|
|
|
$newLocation->publishSingle(); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return \Generator |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function getLocations() |
|
81
|
|
|
{ |
|
82
|
|
|
foreach (Location::get() as $location) { |
|
83
|
|
|
yield $location; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|