Setup the weather on website
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 2 May 2014 12:59:38 +0000 (12:59 +0000)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 2 May 2014 12:59:38 +0000 (12:59 +0000)
station id is Sault Ste Marie - KANJ

Toolkit/Weather.php [new file with mode: 0755]
config/application.ini
templates/template.html

diff --git a/Toolkit/Weather.php b/Toolkit/Weather.php
new file mode 100755 (executable)
index 0000000..1d2a482
--- /dev/null
@@ -0,0 +1,242 @@
+<?php
+/**
+ * Weather.php
+ *
+ * PHP version 5
+ *
+ * @category  Toolkit
+ * @package   Weather
+ * @author    Steve Sutton <steve@gaslightmedia.com>
+ * @copyright 2008 Gaslight Media
+ * @license   http://www.gaslightmedia.com/license.txt The Gaslight License
+ * @version   CVS: $Id: Weather.php,v 1.7 2010/08/16 17:41:03 jamie Exp $
+ * @link      <>
+ */
+
+/**
+ * Weather class for Gaslight Media
+ *
+ * You can get the stationId for weather at
+ * http://www.weather.gov/xml/current_obs/seek.php?state=mi
+ * Uses NOAA for getting the rss feed for weather
+ *
+ * @category  Toolkit
+ * @package   Weather
+ * @author    Steve Sutton <steve@gaslightmedia.com>
+ * @copyright 2008 Gaslight Media
+ * @license   http://www.gaslightmedia.com/license.txt The Gaslight License
+ * @link      <>
+ */
+class Toolkit_Weather
+{
+    //{{{ __Class Properties
+
+
+    /**
+     * The xml file used for caching the feed
+     * @var    $_weatherFile string
+     * @access private
+     */
+    private $_weatherFile;
+
+    /**
+     * Station identifier for the feed
+     * @link http://w1.weather.gov/xml/current_obs/seek.php?state=mi
+     *
+     * @var    $_stationId string
+     * @access private
+     */
+    private $_stationId = 'KANJ';
+
+
+    /**
+     * XMLReader object for parsing feed
+     * @var    $_xml object
+     * @access private
+     */
+    private $_xml;
+
+    /**
+     * Description for private
+     * @var    $_assoc array
+     * @access private
+     */
+    private $_assoc;
+
+    /**
+     * Storage array for the current conditions
+     * @var     $currendCond array
+     * @access public
+     */
+    public $currentCond;
+
+    /**
+     * Boolean if the file exists or not
+     * @var    $_cacheFileExist boolean
+     * @access private
+     */
+    private $_cacheFileExist = false;
+
+
+    /**
+     * Interger for the number of min before reloading file
+     * @var int
+     * @access private
+     */
+    private $_interval;
+    //}}}
+
+    //{{{ __construct()
+
+    /**
+     * Class Constructer
+     *
+     * @return void
+     * @access public
+     */
+    function __construct()
+    {
+        $currentErrorMask = ini_get('error_reporting');
+        error_reporting(0);
+        $this->_weatherFile = BASE.'weather-feed.xml';
+        $this->_interval     = 25; // interval in minutes for page cache
+        $this->fetchWeather();
+        $this->setCurrentCond();
+        error_reporting($currentErrorMask);
+    }
+
+    //}}}
+    //{{{ fetchWeather()
+
+
+    /**
+     * fetchWeather
+     *
+     * THis will use curl to grab the weather feed using
+     * the $_weatherFile as destination
+     * and the $_stationId for the xml file name
+     *
+     * @return void
+     * @access public
+     */
+    function fetchWeather()
+    {
+        if (is_file($this->_weatherFile)) {
+            $timeintervalago = mktime(
+                date('H'),
+                date('i') - $this->_interval,
+                date('s'),
+                date('m'),
+                date('d'),
+                date('Y')
+            );
+            $file_time     = filemtime($this->_weatherFile);
+            if ($file_time < $timeintervalago) {
+                $this->grabFeedUrl();
+            }
+            if (is_file($this->_weatherFile)) {
+                $this->_cacheFileExist = true;
+            }
+        } else {
+            $this->grabFeedUrl();
+            if (is_file($this->_weatherFile)) {
+                $this->_cacheFileExist = true;
+            }
+        }
+    }
+
+    //}}}
+    // {{{ grabFeedUrl()
+
+
+    /**
+     * grabFeedUrl
+     *
+     * @return void
+     * @access public
+     */
+    function grabFeedUrl()
+    {
+        // the NOAA xml feed url to be used
+        // from the _stationId
+        $feed_url = 'http://w1.weather.gov/xml/current_obs/'
+            .$this->_stationId.'.xml';
+        // Curl Options used for connection
+        $curlOptions = array(
+            CURLOPT_URL            => $feed_url,
+            CURLOPT_HEADER         => 0,
+            CURLOPT_RETURNTRANSFER => 1,
+            CURLOPT_TIMEOUT        => 15,
+            CURLOPT_CONNECTTIMEOUT => 5,
+            CURLOPT_FAILONERROR    => 1,
+            );
+        // start curl object
+        $ch = curl_init();
+        // set options
+        curl_setopt_array($ch, $curlOptions);
+        // assign the response to a string variable
+        $response = curl_exec($ch);
+        // grab the connection info to check for http_code = 200
+        $info = curl_getinfo($ch);
+        curl_close($ch);
+        if ($info['http_code'] == '200') {
+            file_put_contents($this->_weatherFile, $response);
+            $oldmask = umask(0);
+            chmod($this->_weatherFile, 0666);
+            umask($oldmask);
+        } else {
+            @touch($this->_weatherFile);
+        }
+    }
+
+
+    // }}}
+    //{{{ setCurrentCond()
+
+    /**
+     * Set currentCond
+     *
+     * Set the currentCond clas var up with variables from the xml file
+     * from the weather feed
+     *
+     * @return void
+     * @access public
+     */
+    function setCurrentCond()
+    {
+        $xml = new XML_Unserializer();
+        $xml->unserialize($this->_weatherFile, true);
+        $this->currentCond = $xml->getUnserializedData();
+    }
+
+    //}}}
+    //{{{
+    /**
+     * Getter for _interval
+     *
+     * @return Int
+     * @access public
+     */
+    public function get_interval() {
+        return $this->_interval;
+    }
+
+    //}}}
+    //{{{
+    /**
+     * Setter for _stationId
+     *
+     * @param string $_stationId Station ID
+     *
+     * @return void
+     * @access public
+     */
+    public function set_stationId($_stationId) {
+        $this->_stationId = $_stationId;
+    }
+
+
+
+
+}
+?>
index f6ef5cb..88622f6 100644 (file)
@@ -114,7 +114,7 @@ photo_gallery.application = On
 rotating_images.application = On
 
 ; Turn the weather application On or Off
-weather.application = Off
+weather.application = On
 
 ; Turn the gift certificate application On or Off
 gift_certificates.application = Off
index fc80d6d..20cbcaa 100644 (file)
@@ -72,9 +72,9 @@
             <div id="cSide">
                 <a href="#" id="cDir" class="glmTxtImg">Get Directions</a>
                 <div id="weather">
-                    <img src="http://app.gaslightmedia.com/weather/ra.png" alt="" width="48" height="48">
-                    <span class="wTemp">52.0&deg;</span>
-                    <span class="wCond">Sunny</span>
+                    <img src="{fullIconUrl:h}" alt="" width="48" height="48">
+                    <span class="wTemp">{tempF}&deg;</span>
+                    <span class="wCond">{weather}</span>
                 </div><!-- /#weather -->
                 <a href="#" id="cFace" class="glmTxtImg">Find us on Facebook</a>
             </div><!-- /#cSide -->