You are here

function weather_es_form_submit in Weather_es 6.3

Same name and namespace in other branches
  1. 7 weather_es.module \weather_es_form_submit()

@author jmsirvent

1 string reference to 'weather_es_form_submit'
weather_es_edit_form in ./weather_es.module
@author jmsirvent

File

./weather_es.module, line 264

Code

function weather_es_form_submit($form, &$form_state) {

  // There's only a AHAH sent...
  if (!empty($form_state['ahah_submission'])) {
    $form_state['rebuild'] = FALSE;
    return;
  }

  // Get data from AEMET
  $aemet = new weather_es_Aemet($form_state['values']['combo']['ciudad']);
  if ($aemet
    ->getName() != '') {

    // Save a new user configuration or rewrite an existing one

    //if ($_POST['op'] == t('Save configuration')) {
    if ($form_state['clicked_button']['#value'] == t('Save configuration')) {

      // Delete previous user configuration
      $sql = "DELETE FROM {weather_es_conf} WHERE wid = %d";
      db_query($sql, $form_state['storage']['wid']);
    }

    // Avoid add a city already in the user configuration
    if (!db_result(db_query("SELECT COUNT(*) FROM {weather_es_conf} WHERE wid = %d AND cit_cod = %d", $form_state['storage']['wid'], $form_state['values']['combo']['ciudad']))) {

      // Save configuration data
      // TODO full info

      //_weather_es_conf_save($form_state['storage']['wid'], $form_state['values']['combo']['ciudad'], $form_state['values']['idioma'], serialize(array_filter($form_state['values']['informacion'])), $form_state['values']['cantidad']);
      _weather_es_conf_save($form_state['storage']['wid'], $form_state['values']['combo']['ciudad'], $form_state['values']['idioma'], serialize(array_filter($form_state['values']['informacion'])), 're');
    }
    else {

      // Change an old configuration
      // TODO full info

      //_weather_es_conf_update($form_state['storage']['wid'], $form_state['values']['combo']['ciudad'], $form_state['values']['idioma'], $form_state['values']['informacion'], $form_state['values']['cantidad']);
      _weather_es_conf_update($form_state['storage']['wid'], $form_state['values']['combo']['ciudad'], $form_state['values']['idioma'], $form_state['values']['informacion'], 're');
    }

    // Delete the previous city data and save the new one
    db_query("DELETE FROM {weather_es_data} WHERE cit_cod = %d", $form_state['values']['combo']['ciudad']);
    $cities = _weather_es_cities($form_state['values']['combo']['provincia']);
    _weather_es_data_save($form_state['values']['combo']['ciudad'], $cities[$form_state['values']['combo']['ciudad']], serialize($aemet));
    drupal_set_message(t('Config save for: %city.', array(
      '%city' => $cities[$form_state['values']['combo']['ciudad']],
    )));
  }
  else {

    // The connection with AEMET has failt, try in 10'
    db_query("UPDATE {weather_es_data} SET nex_upd = %d WHERE cit_cod = %d", time() + 10 * 60, $form_state['values']['combo']['ciudad']);
    drupal_set_message(t('The connection with AEMET has failt.'));
  }

  // Redirect the user to the table. $form['#redirect'] and $form_state['redirect'] don't work...
  drupal_goto(_weather_es_redirect($form_state['storage']['wid']));
}