You are here

function weather_es_configuration_submit in Weather_es 5

Same name and namespace in other branches
  1. 6 weather_es.module \weather_es_configuration_submit()

Handle the submission of the custom weather_es block.

File

./weather_es.module, line 282
Non-displayable characters.

Code

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);
  $cit_nam = $localidades[$form_values['poblacion']['seleccionada']];
  $localidades = _weather_es_cities($cod_pro);

  // Get data from AEMET.
  $aemet = _weather_es_aemet($form_values['wuid'], $form_values['poblacion']['seleccionada'], $form_values['idioma'], $localidades[$form_values['poblacion']['seleccionada']]);
  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);
}