Completed weather and local time plugin

This commit is contained in:
JayWll
2020-05-16 14:49:59 -06:00
parent 8aeceee0eb
commit 48a9226a81
8 changed files with 68 additions and 5 deletions

View File

@@ -1,11 +1,14 @@
<?php namespace JasonWilliams\CalgaryConditions\Components;
use Cms\Classes\ComponentBase;
use October\Rain\Network\Http;
use JasonWilliams\CalgaryConditions\Models\Settings;
use Cache;
use ApplicationException;
class CurrentInfo extends ComponentBase
{
public $fields;
//public $fields;
public function componentDetails()
{
@@ -18,6 +21,23 @@ class CurrentInfo extends ComponentBase
public function onRun()
{
date_default_timezone_set('America/Edmonton');
$this->fields['time'] = date('g:ia');
$fields['time'] = date('g:ia');
$this->page['conditions'] = $fields;
$this->addJs('/plugins/jasonwilliams/calgaryconditions/assets/javascript/currentinfo.js');
}
public function onUpdateRequested()
{
$settings = Settings::instance();
$response = Cache::remember('weatherdata', 60, function() {
return json_decode(Http::get('http://api.weatherstack.com/current?access_key='.$settings->weatherstack_api_key.'&query=Calgary'));
});
date_default_timezone_set('America/Edmonton');
$fields['time'] = date('g:ia');
$fields['temperature'] = $response->current->temperature;
$fields['weather'] = strtolower($response->current->weather_descriptions[0]);
$this->page['conditions'] = $fields;
}
}

View File

@@ -1,3 +1 @@
{% set conditions = __SELF__.fields %}
<p>Right now in Calgary the time is {{ conditions.time }}</p>
<p id="currentinfo">Right now it's {{ conditions.time }} here in Calgary.</p>

View File

@@ -0,0 +1 @@
Right now it's {{ conditions.time }} here in Calgary, it's {{ conditions.temperature }}&deg;c and it's {{ conditions.weather }}.