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

Update allowing multiple locator pages #40

Merged
merged 1 commit into from
Oct 24, 2014
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
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