drupal: allow unit switching between l/min, l/day and m3/year

This commit is contained in:
Bart Van Der Meerssche 2010-07-16 15:22:41 +00:00
parent daa6e79310
commit afed061a2b
1 changed files with 71 additions and 29 deletions

View File

@ -310,7 +310,7 @@ function _logger_dashboard($type, $function, $interval) {
} }
$end = ((int) (time() / $resolution)) * $resolution; $end = ((int) (time() / $resolution)) * $resolution;
$result_me = db_query("SELECT lm.meter, lm.function, lm.unit, lu.private $result_me = db_query("SELECT lm.meter, lm.function, lu.private, lu.electricity_unit, lu.water_unit
FROM {logger_meters} lm FROM {logger_meters} lm
INNER JOIN {logger_users} lu on lm.uid = lu.uid INNER JOIN {logger_users} lu on lm.uid = lu.uid
WHERE lm.uid = %d AND lm.type = '%s' WHERE lm.uid = %d AND lm.type = '%s'
@ -329,27 +329,28 @@ function _logger_dashboard($type, $function, $interval) {
switch ($type) { switch ($type) {
case 'electricity': case 'electricity':
switch ($meter_me->unit) { switch ($meter_me->electricity_unit) {
case 'watt': case 'watt':
$unit = 'watt'; $unit = 'watt';
$factor = 3600; // 1Wh/s = 3600 W $factor = 3600; // 1Wh/s = 3600 W
break; break;
case 'kwh': case 'kwhpyear':
$unit = 'kWh/year'; $unit = 'kWh/year';
$factor = 31536; $factor = 31536;
break; break;
case 'eur': case 'eurpyear':
$unit = 'euro/year'; $unit = 'euro/year';
$factor = 5676; // 18 EURcent/kWh $factor = 5676; // 18 EURcent/kWh
break; break;
case 'aud': case 'audpyear':
$unit = 'aud/year'; $unit = 'aud/year';
$factor = 5991; // 19 AUDcent/kWh $factor = 5991; // 19 AUDcent/kWh
break; break;
} }
break;
case 'water': case 'water':
switch ($meter_me->unit) { switch ($meter_me->water_unit) {
case 'lpmin': case 'lpmin':
$unit = 'liter/min'; $unit = 'liter/min';
$factor = 6; // 1 dl/s = 6 l/min $factor = 6; // 1 dl/s = 6 l/min
@ -358,9 +359,9 @@ function _logger_dashboard($type, $function, $interval) {
$unit = 'liter/day'; $unit = 'liter/day';
$factor = 8640; // 1 dl/s = 8640 l/day $factor = 8640; // 1 dl/s = 8640 l/day
break; break;
case 'lpyear': case 'm3pyear':
$unit = 'liter/year'; $unit = 'm3/year';
$factor = 3153600; // 1 dl/s = 3153600 l/min $factor = 3154; // 1 dl/s = 3153600 l/year
break; break;
} }
} }
@ -508,10 +509,26 @@ function _logger_remove($rid) {
function _logger_unit($unit) { function _logger_unit($unit) {
global $user; global $user;
// hardcoded type and function
db_query("UPDATE {logger_meters} switch($unit) {
SET unit = '%s' case 'watt':
WHERE uid = %d AND type = '%s' AND function = '%s'", $unit, $user->uid, 'electricity', 'main'); case 'kwhpyear':
case 'eurpyear':
case 'audpyear':
db_query("UPDATE {logger_users}
SET electricity_unit = '%s'
WHERE uid = %d", $unit, $user->uid);
//watchdog('_logger_unit', '%unit | %uid', array('%unit' => $unit, '%uid' => $user->uid), WATCHDOG_DEBUG);
break;
case 'lpmin':
case 'lpday':
case 'm3pyear':
db_query("UPDATE {logger_users}
SET water_unit = '%s'
WHERE uid = %d", $unit, $user->uid);
break;
}
_logger_cache_clear('unit'); _logger_cache_clear('unit');
$destination = drupal_get_destination(); $destination = drupal_get_destination();
@ -606,7 +623,7 @@ function logger_block($op = 'list', $delta = 0, $edit = array()) {
$blocks['unit']['region'] = 'right'; $blocks['unit']['region'] = 'right';
$blocks['unit']['weight'] = 2; $blocks['unit']['weight'] = 2;
$blocks['unit']['pages'] = '<front>\nlogger/\nlogger/*'; $blocks['unit']['pages'] = '<front>\nlogger/\nlogger/*';
$blocks['unit']['cache'] = BLOCK_CACHE_PER_USER; $blocks['unit']['cache'] = BLOCK_NO_CACHE;
$blocks['posts']['info'] = t('Recent blog posts'); $blocks['posts']['info'] = t('Recent blog posts');
$blocks['posts']['status'] = TRUE; $blocks['posts']['status'] = TRUE;
@ -625,30 +642,29 @@ function logger_block($op = 'list', $delta = 0, $edit = array()) {
$block['content'] = drupal_get_form('_logger_fluksonians_form'); $block['content'] = drupal_get_form('_logger_fluksonians_form');
} }
elseif ($delta == 'unit' && user_access('logger')) { elseif ($delta == 'unit' && user_access('logger') && strrpos(' ' . $_GET['q'] . '/electricity', 'logger/electricity')) {
//hardcoded the type and function parameters for now $unit = db_result(db_query("SELECT electricity_unit
$unit = db_result(db_query("SELECT unit FROM {logger_users}
FROM {logger_meters} WHERE uid = %d", $user->uid));
WHERE uid = %d AND type = '%s' AND function = '%s'", $user->uid, 'electricity', 'main'));
$items = array(); $items = array();
$items[0] = l('watt', 'logger/unit/watt', array('attributes' => array('title' => "switch to watt"), 'query' => $destination, 'alias' => TRUE)); $items[0] = l('watt', 'logger/unit/watt', array('attributes' => array('title' => "switch to watt"), 'query' => $destination, 'alias' => TRUE));
$items[1] = l('kWh/year', 'logger/unit/kwh', array('attributes' => array('title' => "switch to kWh/year"), 'query' => $destination, 'alias' => TRUE)); $items[1] = l('kWh/year', 'logger/unit/kwhpyear', array('attributes' => array('title' => "switch to kWh/year"), 'query' => $destination, 'alias' => TRUE));
$items[2] = l('euro/year [@ 0.18eur/kWh]', 'logger/unit/eur', array('attributes' => array('title' => "switch to euro/year"), 'query' => $destination, 'alias' => TRUE)); $items[2] = l('euro/year [@ 0.18eur/kWh]', 'logger/unit/eurpyear', array('attributes' => array('title' => "switch to euro/year"), 'query' => $destination, 'alias' => TRUE));
$items[3] = l('aud/year [@ 0.19aud/kWh]', 'logger/unit/aud', array('attributes' => array('title' => "switch to aud/year"), 'query' => $destination, 'alias' => TRUE)); $items[3] = l('aud/year [@ 0.19aud/kWh]', 'logger/unit/audpyear', array('attributes' => array('title' => "switch to aud/year"), 'query' => $destination, 'alias' => TRUE));
// 'unlink' the currently selected unit option // 'unlink' the currently selected unit option
switch ($unit) { switch ($unit) {
case 'watt': case 'watt':
$items[0] = 'watt'; $items[0] = 'watt';
break; break;
case 'kwh': case 'kwhpyear':
$items[1] = 'kWh/year'; $items[1] = 'kWh/year';
break; break;
case 'eur': case 'eurpyear':
$items[2] = 'euro/year [@ 0.18eur/kWh]'; $items[2] = 'euro/year [@ 0.18eur/kWh]';
break; break;
case 'aud': case 'audpyear':
$items[3] = 'aud/year [@ 0.19aud/kWh]'; $items[3] = 'aud/year [@ 0.19aud/kWh]';
break; break;
} }
@ -656,6 +672,32 @@ function logger_block($op = 'list', $delta = 0, $edit = array()) {
$block['content'] = theme('logger_item_list', $items); $block['content'] = theme('logger_item_list', $items);
} }
elseif ($delta == 'unit' && user_access('logger') && strrpos(' ' . $_GET['q'] . '/electricity', 'logger/water')) {
$unit = db_result(db_query("SELECT water_unit
FROM {logger_users}
WHERE uid = %d", $user->uid));
$items = array();
$items[0] = l('l/min', 'logger/unit/lpmin', array('attributes' => array('title' => "switch to l/min"), 'query' => $destination, 'alias' => TRUE));
$items[1] = l('l/day', 'logger/unit/lpday', array('attributes' => array('title' => "switch to l/day"), 'query' => $destination, 'alias' => TRUE));
$items[2] = l('m3/year', 'logger/unit/m3pyear', array('attributes' => array('title' => "switch to m3/year"), 'query' => $destination, 'alias' => TRUE));
// 'unlink' the currently selected unit option
switch ($unit) {
case 'lpmin':
$items[0] = 'l/min';
break;
case 'lpday':
$items[1] = 'l/day';
break;
case 'm3pyear':
$items[2] = 'm3/year';
break;
}
$block['subject'] = t('Unit');
$block['content'] = theme('logger_item_list', $items);
}
elseif ($delta == 'posts' && user_access('access content')) { elseif ($delta == 'posts' && user_access('access content')) {
$result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = 'blogpost' AND n.status = 1 ORDER BY n.created DESC"), 0, 5); $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = 'blogpost' AND n.status = 1 ORDER BY n.created DESC"), 0, 5);
if ($node_title_list = node_title_list($result)) { if ($node_title_list = node_title_list($result)) {