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

Google Maps für Einsatzorte #114

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
36 changes: 36 additions & 0 deletions src/Frontend/ReportList.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ class ReportList
*/
private $splitMonths;

/**
* Gibt an, ob eine Google-Map mit den Einsätzen angezeigt wird
*
* @var bool
*/
private $showMap;

/**
* In diesem String wird der HTML-Code für die Liste aufgebaut
*
Expand Down Expand Up @@ -155,6 +162,7 @@ private function constructList($reports, $args)
// Argumente auswerten
$defaults = array(
'splitMonths' => false,
'showMap' => false,
'columns' => array(),
'linkToVehicles' => $this->options->getBoolOption('einsatzvw_list_fahrzeuge_link'),
'linkToAddForces' => $this->options->getBoolOption('einsatzvw_list_ext_link'),
Expand All @@ -163,11 +171,13 @@ private function constructList($reports, $args)
'showHeading' => true,
'compact' => false,
);

$parsedArgs = wp_parse_args($args, $defaults);

// Variablen setzen
$this->compact = (bool) $parsedArgs['compact'];
$this->splitMonths = (bool) $parsedArgs['splitMonths'] && !$this->compact;
$this->showMap = (bool) $parsedArgs['showMap'] && !$this->compact;
$this->columns = $this->utilities->sanitizeColumnsArray($parsedArgs['columns']);
$this->numberOfColumns = count($this->columns);
$this->linkToVehicles = (true === $parsedArgs['linkToVehicles']);
Expand All @@ -194,6 +204,21 @@ private function constructList($reports, $args)
$this->beginTable(false);
$this->insertTableHeader();
}

if( $this->options->isGMapActivate() != "" && $this->showMap) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eine Methode (isGMapActivate()), die dank toBoolean() immer einen Boolean zurückliefert, auf einen leeren String prüfen?

$latLon = explode(",", $this->options->getGMapDefaultPos() );
$mapstring = "<style>#map-canvas {height: 300px; margin-bottom: 30px; position: relative; overflow: hidden; transform: translateZ(0px); background-color: rgb(229, 227, 223);}</style>";
$mapstring .= "<div class='einsatzliste-map'>";
$mapstring .= "<div id='map-canvas'></div>";
$mapstring .= "</div>";
$mapstring .= "<div style='clear:both'></div>";
$mapstring .= "<script>";
$mapstring .= "google.maps.event.addDomListener(window, 'load', initializeMap(" . $latLon[0] . ", " . $latLon[1] . ", 10));";
$mapstring .= "</script>";

echo "$mapstring";
}

/** @var IncidentReport $report */
foreach ($reports as $report) {
$timeOfAlerting = $report->getTimeOfAlerting();
Expand Down Expand Up @@ -274,6 +299,7 @@ private function beginTable($year)
if ($this->showHeading && $year !== false) {
$this->string .= '<h2>Eins&auml;tze '.$year.'</h2>';
}

$this->string .= '<table class="' . self::TABLECLASS . '"><tbody>';
}

Expand Down Expand Up @@ -329,7 +355,17 @@ private function insertRow($report)
}
$this->string .= '</td>';
}
if( $this->options->isGMapActivate() && $this->showMap && $report->getGmapsLocation() != "") {
$latLon = explode(",", $report->getGmapsLocation());
$timeOfAlerting = $report->getTimeOfAlerting();
$infoContent = '<h1>' . get_the_title($report->getPostId()) . '</h1><p>' . $timeOfAlerting->format('d.m.Y H:i') . '</p><p>' . $report->getLocation() . '</p>';
$marker = '<script>';
$marker .= 'addMarker( ' . $latLon[0] . ', ' . $latLon[1] . ' , "' . $infoContent . '" )';
$marker .= '</script>';
$this->string .= $marker;
}
$this->string .= '</tr>';

}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/Model/IncidentReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public static function getMetaFields()
'einsatz_einsatzort' => array(
'label' => 'Einsatzort'
),
'einsatz_location' => array(
'label' => 'Goolemaps Position'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Google legt wert auf eine korrekte Schreibweise der Produkte: Google Maps anstatt Googlemaps, sowieso ist da ein Typo drin (das g fehlt bei Google).

),
'einsatz_einsatzleiter' => array(
'label' => 'Einsatzleiter'
),
Expand Down Expand Up @@ -194,6 +197,16 @@ public function getLocation()
return $this->getPostMeta('einsatz_einsatzort');
}

/**
* Gibt den eingetragenen Einsatzort als googlemaps koordinate zurück
*
* @return string
*/
public function getGmapsLocation()
{
return $this->getPostMeta('einsatz_location');
}

/**
* Gibt die Einsatznummer zurück
*
Expand Down
45 changes: 44 additions & 1 deletion src/einsatzverwaltung-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ public function enqueueEditScripts($hook)
array(),
Core::VERSION
);

if($this->options->isGMapActivate())
{
wp_enqueue_script( 'einsatzvw_GoogleMap' );
wp_enqueue_script(
'einsatzverwaltung-gmap',
$this->core->scriptUrl . 'einsatzverwaltung-gmaps.js'
);
}
}

/**
Expand Down Expand Up @@ -188,10 +197,11 @@ public function displayMetaBoxEinsatzdetails($post)
$einsatzort = $report->getLocation();
$einsatzleiter = $report->getIncidentCommander();
$mannschaftsstaerke = $report->getWorkforce();
$gmapslocation = $report->getGmapsLocation();

$names = Data::getEinsatzleiterNamen();
echo '<input type="hidden" id="einsatzleiter_used_values" value="' . implode(',', $names) . '" />';
echo '<table><tbody>';
echo '<table style="width: 100%;"><tbody>';

if ($this->options->isAutoIncidentNumbers()) {
echo '<tr><td>Einsatznummer</td><td>' . esc_html($nummer) . '</td></tr>';
Expand Down Expand Up @@ -227,6 +237,11 @@ public function displayMetaBoxEinsatzdetails($post)
esc_attr($einsatzort)
);

if($this->options->isGMapActivate())
{
$this->echoGMap($gmapslocation);
}

$this->echoInputText(
'Einsatzleiter',
'einsatzverwaltung_einsatzleiter',
Expand Down Expand Up @@ -261,6 +276,34 @@ private function echoInputText($label, $name, $value, $placeholder = '', $size =
echo '/></td></tr>';
}

/**
* Generiert eine Googlemaps Karte zur bestimmung der Koordinate
*/
private function echoGMap($location)
{
if($location != "")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wäre empty() an dieser Stelle nicht besser geeignet?

{
$latLon = explode(",",$location);
}
else
{
$latLon = explode(",",$this->options->getGMapDefaultPos());
}

echo '<tr><td>';
echo '<label for="einsatzverwaltung_location">Koordinate</label>';
echo '</td><td style="width: 100%;">';
echo '<input type="text" id="einsatzverwaltung_location" name="einsatzverwaltung_location" value="' . $location . '" size="20" readonly/>';
echo '<a class="button" id="einsatzverwaltung_get_location" onClick="geocodeAddress(document.getElementById(\'einsatzverwaltung_einsatzort\').value , \'einsatzverwaltung_location\')"><i class="fa fa-map-marker"></i></a>';
echo '</td></tr>';
echo '<tr><td colspan="2">';
echo '<div id="map-canvas" style="height: 400px;"></div>';
echo '</td></tr>';
echo '<script>';
echo ' google.maps.event.addDomListener(window, "load", initializeMap(' . $latLon[0] . ', ' . $latLon[1] . '));';
echo '</script>';
}

/**
* Gibt eine Checkbox für die Metabox aus
*
Expand Down
20 changes: 18 additions & 2 deletions src/einsatzverwaltung-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ class Core
* @var Data
*/
private $data;

/**
* @var Options
*/
private $options;

/**
* @var Utilities
*/
Expand Down Expand Up @@ -333,6 +333,7 @@ public function onDeactivation()
public function onInit()
{
$this->registerTypes();
$this->registerScripts();
$this->addRewriteRules();
if ($this->options->isFlushRewriteRules()) {
flush_rewrite_rules();
Expand Down Expand Up @@ -388,6 +389,21 @@ private function registerTypes()
));
}

/**
* Registriert externe Scripts
*/
private function registerScripts()
{
if( $this->options->isGMapActivate() ) {
/* Google Maps */
$protocal = is_ssl() ? 'https://' : 'http:https://';
$url = add_query_arg( array(
'key' => $this->options->getGMapAPI(),
), "{$protocal}maps.googleapis.com/maps/api/js");
wp_register_script( 'einsatzvw_GoogleMap', $url );
}
}

private function addRewriteRules()
{
global $wp_rewrite;
Expand Down
6 changes: 5 additions & 1 deletion src/einsatzverwaltung-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ public function savePostdata($postId, $post)
// Einsatzort validieren
$einsatzort = sanitize_text_field($_POST['einsatzverwaltung_einsatzort']);

// Einsatzposition validieren
$location = sanitize_text_field($_POST['einsatzverwaltung_location']);

// Einsatzleiter validieren
$einsatzleiter = sanitize_text_field($_POST['einsatzverwaltung_einsatzleiter']);

Expand All @@ -243,6 +246,7 @@ public function savePostdata($postId, $post)
// Metadaten schreiben
update_post_meta($postId, 'einsatz_einsatzende', $einsatzende);
update_post_meta($postId, 'einsatz_einsatzort', $einsatzort);
update_post_meta($postId, 'einsatz_location', $location);
update_post_meta($postId, 'einsatz_einsatzleiter', $einsatzleiter);
update_post_meta($postId, 'einsatz_mannschaft', $mannschaftsstaerke);
update_post_meta($postId, 'einsatz_fehlalarm', $fehlalarm);
Expand Down Expand Up @@ -313,7 +317,7 @@ public function onPublish($postId, $post)
$this->utilities->removePostFromCategory($postId, $category);
}
}

// Zwischenspeicher wird nur in der Entwurfsphase benötigt
delete_post_meta($postId, '_einsatz_timeofalerting');
}
Expand Down
46 changes: 43 additions & 3 deletions src/einsatzverwaltung-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Frontend
* @var Options
*/
private $options;

/**
* @var Utilities
*/
Expand Down Expand Up @@ -69,6 +69,15 @@ public function enqueueStyleAndScripts()
Core::VERSION
);
wp_add_inline_style('einsatzverwaltung-frontend', ReportList::getDynamicCss());

if($this->options->isGMapActivate())
{
wp_enqueue_script( 'einsatzvw_GoogleMap' );
wp_enqueue_script(
'einsatzverwaltung-gmap',
$this->core->scriptUrl . 'einsatzverwaltung-gmaps.js'
);
}
}

/**
Expand Down Expand Up @@ -151,6 +160,32 @@ public function getEinsatzberichtHeader($post, $mayContainLinks = true, $showArc
return "";
}

/**
* Erzeugt eine Google Map des Einsatzortes
*
* @return string Code zum Erzeugen der Google-Map mit Markierten einsatzort
*/
public function getEinsatzberichtMap($post)
{
$report = new IncidentReport($post);
$location = $report->getGmapsLocation();
if($this->options->isGMapActivate() && $location)
{
$latLon = explode(",",$location);

$mapstring = "<style>#map-canvas {height: 300px; position: relative; overflow: hidden; transform: translateZ(0px); background-color: rgb(229, 227, 223);}</style>";
$mapstring .= "<div class='einsatzliste-map'>";
$mapstring .= "<div id='map-canvas'></div>";
$mapstring .= "</div>";
$mapstring .= "<div style='clear:both'></div>";
$mapstring .= "<script>";
$mapstring .= "google.maps.event.addDomListener(window, 'load', initializeMap(" . $latLon[0] . ", " . $latLon[1] . "));";
$mapstring .= "addMarker( " . $latLon[0] . ", " . $latLon[1] . ", '" . $report->getLocation() . "', true )";
$mapstring .= "</script>";

return "<p>$mapstring</p>";
}
}

/**
* Erzeugt eine Zeile für die Einsatzdetails
Expand Down Expand Up @@ -192,8 +227,13 @@ public function renderContent($content)

$header = $this->getEinsatzberichtHeader($post, true, true);
$content = $this->prepareContent($content);
$map = "";
if($this->options->isGMapActivate())
{
$map = $this->getEinsatzberichtMap($post);
}

return $header . '<hr>' . $content;
return $header . $map . '<hr>' . $content;
}


Expand Down Expand Up @@ -319,7 +359,7 @@ public function addReportsToQuery($query)
} else {
$postTypes = array('post');
}

// Einsatzberichte nur zusammen mit Beiträgen abfragen
if (!in_array('post', $postTypes)) {
return;
Expand Down
30 changes: 30 additions & 0 deletions src/einsatzverwaltung-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class Options
'einsatzvw_category' => false,
'einsatzvw_loop_only_special' => false,
'einsatzverwaltung_incidentnumbers_auto' => false,
'einsatzvw_gmap' => false,
'einsatzvw_gmap_api' => '',
'einsatzvw_gmap_default_pos' => '53.523463,9.482329',
);

/**
Expand Down Expand Up @@ -152,6 +155,33 @@ public function getExcerptTypeFeed()
return $this->utilities->sanitizeExcerptType($option);
}

/**
* @return string
*/
public function getGMapAPI()
{
$option = $this->getOption('einsatzvw_gmap_api');
return $option;
}

/**
* @return string
*/
public function getGMapDefaultPos()
{
$option = $this->getOption('einsatzvw_gmap_default_pos');
return $option;
}

/**
* @return bool
*/
public function isGMapActivate()
{
$option = $this->getOption('einsatzvw_gmap');
return $this->toBoolean($option);
}

/**
* Gibt die Basis für die URL zu Einsatzberichten zurück
*
Expand Down
Loading