diff --git a/public/class-water-the-theme-public.php b/public/class-water-the-theme-public.php index b7807c6..f08afca 100644 --- a/public/class-water-the-theme-public.php +++ b/public/class-water-the-theme-public.php @@ -20,7 +20,8 @@ * @subpackage Water_The_Theme/public * @author Jonathan Rosenbaum */ -class Water_The_Theme_Public { +class Water_The_Theme_Public +{ /** * The ID of this plugin. @@ -47,11 +48,11 @@ class Water_The_Theme_Public { * @param string $plugin_name The name of the plugin. * @param string $version The version of this plugin. */ - public function __construct( $plugin_name, $version ) { + public function __construct($plugin_name, $version) + { $this->plugin_name = $plugin_name; $this->version = $version; - } /** @@ -59,7 +60,8 @@ class Water_The_Theme_Public { * * @since 1.0.0 */ - public function enqueue_styles() { + public function enqueue_styles() + { /** * This function is provided for demonstration purposes only. @@ -73,8 +75,7 @@ class Water_The_Theme_Public { * class. */ - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/water-the-theme-public.css', array(), $this->version, 'all' ); - + wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/water-the-theme-public.css', array(), $this->version, 'all'); } /** @@ -82,7 +83,8 @@ class Water_The_Theme_Public { * * @since 1.0.0 */ - public function enqueue_scripts() { + public function enqueue_scripts() + { /** * This function is provided for demonstration purposes only. @@ -96,27 +98,27 @@ class Water_The_Theme_Public { * class. */ - wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/water-the-theme-public.js', array( 'jquery' ), $this->version, false ); - + wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/water-the-theme-public.js', array('jquery'), $this->version, false); } - - public function water_the_theme($thePage) { + + public function water_the_theme($thePage) + { $doc = new DOMDocument(); $doc->loadHTML($thePage); $xml_tree = simplexml_import_dom($doc); - - $short_code_name = (string)$xml_tree->body->div->attributes()->class; - $thePage =""; - + $short_code_name = (string)$xml_tree->body->div->attributes()->class; + + $thePage = "
"; + // front page // [usgs_custom name="Timely Information" location='03071590,03071600,03071605' parameters='00010,00065,62614'] if ($short_code_name === 'timely-information') { foreach ($xml_tree->body->div->div as $div) { - + $value = (string)$div; - $class = (string)$div->attributes()->class; + $class = (string)$div->attributes()->class; $datetime = (string)$div->attributes()->datetime; @@ -130,57 +132,57 @@ class Water_The_Theme_Public { if ($sitename === 'Cheat Lake Near Stewartstown Wv') { if ($parameter === '(AMSL)') { $sitename = 'Lake Level'; - } else { + } else { $sitename = 'Lake'; - } + } } if ($sitename === 'Cheat River At Lake Lynn Pa') { if ($parameter === 'Gage Height') { $sitename = 'Below Dam'; } else { $sitename = 'At Dam'; - } + } } if ($sitename === 'Cheat River At Davidson Pa') { - $sitename = 'Below Dam'; + $sitename = 'Below Dam'; } - if ( $sitename !== 'Lake') { - $thePage .= ''; - $thePage .= ""; - $thePage .= ''; + if ($sitename !== 'Lake') { + $thePage .= ''; + $thePage .= ""; + $thePage .= ''; } - } - + } + //[nws_custom location='lldp1'] $att = [ - 'name' => 'Timely Information', - 'location' => 'lldp1', - 'parameters' => null, - 'date_range' => 'current', - 'order' => 'asc' - ]; - - $nws_custom = Water_The_Theme_Public::nws_custom($att); - - $thePage .= $nws_custom; - + 'name' => 'Timely Information', + 'location' => 'lldp1', + 'parameters' => null, + 'date_range' => 'current', + 'order' => 'asc' + ]; + + $nws_custom = Water_The_Theme_Public::nws_custom($att); + + $thePage .= $nws_custom; } // end Timely Information - + // Statics Page - last 5 lake data // [usgs_custom name="Statistics" location='03071590,03071600' parameters='00010,00065,62614' date_range='previous,5,16:00' order='desc'] if ($short_code_name === 'Statistics') { - $statistics = []; - + $statistics = []; + foreach ($xml_tree->body->div->div as $div) { - + $value = (string)$div; - $class = (string)$div->attributes()->class; + $class = (string)$div->attributes()->class; $datetime = (string)$div->attributes()->datetime; list($sitename, $parameter) = explode(' ', $class); + $sitename = ucwords(preg_replace('/_/', ' ', $sitename)); $parameter = ucwords(preg_replace('/_/', ' ', $parameter)); @@ -190,117 +192,134 @@ class Water_The_Theme_Public { if ($sitename === 'Cheat Lake Near Stewartstown Wv') { if ($parameter === '(AMSL)') { $sitename = 'Lake Level'; - } else { + } else { $sitename = 'Lake'; - } + } } if ($sitename === 'Cheat River At Lake Lynn Pa') { $sitename = 'Tail Race Level'; - $parameter = ' (AMSL)'; + $parameter = ' (AMSL)'; } if ($sitename === 'Cheat River At Davidson Pa') { - $sitename = 'Below Dam'; + $sitename = 'Below Dam'; } - + if ($sitename === 'Tail Race Level') { - $value = (float)$value + 776.63; + $value = (float)$value + 776.63; + } + + if ($value !== 'n/a') { + $statistics[] = ""; } - $statistics[] = ""; - + //PC::debug($value . " " . $sitename . " " . $parameter); } // end foreach - - list($array1, $array2, $array3) = array_chunk($statistics, ceil(count($statistics) / 3)); - foreach($array1 as $key => $value ) { - $thePage .= $array2[$key]; - $thePage .= $array3[$key]; + + + usort($statistics, function ($a, $b) { + preg_match('/datetime=\'(\d+)\'/', $a, $matchesA); + preg_match('/datetime=\'(\d+)\'/', $b, $matchesB); + + $dateTimeA = isset($matchesA[1]) ? (int)$matchesA[1] : 0; + $dateTimeB = isset($matchesB[1]) ? (int)$matchesB[1] : 0; + + return $dateTimeB - $dateTimeA; + }); + + // highlight_string(var_export($statistics, true)); + list($array1, $array2, $array3) = array_chunk($statistics, ceil(count($statistics) / 1)); + // highlight_string(var_export($array1, true)); + + foreach ($array1 as $key => $value) { + + $thePage .= $array2[$key]; + $thePage .= $array3[$key]; $thePage .= $value; } //PC::debug($count); - + } // end Statistics - + $thePage .= '
$sitename $parameter$value
$sitename $parameter$value
$sitename $parameter$value
$sitename $parameter$value
'; return $thePage; - } // function water the theme - - public function nws_custom( $att ) { - - + + public function nws_custom($att) + { + + list($name, $location, $parameters, $date_range, $order) = [ - $att['name'], - $att['location'], - $att['parameters'], - $att['date_range'], - $att['order'] - ]; + $att['name'], + $att['location'], + $att['parameters'], + $att['date_range'], + $att['order'] + ]; $locations = explode(',', $location); - + // Tail Race Level = Gage Datum + Gage Height foreach ($locations as $location) { - $thePage = get_transient('nws_custom-' . $name . $location . $date_range . $parameters . $order ); + $thePage = get_transient('nws_custom-' . $name . $location . $date_range . $parameters . $order); - if ( !$thePage ) { - - $url = "http://water.weather.gov/ahps2/hydrograph_to_xml.php?gage=$location&output=xml"; + if (!$thePage) { - $response = wp_remote_get( $url ); - $data = wp_remote_retrieve_body( $response ); + $url = "http://water.weather.gov/ahps2/hydrograph_to_xml.php?gage=$location&output=xml"; - if ( ! $data ) { - return 'Nation Weather Service is not Responding.'; - } + $response = wp_remote_get($url); + $data = wp_remote_retrieve_body($response); + if (!$data) { + return 'Nation Weather Service is not Responding.'; + } - $xml_tree = simplexml_load_string( $data ); - if ( False === $xml_tree ) { - return 'Unable to parse NWS XML'; - } - - // space to underscore; all lower case; only special character allowed is underscored - $SiteName = (string)$xml_tree->attributes()->name; - $SiteName = preg_replace('/[^A-Za-z0-9_]/', '', strtolower(preg_replace('/\s+/', '_', $SiteName))); - - $waterlevel = (string)$xml_tree->zerodatum; - - $c = 0; - foreach ( $xml_tree->observed->datum as $datum ) { - - // in javascript this works out of the box (* 1000) - $datetime = strtotime($datum->valid) * 1000; - $gageheight = $datum->primary; - $waterflow = (string)$datum->secondary; - - if ($waterflow === '-999' || $waterflow === 0) { - $waterflow = '0 cfs'; - } else { - $waterflow = $waterflow * 1000; - $waterflow = $waterflow . " cfs"; + + $xml_tree = simplexml_load_string($data); + if (False === $xml_tree) { + return 'Unable to parse NWS XML'; } - - $tail_race_level = (float)$waterlevel + (float)$gageheight; - - if($c === 0 && $date_range === 'current') { - $thePage .= "Tail Race Level (AMSL)$tail_race_level ft"; - //$thePage .= "Gage Datum (AMSL)$waterlevel ft"; - //$thePage .= "Tail Water Flow$waterflow"; - break; - } else { - $thePage .= "Tail Race Level (AMSL)$tail_race_evel"; - //$thePage .= "Gage Datum (AMSL)$waterlevel ft"; - //$thePage .= "Tail Water Flow$waterflow"; - } - $c++; - - } // foreach xml_tree as site data - - } // foreach NWS location - - - set_transient( 'nws_custom-' . $name . $location . $date_range . $parameters . $order, $thePage, 60 * 15 ); + + // space to underscore; all lower case; only special character allowed is underscored + $SiteName = (string)$xml_tree->attributes()->name; + $SiteName = preg_replace('/[^A-Za-z0-9_]/', '', strtolower(preg_replace('/\s+/', '_', $SiteName))); + + $waterlevel = (string)$xml_tree->zerodatum; + + $c = 0; + foreach ($xml_tree->observed->datum as $datum) { + + // in javascript this works out of the box (* 1000) + $datetime = strtotime($datum->valid) * 1000; + $gageheight = $datum->primary; + $waterflow = (string)$datum->secondary; + + if ($waterflow === '-999' || $waterflow === 0) { + $waterflow = '0 cfs'; + } else { + $waterflow = $waterflow * 1000; + $waterflow = $waterflow . " cfs"; + } + + $tail_race_level = (float)$waterlevel + (float)$gageheight; + + if ($c === 0 && $date_range === 'current') { + $thePage .= "Tail Race Level (AMSL)$tail_race_level ft"; + //$thePage .= "Gage Datum (AMSL)$waterlevel ft"; + //$thePage .= "Tail Water Flow$waterflow"; + break; + } else { + $thePage .= "Tail Race Level (AMSL)$tail_race_evel"; + //$thePage .= "Gage Datum (AMSL)$waterlevel ft"; + //$thePage .= "Tail Water Flow$waterflow"; + } + $c++; + } // foreach xml_tree as site data + + } // foreach NWS location + + + set_transient('nws_custom-' . $name . $location . $date_range . $parameters . $order, $thePage, 60 * 15); } return $thePage; diff --git a/public/js/water-the-theme-public.js b/public/js/water-the-theme-public.js index 72b03b4..d3c7639 100644 --- a/public/js/water-the-theme-public.js +++ b/public/js/water-the-theme-public.js @@ -32,8 +32,8 @@ $( window ).load(function() { // #BED8D3' '#D8E4E6' - $('#Statistics tr:nth-child(6n), #Statistics tr:nth-child(6n-1), #Statistics tr:nth-child(6n-2)').css({backgroundColor:'#D8E4E6'}); - $('#Statistics tr:nth-child(6n-3), #Statistics tr:nth-child(6n-4), #Statistics tr:nth-child(6n-5)').css({backgroundColor:'#BED8D3'}); + // $('#Statistics tr:nth-child(6n), #Statistics tr:nth-child(6n-1), #Statistics tr:nth-child(6n-2)').css({backgroundColor:'#D8E4E6'}); + // $('#Statistics tr:nth-child(6n-3), #Statistics tr:nth-child(6n-4), #Statistics tr:nth-child(6n-5)').css({backgroundColor:'#BED8D3'}); $("td[datetime]").each(function(key,value){ var dt = $(value).attr('datetime');