Skip to content
Browse files

Initial commit - widget v0.1

  • Loading branch information...
1 parent ea47299 commit 356218f31417f8a9e83d483ea88f79140b5270ce @nhudinhtuan committed Mar 25, 2012
Showing with 192 additions and 0 deletions.
  1. BIN .DS_Store
  2. +135 −0 GoogleWeatherWidget.php
  3. +20 −0 GoogleWeatherWidget.ss
  4. 0 README
  5. +35 −0 README.txt
  6. +2 −0 _config.php
View
BIN .DS_Store
Binary file not shown.
View
135 GoogleWeatherWidget.php
@@ -0,0 +1,135 @@
+<?php
+/**
+ * The widget that shows the 5 days weather forecast.
+ * The data is given by Google Weather Forecast API.
+ *
+ * @author: nhudinhtuan - nhudinhtuan@gmail.com
+ */
+
+class GoogleWeatherWidget extends Widget {
+ static $db = array(
+ "Title" => "Varchar(255)",
+ "Location" => "Varchar(255)",
+ "Language" => "Varchar(2)",
+ "Limit" => "Varchar(1)",
+ "Scale" => "Varchar(1)"
+ );
+ static $defaults = array(
+ "Title" => "Weather for London",
+ "Location" => "London",
+ "Language" => "en",
+ "Limit" => "5",
+ "Scale" => "C"
+ );
+
+ static $title = "Weather Forecast";
+ static $cmsTitle = "Weather Forecast";
+ static $description = "Show weather forecast from Google.";
+
+ function getForecast() {
+ $content = utf8_encode(file_get_contents('http://www.google.com/ig/api?weather='.$this->Location."&hl=".$this->Language));
+ $xml = simplexml_load_string($content);
+ $information = $xml->xpath("/xml_api_reply/weather/forecast_information");
+ $current = $xml->xpath("/xml_api_reply/weather/current_conditions");
+ $forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");
+
+ // check null
+ if (empty($information)) {
+ return "";
+ }
+
+ $output = new DataObjectSet();
+ // current weather
+ $temp = $current[0]->temp_f['data'];
+ if ($this->Scale == "C") {
+ // convert from Fahrenheit to Celsius
+ $temp = self::convertFahToCel($temp);
+ }
+ $output->push(new ArrayData(array(
+ "date" => "".$information[0]->forecast_date['data'],
+ "icon" => "http://www.google.com".$current[0]->icon['data'],
+ "temp" => $temp."&deg; ".$this->Scale ,
+ "condition" => $current[0]->condition['data']."",
+ )));
+
+ $count = 1; // count the number of days
+ foreach ($forecast_list as $forecast) {
+ // limit number of days
+ if ($count == $this->Limit) {
+ break;
+ }
+
+ // temperature scale
+ $high = $forecast->high['data'];
+ $low = $forecast->low['data'];
+ if ($this->Scale == "C") {
+ $high = self::convertFahToCel($high);
+ $low = self::convertFahToCel($low);
+ }
+
+ $output->push(new ArrayData(array(
+ "date" => "".$forecast->day_of_week['data'],
+ "icon" => "http://www.google.com".$forecast->icon['data'],
+ "temp" => $low."&deg; ".$this->Scale." - ".$high."&deg; ".$this->Scale,
+ "condition" => "".$forecast->condition['data'],
+ )));
+ $count++;
+ }
+
+ return $output;
+ }
+
+ function getCMSFields() {
+ $fields = new FieldSet(
+ new TextField("Title", _t("WeatherForecast.TITLE", "Title")),
+ new TextField("Location", _t("WeatherForecast.LOCATION", "Location")),
+ new DropdownField('Language', _t("WeatherForecast.LANGUAGE", "Language"), array(
+ 'ar' => 'Arabic',
+ 'bg' => 'Bulgarian',
+ 'cs' => 'Czech',
+ 'nl' => 'Dutch',
+ 'da' => 'Danish',
+ 'en' => 'English',
+ 'fr' => 'French',
+ 'de' => 'German',
+ 'el' => 'Greek',
+ 'hi' => 'Hindi',
+ 'id' => 'Indonesian',
+ 'it' => 'Italian',
+ 'ja' => 'Japanese',
+ 'ko' => 'Korean',
+ 'es' => 'Spanish',
+ 'vi' => 'Vietnamese'
+ ), $value = $this->Language),
+ new OptionsetField('Limit', _t("WeatherForecast.LIMIT", "Limit number of days"), array(
+ '1' => 'show current weather conditions',
+ '2' => '2 days',
+ '3' => '3 days',
+ '4' => '4 days',
+ '5' => '5 days',
+ ), $value = $this->Limit),
+ new OptionsetField('Scale', _t("WeatherForecast.SCALE", "Temperature scale"), array(
+ 'C' => 'Celsius ',
+ 'F' => 'Fahrenheit '
+ ), $value = $this->Scale)
+ );
+ $this->extend('updateCMSFields', $fields);
+
+ return $fields;
+ }
+
+ function Title() {
+ return $this->Title ? $this->Title : "Weather for ".$this->Location;
+ }
+
+ /**
+ * Convert Fahrenheit to Celsius
+ *
+ * @return float
+ */
+ function convertFahToCel($fah) {
+ return ceil((5/9)*($fah-32));
+ }
+}
+
+?>
View
20 GoogleWeatherWidget.ss
@@ -0,0 +1,20 @@
+<div class="WeatherWidget">
+ <% if getForecast %>
+ <table width="100%">
+ <% control getForecast %>
+ <tr>
+ <td>
+ <center><img src="$icon" alt="weather icon" id="weather_widget_icon"/><center>
+ </td>
+ <td>
+ <b>$date</b><br>
+ $temp<br>
+ $condition
+ </td>
+ </tr>
+ <% end_control %>
+ </table>
+ <% else %>
+ <center>no such location!</center>
+ <% end_if %>
+</div>
View
0 README
No changes.
View
35 README.txt
@@ -0,0 +1,35 @@
+Google Weather Widget 0.0.1 for SilverStripe
+26.03.2012
+Created by Nhu Dinh Tuan
+nhudinhtuan@gmail.com
+========================================================================
+This widget enables you to display weather forecast on your blog.
+The weather forecast information is given by Google Weather API.
+========================================================================
+Installation:
+
+
++++If you are using the blog module:
+1. Extract the widgets_googleweather folder into the top level of your site.
+2. Run "/dev/build?flush=all".
+3. You can add this widget to your blog using the Widgets tab.
+
++++If you are NOT using the blog module:
+1. Extract the widgets_googleweather folder into the top level of your site.
+2. Ensure that widgets have been enabled in your site. You should have something like the following code in "mysite/code/Page.php":
+
+ public static $has_one = array(
+ 'SideBar' => 'WidgetArea',
+ );
+ public function getCMSFields(){
+ $fields = parent::getCMSFields();
+ $fields->addFieldToTab(
+ 'Root.Content.Widgets',
+ new WidgetAreaEditor('SideBar')
+ );
+ return $fields;
+ }
+
+3. Add the placeholder "$SideBar" to your template where you want to display your Facebook feed.
+4. Run "/dev/build?flush=all".
+5. Reload the CMS interface, the widget should be usable on the *Widgets* tab.
View
2 _config.php
@@ -0,0 +1,2 @@
+<?php
+?>

0 comments on commit 356218f

Please sign in to comment.
Something went wrong with that request. Please try again.