yr_verdata.admin.inc in Yr Weatherdata 7.3
Same filename and directory in other branches
This file contains the functions for the admin interface for yr_verdata.
File
yr_verdata.admin.incView source
<?php
/**
* @file
* This file contains the functions for the admin interface for yr_verdata.
*/
/**
* Administrative settings for yr_verdata.
*
* @return
* Returns a form array to be processed by drupal_get_form().
*/
function yr_verdata_settings($form, &$form_state) {
// @todo Lage config skjema.
$form = array();
return system_settings_form($form);
}
/**
* Submit handler for yr_verdata_settings().
*/
function yr_verdata_settings_submit($form, &$form_state) {
foreach ($form_state['values'] as $key => $value) {
if (drupal_substr($key, 0, 10) == 'yr_verdata') {
variable_set($key, $value);
}
// Remove any multiblocks, if that feature was disabled.
if ($key == 'yr_verdata_multiblocks' && $value == 'off') {
$result = db_query("SELECT * FROM {yr_verdata} ORDER BY name ASC");
$records = $result
->fetchAll();
foreach ($records as $record) {
db_update('block')
->fields(array(
'status' => 0,
))
->condition('module', 'yr_verdata')
->condition('delta', 'yr_verdata_block_' . $record->yid)
->execute();
}
}
// Remove the randomblock, if that feature was disabled.
if ($key == 'yr_verdata_randomblock' && $value == 'off') {
db_update('block')
->fields(array(
'status' => 0,
))
->condition('module', 'yr_verdata')
->condition('delta', 'yr_verdata_randomblock')
->execute();
}
}
drupal_set_message(t('Settings saved.'));
// And since we want changes to show up immediately, we clear the cache.
cache_clear_all('yr_verdata', 'cache', TRUE);
}
/**
* Function for adding locations to the database table.
*/
function yr_verdata_add_form() {
$form['yvo'] = array(
'#type' => 'fieldset',
'#title' => t('Add location by URL'),
);
$form['yvo']['url'] = array(
'#type' => 'textfield',
'#title' => t('URL'),
'#description' => t('The base URL to the forecast for this location. For example !example_url.', array(
'!example_url' => l(t('http://www.yr.no/sted/Norge/Vest-Agder/Kristiansand/Kristiansand/'), 'http://www.yr.no/sted/Norge/Vest-Agder/Kristiansand/Kristiansand/'),
)),
'#size' => 60,
'#required' => TRUE,
);
$form['yvo']['weight'] = array(
'#type' => 'select',
'#title' => t('Weight'),
'#options' => drupal_map_assoc(array(
1,
2,
3,
4,
5,
6,
7,
8,
9,
)),
'#default_value' => 5,
'#description' => t('Lower values will be displayed first in listings, tables and blocks. Note: This depends on the sorting settings in the !conf page.', array(
'!conf' => l(t('configuration'), 'admin/config/services/yr_verdata'),
)),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add location'),
);
drupal_set_breadcrumb(array(
l(t('Home'), '<front>'),
l(t('Forecast'), 'forecast'),
));
return $form;
}
/**
* Validation handler for yr_verdata_add_form().
*/
function yr_verdata_add_form_validate($form, &$form_state) {
// Verify that the url entered is actually for yr.no.
$start = drupal_substr($form_state['values']['url'], 0, 17);
if ($start !== 'http://www.yr.no/') {
form_set_error('url', t('The url must be a valid yr.no address.'));
}
// Check if the location already exists.
$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 .= '/';
}
// Make sure we don't add a location twice if it's added from different browsers.
// Some browsers automatically urlencode urls if you copy them from the address field.
$url = rawurldecode($url);
$result = db_query("SELECT yid FROM {yr_verdata} WHERE url = :url", array(
':url' => $url,
));
if ($result
->fetch() == TRUE) {
form_set_error('url', t('The location already exists in the database.'));
}
}
/**
* Submit handler for yr_verdata_add_form().
*/
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,
)));
}
}
/**
* Function for deleting locations from the database table.
*
* @param $yid
* The unique id of the location.
* @return
* Returns a confirmation form for deleting the given location.
*/
function yr_verdata_delete_confirm($form, &$form_state, $yid) {
$location = yr_verdata_load_location($yid, TRUE);
if ($location['status'] == TRUE) {
$form = array();
$form['yid'] = array(
'#type' => 'hidden',
'#value' => $yid,
);
$form['filename'] = array(
'#type' => 'hidden',
'#value' => $location['data']->filepath,
);
$question = t('Are you sure you want to remove the location %location, ID %yid from the database?', array(
'%location' => $location['data']->name,
'%yid' => $location['data']->yid,
));
$description = t('The location and all related data will be removed from your site. You can re-add it later at !add.', array(
'!add' => l('forecast/add', 'forecast/add'),
));
$yes = t('Remove');
return confirm_form($form, $question, 'forecast', $description, $yes);
}
else {
$msg = t('No location found with the given ID.');
drupal_set_message($msg, 'error');
drupal_goto('forecast');
}
}
/**
* Submit handler for the delete location form.
*/
function yr_verdata_delete_confirm_submit($form, &$form_state) {
// The user wanted this location gone. Delete it from the database.
$query = db_delete('yr_verdata')
->condition('yid', $form_state['values']['yid'])
->execute();
// Clean out the stored xml file.
file_unmanaged_delete($form_state['values']['filename']);
// 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);
// Redirect.
drupal_set_message(t('The location was deleted.'));
drupal_goto('forecast');
}
Functions
Name![]() |
Description |
---|---|
yr_verdata_add_form | Function for adding locations to the database table. |
yr_verdata_add_form_submit | Submit handler for yr_verdata_add_form(). |
yr_verdata_add_form_validate | Validation handler for yr_verdata_add_form(). |
yr_verdata_delete_confirm | Function for deleting locations from the database table. |
yr_verdata_delete_confirm_submit | Submit handler for the delete location form. |
yr_verdata_settings | Administrative settings for yr_verdata. |
yr_verdata_settings_submit | Submit handler for yr_verdata_settings(). |