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 b2eb290
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
20 changes: 19 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 @@ -118,6 +119,14 @@ public function getCMSFields() {
return $fields;
}

public function validate() {
$result = parent::validate();
if(Locator::getMultipleLocators() && $this->LocatorID == 0) {
$result->error('You must associate this location with a locator page. Add the location from the desired locator page.');
}
return $result;
}

/**
* @param Member $member
* @return boolean
Expand Down Expand Up @@ -147,5 +156,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();
}

}
41 changes: 29 additions & 12 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,11 @@ 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;
}

}

class Locator_Controller extends Page_Controller {
Expand Down Expand Up @@ -101,13 +111,16 @@ public function init() {

$kilometer = ($this->data()->Unit == 'km') ? 'lengthUnit: "km"' : 'lengthUnit: "m"';

$link = $this->Link() . "xml.xml";
$link .= (Locator::getMultipleLocators()) ? "?locatorID=" . $this->data()->ID : "" ;

// init map
if(Locator::getLocations()) {
Requirements::customScript("
$(function($) {
$('#map-container').storeLocator({
" . $load . "
dataLocation: '" . $this->Link() . "xml.xml',
dataLocation: '" . $link . "',
listTemplatePath: '" . $listTemplatePath . "',
infowindowTemplatePath: '" . $infowindowTemplatePath . "',
originMarker: true,
Expand All @@ -131,17 +144,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 +182,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 b2eb290

Please sign in to comment.