Skip to content
This repository has been archived by the owner on Jul 21, 2021. It is now read-only.

Commit

Permalink
Converted to PSR-2
Browse files Browse the repository at this point in the history
  • Loading branch information
helpfulrobot committed Dec 31, 2015
1 parent 871ec68 commit 61108c6
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 163 deletions.
211 changes: 112 additions & 99 deletions code/GoogleDirections.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,62 @@
* @version 1.0
* @author Martine Bloem (martimiz at gmail dot com)
*/
class GoogleDirections extends DataExtension {

private static $has_many = array(
'GoogleMaps' => 'GoogleDirectionsMap'
);

private static $use_browser_language = false;

private static $directions_enabled = true;

public function updateCMSFields(FieldList $fields) {
$tab = _t('GoogleDirections.GOOGLEDIRECTIONSTAB', 'GoogleDirections');

$fields->addFieldsToTab(
"Root.$tab",
array(
$gridField = new GridField(
'GoogleMaps',
'GoogleMaps',
$this->owner->GoogleMaps(),
GridFieldConfig_RecordEditor::create()
)
));
}

public function contentcontrollerInit() {
class GoogleDirections extends DataExtension
{

private static $has_many = array(
'GoogleMaps' => 'GoogleDirectionsMap'
);

private static $use_browser_language = false;

private static $directions_enabled = true;

public function updateCMSFields(FieldList $fields)
{
$tab = _t('GoogleDirections.GOOGLEDIRECTIONSTAB', 'GoogleDirections');

$fields->addFieldsToTab(
"Root.$tab",
array(
$gridField = new GridField(
'GoogleMaps',
'GoogleMaps',
$this->owner->GoogleMaps(),
GridFieldConfig_RecordEditor::create()
)
));
}

public function contentcontrollerInit()
{
$useBrowserLanguage = Config::inst()->get('GoogleDirections', 'use_browser_language');

if ($useBrowserLanguage) {
Requirements::javascript("http://maps.google.com/maps/api/js?sensor=false");
} else {
$locale = i18n::get_locale();
$language = i18n::get_lang_from_locale($locale);
Requirements::javascript("http://maps.google.com/maps/api/js?sensor=false&language=$language");
}

Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.min.js');
Requirements::add_i18n_javascript(GOOGLEDIRECTIONS_BASE . '/javascript/lang');
Requirements::javascript(GOOGLEDIRECTIONS_BASE . '/javascript/googledirections.js');

$useBrowserLanguage = Config::inst()->get('GoogleDirections', 'use_browser_language');

if ($useBrowserLanguage) {
Requirements::javascript("http://maps.google.com/maps/api/js?sensor=false");
}
else {
$locale = i18n::get_locale();
$language = i18n::get_lang_from_locale($locale);
Requirements::javascript("http://maps.google.com/maps/api/js?sensor=false&language=$language");
}

Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.min.js');
Requirements::add_i18n_javascript(GOOGLEDIRECTIONS_BASE . '/javascript/lang');
Requirements::javascript(GOOGLEDIRECTIONS_BASE . '/javascript/googledirections.js');
Requirements::css(GOOGLEDIRECTIONS_BASE . '/css/googledirections.css');

// Build a defaultmap if there is one defined
$startupMap = $this->owner->GoogleMaps()->filter(array('ShowOnStartup' => true))->first();

if ($startupMap) {
$address = $startupMap->Address;
$latlng = $startupMap->LatLng;
$infoText = $startupMap->InfoText;
$infoText = str_replace("", "''", $infoText);

Requirements::css(GOOGLEDIRECTIONS_BASE . '/css/googledirections.css');

// Build a defaultmap if there is one defined
$startupMap = $this->owner->GoogleMaps()->filter(array('ShowOnStartup' => true))->first();

if ($startupMap) {
$address = $startupMap->Address;
$latlng = $startupMap->LatLng;
$infoText = $startupMap->InfoText;
$infoText = str_replace("", "''", $infoText);

if ($address || $latlng) {
Requirements::customScript(<<<JS
if ($address || $latlng) {
Requirements::customScript(<<<JS
(function($) {
$(document).ready(function() {
locations.defaultMap = {
Expand All @@ -72,50 +73,62 @@ public function contentcontrollerInit() {
});
}(jQuery));
JS
);
}
}
}

/**
* Global setting: Is the display of directions enabled for this website?
* Default: true. If false only the map is displayed
*
* @return Boolean
*/
public function DirectionsEnabled() {
return Config::inst()->get('GoogleDirections', 'directions_enabled');
}

/**
* Allow shortcodes like this:
* [GMap, link='My Link']
* [GMap, link="My Link"]This is a link[/GMap]
*
* Note: for this you need to register the handler in _config.php:
*
* ShortcodeParser::get('default')->register('GMap', array('GoogleDirections', 'link_shortcode_handler'));
*
* @return string - the link that will display the map
*/
public static function link_shortcode_handler($arguments, $content='') {
if (!isset($arguments['link'])) return '';

if (!($link = GoogleDirectionsMap::get()->filter(array('LinkID' => $arguments['link']))->first())) return '';

$latLng = $link->LatLng;
$address = $link->Address;
if (!$latLng && !$address) return '';

$location = ($latLng)? "data-latlng=\"$latLng\"" : "data-address=\"$address\"";
if($link->InfoText) $location .= 'data-infotext="' . Convert::raw2xml($link->InfoText) . '"';
if (!$content) $content = Convert::raw2xml($arguments['link']);

return "<a class=\"googleDirections\" $location href=\"#route\">$content</a>";
}

public function getGoogleDirections() {
return $this->owner->renderWith('GoogleDirections');
}
);
}
}
}

/**
* Global setting: Is the display of directions enabled for this website?
* Default: true. If false only the map is displayed
*
* @return Boolean
*/
public function DirectionsEnabled()
{
return Config::inst()->get('GoogleDirections', 'directions_enabled');
}

/**
* Allow shortcodes like this:
* [GMap, link='My Link']
* [GMap, link="My Link"]This is a link[/GMap]
*
* Note: for this you need to register the handler in _config.php:
*
* ShortcodeParser::get('default')->register('GMap', array('GoogleDirections', 'link_shortcode_handler'));
*
* @return string - the link that will display the map
*/
public static function link_shortcode_handler($arguments, $content='')
{
if (!isset($arguments['link'])) {
return '';
}

if (!($link = GoogleDirectionsMap::get()->filter(array('LinkID' => $arguments['link']))->first())) {
return '';
}

$latLng = $link->LatLng;
$address = $link->Address;
if (!$latLng && !$address) {
return '';
}

$location = ($latLng)? "data-latlng=\"$latLng\"" : "data-address=\"$address\"";
if ($link->InfoText) {
$location .= 'data-infotext="' . Convert::raw2xml($link->InfoText) . '"';
}
if (!$content) {
$content = Convert::raw2xml($arguments['link']);
}

return "<a class=\"googleDirections\" $location href=\"#route\">$content</a>";
}

public function getGoogleDirections()
{
return $this->owner->renderWith('GoogleDirections');
}
}

130 changes: 66 additions & 64 deletions code/GoogleDirectionsMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,70 @@
*
* @author Martine Bloem (martimiz at gmail dot com)
*/
class GoogleDirectionsMap extends DataObject {

private static $db = array(
'LinkID' => 'Varchar',
'InfoText' => 'HTMLText',
'LatLng' => 'Varchar(50)',
'Address' => 'Varchar(255)',
'ShowOnStartup' => 'Boolean'
);

private static $has_one = array(
'Page' => 'SiteTree'
);

private static $summary_fields = array(
'LatLng',
'Address',
'ShowOnStartup',
'LinkID'
);

private static $searchable_fields = array();

public function getCMSFields() {

$fields = FieldList::create();

$fields->push(
HeaderField::create('Note', _t('GoogleDirectionsMap.HEADER', 'Create a Map...'), 2)
);
$fields->push(
LiteralField::create('Note', _t('GoogleDirectionsMap.NOTE', '<strong>Note:</strong> lattitude and longitude take presedence over address'))
);
$fields->push(
TextField::create('LinkID', _t('GoogleDirectionsMap.LINKID', 'Title'))
);
$fields->push(
TextField::create('LatLng', _t('GoogleDirectionsMap.LATLNG', 'LatLng'))
);
$fields->push(
TextField::create('Address', _t('GoogleDirectionsMap.ADDRESS', 'Address'))
);
$fields->push(
CheckboxField::create('ShowOnStartup', _t('GoogleDirectionsMap.SHOWONSTARTUP', 'Display on startup'))
);
$fields->push(
HtmlEditorField::create('InfoText', _t('GoogleDirections.INFOTEXT', 'Info text'))->setRows(16)
);
$fields->push(
HiddenField::create('PageID', 'PageID')
);
return $fields;
}

public function fieldLabels($includerelations = true){
$labels = parent::fieldLabels($includerelations);
$labels['LinkID'] = _t('GoogleDirectionsMap.LINKID', 'Title');
$labels['InfoText'] = _t('GoogleDirectionsMap.INFOTEXT', 'Info text');
$labels['LatLng'] = _t('GoogleDirectionsMap.LATLNG', 'LatLng');
$labels['Address'] = _t('GoogleDirectionsMap.ADDRESS', 'Address');
$labels['ShowOnStartup'] = _t('GoogleDirectionsMap.SHOWONSTARTUP', 'Display on startup');
$labels['Page'] = _t('GoogleDirectionsMap.PAGE', 'Page');
return $labels;
}
class GoogleDirectionsMap extends DataObject
{

private static $db = array(
'LinkID' => 'Varchar',
'InfoText' => 'HTMLText',
'LatLng' => 'Varchar(50)',
'Address' => 'Varchar(255)',
'ShowOnStartup' => 'Boolean'
);

private static $has_one = array(
'Page' => 'SiteTree'
);

private static $summary_fields = array(
'LatLng',
'Address',
'ShowOnStartup',
'LinkID'
);

private static $searchable_fields = array();

public function getCMSFields()
{
$fields = FieldList::create();

$fields->push(
HeaderField::create('Note', _t('GoogleDirectionsMap.HEADER', 'Create a Map...'), 2)
);
$fields->push(
LiteralField::create('Note', _t('GoogleDirectionsMap.NOTE', '<strong>Note:</strong> lattitude and longitude take presedence over address'))
);
$fields->push(
TextField::create('LinkID', _t('GoogleDirectionsMap.LINKID', 'Title'))
);
$fields->push(
TextField::create('LatLng', _t('GoogleDirectionsMap.LATLNG', 'LatLng'))
);
$fields->push(
TextField::create('Address', _t('GoogleDirectionsMap.ADDRESS', 'Address'))
);
$fields->push(
CheckboxField::create('ShowOnStartup', _t('GoogleDirectionsMap.SHOWONSTARTUP', 'Display on startup'))
);
$fields->push(
HtmlEditorField::create('InfoText', _t('GoogleDirections.INFOTEXT', 'Info text'))->setRows(16)
);
$fields->push(
HiddenField::create('PageID', 'PageID')
);
return $fields;
}

public function fieldLabels($includerelations = true)
{
$labels = parent::fieldLabels($includerelations);
$labels['LinkID'] = _t('GoogleDirectionsMap.LINKID', 'Title');
$labels['InfoText'] = _t('GoogleDirectionsMap.INFOTEXT', 'Info text');
$labels['LatLng'] = _t('GoogleDirectionsMap.LATLNG', 'LatLng');
$labels['Address'] = _t('GoogleDirectionsMap.ADDRESS', 'Address');
$labels['ShowOnStartup'] = _t('GoogleDirectionsMap.SHOWONSTARTUP', 'Display on startup');
$labels['Page'] = _t('GoogleDirectionsMap.PAGE', 'Page');
return $labels;
}
}

0 comments on commit 61108c6

Please sign in to comment.