function yr_verdata_add_form_submit in Yr Weatherdata 7.3
Same name and namespace in other branches
- 6.2 yr_verdata.admin.inc \yr_verdata_add_form_submit()
- 7 yr_verdata.admin.inc \yr_verdata_add_form_submit()
Submit handler for yr_verdata_add_form().
File
- ./
yr_verdata.admin.inc, line 115 - 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;
$query = db_insert('yr_verdata')
->fields(array(
'url' => $url,
'lang' => $lang,
'file' => md5($url) . '.xml',
'weight' => $form_state['values']['weight'],
'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])),
));
if ($id = $query
->execute()) {
drupal_set_message(t('Location added.'));
$location = new stdClass();
$location->file = md5($url) . '.xml';
$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', 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,
)));
}
}