Skip to content

Commit

Permalink
Locator - allow filter by LocationCategory
Browse files Browse the repository at this point in the history
In CMS Settings tab on Locator page, added following:

has_one LocationCategory

If a user selects a category, list is filtered by that category on load, and category drop down is removed.

Allows module to have multiple Locator pages in a site and show different locations on each.

fixes #57
  • Loading branch information
jsirish committed Nov 10, 2015
1 parent fa7f029 commit b1a642e
Showing 1 changed file with 42 additions and 38 deletions.
80 changes: 42 additions & 38 deletions code/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

class Locator extends Page
{

private static $db = array(
'AutoGeocode' => 'Boolean',
'ModalWindow' => 'Boolean',
'Unit' => 'Enum("km,m","m")'
'Unit' => 'Enum("m,km","m")',
);

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

private static $has_many = array();

private static $defaults = array(
'AutoGeocode' => true
'AutoGeocode' => true,
);

private static $singular_name = "Locator";
private static $plural_name = "Locators";
private static $singular_name = 'Locator';
private static $plural_name = 'Locators';
private static $description = 'Find locations on a map';

public function getCMSFields()
Expand All @@ -26,12 +29,12 @@ public function getCMSFields()
// Locations Grid Field
$config = GridFieldConfig_RecordEditor::create();
$locations = Location::get();
$fields->addFieldToTab("Root.Locations", GridField::create("Locations", "Locations", $locations, $config));
$fields->addFieldToTab('Root.Locations', GridField::create('Locations', 'Locations', $locations, $config));

// Location categories
$config = GridFieldConfig_RecordEditor::create();
$fields->addFieldToTab("Root.Categories",
GridField::create("Categories", "Categories", LocationCategory::get(), $config));
$fields->addFieldToTab('Root.Categories',
GridField::create('Categories', 'Categories', LocationCategory::get(), $config));

// Settings
$fields->addFieldsToTab('Root.Settings', array(
Expand All @@ -40,7 +43,11 @@ public function getCMSFields()
CheckboxField::create('AutoGeocode',
'Auto Geocode - Automatically filter map results based on user location')
->setDescription('Note: if any locations are set as featured, the auto geocode is automatically disabled.'),
CheckboxField::create('ModalWindow', 'Modal Window - Show Map results in a modal window')
CheckboxField::create('ModalWindow', 'Modal Window - Show Map results in a modal window'),
HeaderField::create('CategoryOptionsHeader', 'Location Filtering', 3),
DropdownField::create('CategoryID', 'Category', LocationCategory::get()->map())
->setEmptyString('All')
->setDescription('only show locations from the selected category'),
));

$this->extend('updateCMSFields', $fields);
Expand All @@ -51,6 +58,7 @@ public function getCMSFields()
public static function getLocations($filter = array(), $exclude = array())
{
$filter['ShowInLocator'] = true;

return Location::get()
->exclude($exclude)
->exclude('Lat', 0)
Expand All @@ -66,12 +74,10 @@ public function getAllCategories()
{
return LocationCategory::get();
}

}

class Locator_Controller extends Page_Controller
{

// allowed actions
private static $allowed_actions = array('xml');

Expand Down Expand Up @@ -101,79 +107,80 @@ public function init()
'autoGeocode: true, fullMapStart: false,' :
'autoGeocode: false, fullMapStart: true, storeLimit: 1000, maxDistance: true,';

$absoluteBase = getcwd();//get current working dir
$base = str_replace('/framework', '', $absoluteBase);//remove framework if .htaccess is working
$themePath = $base . "/" . $themeDir;
$base = Director::baseFolder();
$themePath = $base.'/'.$themeDir;

$listTemplatePath = (file_exists($themePath . '/templates/location-list-description.html')) ?
$themeDir . '/templates/location-list-description.html' :
$listTemplatePath = (file_exists($themePath.'/templates/location-list-description.html')) ?
$themeDir.'/templates/location-list-description.html' :
'locator/templates/location-list-description.html';
$infowindowTemplatePath = (file_exists($themePath . '/templates/infowindow-description.html')) ?
$themeDir . '/templates/infowindow-description.html' :
$infowindowTemplatePath = (file_exists($themePath.'/templates/infowindow-description.html')) ?
$themeDir.'/templates/infowindow-description.html' :
'locator/templates/infowindow-description.html';

// in page or modal
$modal = ($this->data()->ModalWindow) ? 'modalWindow: true' : 'modalWindow: false';

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

$link = $this->Link() . "xml.xml";
$link = $this->Link().'xml.xml';

// init map
if (Locator::getLocations()) {
Requirements::customScript("
$(function($) {
$('#map-container').storeLocator({
" . $load . "
dataLocation: '" . $link . "',
listTemplatePath: '" . $listTemplatePath . "',
infowindowTemplatePath: '" . $infowindowTemplatePath . "',
".$load."
dataLocation: '".$link."',
listTemplatePath: '".$listTemplatePath."',
infowindowTemplatePath: '".$infowindowTemplatePath."',
originMarker: true,
" . $modal . ",
" . $featured . ",
".$modal.',
'.$featured.",
slideMap: false,
zoomLevel: 0,
distanceAlert: 120,
formID: 'Form_LocationSearch',
inputID: 'Form_LocationSearch_address',
categoryID: 'Form_LocationSearch_category',
distanceAlert: -1,
" . $kilometer . "
".$kilometer.'
});
});
");
');
}

}

/**
* Find all locations for map
* Find all locations for map.
*
* 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
* @todo allow $filter to run off of getVars key/val pair
*/
public function xml(SS_HTTPRequest $request)
{
$filter = array();

//if a category filter selected
if ($this->CategoryID) {
$filter['CategoryID'] = $this->CategoryID;
}

$Locations = Locator::getLocations($filter);

return $this->customise(array(
'Locations' => $Locations
'Locations' => $Locations,
))->renderWith('LocationXML');

}


/**
* LocationSearch form.
*
* Search form for locations, updates map and results list via AJAX
*
* @access public
* @return Form
*/
public function LocationSearch()
Expand All @@ -183,8 +190,7 @@ public function LocationSearch()
);
$address->setAttribute('placeholder', 'address or zip code');

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

if (LocationCategory::get()->Count() > 0 && $this->data()->CategoryID == 0) {
$filter = array();
$locals = Locator::getLocations($filter, $exclude = array('CategoryID' => 0));
//debug::show($locals);
Expand All @@ -209,7 +215,5 @@ public function LocationSearch()
);

return Form::create($this, 'LocationSearch', $fields, $actions);

}

}

0 comments on commit b1a642e

Please sign in to comment.