diff --git a/code/Location.php b/code/Location.php index db78912..393c781 100644 --- a/code/Location.php +++ b/code/Location.php @@ -12,7 +12,8 @@ class Location extends DataObject implements PermissionProvider{ ); static $has_one = array( - 'Category' => 'LocationCategory' + 'Category' => 'LocationCategory', + 'Locator' => 'Locator' ); static $casting = array( @@ -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(); + } } \ No newline at end of file diff --git a/code/Locator.php b/code/Locator.php index 1be3432..a3504d1 100644 --- a/code/Locator.php +++ b/code/Locator.php @@ -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 @@ -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(); @@ -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 { @@ -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 @@ -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();