From a244592f7eed3d528f49ca96fb1ca421ee1f77f3 Mon Sep 17 00:00:00 2001 From: Jason Irish Date: Fri, 10 Feb 2017 16:43:25 -0600 Subject: [PATCH 1/3] Location - versioning closes #140 --- code/objects/Location.php | 20 ++++++++++------- code/pages/Locator.php | 4 +--- composer.json | 2 ++ tasks/LocationPublishTask.php | 42 +++++++++++++++++++++++++++++++++++ tests/LocationTest.php | 2 ++ 5 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 tasks/LocationPublishTask.php diff --git a/code/objects/Location.php b/code/objects/Location.php index 6ed0358..57bed74 100644 --- a/code/objects/Location.php +++ b/code/objects/Location.php @@ -89,7 +89,6 @@ class Location extends DataObject implements PermissionProvider 'Phone', 'Email', 'Category.ID', - 'ShowInLocator', 'Featured', ); @@ -106,11 +105,17 @@ class Location extends DataObject implements PermissionProvider 'Postcode', 'Country', 'Category.Name', - 'ShowInLocator.NiceAsBoolean', 'Featured.NiceAsBoolean', 'Coords', ); + /** + * @var array + */ + private static $extensions = [ + 'VersionedDataObject', + ]; + /** * Coords status for $summary_fields * @@ -164,8 +169,6 @@ public function getCMSFields() ->setAttribute('placeholder', 'http://'), DropdownField::create('CategoryID', 'Category', LocationCategory::get()->map('ID', 'Title')) ->setEmptyString('-- select --'), - CheckboxField::create('ShowInLocator', 'Show in results') - ->setDescription('Location will be included in results list'), CheckboxField::create('Featured') ->setDescription('Location will show at/near the top of the results list') ) @@ -175,13 +178,14 @@ public function getCMSFields() // allow to be extended via DataExtension $this->extend('updateCMSFields', $fields); + $fields->removeByName([ + 'ShowInLocator', + 'Import_ID', + ]); + // override Suburb field name $fields->dataFieldByName('Suburb')->setTitle('City'); - $fields->removeByName(array( - 'Import_ID', - )); - return $fields; } diff --git a/code/pages/Locator.php b/code/pages/Locator.php index fecc6a1..26b715d 100644 --- a/code/pages/Locator.php +++ b/code/pages/Locator.php @@ -167,9 +167,7 @@ class Locator_Controller extends Page_Controller /** * @var array */ - private static $base_filter = [ - 'ShowInLocator' => true, - ]; + private static $base_filter = []; /** * @var array diff --git a/composer.json b/composer.json index d0744a2..922caf6 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,8 @@ "silverstripe/framework": "^3.4", "silverstripe/cms": "^3.4", "silverstripe-australia/addressable": "^1.1", + "heyday/silverstripe-versioneddataobjects": "^1.4", + "unclecheese/betterbuttons": "^1.3", "muskie9/data-to-arraylist": "^1.0" }, "suggest": { diff --git a/tasks/LocationPublishTask.php b/tasks/LocationPublishTask.php new file mode 100644 index 0000000..ed4d538 --- /dev/null +++ b/tasks/LocationPublishTask.php @@ -0,0 +1,42 @@ +publishLocations(); + } + /** + * mark all ProductDetail records as ShowInMenus = 0. + */ + public function publishLocations() + { + $locations = Location::get(); + $ct = 0; + foreach ($locations as $location) { + if ($location->ShowInLocator == 1 && !$location->isPublished()) { + $title = $location->Title; + $location->writeToStage('Stage'); + $location->publish('Stage', 'Live'); + echo $title.'

'; + ++$ct; + } + } + echo '

'.$ct.' locations updated.

'; + } +} \ No newline at end of file diff --git a/tests/LocationTest.php b/tests/LocationTest.php index e899fa1..68ba71c 100644 --- a/tests/LocationTest.php +++ b/tests/LocationTest.php @@ -53,6 +53,8 @@ public function testFieldLabels() 'Coords' => 'Coords', 'Import_ID' => 'Import_ID', 'LatLngOverride' => 'Lat Lng Override', + 'Version' => 'Version', + 'Versions' => 'Versions' ); $this->assertEquals($expected, $labels); } From 10ca298c0689c3db9fd14ce8a01bfc564ece8935 Mon Sep 17 00:00:00 2001 From: Nic Horstmeier Date: Tue, 21 Feb 2017 20:17:03 -0600 Subject: [PATCH 2/3] UPDATE publish task with generator and closure --- tasks/LocationPublishTask.php | 50 ++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/tasks/LocationPublishTask.php b/tasks/LocationPublishTask.php index ed4d538..7f943d4 100644 --- a/tasks/LocationPublishTask.php +++ b/tasks/LocationPublishTask.php @@ -1,5 +1,8 @@ publishLocations(); + $class = ($request->getVar('locationclass')) ? $request->getVar('locationclass') : 'Location'; + $this->publishLocations($class); + } + + /** + * @param string $class + * @return Generator + */ + protected function iterateLocations($class) + { + foreach ($class::get()->filter('ShowInLocator', true) as $location) { + yield $location; + } } + /** * mark all ProductDetail records as ShowInMenus = 0. + * + * @param string $class */ - public function publishLocations() + public function publishLocations($class) { - $locations = Location::get(); + if (!isset($class) || !class_exists($class) || !$class instanceof Location) $class = 'Location'; $ct = 0; - foreach ($locations as $location) { - if ($location->ShowInLocator == 1 && !$location->isPublished()) { + $publish = function ($location) use (&$ct) { + if (!$location->isPublished()) { $title = $location->Title; $location->writeToStage('Stage'); $location->publish('Stage', 'Live'); - echo $title.'

'; + static::write_message($title); ++$ct; } + }; + + foreach ($this->iterateLocations($class) as $location) { + $publish($location); + } + + static::write_message("{$ct} locations updated."); + } + + /** + * @param $message + */ + protected static function write_message($message) + { + if (Director::is_cli()) { + echo "{$message}\n"; + } else { + echo "{$message}

"; } - echo '

'.$ct.' locations updated.

'; } } \ No newline at end of file From def6fdf6a8315b5914e0f1279768c1b92a1aa7cc Mon Sep 17 00:00:00 2001 From: Nic Horstmeier Date: Tue, 21 Feb 2017 20:24:49 -0600 Subject: [PATCH 3/3] BUGFIX missing comma --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index fd849df..373b165 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "silverstripe-australia/addressable": "^1.1", "heyday/silverstripe-versioneddataobjects": "^1.4", "unclecheese/betterbuttons": "^1.3", - "muskie9/data-to-arraylist": "^1.0" + "muskie9/data-to-arraylist": "^1.0", "silverstripe-australia/gridfieldextensions": "^1.2" }, "suggest": {