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 3 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
16 changes: 3 additions & 13 deletions src/objects/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,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 +94,6 @@ class Location extends DataObject implements PermissionProvider
'Website',
'Phone',
'Email',
'Category.ID',
'Featured',
);

Expand All @@ -113,7 +109,6 @@ class Location extends DataObject implements PermissionProvider
'State',
'PostalCode',
'Country',
'Category.Name',
'Featured.NiceAsBoolean',
'Coords',
);
Expand Down Expand Up @@ -161,11 +156,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
10 changes: 2 additions & 8 deletions src/objects/LocationCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Class LocationCategory
*
* @property string $Name
* @method Locations|HasManyList $Locations
* @method Locations|ManyManyList $Locations
* @method Locators|ManyManyList $Locators
*/
class LocationCategory extends DataObject
Expand All @@ -35,18 +35,12 @@ class LocationCategory extends DataObject
'Name' => 'Varchar(100)',
);

/**
* @var array
*/
private static $has_many = array(
'Locations' => Location::class,
);

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

/**
Expand Down
4 changes: 1 addition & 3 deletions src/pages/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ 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();
}

/**
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
66 changes: 66 additions & 0 deletions tasks/LocationCategoriesTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Dynamic\Locator\Tasks;

use Dynamic\Locator\Location;
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);

$ct = 0;

$convert = function (DataObject $location) use (&$ct) {
Copy link
Member

Choose a reason for hiding this comment

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

Let's add some checks for instances where a location might not have a category. Could skip the add code then. Also work in some memoization to reduce the number of calls to the db for the categories.

$location->Categories()->add($location->Category());
$location->write();
$ct++;
};

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

echo "{$ct} categories converted";
}

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

}