2020-05-16 18:03:00 +00:00
|
|
|
<?php namespace JasonWilliams\CalgaryConditions\Components;
|
|
|
|
|
|
|
|
use Cms\Classes\ComponentBase;
|
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
|
|
|
use ApplicationException;
|
|
|
|
|
|
|
|
class CurrentInfo extends ComponentBase
|
|
|
|
{
|
2020-05-16 20:49:59 +00:00
|
|
|
//public $fields;
|
2020-05-16 18:03:00 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
$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;
|
2020-05-16 18:03:00 +00:00
|
|
|
}
|
|
|
|
}
|