You are here

class weather_es_citPro in Weather_es 6.3

Same name and namespace in other branches
  1. 7 weather_es.inc \weather_es_citPro

Class that gets the cities of a province

Hierarchy

Expanded class hierarchy of weather_es_citPro

File

./weather_es.inc, line 145

View source
class weather_es_citPro {
  private $xprovince_code;
  public $xcities;
  private $xerror_message;
  private static $url = 'http://www.aemet.es/es/eltiempo/prediccion/municipios?p=';

  //private static $url = 'http://drupal.local/alicante.html';

  //private static $url = 'file:///var/www/jose/';
  private static $cit_cod_len = 5;
  public function __construct($province_code) {

    // Can't use DOM because AEMET's files aren't valid XHTML files...
    $this->xprovince_code = $province_code;
    $this->xerror_message = '';
    if ($province_code == '---') {
      $this->xcities = array(
        t('Any city'),
      );
    }
    else {
      if (function_exists('fsockopen')) {

        //$this->cities(drupal_http_request(self::$url));
        $this
          ->cities(drupal_http_request(self::$url . $this->xprovince_code));
      }
      else {
        $this->xerror_message = t('Function fsockopen does not exist, you can\'t use this module...');
      }
    }
  }

  /**
   *
   * @param drupal_http_request $result
   */
  public function cities($result) {
    switch ($result->code) {
      case '301':
      case '302':
      case '307':
        $result = drupal_http_request('http://www.aemet.es' . $result->redirect_url);

      //$result = drupal_http_request('http://drupal.local/alicante.html'. $result->redirect_url);
      case '200':
      case '304':

        // Get the table
        preg_match('/<select.+localidades_selector.+select>/sU', $result->data, $captured);

        // Pass it to a string
        $data_string = implode(" ", $captured);
        $length = strlen($data_string);
        $position = strpos($data_string, "select>");
        $data_string = substr($data_string, 0, $position - $length);
        $clean_string = preg_replace('/[ \\f\\r\\t\\n]+/', ' ', $data_string);
        preg_match_all('/<option.+option>/sU', $clean_string, $captured_option);
        break;
      default:
        $this->xerror_message = t('fsockopen fail to open AEMET.');
    }

    // The first code is [0] => ... so it's no useful
    for ($i = 1; $i < sizeof($captured_option[0]); $i++) {
      preg_match('/id[0-9]{5}/', $captured_option[0][$i], $cit_cod);
      $cit_cod = substr($cit_cod[0], 2, self::$cit_cod_len);
      preg_match('/>.+</', $captured_option[0][$i], $cit_nam);
      $cit_nam_lon = strlen($cit_nam[0]);

      //$this->xcities[$this->province_code][$cit_cod] = substr($cit_nam[0], 1, $cit_nam_lon - 2);
      $this->xcities[$cit_cod] = drupal_convert_to_utf8(substr($cit_nam[0], 1, $cit_nam_lon - 2), 'ISO-8859-15');
    }
    unset($result);
  }

  /**
   *
   * @return array [city_code] => city_name
   */
  public function getCities() {
    return $this->xcities;
  }

  /**
   *
   * @return string 'nn' n [0-9]
   */
  public function getProvince() {
    return $this->xprovince_code;
  }

  /**
   *
   * @return bool
   */
  public function error() {
    if ($this->xerror_message === '') {
      return false;
    }
    else {
      return true;
    }
  }
  public function errorMessage() {
    return $this->xerror_message;
  }

}

Members