Changeset 72


Ignore:
Timestamp:
19/11/06 05:50:04 (6 years ago)
Author:
nigel
Message:

Editing of entries

File:
1 edited

Legend:

Unmodified
Added
Removed
  • meter/meter.module

    r71 r72  
    135135 
    136136function meter_insert($node) { 
    137   print "<p>meter_insert</p>"; 
    138137  /* 
    139138  if (!user_access('administer nodes')) { 
     
    186185          'weight' => 3, 
    187186          'type' => MENU_LOCAL_TASK); 
    188  
    189  
     187      $items[] = array('path' => 'node/'. arg(1) .'/edit_readings', 
     188          'title' => t('edit readings'), 
     189          'callback' => 'meter_edit_readings', 
     190          'access' => user_access('access content'), 
     191          'weight' => 3, 
     192          'type' => MENU_LOCAL_TASK); 
     193      $items[] = array('path' => 'node/'. arg(1) .'/edit_reading', 
     194          'title' => t('edit reading'), 
     195          'callback' => 'meter_edit_reading', 
     196          'access' => user_access('access content'), 
     197          'weight' => 3, 
     198          'type' => MENU_CALLBACK); 
    190199    } 
    191200  } 
     
    286295 
    287296  $links = array(); 
    288   //$links[] = l(t('add reading'), 'node/'.$node->nid.'/add_reading',array('title'=>'Add a meter reading')); 
     297  $links[] = l(t('edit readings'), 'node/'.$node->nid.'/edit_readings',array('title'=>'Edit meter readings')); 
    289298  $links[] = l(t('export readings'), 'node/'.$node->nid.'/csv_readings',array('title'=>'Export the meter readings as CSV')); 
    290299 
     
    316325 
    317326  $res = meter_extrapolate(meter_get_values($node), 1,30); 
    318   /*foreach ($res as $val){ 
    319     $output .= "<p>$val->to $val->value </p>"; 
    320   }*/ 
    321327 
    322328  $output .= meter_graph($res); 
    323   $output .= drupal_get_form('meter_add_reading', $form); 
    324329 
    325330  $node->links = $links; 
    326   $node->body = $node->teaser = $output; 
    327 } 
     331  // Only show the add readings form when in full view 
     332  $node->teaser = $output; 
     333  $node->body = $output.drupal_get_form('meter_add_reading', $form); 
     334} 
     335 
     336/** 
     337  * 
     338  */ 
     339function meter_edit_readings() { 
     340  $nid = arg(1); 
     341  $node = node_load($nid); 
     342 
     343  if ($_GET['action'] == 'delete'){ 
     344    db_query("DELETE FROM {meter_readings} WHERE nid=%d AND time='%s';", $nid, $_GET['time']); 
     345    drupal_set_message(t('Reading removed.')); 
     346  } 
     347 
     348  $headers = array( 
     349    array('data'=>t('Date')), 
     350    array('data'=>t('Reading')), 
     351    array('data'=>t('Comment')), 
     352    array('data'=>t('Edit')), 
     353    array('data'=>t('Delete')), 
     354    ); 
     355 
     356  $rows = array(); 
     357  $rows_ = meter_get_values($node); 
     358  foreach ($rows_ as $r){ 
     359    $rows[] = array( 
     360        $r->time, 
     361        $r->value, 
     362        $r->comment, 
     363        l(t('edit'), "node/$nid/edit_reading", array(), 'time='.urlencode($r->time)), 
     364        l(t('delete'), "node/$nid/edit_readings", array(), 'action=delete&time='.urlencode($r->time)), 
     365        ); 
     366  } 
     367 
     368  $output .= theme("table", $headers, $rows); 
     369  return $output; 
     370} 
     371 
     372/** 
     373  * Edit the reading 
     374  */ 
     375function meter_edit_reading() { 
     376  $nid = arg(1); 
     377  $node = node_load($nid); 
     378  $edit = $_REQUEST['edit']; 
     379 
     380  if ($_REQUEST['op'] == t('Save')){ 
     381    db_query("UPDATE {meter_readings} SET time='%s', value=%d, comment='%s' WHERE nid=%d AND time='%s';", $edit['time'], $edit['value'], $edit['comment'], $edit['nid'], $edit['orig_time']); 
     382    drupal_set_message("Saved reading."); 
     383    drupal_goto("node/$nid/edit_readings"); 
     384  } else if ($_REQUEST['op'] == t('Cancel')){ 
     385    drupal_set_message("Cancelled editing."); 
     386    drupal_goto("node/$nid/edit_readings"); 
     387  } 
     388 
     389  $r = db_query("SELECT * FROM {meter_readings} WHERE nid=%d AND time='%s';", $nid, $_GET['time']); 
     390  $r = db_fetch_object($r); 
     391 
     392  $form['nid'] = array('#type' => 'hidden', '#value' => $node->nid); 
     393  $form['orig_time'] = array('#type' => 'hidden', '#value' => $r->time); 
     394  $form['time'] = array( 
     395            '#title' => t('Date'), 
     396            '#type' => 'textfield', 
     397            '#default_value' => $r->time, 
     398            '#attributes' => array('class' => 'jscalendar'), 
     399            '#jscalendar_ifFormat' => '%Y-%m-%d %H:%M', 
     400            '#jscalendar_showsTime' => 'true', 
     401            '#jscalendar_timeFormat' => variable_get('event_ampm', '0') == 0 ? '24' : '12', 
     402            '#size' => 19, 
     403            '#maxlength' => 19, 
     404            '#weight' => -14, 
     405            '#description' => t('YYYY-MM-DD HH:MM'), 
     406            ); 
     407 
     408  $form['value'] = array( 
     409        '#title' => t('Value'), 
     410        '#type' => 'textfield', 
     411        '#default_value' => $r->value); 
     412 
     413  $form['comment'] = array( 
     414        '#title' => t('Comment'), 
     415        '#type' => 'textfield', 
     416        '#default_value' => $r->comment); 
     417 
     418  $form['action_save'] = array('#type' => 'submit', '#value' => t('Save')); 
     419  $form['action_cancel'] = array('#type' => 'submit', '#value' => t('Cancel')); 
     420  $form['#action'] = url('node/'. $node->nid.'/edit_reading'); 
     421 
     422  return drupal_get_form('meter_edit_reading', $form); 
     423} 
     424 
    328425 
    329426/** 
    330427  * Export the meter readings as a CSV file 
    331428  */ 
    332 function meter_csv_readings(&$node, $teaser = FALSE, $page = FALSE, $block = FALSE) { 
     429function meter_csv_readings() { 
    333430  $nid = arg(1); 
     431  $node = node_load($nid); 
    334432  $results = meter_get_values($node); 
    335433  drupal_set_header('Content-Type: text/csv; charset=utf-8'); 
     
    339437    print '"'.$row->time.'",'.$row->value."\n"; 
    340438  } 
    341  
    342439} 
    343440 
     
    412509} 
    413510 
     511 
     512/** 
     513  * Draw a CSS/HTML graph 
     514  */ 
    414515function meter_graph($entries){ 
    415516  if (count($entries) == 0) 
Note: See TracChangeset for help on using the changeset viewer.