Browse Source

Ajoute le module météo WorldWeatherOnline

Pour #1
master
Clement Desmidt 2 years ago
parent
commit
8dd16c5f34

+ 38
- 0
modules/worldweatheronline/Worldweatheronline.php View File

@@ -0,0 +1,38 @@
1
+<?php
2
+
3
+class worldweatheronline extends Module
4
+{
5
+    protected $moduleTitle = 'Météo';
6
+
7
+    protected $paramsList = array(
8
+        'visibility',
9
+        'city',
10
+        'x',
11
+        'y'
12
+    );
13
+
14
+    public function __construct($params){
15
+        parent::__construct(__CLASS__, $params);
16
+    }
17
+
18
+    public function build(){
19
+        $ville = $this->getParam('city');
20
+        $future_weather = [];
21
+        include $this->pathToModule.'includes/WorldweatheronlineApi.php';
22
+        $api = new WorldweatheronlineApi($ville);
23
+        if ($api->isFound()) {
24
+            $forecast = $api->getForecast();
25
+            $current = $api->getCurrent();
26
+            echo '<script type="text/javascript" src="'.$this->pathToModule.'js/'.$this->moduleName.'.js"></script>
27
+				<link rel="stylesheet" type="text/css" href="'.$this->pathToModule.'css/'.$this->moduleName.'.css">';
28
+            echo '<div id="worldweatheronline" style="left:'.$this->getParam("x").'; top:'.$this->getParam("y").';">
29
+				'.$ville.'<br/>
30
+				<div class="weatherpic"><img src="'.$api->getIcon().'"/></div>
31
+				<strong>'.'Actuellement : ' . $current['temp_c'] . '°C - ' . $current['condition'].'</strong><br/>
32
+				'.implode('<br/>', array_map(function($f) {
33
+                    return sprintf('%s : %s°C | %s°C - %s', $f['day_of_week'], $f['low'], $f['high'], $f['condition']);
34
+                }, $forecast)).'
35
+			</div>';
36
+        }
37
+    }
38
+}

+ 9
- 0
modules/worldweatheronline/css/worldweatheronline.css View File

@@ -0,0 +1,9 @@
1
+	#worldweatheronline {position:absolute; top:10px; left: 100px;padding:5px;-webkit-border-radius: 5px;
2
+		-moz-border-radius: 5px;border: 1px solid rgba(0,0,0,0.3);
3
+		-webkit-box-shadow: 0px 3px 3px rgba(0, 0, 0, 0.15);
4
+		-moz-box-shadow: 0px 3px 3px rgba(0, 0, 0, 0.15);}
5
+		#worldweatheronline .weatherpic img {
6
+			float: left; width: 53px; height: 53px; margin-right:10px;
7
+		}
8
+		
9
+	#city-submit{cursor:pointer;}

+ 160
- 0
modules/worldweatheronline/includes/WorldweatheronlineApi.php View File

@@ -0,0 +1,160 @@
1
+<?php
2
+
3
+class WorldweatheronlineApi
4
+{
5
+    /** City code input **/
6
+    private $city_code = '613135'; // Montsoult
7
+
8
+    /** City label get on the google webservice **/
9
+    private $city = '';
10
+
11
+    /** Domain of the google website **/
12
+    private $domain = 'www.google.com';
13
+
14
+    /** Prefix of the img link **/
15
+    private $prefix_images = '';
16
+
17
+    /** Array with current weather **/
18
+    private $current_conditions = array();
19
+
20
+    /** Array with forecast weather **/
21
+    private $forecast_conditions = array();
22
+
23
+    /** If the city was found **/
24
+    private $is_found = true;
25
+
26
+    /** The HTML response send by the service **/
27
+    private $response;
28
+
29
+    /** API KEY **/
30
+    const KEY = '60dc184fe4be1b80bbaa8cd1103b0ab8e45a25f0';
31
+
32
+    /**
33
+     * Class constructor
34
+     * @param string $city_code is the label of the city
35
+     * @param string $lang the lang of the return weather labels
36
+     * @return ...
37
+     */
38
+
39
+    function __construct($city_code, $lang = 'fr')
40
+    {
41
+        $this->city_code = $city_code;
42
+        $this->prefix_images = 'http://' . $this->domain;
43
+        //$this->url = 'http://'.$this->domain.'/ig/api?weather='.urlencode($this->city_code).'&hl='.$lang;
44
+        $this->url = 'http://api.worldweatheronline.com/free/v1/weather.ashx?q=' . $this->city_code . '&format=xml&num_of_days=3&key=' . self::KEY;
45
+
46
+        $getContentCode = $this->getContent($this->url);
47
+
48
+        if ($getContentCode == 200) {
49
+
50
+            $content = utf8_encode($this->response);
51
+
52
+            $xml = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA);
53
+
54
+            if (!isset($xml->error)) {
55
+                //if(!isset($xml->weather->problem_cause)) {
56
+
57
+                //$this->city = (string)$xml->weather->forecast_information->city->attributes()->data;
58
+                $this->city = (string)$xml->request->query;
59
+
60
+                $this->current_conditions['condition'] = (string)$xml->current_condition->weatherDesc;
61
+                $this->current_conditions['temp_f'] = (string)$xml->current_condition->temp_F;
62
+                $this->current_conditions['temp_c'] = (string)$xml->current_condition->temp_C;
63
+                //$this->current_conditions['temp_c'] = (string)$xml->weather->current_conditions->temp_c->attributes()->data;
64
+                $this->current_conditions['humidity'] = (string)$xml->current_condition->humidity;
65
+                //$this->current_conditions['humidity'] = (string)$xml->weather->current_conditions->humidity->attributes()->data;
66
+                $this->current_conditions['icon'] = (string)$xml->current_condition->weatherIconUrl;
67
+                //$this->current_conditions['icon'] = $this->prefix_images.(string)$xml->weather->current_conditions->icon->attributes()->data;
68
+                $this->current_conditions['wind_condition'] = (string)$xml->current_condition->windspeedKmph;
69
+                //$this->current_conditions['wind_condition'] = (string)$xml->weather->current_conditions->wind_condition->attributes()->data;
70
+
71
+                foreach ($xml->weather as $forecast_conditions_value) {
72
+                    $forecast_conditions_temp = array();
73
+                    $forecast_conditions_temp['day_of_week'] = (string)$forecast_conditions_value->date;
74
+                    $forecast_conditions_temp['low'] = (string)$forecast_conditions_value->tempMinC;
75
+                    $forecast_conditions_temp['high'] = (string)$forecast_conditions_value->tempMaxC;
76
+                    $forecast_conditions_temp['icon'] = (string)$forecast_conditions_value->weatherIconUrl;
77
+                    $forecast_conditions_temp['condition'] = (string)$forecast_conditions_value->weatherDesc;
78
+                    $this->forecast_conditions[] = $forecast_conditions_temp;
79
+                }
80
+            } else {
81
+                $this->is_found = false;
82
+            }
83
+
84
+        } else {
85
+            trigger_error('Google results parse problem : http error ' . $getContentCode, E_USER_WARNING);
86
+            return null;
87
+        }
88
+    }
89
+
90
+    /**
91
+     * Get URL content using cURL.
92
+     *
93
+     * @param string $url the url
94
+     *
95
+     * @return string the html code
96
+     */
97
+
98
+    protected function getContent($url)
99
+    {
100
+        if (!extension_loaded('curl')) {
101
+            throw new Exception('curl extension is not available');
102
+        }
103
+        $useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
104
+        $curl = curl_init();
105
+        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
106
+        curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
107
+        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
108
+        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
109
+        curl_setopt($curl, CURLOPT_URL, $url);
110
+        $this->response = curl_exec($curl);
111
+        $infos = curl_getinfo($curl);
112
+        curl_close($curl);
113
+        return $infos['http_code'];
114
+    }
115
+
116
+    /**
117
+     * Get the city
118
+     */
119
+
120
+    public function getCity()
121
+    {
122
+        return $this->city;
123
+    }
124
+
125
+    /**
126
+     * Get the current weather
127
+     */
128
+
129
+    public function getCurrent()
130
+    {
131
+        return $this->current_conditions;
132
+    }
133
+
134
+    /**
135
+     * Get the forecast weather
136
+     */
137
+
138
+    public function getForecast()
139
+    {
140
+        return $this->forecast_conditions;
141
+    }
142
+
143
+    /**
144
+     * Get the icon
145
+     */
146
+
147
+    public function getIcon()
148
+    {
149
+        return $this->current_conditions['icon'];
150
+    }
151
+
152
+    /**
153
+     * If teh city was found
154
+     */
155
+
156
+    public function isFound()
157
+    {
158
+        return $this->is_found;
159
+    }
160
+}

+ 30
- 0
modules/worldweatheronline/js/worldweatheronline.js View File

@@ -0,0 +1,30 @@
1
+$(document).ready(function(){
2
+	$('body').append('<a href="#city" id="city-menu"><img src="images/interface/weather.png" /></a><div style="display:none;"><div id="city"></div></div>');
3
+	$('#menu-bar').prepend($('#city-menu'));
4
+	$('#city').append('<h3>Quelle ville pour la meteo ?</h3><br/><input type="text" id="cityChoice" name="cityChoice"/><span id="city-submit" class="green-button">Enregistrer</span>');
5
+	$('#city-menu').fancybox({
6
+		'zoomSpeedIn'		: 600,
7
+		'zoomSpeedOut'		: 500,
8
+		'easingIn'			: 'easeOutBack',
9
+		'easingOut'			: 'easeInBack',
10
+		'hideOnContentClick': false,
11
+		'padding'			: 15
12
+	});
13
+	$('#city-submit').live('click', function(){
14
+		var ville = $('#cityChoice').val();
15
+		if(ville != '' || ville != null){
16
+			$.get('ajax/update.php', {id:'worldweatheronline', city: ville}, function(msg){
17
+					location.reload();
18
+			});
19
+		}else{
20
+			$('#city').append('<span class="error">City can\'t be empty.</span>');
21
+		}
22
+	});
23
+	
24
+	var tmp;
25
+
26
+	/* A helper function for converting a set of elements to draggables: */
27
+	make_draggable($('#worldweatheronline'));
28
+
29
+
30
+});

Loading…
Cancel
Save