You are here

function weather_es_configuration_form_submit in Weather_es 6.2

Handle the submission of the custom weather_es block.

File

./weather_es.module, line 282
Shows weather data of Spain (by AEMET)

Code

function weather_es_configuration_form_submit($form, &$form_state) {

  // Wait until last step

  /*
  if ($form_state['values']['step'] < 2) {
    //$form_state['rebuild'] = TRUE;

    return;
  }
  */
  unset($form_state['rebuild']);
  $cod_pro = substr($form_state['values']['poblacion']['seleccionada'], 0, 2);
  $cod_loc = substr($form_state['values']['poblacion']['seleccionada'], 2, 4);

  //$cit_nam = $localidades[$form_state['values']['poblacion']['seleccionada']];
  $localidades = _weather_es_cities($cod_pro);

  // Get data from AEMET.
  $aemet = _weather_es_aemet($form_state['values']['wuid'], $form_state['values']['poblacion']['seleccionada'], $form_state['values']['idioma'], $localidades[$form_state['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_state['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($form_state['values']['wuid'], $cod_pro, $cod_loc, $localidades[$form_state['values']['poblacion']['seleccionada']], $form_state['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_state['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, $form_state['values']['wuid'], $cod_pro, $cod_loc);
    drupal_set_message(t('The connection with AEMET has failt.'));
  }

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