diff --git a/freifunkmeta.php b/freifunkmeta.php index ece7799..76a8bc2 100644 --- a/freifunkmeta.php +++ b/freifunkmeta.php @@ -1,9 +1,9 @@ directory = FF_Meta_Externaldata::get(FF_META_DEFAULT_DIR); } - if (false === ($directory = ff_meta_getmetadata ( FF_META_DEFAULT_DIR )) - || empty($directory[$city])) { - return "\n"; - } - $url = $directory[$city]; + function get_url_by_city($city) { + $val = $this->directory[$city]; - if (false === ($metadata = ff_meta_getmetadata ($url))) { - return "\n"; + if (empty($val)) { + return false; + } else { + return $val; + } + } +} + +/** + * OO interface to handle a single community/city + */ +class FF_Community +{ + public $name; + public $street; + public $zip; + public $city; + public $lon; + public $lat; + + /** + * Default constructor from metadata + */ + function __construct($metadata) { + $loc = $metadata['location']; + $this->name = (isset($loc['address']) && isset($loc['address']['Name'])) ? $loc['address']['Name'] : ''; + $this->street = (isset($loc['address']) && isset($loc['address']['Street'])) ? $loc['address']['Street'] : ''; + $this->zip = (isset($loc['address']) && isset($loc['address']['Zipcode'])) ? $loc['address']['Zipcode'] : ''; + $this->city = isset($loc['city']) ? $loc['city'] : ''; + $this->lon = isset($loc['lon']) ? $loc['lon'] : ''; + $this->lat = isset($loc['lat']) ? $loc['lat'] : ''; } - $outstr = "
"; - switch ($name) { - case 'ff_state': - $state = $metadata['state']; - $outstr .= sprintf('%s', $state['nodes']); - break; + /** + * Alternative constructor from city name + */ + static function make_from_city($city) { + // TODO: test + if (false === ($url = $this->dir->get_url_by_city($city))) { + return "\n"; + } + if (false === ($metadata = FF_Meta_Externaldata::get($url))) { + return "\n"; + } + return new FF_Community($metadata); + } - case 'ff_location': - // normal per-city code - $loc = $metadata['location']; - $loc_name = (isset($loc['address']) && isset($loc['address']['Name'])) ? $loc['address']['Name'] : ''; - $loc_street = (isset($loc['address']) && isset($loc['address']['Street'])) ? $loc['address']['Street'] : ''; - $loc_zip = (isset($loc['address']) && isset($loc['address']['Zipcode'])) ? $loc['address']['Zipcode'] : ''; - $loc_city = isset($loc['city']) ? $loc['city'] : ''; - $loc_lon = isset($loc['lon']) ? $loc['lon'] : ''; - $loc_lat = isset($loc['lat']) ? $loc['lat'] : ''; - if (empty($loc_name) || empty($loc_street) || empty($loc_zip)) { - return ''; - } + function format_address() { + if (empty($this->name) || empty($this->street) || empty($this->zip)) { + return ''; + } + // TODO: style address + map as single box + // TODO: once it is "ready" package openlayers.js into the plugin (cf. http://docs.openlayers.org/library/deploying.html) + // TODO: handle missing values (i.e. only name & city) + return '

' . sprintf('%s
%s
%s %s', $this->name, $this->street, $this->zip, $this->city) . '

'; + } +} - // TODO: style address + map as single box - // TODO: once it is "ready" package openlayers.js into the plugin (cf. http://docs.openlayers.org/library/deploying.html) - // TODO: handle missing values (i.e. only name & city) - $outstr .= '

'; - $outstr .= sprintf('%s
%s
%s %s', - $loc_name, $loc_street, $loc_zip, $loc_city); - $outstr .= '

'; +/** + * main class for whole plugin + */ +class FF_Meta +{ + private $dir; - // gather all location data - if ( false === ( $json_locs = get_transient( "ff_metadata_json_locs" ) ) ) { - $all_locs = array(); - $arr_select = array('lat' => 1, 'lon' => 1); - foreach ($directory as $tmp_city => $url) { - try { - $tmp_meta = ff_meta_getmetadata($url); - if (!empty($tmp_meta['location'])) { - $tmp_loc = array_intersect_key($tmp_meta['location'], $arr_select); - $all_locs[$tmp_city] = $tmp_loc; - } - } catch (Exception $e) { - // pass + function __construct() { + $this->dir = new FF_Directory(); + } + + function register_stuff() { + if ( ! shortcode_exists('ff_state') ) { + add_shortcode('ff_state', array($this, 'shortcode_handler')); + } + if ( ! shortcode_exists('ff_services') ) { + add_shortcode('ff_services', array($this, 'shortcode_handler')); + } + if ( ! shortcode_exists('ff_contact') ) { + add_shortcode('ff_contact', array($this, 'shortcode_handler')); + } + if ( ! shortcode_exists('ff_location') ) { + add_shortcode('ff_location', array($this, 'shortcode_handler')); + } + + add_action('admin_menu', array($this, 'admin_menu')); + add_action('admin_init', array($this, 'admin_init')); + register_uninstall_hook( __FILE__, array('ff_meta', 'uninstall_hook')); + } + + function output_ff_state($citydata) { + $state = $citydata['state']; + return sprintf('%s', $state['nodes']); + } + + function aux_get_all_locations() { + // gather all location data + if (false === ( $json_locs = get_transient("FF_metadata_json_locs") )) { + $all_locs = array(); + $arr_select = array('lat' => 1, 'lon' => 1); + foreach ($this->dir as $tmp_city => $url) { + try { + $tmp_meta = FF_Meta_Externaldata::get($url); + if (!empty($tmp_meta['location'])) { + $tmp_loc = array_intersect_key($tmp_meta['location'], $arr_select); + $all_locs[$tmp_city] = $tmp_loc; } + } catch (Exception $e) { + // pass } - $json_locs = json_encode($all_locs); - $cachetime = get_option( 'ff_meta_cachetime', FF_META_DEFAULT_CACHETIME) * MINUTE_IN_SECONDS; - set_transient( "ff_metadata_json_locs", $json_locs, $cachetime ); } + $json_locs = json_encode($all_locs); + $cachetime = get_option('FF_meta_cachetime', FF_META_DEFAULT_CACHETIME) * MINUTE_IN_SECONDS; + set_transient("FF_metadata_json_locs", $json_locs, $cachetime); + } + return $json_locs; + } - if ( !empty($loc_name) && !empty($loc_name) ) { - $icon_url = plugin_dir_url(__FILE__) . "freifunk_marker.png"; - $outstr .= <<format_address(); + $json_locs = $this->aux_get_all_locations(); + + if (!empty($loc_name) && !empty($loc_name)) { + $icon_url = plugin_dir_url(__FILE__) . "freifunk_marker.png"; + $outstr .= <<
- +