You are here

function yr_verdata_add_form_submit in Yr Weatherdata 6.2

Same name and namespace in other branches
  1. 7.3 yr_verdata.admin.inc \yr_verdata_add_form_submit()
  2. 7 yr_verdata.admin.inc \yr_verdata_add_form_submit()

Submit handler for yr_verdata_add_form().

File

./yr_verdata.admin.inc, line 273
This file contains the functions for the admin interface for yr_verdata.

Code

function yr_verdata_add_form_submit($form, &$form_state) {
  $url = trim($form_state['values']['url']);

  // Make sure we are not using yr.no's cache, which sometimes adds numbers at the end of a url.
  $tilde = mb_strpos($url, '~');
  if ($tilde == TRUE) {
    $url = drupal_substr($url, 0, $tilde);
  }

  // Make sure we always have a trailing slash.
  if (drupal_substr($url, -1) != '/') {
    $url .= '/';
  }

  // Figure out what language this is.
  $components = explode('/', drupal_substr(rawurldecode($url), 17, -1));
  switch ($components[0]) {
    case 'place':

      // English
      $lang = 'en';
      break;
    case 'sted':

      // Norwegian Bokmål
      $lang = 'nb';
      break;
    case 'stad':

      // Norwegian Nynorsk
      $lang = 'nn';
      break;
    case 'paikka':

      // Kvääni
      $lang = 'no-kv';
      break;
    case 'sadji':

      // Sami
      $lang = 'smi';
      break;
  }
  $n = count($components) - 1;
  $file = md5($url) . '.xml';
  $name = str_replace('_', ' ', trim($components[$n]));

  // I think this is the only character we need to replace for pretty names.
  $region = str_replace('_', ' ', trim($components[2]));

  // I think this is the only character we need to replace for pretty names.
  $country = str_replace('_', ' ', trim($components[1]));

  // I think this is the only character we need to replace for pretty names.
  $query = "INSERT INTO {yr_verdata} (url, lang, file, weight, name, region, country) VALUES ('%s', '%s', '%s', %d, '%s', '%s', '%s')";
  if (db_query($query, $url, $lang, $file, $form_state['values']['weight'], $name, $region, $country)) {
    drupal_set_message(t('Location added.'));
    $id = db_last_insert_id('yr_verdata', 'yid');
    $location = new stdClass();
    $location->file = $file;
    $location->url = $url;
    $location->yid = $id;
    _yr_verdata_refresh_xml($location);

    // Flush the cache. We flush all of yr_verdata's cached stuff,
    // for simplicity. Maybe I'm just being lazy, but it's late...
    cache_clear_all('yr_verdata', 'cache_page', TRUE);
  }
  else {
    drupal_set_message(t('The location at @url could not be added to the database. If this problem persists, the administrator should be notified.', array(
      '@url' => $url,
    )));
  }
}