You are here

weather_es.module in Weather_es 6

Non-displayable characters.

File

weather_es.module
View source
<?php





// Include the parser for AEMET data
require_once drupal_get_path('module', 'weather_es') . '/weather_es_parser.inc';

/**
 * Implementation of hook_help().
 */
function weather_es_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Spanish forecast by AEMET.');
    case 'admin/settings/weather_es':
      return t('<p>Here you can create a configuration with one or more locations or delete a location from your configuration.</p>');
  }
}

/**
 * Implementation of hook_perm().
 */
function weather_es_perm() {
  return array(
    'administer custom weather_es block',
  );
}

/**
 * Implementation of hook_menu().
 */
function weather_es_menu($may_cache) {
  global $user;
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/weather_es',
      'title' => t('Weather_es'),
      'description' => t('Configure the system weather_es block.'),
      'callback' => 'weather_es_main',
      'callback arguments' => array(
        0,
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
    $items[] = array(
      'path' => 'admin/settings/weather_es/edit',
      'title' => t('Weather_es'),
      'description' => t('Configure the system weather_es block'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'weather_es_configuration',
        0,
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/settings/weather_es/delete',
      'title' => t('Delete a location'),
      'description' => t('Delete a location from the system weather_es block'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'weather_es_confirm_delete',
        0,
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'user/' . $user->uid . '/weather_es',
      'title' => t('My weather_es'),
      'description' => t('Configure your custom weather_es block.'),
      'callback' => 'weather_es_main',
      'callback arguments' => array(
        $user->uid,
      ),
      'access' => user_access('administer custom weather_es block'),
      'type' => MENU_LOCAL_TASK,
    );
    $items[] = array(
      'path' => 'user/' . $user->uid . '/weather_es/edit',
      'title' => t('Edit my weather_es'),
      'description' => t('Configure your custom weather_es block'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'weather_es_configuration',
        $user->uid,
      ),
      'access' => user_access('administer custom weather_es block'),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'user/' . $user->uid . '/weather_es/delete',
      'title' => t('Delete a location from my weather_es'),
      'description' => t('Delete a location from my weather_es block'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'weather_es_confirm_delete',
        $user->uid,
      ),
      'access' => user_access('administer custom weather_es block'),
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}

/**
 * Main weather_es_page
 */
function weather_es_main($wuid) {

  // Table header
  $header = array(
    array(
      'data' => t('City'),
      'field' => 'city',
    ),
    array(
      'data' => t('Operation'),
    ),
  );
  $path = _weather_es_redirect($wuid);
  $rows = array();
  $sql = "SELECT * FROM {weather_es_config} wec WHERE wec.uid = %d";
  $result = db_query(db_rewrite_sql($sql), $wuid);
  while ($row = db_fetch_array($result)) {

    // delete the '/' caracter from the city name (Spanish/one_of_the_other_3_official_languages) and replace it with '-'
    $pos = strpos($row['cit_nam'], '/');
    if ($pos != 0) {
      $row['cit_nam'] = str_replace('/', '-', $row['cit_nam']);
    }
    $rows[] = array(
      'cit_nam' => $row['cit_nam'],
      l(t('delete'), $path . '/delete/' . $row['cod_pro'] . $row['cod_loc'] . '/' . $row['cit_nam']),
    );
  }
  if (count($rows) == 0) {
    $rows[] = array(
      array(
        'data' => '<em>' . t('There are currently no locations.') . '</em>',
        'colspan' => 4,
      ),
    );
  }
  $output = theme('table', $header, $rows);

  // Create a new location
  $output .= '<p>' . l(t('Create a new configuration or add a new location to an exisiting configuration.'), $path . '/edit') . '</p>';
  return $output;
}

/**
 * Show a configuration page for a custom weather_es block
 */
function weather_es_configuration($wuid, $form_values = NULL) {
  if (!isset($form_values)) {
    $step = 1;
  }
  else {

    /* This commented lines are to show a Back button
       if ($_POST['op'] == t('Back')) {
         $step = $form_values['step'] - 1;
       }
       else {
         $step = $form_values['step'] + 1;
       }
       */
    $step = $form_values['step'] + 1;
  }
  $form['step'] = array(
    '#type' => 'hidden',
    '#value' => $step,
  );
  switch ($step) {
    case 1:

      // Page 1
      $form['provincia'] = array(
        '#type' => 'fieldset',
        '#title' => t('Select a province'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#tree' => TRUE,
      );
      $form['provincia']['seleccionada'] = array(
        '#type' => 'select',
        '#title' => t('Province'),
        '#default_value' => 00,
        '#options' => _weather_es_provincies(),
        '#description' => t('Province of the city'),
      );
      break;
    case 2:

      // Show page 2
      $form['poblacion'] = array(
        '#type' => 'fieldset',
        '#title' => t('Select a city'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#tree' => TRUE,
      );
      $form['poblacion']['seleccionada'] = array(
        '#type' => 'select',
        '#title' => t('City'),
        '#options' => _weather_es_cities($form_values['provincia']['seleccionada']),
        '#description' => t('City to show the forecast'),
      );
      $form['idioma'] = array(
        '#type' => 'radios',
        '#title' => t('Languages'),
        '#options' => array(
          'es' => t('Spanish'),
          'ca' => t('Catalan'),
          'eu' => t('Euskera'),
          'gl' => t('Gallego'),
          'en' => t('English'),
          'fr' => t('French'),
        ),
        '#description' => t('Choose the language you will see the weather data.'),
        '#default_value' => 'es',
      );
      $form['save'] = array(
        '#type' => 'submit',
        '#value' => t('Save configuration'),
      );
      $form['add'] = array(
        '#type' => 'submit',
        '#value' => t('Add new location'),
      );
      $form['wuid'] = array(
        '#type' => 'value',
        '#value' => $wuid,
      );

      /*
      $form['back'] = array(
      '#type' => 'button',
      '#value' => t('Back')
      );
      */
      break;
  }
  $form['next'] = array(
    '#type' => 'button',
    '#value' => t('Next'),
  );

  // Hide the button at last step
  if ($step >= 2) {
    $form['next'] = array(
      '#type' => 'hidden',
    );
  }

  // This part is important!
  $form['#multistep'] = TRUE;
  $form['#redirect'] = FALSE;
  return $form;
}

/**
 * Validate the form.
 */
function weather_es_configuration_validate($form_id, $form_values) {
  if ($form_values['step'] == 1 and $form_values['provincia']['seleccionada'] == 0) {
    form_set_error('seleccionada', t('You must select a valid province.'));
  }
  if ($form_values['step'] == 2 and (strlen($form_values['poblacion']['seleccionada']) != 5 or !in_array($form_values['idioma'], array(
    'es',
    'ca',
    'gl',
    'eu',
    'en',
    'fr',
  )))) {
    form_set_error('seleccionada', t('You must select a valid city and a valid language.'));
  }
}

/**
 * Handle the submission of the custom weather_es block.
 */
function weather_es_configuration_submit($form_id, $form_values) {

  // Wait until last step
  if ($form_values['step'] < 2) {
    return;
  }
  $cod_pro = substr($form_values['poblacion']['seleccionada'], 0, 2);
  $cod_loc = substr($form_values['poblacion']['seleccionada'], 2, 4);

  
  $localidades = _weather_es_cities($cod_pro);

  // Handle city name...
  $cit_nam = drupal_strtolower($localidades[$form_values['poblacion']['seleccionada']]);
  $cit_nam = _weather_es_city_name($cit_nam);

  // Get data from AEMET
  $aemet = _weather_es_aemet($form_values['wuid'], $form_values['poblacion']['seleccionada'], $form_values['idioma'], $cit_nam);
  if ($aemet != -1) {

    // Save a new user configuration or rewrite an existing one
    if ($_POST['op'] == t('Save configuration')) {

      // Conection OK, delete previous user configuration
      $sql = "DELETE FROM {weather_es_config} WHERE uid = %d";
      db_query($sql, $form_values['wuid']);
    }

    // Avoid add a city already in the user configuration
    if (!db_result(db_query("SELECT COUNT(*) FROM {weather_es_config} WHERE uid = %d AND cod_pro = %d AND cod_loc = %d", $wuid, $cod_pro, $cod_loc))) {

      // Save config data
      weather_es_save_config($wuid, $cod_pro, $cod_loc, $localidades[$form_values['poblacion']['seleccionada']], $form_values['idioma'], $aemet);
    }

    // Delete the previous city data and save the new one
    db_query("DELETE FROM {weather_es_data} WHERE cod_pro = %d AND cod_loc = %d", $cod_pro, $cod_loc);

    // Save the data
    weather_es_save_data($cod_pro, $cod_loc, $aemet);
    drupal_set_message(t('Config save for: %city.', array(
      '%city' => $localidades[$form_values['poblacion']['seleccionada']],
    )));
  }
  else {

    // The connection with AEMET has failt, try in 10'
    db_query("UPDATE {weather_es_config} SET pro_act = %d WHERE uid = %d AND cod_pro = %d AND cod_loc = %d", time() + 10 * 60, $wuid, $cod_pro, $cod_loc);
    drupal_set_message(t('The connection with AEMET has failt.'));
  }

  // Redirect the user
  $path = _weather_es_redirect($form_values['wuid']);
  drupal_goto($path);
}

/**
 * Confirmation page before deleting a location
 */
function weather_es_confirm_delete($wuid, $cit_cod, $cit_nam) {
  $path = _weather_es_redirect($wuid);
  $form = array();
  $form['wuid'] = array(
    '#type' => 'hidden',
    '#value' => $wuid,
  );
  $form['cit_cod'] = array(
    '#type' => 'hidden',
    '#value' => $cit_cod,
  );
  $form['path'] = array(
    '#type' => 'hidden',
    '#value' => $path,
  );
  return confirm_form($form, t('Are you sure you want to delete the location %name?', array(
    '%name' => $cit_nam,
  )), '' . $path, t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}

/**
 * Handle the deletion of a location
 */
function weather_es_confirm_delete_submit($form_id, $form_values) {

  // delete the entry
  $sql = "DELETE FROM {weather_es_config} WHERE uid = %d AND cod_pro = %d AND cod_loc = %d";
  db_query($sql, $form_values['wuid'], substr($form_values['cit_cod'], 0, 2), substr($form_values['cit_cod'], 2, 4));
  drupal_set_message(t('The location has been deleted.'));
  $path = $form_values['path'];
  return "{$path}";
}

/**
 * Implementation of hook_block().
 */
function weather_es_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('Weather_es: system-wide');
      $blocks[1]['info'] = t('Weather_es: custom user');
      return $blocks;
    case 'configure':
      return $form;
    case 'view':
    default:
      if ($delta == 0 and user_access('access content')) {

        // Show the system weather_es block
        $title = t('System weather');
        $content = block_weather_es_contents(0);
      }
      else {
        if ($delta == 1 and user_access('administer custom weather_es block')) {

          // Show the user weather_es block
          $title = t('User weather');
          $content = block_weather_es_contents(1);
        }
      }
      $block['subject'] = $title;
      $block['content'] = $content;
      return $block;
  }
}

/**
 * Show the weather_es contents
 */
function block_weather_es_contents($which_block) {
  global $user;
  $sql = "SELECT * FROM {weather_es_config} wec WHERE wec.uid = %d";

  // Block 0 is about the site, 1 is about the user
  if ($which_block == 0) {
    $usrcnfresult = db_query(db_rewrite_sql($sql), 0);
  }
  else {
    $usrcnfresult = db_query(db_rewrite_sql($sql), $user->uid);
  }
  while ($usrcnf = db_fetch_object($usrcnfresult)) {

    // Update ancient data
    if ($usrcnf->pro_act < time()) {
      if ($which_block == 0) {
        $wuid = 0;
      }
      else {
        $wuid = $usrcnf->uid;
      }
      $ciudad = $usrcnf->cod_pro . $usrcnf->cod_loc;
      $aemet = _weather_es_aemet($wuid, $usrcnf->cod_pro . $usrcnf->cod_loc, $usrcnf->lan, $usrcnf->cit_nam);
      if ($aemet != -1) {

        // Delete the previous city data and save the new one
        db_query("DELETE FROM {weather_es_data} WHERE cod_pro = %d AND cod_loc = %d", $usrcnf->cod_pro, $usrcnf->cod_loc);
        weather_es_save_data($usrcnf->cod_pro, $usrcnf->cod_loc, $aemet);

        // Next connection to AEMET in 4h
        db_query("UPDATE {weather_es_config} SET pro_act = %d WHERE uid = %d AND cod_pro = %d AND cod_loc = %d", time() + 4 * 3600, $wuid, $usrcnf->cod_pro, $usrcnf->cod_loc);
      }
      else {

        // The connection with AEMET has failt, try in 10'
        db_query("UPDATE {weather_es_config} SET pro_act = %d WHERE uid = %d AND cod_pro = %d AND cod_loc = %d", time() + 10 * 60, $wuid, $cod_pro, $cod_loc);
      }
    }
    $content .= '<strong>' . check_markup($usrcnf->cit_nam) . '</strong>';
    $content .= t('<small><strong>Weather forecast by AEMET.</strong></small>');
    $sql = "SELECT * FROM {weather_es_data} wed WHERE wed.cod_loc = %d ORDER BY wed.did ASC";
    $result = pager_query(db_rewrite_sql($sql), 2, 0, "SELECT COUNT(cod_loc) FROM {weather_es_data} WHERE cod_loc = %d", $usrcnf->cod_loc, $usrcnf->cod_loc);

    // Show the data by the theme
    while ($data = db_fetch_object($result)) {
      $content .= theme('weather_es', $data, $usrcnf);
    }
    $content .= theme('pager', NULL, NULL);
  }
  return $content;
}

/**
 * Save the config data
 */
function weather_es_save_config($wuid, $cod_pro, $cod_loc, $cit_nam, $lan, $aemet) {

  // Is there snow info?
  $snow = FALSE;
  for ($i = 0; $i < sizeof($aemet[2]); $i++) {
    if (preg_match('#\\(m\\)#', $aemet[2][$i])) {
      $snow = TRUE;
    }
  }
  if ($snow) {
    db_query("INSERT INTO {weather_es_config} (uid, cod_pro, cod_loc, cit_nam, lan, pro_act, sky, rain, snow, tmax, tmin, win_dir, win_spd, iv, rsk)\n    VALUES(%d, %d, %d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $wuid, $cod_pro, $cod_loc, $cit_nam, $lan, time() + 4 * 3600, $aemet[2][0], $aemet[2][1], $aemet[2][2], $aemet[2][3], $aemet[2][4], $aemet[2][5], $aemet[2][6], $aemet[2][7], $aemet[2][8]);
  }
  else {
    db_query("INSERT INTO {weather_es_config} (uid, cod_pro, cod_loc, cit_nam, lan, pro_act, sky, rain, tmax, tmin, win_dir, win_spd, iv, rsk)\n    VALUES(%d, %d, %d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $wuid, $cod_pro, $cod_loc, $cit_nam, $lan, time() + 4 * 3600, $aemet[2][0], $aemet[2][1], $aemet[2][2], $aemet[2][3], $aemet[2][4], $aemet[2][5], $aemet[2][6], $aemet[2][7]);
  }
}

/**
 * Save the data
 */
function weather_es_save_data($cod_pro, $cod_loc, $aemet) {

  // Delete the previous city data and save the new one
  db_query("DELETE FROM {weather_es_data} WHERE cod_pro = %d AND cod_loc = %d", $cod_pro, $cod_loc);

  // Save the data of the three first days
  // am
  if ($aemet[1][0] == 'am') {
    for ($i = 0; $i < 3; $i++) {
      db_query("INSERT INTO {weather_es_data} (cod_pro, cod_loc, day, ampm, sky_txt1, sky_img1, sky_txt2, sky_img2, rain, snow, tmax, tmin, win_dir1, win_spd1, win_dir2, win_spd2, iv_max, rsk)\n      VALUES(%d, %d, '%s', '%s', '%s', %d, '%s', %d, %d, '%s', %d, %d, '%s', %d, '%s', %d, %d, '%s')", $cod_pro, $cod_loc, $aemet[0][$i], $aemet[1][2 * $i], $aemet[4][2 * $i], $aemet[3][2 * $i], $aemet[4][2 * $i + 1], $aemet[3][2 * $i + 1], $aemet[5][$i], $aemet[6][$i], $aemet[7][$i], $aemet[8][$i], $aemet[9][2 * $i], $aemet[10][2 * $i], $aemet[9][2 * $i + 1], $aemet[10][2 * $i + 1], $aemet[11][$i], $aemet[12][$i]);
    }
  }
  else {
    db_query("INSERT INTO {weather_es_data} (cod_pro, cod_loc, day, ampm, sky_txt2, sky_img2, rain, snow, tmax, tmin, win_dir1, win_spd1, iv_max, rsk)\n    VALUES(%d, %d, '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', %d, %d, '%s')", $cod_pro, $cod_loc, $aemet[0][0], $aemet[1][0], $aemet[4][0], $aemet[3][0], $aemet[5][0], $aemet[6][0], $aemet[7][0], $aemet[8][0], $aemet[9][0], $aemet[10][0], $aemet[11][0], $aemet[12][0]);
    for ($i = 1; $i < 3; $i++) {
      db_query("INSERT INTO {weather_es_data} (cod_pro, cod_loc, day, ampm, sky_txt1, sky_img1, sky_txt2, sky_img2, rain, snow, tmax, tmin, win_dir1, win_spd1, win_dir2, win_spd2, iv_max, rsk)\n      VALUES(%d, %d, '%s', '%s', '%s', %d, '%s', %d, %d, '%s', %d, %d, '%s', %d, '%s', %d, %d, '%s')", $cod_pro, $cod_loc, $aemet[0][$i], $aemet[1][2 * $i - 1], $aemet[4][2 * $i - 1], $aemet[3][2 * $i - 1], $aemet[4][2 * $i], $aemet[3][2 * $i], $aemet[5][$i], $aemet[6][$i], $aemet[7][$i], $aemet[8][$i], $aemet[9][2 * $i - 1], $aemet[10][2 * $i - 1], $aemet[9][2 * $i], $aemet[10][2 * $i], $aemet[11][$i], $aemet[12][$i]);
    }
  }

  // Rest of the week
  if ($aemet[1][0] == 'am') {
    $offset = 0;
  }
  else {
    $offset = -1;
  }
  for ($i = 3; $i < 7; $i++) {
    db_query("INSERT INTO {weather_es_data} (cod_pro, cod_loc, day, sky_txt1, sky_img1, rain, snow, tmax, tmin, win_dir1, win_spd1)\n    VALUES(%d, %d, '%s', '%s', %d, %d, '%s', %d, %d, '%s', %d)", $cod_pro, $cod_loc, $aemet[0][$i], $aemet[4][$i + 3 + $offset], $aemet[3][$i + 3 + $offset], $aemet[5][$i], $aemet[6][$i], $aemet[7][$i], $aemet[8][$i], $aemet[9][$i + 3 + $offset], $aemet[10][$i + 3 + $offset]);
  }
}

/**
 * Custom theme function for the weather_es block output
 */
function theme_weather_es($data, $usrcnf) {
  $img_ext = '.png';
  $content .= t('<p><strong>Day: ' . filter_xss($data->day) . '</strong></p>');
  $content .= '<ul>';
  if ($data->sky_txt1 != '') {
    $content .= '<li>' . filter_xss($usrcnf->sky) . ' ' . filter_xss($data->ampm) . ' : <div style="text-align:center;"><img src="' . drupal_get_path(module, weather_es) . '/images/' . filter_xss($data->sky_img1) . $img_ext . '" title="' . filter_xss($data->sky_txt1) . '" alt="' . filter_xss($data->sky_txt1) . '" width="64" height="64" /></div></li>';
  }
  if ($data->sky_txt2 != '') {
    $content .= '<li>' . filter_xss($usrcnf->sky) . ' pm: <div style="text-align:center;"><img src="' . drupal_get_path(module, weather_es) . '/images/' . filter_xss($data->sky_img2) . $img_ext . '" title="' . filter_xss($data->sky_txt2) . '" alt="' . filter_xss($data->sky_txt2) . '" width="64" height="64" /></div></li>';
  }
  if ($data->rain != 999) {
    $content .= '<li>' . filter_xss($usrcnf->rain) . ': ' . filter_xss($data->rain) . '</li>';
  }
  if ($data->snow != 9999) {
    $content .= '<li>' . filter_xss($usrcnf->snow) . ': ' . filter_xss($data->snow) . '</li>';
  }
  if ($data->tmax != 99) {
    $content .= '<li>' . filter_xss($usrcnf->tmax) . ': ' . filter_xss($data->tmax) . '</li>';
  }
  if ($data->tmin != 99) {
    $content .= '<li>' . filter_xss($usrcnf->tmin) . ': ' . filter_xss($data->tmin) . '</li>';
  }
  if ($data->win_dir1 != '') {
    $content .= '<li>' . filter_xss($usrcnf->win_dir) . ' ' . filter_xss($data->ampm) . ': ' . filter_xss($data->win_dir1) . '</li>';
  }
  if ($data->win_dir2 != '') {
    $content .= '<li>' . filter_xss($usrcnf->win_dir) . ' pm: ' . filter_xss($data->win_dir2) . '</li>';
  }
  if ($data->win_spd1 != 999) {
    $content .= '<li>' . filter_xss($usrcnf->win_spd) . ' ' . filter_xss($data->ampm) . ': ' . filter_xss($data->win_spd1) . '</li>';
  }
  if ($data->win_spd2 != 999) {
    $content .= '<li>' . filter_xss($usrcnf->win_spd) . ' pm: ' . filter_xss($data->win_spd2) . '</li>';
  }
  if ($data->iv_max != 99) {
    $content .= '<li>' . filter_xss($usrcnf->iv) . ': ' . filter_xss($data->iv_max) . '</li>';
  }
  if ($data->rsk != '') {
    $content .= '<li>' . filter_xss($usrcnf->rsk) . ': ' . filter_xss($data->rsk) . '</li>';
  }
  $content .= '</ul>';
  return $content;
}

Functions

Namesort descending Description
block_weather_es_contents Show the weather_es contents
theme_weather_es Custom theme function for the weather_es block output
weather_es_block Implementation of hook_block().
weather_es_configuration Show a configuration page for a custom weather_es block
weather_es_configuration_submit Handle the submission of the custom weather_es block.
weather_es_configuration_validate Validate the form.
weather_es_confirm_delete Confirmation page before deleting a location
weather_es_confirm_delete_submit Handle the deletion of a location
weather_es_help Implementation of hook_help().
weather_es_main Main weather_es_page
weather_es_menu Implementation of hook_menu().
weather_es_perm Implementation of hook_perm().
weather_es_save_config Save the config data
weather_es_save_data Save the data