WeatherHelper.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. App::uses('AppHelper', 'View/Helper');
  3. App::uses('WeatherLib', 'Tools.Lib');
  4. /**
  5. * Display weather in the view
  6. *
  7. * @author Mark Scherer
  8. * @license http://opensource.org/licenses/mit-license.php MIT
  9. */
  10. class WeatherHelper extends AppHelper {
  11. public $helpers = ['Html'];
  12. protected $_defaultConfig = [
  13. 'imageUrl' => 'http://www.google.com/ig/images/weather/'
  14. ];
  15. public function __construct($View = null, $settings = []) {
  16. $this->_defaultConfig = (array)Configure::read('Weather') + $this->_defaultConfig;
  17. parent::__construct($View, $settings + $this->_defaultConfig);
  18. }
  19. /**
  20. * Generates icon URL.
  21. *
  22. * @param string $icon
  23. * @param string $ext
  24. * @param bool $full
  25. * @return string URL
  26. */
  27. public function imageUrl($icon, $ext = 'gif', $full = false) {
  28. return $this->Html->url($this->settings['imageUrl'] . $icon . '.' . $ext, $full);
  29. }
  30. /**
  31. * Gets weather data.
  32. *
  33. * @return array
  34. */
  35. public function get($location, $options = []) {
  36. $Weather = new WeatherLib();
  37. $defaults = [
  38. 'cache' => '+1 hour'
  39. ];
  40. $options += $defaults;
  41. return $Weather->get($location, $options);
  42. }
  43. }