Skip to content

Commit

Permalink
Update allowing multiple locator pages
Browse files Browse the repository at this point in the history
  • Loading branch information
muskie9 committed Oct 24, 2014
1 parent 0397794 commit f0ae9dd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
12 changes: 11 additions & 1 deletion code/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class Location extends DataObject implements PermissionProvider{
);

static $has_one = array(
'Category' => 'LocationCategory'
'Category' => 'LocationCategory',
'Locator' => 'Locator'
);

static $casting = array(
Expand Down Expand Up @@ -147,5 +148,14 @@ public function providePermissions() {
'Location_CREATE' => 'Create a Location'
);
}

public function onBeforeWrite(){

if(Locator::get()->count() == 1){
$this->LocatorID = Locator::get()->first()->ID;
}

parent::onBeforeWrite();
}

}
45 changes: 34 additions & 11 deletions code/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class Locator extends Page {
'ModalWindow' => 'Boolean',
'Unit' => 'Enum("km,m","m")'
);

private static $has_many = array(
'Locations' => 'Location'
);

private static $defaults = array(
'AutoGeocode' => true
Expand All @@ -20,8 +24,10 @@ public function getCMSFields() {
$fields = parent::getCMSFields();

// Locations Grid Field
$config = GridFieldConfig_RecordEditor::create();
$fields->addFieldToTab("Root.Locations", GridField::create("Locations", "Locations", Location::get(), $config));
$config = (self::getMultipleLocators())
? GridFieldConfig_RelationEditor::create() : GridFieldConfig_RecordEditor::create();
$locations = (self::getMultipleLocators()) ? $this->Locations() : Location::get();
$fields->addFieldToTab("Root.Locations", GridField::create("Locations", "Locations", $locations, $config));

// Location categories
$config = GridFieldConfig_RecordEditor::create();
Expand Down Expand Up @@ -52,7 +58,20 @@ public static function getLocations($filter = array(), $exclude = array()){
public function getAreLocations(){
return self::getLocations();
}

public static function getMultipleLocators(){
return (Locator::get()->count() > 1) ? true : false;
}

//TODO implement this on delete, not unpublish as it is now
/*public function onBeforeDelete(){
$locations = Location::get()->filter(array('LocatorID' => $this->ID));
foreach($locations as $location){
$location->LocatorID = 0;
$location->write();
}
parent::onBeforeDelete();
}*/

}

class Locator_Controller extends Page_Controller {
Expand Down Expand Up @@ -131,17 +150,20 @@ public function init() {
/**
* Find all locations for map
*
* By default, will return a XML feed of all locations marked "show in locator".
*
* If Locator::set_datasource('http:www.example.com') is set in _config.php, Locator will look
* for a JSON feed and return the results.
* Will return a XML feed of all locations marked "show in locator".
*
* @access public
* @return XML file
* @todo rename/refactor to allow for json/xml
*/
public function xml() {

$Locations = Locator::getLocations();
public function xml(SS_HTTPRequest $request) {
$filter = array();
if(Locator::getMultipleLocators()){//only checks published locators
if($request->getVar('locatorID')){
$filter['LocatorID'] = $request->getVar('locatorID');
}
}
$Locations = Locator::getLocations($filter);

return $this->customise(array(
'Locations' => $Locations
Expand All @@ -166,7 +188,8 @@ public function LocationSearch() {

if (LocationCategory::get()->Count() > 0) {

$locals = Locator::getLocations($filter = array(), $exclude = array('CategoryID' => 0));
$filter = ($this->data()->getMultipleLocators()) ? array('LocatorID' => $this->data()->ID) : array();
$locals = Locator::getLocations($filter, $exclude = array('CategoryID' => 0));
//debug::show($locals);
$categories = ArrayList::create();

Expand Down

0 comments on commit f0ae9dd

Please sign in to comment.