Skip to content

Instantly share code, notes, and snippets.

@stas
Created August 20, 2009 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stas/171157 to your computer and use it in GitHub Desktop.
Save stas/171157 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Lug Map
Plugin URI: http://softwareliber.ro/harta/
Description: Create a map with lug locations, powered by Google Maps.
Version: 1.5
Author: Stas Sushkov
Author URI: http://stas.nerd.ro/
*/
?>
<?php
/* Copyright 2008 Stas Sushkov (email : stas@nerd.ro)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
?>
<?php
/**
* Admin cpanel
*/
function lugmap_admin() {
add_management_page(__("Lug Map","lug-map"), __("Lug Map","lug-map"), 3, "lug-map", "lugmap_menu");
}
/**
* Admin actions
*/
function lugmap_menu() {
global $wpdb;
$table = $wpdb->prefix."lugmap";
if ( isset( $_POST['lugmap_api_key'] ) && isset( $_POST['lugmap_coord'] ) ) {
if($_POST['lugmap_api_key'])
update_option('lugmap_api_key', attribute_escape($_POST['lugmap_api_key']));
if($_POST['lugmap_coord'])
update_option('lugmap_coord', attribute_escape($_POST['lugmap_coord']));
?><div id="message" class="updated fade"><p><strong><?php echo __("Options saved.",'lug-map') ?></strong></p></div><?php
}
if( isset($_GET['delete_lugmap_marker'])) {
$lugmap_marker_id = attribute_escape($_GET['delete_lugmap_marker']);
$wpdb->query("DELETE FROM $table WHERE id = $lugmap_marker_id");
?><div id="message" class="updated fade"><p><strong><?php printf(__('Marker %1$s deleted.','lug-map'),"$lugmap_marker_id"); ?></strong></p></div><?php
}
if($_POST['lugmap_clean'] == 'true')
lugmap_clean();
?>
<div id="icon-tools" class="icon32"><br /></div>
<div class="wrap">
<h2><?php echo __('Lug Map Options','lug-map')?></h2>
<div id="poststuff" class="metabox-holder">
<div class="postbox">
<h3 class="hndle" ><?php echo __('How to use it?','lug-map')?></h3>
<div class="inside">
<p><?php echo __('Insert inside the content of a page or a post the shortcode: <code>[LUGMAP]</code>','lug-map')?></p>
</div>
</div>
<div class="postbox">
<h3 class="hndle" ><?php echo __('Google Maps API Key','lug-map')?>, <?php echo __('Lug Map Start-Up Location','lug-map')?></h3>
<div class="inside">
<form action="" method="post" >
<p>
<small><?php printf(__('Can be obtained <a href="%1$s">here</a>','lug-map'), 'http://code.google.com/apis/maps/signup.html')?>.</small>
</p>
<p>
<input size="86" maxlength="86" style="font-family: 'Courier New',Courier,mono; font-size: 1.5em;" type="text" name="lugmap_api_key" value="<?php form_option('lugmap_api_key') ?>" />
</p>
<p>
<small><?php printf(__('Please add the coordinates for the location wich should be loaded on showing up the map. You can use <a href="%1$s">this tool</a>','lug-map'),'http://itouchmap.com/latlong.html')?>.</small><br />
</p>
<p>
<input size="20" style="font-family: 'Courier New',Courier,mono; font-size: 1.5em;" type="text" name="lugmap_coord" value="<?php form_option('lugmap_coord') ?>" />
</p>
<p>
<input type="submit" class="button" value="<?php echo __('Save Changes','lug-map')?>" />
</p>
</form>
</div>
</div>
<div class="postbox">
<h3 class="hndle" ><?php echo __('Here you can delete entries','lug-map')?></h3>
<div class="inside">
<ol>
<?php
$markers = $wpdb->get_results("SELECT * FROM $table");
$wpdb->show_errors();
foreach($markers as $marker) {
printf(__('<li><a href="%1$s" title="Delete this marker" style="font-weight: bolder;" >X</a> | Geolocation: %2$s, Name: <strong>%3$s</strong>, URL: <a href="%4$s">%4$s</a>, Contact: <a href="mailto:%5$s">%6$s</a><br />%7$s</li>','lug-map'), "?page=lug-map&delete_lugmap_marker=$marker->id", "$marker->point", "$marker->name", "$marker->web", "$marker->email", "$marker->cord", "$marker->dsc");
echo "\n";
}
?>
</ol>
</div>
</div>
<div class="postbox">
<h3 class="hndle" ><?php echo __('Remove Lug Map Content','lug-map')?></h3>
<div class="inside">
<p><?php echo __('Warnning! This will drop lugmap table and recreate it, so the present markers will be removed totally','lug-map')?>.</p>
<form method="post" action="">
<input type="hidden" name="lugmap_clean" value="true" />
<input type="submit" class="button" value="<?php echo __('Clean Database','lug-map')?>" />
</form>
</div>
</div>
</div>
</div>
<?php
}
/**
* Insert posted data
*/
if ( !empty( $_POST['lm_name'] ) && !empty( $_POST['lm_email'] ) && !empty( $_POST['lm_point'] ) && !empty( $_POST['lm_org'] ) ) {
global $wpdb;
$table = $wpdb->prefix."lugmap";
$lm_name = $wpdb->escape($_POST['lm_name']);
$lm_org = $wpdb->escape($_POST['lm_org']);
$lm_email = $wpdb->escape($_POST['lm_email']);
$lm_web = $wpdb->escape(apply_filters("the_title",$_POST['lm_web']));
$lm_dsc = $wpdb->escape(apply_filters("comment_status_pre ",$_POST['lm_dsc']));
$lm_point = $wpdb->escape(strtok($_POST['lm_point'],"()"));
$wpdb->query("
INSERT INTO $table(name, cord, email, web, dsc, point)
VALUES('$lm_name', '$lm_org', '$lm_email', '$lm_web', '$lm_dsc', '$lm_point')
");
}
/**
* Gmaps markers generation
*/
function lugmap_markers() {
global $wpdb;
$table = $wpdb->prefix."lugmap";
$markers = $wpdb->get_results("SELECT point FROM $table");
foreach($markers as $marker) {
echo "\t"."var point = new GLatLng(".stripslashes($marker->point).");"."\n";
$point_data = null; //otherwise it will collect the whole world :)
$marker_data = $wpdb->get_results("SELECT `name`,`cord`,`email`,`web`,`dsc` FROM `$table` WHERE `point` = '$marker->point'");
foreach($marker_data as $item) {
$point_data .= "<div class=\"bubble\"><strong><a href=\"".js_escape($item->web)."\">".js_escape($item->name)."</a></strong><br /><em>".js_escape($item->cord)."</em><br />".js_escape($item->dsc)."</div>";
}
echo "\t"."var marker = createMarker(point,'$point_data');"."\n";
echo "\t"."map.addOverlay(marker);"."\n\n";
}
}
/**
* Loading meta stuff like js files and etc.
*/
function lugmap_header() {
$lugmap_api_key = get_option('lugmap_api_key');
$lugmap_coord = get_option('lugmap_coord');
?>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=<?php echo $lugmap_api_key; ?>" type="text/javascript"></script>
<script type="text/javascript">
<!--
var map = null;
var geocoder = null;
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.setCenter(new GLatLng(<?php echo $lugmap_coord ?>), 6);
geocoder = new GClientGeocoder();
map.setMapType(G_HYBRID_MAP);
<?php lugmap_markers(); ?>
}
}
var gmarkers = [];
function createMarker(point,html) {
var marker = new GMarker(point);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(html);
});
gmarkers.push(marker);
return marker;
}
function srch() {
var adr = document.getElementById('adr').value; showAddress(adr); return false;
}
function chck(form) {
if(form.lm_point.value == "")
{
alert('Please add your geographical position and click "search"!');
return false;
}
else if (form.lm_name.value == "")
{
alert("Please add a name!");
return false;
}
else if (form.lm_email.value == "")
{
alert("Please add your email!");
return false;
}
return true;
}
function addPoint(point) {
if(point)
document.getElementById('point').value = point;
}
function lm_click(i) {
GEvent.trigger(gmarkers[i], "click");
}
function showAddress(address) {
if (geocoder) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
addPoint(point);
map.setCenter(point, 13);
var marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(address);
}
}
);
}
}
//-->
</script>
<style type="text/css">
#lug-map form p {margin: 15px 0; text-align: left;}
#lug-map form p label {font-weight: normal; padding: 4px 0;}
#lug-map form p .inpt {clear: both; display: block; width: 500px; padding: 2px;}
#lug-map form p textarea {width: 500px; padding: 4px;}
#point {border: none; padding: 0; width: 0; height: 0;}
#lug-map form p .lb_adr {display: block;}
#lug-map form p #adr {width: 300px;}
#sbmt { font-size: 12px; padding: 4px;}
#srch {padding-left: 10px;}
#map {height: 500px; border:4px solid #423E46;}
#lug-map {width: auto; margin: 10px auto;}
#lug-map small {float: right}
#lug-map .bubble { font-size: 10px;}
.entry-content img {margin: 0 0 16px 0; max-width: none;} /* hack */
.bubble { width: 300px; }
</style>
<?php
}
/**
* Page hook
*/
function lugmap_content($attr, $c = NULL) {
$output = '<div id="lug-map">';
$output .= '<form action="" method="post" onSubmit="javascript: return chck(this)">';
$output .= '
<div id="map"></div>
<script type="text/javascript">
initialize();
</script>
';
$output .= '
<p>
<label for="lm_name">'.__("LUG Name","lug-map").' *:</label>
<input class="inpt" type="text" name="lm_name" />
<label for="lm_org">'.__("LUG Contact person","lug-map").' *:</label>
<input class="inpt" type="text" name="lm_org" />
<lable>'.__("LUG Description","lug-map").' :</label>
<textarea name="lm_dsc" class="inpt" tabindex="4"></textarea>
<label for="lm_email">'.__("LUG E-mail","lug-map").' *:</label>
<input class="inpt" type="text" name="lm_email" />
<label for="lm_web">'.__("LUG Web Page","lug-map").':</label>
<input class="inpt" type="text" name="lm_web" value="http://" />
<label for="lm_adr" class="lb_adr">'.__("Geographical Position","lug-map").' *:</label>
<input id="adr" type="text" name="lm_adr" value="'.__("Town, County, Country","lug-map").'" />
<a href="#" id="srch" onClick="javascript: srch();" >'.__("Search","lug-map").'</a>
</p>
<small>'.__("Required fields are marked with <em>*</em>.","lug-map").'</small>
<input type="hidden" id="point" name="lm_point" />
<input type="submit" id="sbmt" value="'.__("Add It","lug-map").'" />
</form>';
$output .= '</div>';
return $output;
}
/**
* Clean lugmap table
*/
function lugmap_clean() {
global $wpdb;
$table = $wpdb->prefix."lugmap";
$wpdb->query("DROP TABLE $table");
lugmap_install();
}
/**
* Generate widget content
*/
function lugmap_widget_content() {
global $wpdb;
$table = $wpdb->prefix."lugmap";
$markers = $wpdb->get_results("SELECT `name`,`web` FROM `$table` ORDER BY `name`");
$content = '<ul class="lm_widget_markers">';
$i = 0;
foreach($markers as $item) {
$content .= "
<li class=\"lm_widget_marker\">
<span clas=\"lm_widget_marker_name\">
<a href=\"javascript:lm_click($i)\">".$item->name."</a></span>
<a href=\"".$item->web."\" class=\"lm_widget_marker_web\">".$item->web."</a>
</li>";
$i++;
}
$content .= '</ul>';
return $content;
}
/**
* Generate widget
*/
function lugmap_widget() {
register_widget( 'LugMap_Widget' );
}
class LugMap_Widget extends WP_Widget {
function LugMap_Widget() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'lm_widget', 'description' => 'Widget displays LugMap entries.' );
/* Widget control settings. */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'lm_widget' );
/* Create the widget. */
$this->WP_Widget( 'lm_widget', 'LugMap Widget', $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', $instance['title'] );
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
echo lugmap_widget_content();
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
function form( $instance ) {
/* Set up some default widget settings. */
$defaults = array( 'title' => 'LugMap' );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
</p>
<?php
}
}
/**
* Creating database tables and the rest of needed stuff
*/
function lugmap_install() {
global $wpdb;
$table = $wpdb->prefix."lugmap";
$structure = "CREATE TABLE $table (
id INT(9) NOT NULL AUTO_INCREMENT,
name VARCHAR(80) NOT NULL,
cord VARCHAR(40) NOT NULL,
email VARCHAR(40) NOT NULL,
web VARCHAR(1000) NOT NULL,
dsc VARCHAR(2000) NOT NULL,
point VARCHAR(20) NOT NULL,
UNIQUE KEY id (id)
);";
$wpdb->query('CHARACTER SET utf8 COLLATE utf8_general_ci');
$wpdb->query($structure);
// Populate table
$wpdb->query("INSERT INTO $table(name, cord, email, web, dsc, point)
VALUES('Grupul pentru software liber', 'Adi Roiban', 'adiroiban@gmail.com', 'http://softwareliber.ro/', 'Grupul pentru software liber s-a conturat la începutul anului 2005 in jurul unor studenți din cadrul Universității Tehnice din Cluj-Napoca. Ținta grupului a fost promovarea software-lui liber în rândurile studenților și profesorilor din facultate și continuarea activitații începute de Igor Știrbu la sfarșitul anului 2004.', '46.776306, 23.60429')");
add_option('lugmap_coord','45.943161,24.96676');
add_option('lugmap_api_key','Please add your Google Maps key');
}
//l10n stuff
$lugmap_domain = 'lug-map';
$lugmap_domain_path = PLUGINDIR.'/'.dirname(plugin_basename(__FILE__));
load_plugin_textdomain($lugmap_domain,$lugmap_domain_path.'/l10n' );
register_activation_hook(__FILE__,'lugmap_install');
add_action('admin_menu', 'lugmap_admin');
add_action('wp_head', 'lugmap_header');
add_action( 'widgets_init', 'lugmap_widget' );
add_shortcode('LUGMAP', 'lugmap_content');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment