Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locations many categories #178

Merged
merged 5 commits into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/admin/LocationAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public function getExportFields()
'Phone' => 'Phone',
'Fax' => 'Fax',
'Email' => 'Email',
'Category.Name' => 'Category',
'ShowInLocator' => 'ShowInLocator',
'Featured' => 'Featured',
'Lat' => 'Lat',
Expand Down
4 changes: 2 additions & 2 deletions src/bulkloader/LocationCsvBulkLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class LocationCsvBulkLoader extends CsvBulkLoader
'Name' => 'Title',
'City' => 'Suburb',
'EmailAddress' => 'Email',
'Category' => 'Category.Name',
'Import_ID' => 'Import_ID',
);

Expand All @@ -32,12 +31,13 @@ class LocationCsvBulkLoader extends CsvBulkLoader
/**
* @var array
*/
/*
public $relationCallbacks = array(
'Category.Name' => array(
'relationname' => 'Category',
'callback' => 'getCategoryByName',
),
);
);*/

/**
* @param $obj
Expand Down
21 changes: 6 additions & 15 deletions src/objects/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Dynamic\Locator;

use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\Security\PermissionProvider;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\EmailField;
Expand All @@ -19,8 +20,8 @@
* @property string $Email
* @property string $EmailAddress
* @property int $Import_ID
* @property int $CategoryID
* @method LocationCategory $Category
*
* @method ManyManyList Categories
*/
class Location extends DataObject implements PermissionProvider
{
Expand Down Expand Up @@ -51,12 +52,9 @@ class Location extends DataObject implements PermissionProvider
'Import_ID' => 'Int',
);

/**
* @var array
*/
private static $has_one = array(
'Category' => LocationCategory::class,
);
private static $many_many = [
'Categories' => LocationCategory::class,
];

/**
* @var string
Expand Down Expand Up @@ -97,7 +95,6 @@ class Location extends DataObject implements PermissionProvider
'Website',
'Phone',
'Email',
'Category.ID',
'Featured',
);

Expand All @@ -113,7 +110,6 @@ class Location extends DataObject implements PermissionProvider
'State',
'PostalCode',
'Country',
'Category.Name',
'Featured.NiceAsBoolean',
'Coords',
);
Expand Down Expand Up @@ -161,11 +157,6 @@ public function getCMSFields()

$fields->replaceField('Email', EmailField::create('Email'));

$fields->replaceField(
'CategoryID',
DropdownField::create('CategoryID', 'Category', LocationCategory::get()->map())->setEmptyString('')
);

$featured = $fields->dataFieldByName('Featured')
->setDescription('Location will display near the top of the results list');
$fields->insertAfter(
Expand Down
24 changes: 14 additions & 10 deletions src/objects/LocationCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\Security\Permission;

/**
* Class LocationCategory
*
* @property string $Name
* @method Locations|HasManyList $Locations
* @method Locators|ManyManyList $Locators
* @method Locations|ManyManyList LocationSet()
* @method Locators|ManyManyList Locators()
*/
class LocationCategory extends DataObject
{
Expand All @@ -38,15 +39,9 @@ class LocationCategory extends DataObject
/**
* @var array
*/
private static $has_many = array(
'Locations' => Location::class,
);

/**
* @var array
*/
private static $belogs_many_many = array(
private static $belongs_many_many = array(
'Locators' => Locator::class,
'LocationSet' => Location::class,
);

/**
Expand All @@ -68,6 +63,7 @@ public function getCMSFields()

$fields->removeByName([
'Locations',
'LocationSet',
]);

if ($this->ID) {
Expand All @@ -87,6 +83,14 @@ public function getCMSFields()
return $fields;
}

/**
* For backwards compatability
* @return Locations|ManyManyList
*/
public function Locations() {
return $this->LocationSet();
}

/**
* @param null $member
* @param array $context
Expand Down
6 changes: 3 additions & 3 deletions src/pages/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ public function getShowRadius()
*/
public function getCategories()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this method still needed if it's no longer altering the relation list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alteration has been added back in. A typo in a relation array caused the initial problem.

{
return $this->Categories()->filter(array(
'Locations.ID:GreaterThan' => 0
));
return $this->Categories()->filter([
'LocationSet.ID:GreaterThan' => 0,
]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/pages/LocatorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ public function setLocations(HTTPRequest $request = null)

$categoryVar = Config::inst()->get(Locator::class, 'category_var');
if ($request->getVar($categoryVar)) {
$filter['CategoryID'] = $request->getVar($categoryVar);
$filter['Categories.ID'] = $request->getVar($categoryVar);
} else {
if ($this->getPageCategories()->exists()) {
foreach ($this->getPageCategories() as $category) {
$filter['CategoryID'][] = $category->ID;
$filter['Categories.ID'][] = $category->ID;
}
}
}
Expand Down
86 changes: 86 additions & 0 deletions tasks/LocationCategoriesTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Dynamic\Locator\Tasks;

use Dynamic\Locator\Location;
use Dynamic\Locator\LocationCategory;
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DataObject;

/**
* Class LocationCategoriesTask
* @package Dynamic\Locator\Tasks
*/
class LocationCategoriesTask extends BuildTask
{

/**
* @var string
*/
protected $title = 'Location categories to many_many';

/**
* @var string
*/
protected $description = 'Migration task - Converts locations to have multiple categories';

/**
* @var bool
*/
protected $enabled = true;

/**
* @param $request
*/
public function run($request)
{
/** @var DataObject $class */
$class = ($request->getVar('locationclass')) ? $request->getVar('locationclass') : Location::class;
$class::add_extension(LocationCategoryExtension::class);


$categories = [];

$convert = function (DataObject $location) use (&$categories) {
/** @var Location $location */
// skip if no category
if ($location->CategoryID > 0) {
$categories[$location->CategoryID][] = $location->ID;
}
};

foreach ($this->iterateLocations($class) as $location) {
$convert($location);
}

$catCt = 0;
$locCT = 0;

foreach ($categories as $categoryID => $locations) {
/** @var LocationCategory $category */
$category = LocationCategory::get()->byID($categoryID);
$category->Locations()->addMany($locations);

$catCt++;
$locCT += count($locations);
}

echo "{$catCt} categories converted<br />";
echo "{$locCT} location relations converted<br />";

$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo "Process Time: {$time} seconds";
}

/**
* @param string $class
* @return Generator
*/
protected function iterateLocations($class)
{
foreach ($class::get() as $location) {
yield $location;
}
}

}
13 changes: 9 additions & 4 deletions tests/fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ SilverStripe\Security\Group:
Title: Edit Locations
locationdelete:
Title: Location Delete

SilverStripe\Security\Permission:
locationcreate:
Code: Location_CREATE
Expand All @@ -18,6 +19,7 @@ SilverStripe\Security\Permission:
Code: Location_DELETE
Type: 1
GroupID: =>SilverStripe\Security\Group.locationdelete

SilverStripe\Security\Member:
locationcreate:
FirstName: Create
Expand All @@ -38,6 +40,7 @@ SilverStripe\Security\Member:
FirstName: Default
Surname: Member
email: [email protected]

Dynamic\Locator\LocationCategory:
service:
Name: Service
Expand All @@ -47,10 +50,12 @@ Dynamic\Locator\LocationCategory:
Name: Entertainment
technology:
Name: Technology

Dynamic\Locator\Locator:
locator1:
Title: Locator
Categories: =>Dynamic\Locator\LocationCategory.service,=>Dynamic\Locator\LocationCategory.technology

Dynamic\Locator\Location:
dynamic:
Title: Dynamic, Inc.
Expand All @@ -63,14 +68,14 @@ Dynamic\Locator\Location:
State: WI
PostalCode:
Country: US
Category: =>Dynamic\Locator\LocationCategory.service
Categories: =>Dynamic\Locator\LocationCategory.service
silverstripe:
Title: Silverstripe
Featured: true
Website: http://silverstripe.org
Phone: 555-555-5555
Email: [email protected]
Category: =>Dynamic\Locator\LocationCategory.service
Categories: =>Dynamic\Locator\LocationCategory.service
3sheeps:
Title: 3 Sheeps Brewing
Featured: false
Expand All @@ -82,7 +87,7 @@ Dynamic\Locator\Location:
State: WI
PostalCode:
Country: US
Category: =>Dynamic\Locator\LocationCategory.entertainment
Categories: =>Dynamic\Locator\LocationCategory.entertainment
Lambeau:
Title: Lambeau Field
Featured: true
Expand All @@ -92,4 +97,4 @@ Dynamic\Locator\Location:
State: WI
PostalCode:
Country: US
Category: =>Dynamic\Locator\LocationCategory.entertainment
Categories: =>Dynamic\Locator\LocationCategory.entertainment
2 changes: 1 addition & 1 deletion tests/objects/LocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testFieldLabels()
'Country' => 'Country',
'Lat' => 'Lat',
'Lng' => 'Lng',
'Category' => 'Category',
'Categories' => 'Categories',
'Category.Name' => 'Category',
'Category.ID' => 'Category',
'Featured.NiceAsBoolean' => 'Featured',
Expand Down