ja.son-williams.ca/www/plugins/jasonwilliams/calgaryconditions/components/CurrentInfo.php

44 lines
1.3 KiB
PHP
Raw Normal View History

<?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 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()
{
$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;
}
}