ja.son-williams.ca/www/plugins/jasonwilliams/calgaryconditions/components/CurrentInfo.php
2020-05-16 15:34:59 -06:00

40 lines
1.3 KiB
PHP

<?php namespace JasonWilliams\CalgaryConditions\Components;
use Cms\Classes\ComponentBase;
use ApplicationException;
use October\Rain\Network\Http;
use JasonWilliams\CalgaryConditions\Models\Settings;
use Cache;
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');
$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() {
return json_decode(Http::get('http://api.weatherstack.com/current?access_key='.Settings::get('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;
}
}