2020-05-16 18:03:00 +00:00
|
|
|
<?php namespace JasonWilliams\CalgaryConditions\Components;
|
|
|
|
|
|
|
|
use Cms\Classes\ComponentBase;
|
2020-05-16 21:34:59 +00:00
|
|
|
use ApplicationException;
|
2020-05-16 20:49:59 +00:00
|
|
|
use October\Rain\Network\Http;
|
|
|
|
use JasonWilliams\CalgaryConditions\Models\Settings;
|
|
|
|
use Cache;
|
2020-05-16 18:03:00 +00:00
|
|
|
|
|
|
|
class CurrentInfo extends ComponentBase
|
|
|
|
{
|
|
|
|
public function componentDetails()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'name' => 'Current Info',
|
|
|
|
'description' => 'Displays the current weather and time in Calgary, AB, Canada'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onRun()
|
|
|
|
{
|
|
|
|
date_default_timezone_set('America/Edmonton');
|
2020-05-16 20:49:59 +00:00
|
|
|
$fields['time'] = date('g:ia');
|
|
|
|
$this->page['conditions'] = $fields;
|
|
|
|
$this->addJs('/plugins/jasonwilliams/calgaryconditions/assets/javascript/currentinfo.js');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onUpdateRequested()
|
|
|
|
{
|
|
|
|
$response = Cache::remember('weatherdata', 60, function() {
|
2020-05-16 21:34:59 +00:00
|
|
|
return json_decode(Http::get('http://api.weatherstack.com/current?access_key='.Settings::get('weatherstack_api_key').'&query=Calgary'));
|
2020-05-16 20:49:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
2020-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
}
|