| 1 |
<?php
|
| 2 |
/*
|
| 3 |
* $Id: carbon_view.inc,v 1.21 2009/05/05 13:13:44 johnackers Exp $
|
| 4 |
*
|
| 5 |
* Created on 19-Feb-2008
|
| 6 |
*
|
| 7 |
*
|
| 8 |
* Functions in this file create the first tabbed pane that
|
| 9 |
* shows the carbon _account status.
|
| 10 |
*
|
| 11 |
* Javascript to handle client side form modification
|
| 12 |
* It relies on JQuery which is now part of the drupal core
|
| 13 |
* and carbon_debug.js;
|
| 14 |
*/
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
/**
|
| 19 |
* Menu entry point to display different pages
|
| 20 |
*
|
| 21 |
* @param $account
|
| 22 |
* @param $pagename 'meter' | 'travel'
|
| 23 |
* @return unknown_type
|
| 24 |
*/
|
| 25 |
|
| 26 |
|
| 27 |
function carbon_view($account, $pagename)
|
| 28 |
{
|
| 29 |
// load the basic javascript routines
|
| 30 |
$path = drupal_get_path("module", 'carbon');
|
| 31 |
// submit a form via ajax
|
| 32 |
drupal_add_js('misc/jquery.form.js', 'core', 'header', FALSE, TRUE);
|
| 33 |
$path = carbon_include_core_js(); // need the .css
|
| 34 |
drupal_add_js($path . "/carbon_debug.js", "module");
|
| 35 |
drupal_add_js($path . "/carbon_view.js", "module");
|
| 36 |
|
| 37 |
// should be removed by javascript
|
| 38 |
$output = "<div id='nojavascript'>Javascript not working or not enabled.</div>";
|
| 39 |
|
| 40 |
// are we previewing a new account, then don't try to recall account activity
|
| 41 |
if (empty($account->nid)) return $output ;
|
| 42 |
|
| 43 |
require_once(CARBON_PATH ."/carbon_chart.inc"); // generate bar and pie PNGs
|
| 44 |
require_once(CARBON_PATH ."/carbon_calculate.inc");// co2 & kWh settings
|
| 45 |
|
| 46 |
switch ($pagename)
|
| 47 |
{
|
| 48 |
|
| 49 |
case 'meter' :
|
| 50 |
case 'travel' :
|
| 51 |
$output .= carbon_view_stamp_one($account, $tablename = $pagename, null);
|
| 52 |
break ;
|
| 53 |
|
| 54 |
default :
|
| 55 |
$output = _view_historic_chart($account, null, null);
|
| 56 |
$output .= _buildPeriodPanels($account);
|
| 57 |
if (variable_get("carbon_enable_crag_functions", 0))
|
| 58 |
$output .= theme("carbon_loadable_box", "Carbon Trades", "?q=carbon/".$account->nid."/transfer");
|
| 59 |
break ;
|
| 60 |
}
|
| 61 |
return $output ;
|
| 62 |
}
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
//http://chart.apis.google.com/chart?chs=450x150&chd=t:10,20,30|10,9,30|178,87,90&cht=bhs&chl=Hello|World|basic&chco=ff0000,00ff00,0000ff&chds=5,500&chdl=heating|elec|gas&chxp=alison|doug|chris
|
| 67 |
/**
|
| 68 |
* Render an image showing multiple carbon footprints
|
| 69 |
*
|
| 70 |
* Note that the param $uid is the user we are focused on, not the user doing
|
| 71 |
* the browsing.
|
| 72 |
*
|
| 73 |
* @param $account_map an array of $nid => $footprint
|
| 74 |
* @param $uid select by uid; set to zero to retrieve footprints from all users
|
| 75 |
* @param $org select by organisation; set to '' or null to retrieve footprints
|
| 76 |
* from users in any group.
|
| 77 |
*/
|
| 78 |
|
| 79 |
function _view_historic_chart($account, $uid, $org){
|
| 80 |
global $user;
|
| 81 |
// for each item in the map, change the key and value to title and amount ;
|
| 82 |
$footprint_by_year = array();
|
| 83 |
|
| 84 |
$stamps = carbon_stamp_array_load($account->nid, $uid, $org);
|
| 85 |
|
| 86 |
$dateRange = _stamp_date_range($stamps);
|
| 87 |
|
| 88 |
$reportDates = carbon_calculate_reporting_dates($dateRange, $account->firstdate, $account->period);
|
| 89 |
|
| 90 |
for ($i = count($reportDates) - 2 ; $i >= 0 ; $i--)
|
| 91 |
{
|
| 92 |
$results = carbon_calculate_total($account, $stamps, $reportDates[$i], $reportDates[$i+1]);
|
| 93 |
|
| 94 |
$firstdate_s = CarbonDate::asYYYYMMDD($reportDates[$i]);
|
| 95 |
$footprint_by_year[]
|
| 96 |
= array("data" => $results->sector_co2,
|
| 97 |
"comment" => _format_date_range($reportDates[$i], $reportDates[$i+1]));
|
| 98 |
}
|
| 99 |
if (carbon_chart_requested())
|
| 100 |
return carbon_chart_generate($footprint_by_year, variable_get("carbon_units", "Kg of C02"));
|
| 101 |
|
| 102 |
return count($footprint_by_year)==0 ? "" : theme("carbon_static_box", "Chart", carbon_chart_embed("bar", null, null));
|
| 103 |
}
|
| 104 |
|
| 105 |
|
| 106 |
/**
|
| 107 |
* Add a link for every accounting period for which there is carbon data
|
| 108 |
* @param $aid
|
| 109 |
* @return unknown_type
|
| 110 |
*/
|
| 111 |
function _buildPeriodPanels($account)
|
| 112 |
{
|
| 113 |
$account->stamp = carbon_stamp_array_load($account->nid, null, null);
|
| 114 |
|
| 115 |
if (count($account->stamp) == 0)
|
| 116 |
return carbon_calculate_empty_table($account);
|
| 117 |
|
| 118 |
// find out the range of dates that we are dealing with
|
| 119 |
$dateRange = _stamp_date_range($account->stamp);
|
| 120 |
|
| 121 |
// then work out the dates for each footprint
|
| 122 |
$reportDates = carbon_calculate_reporting_dates($dateRange, $account->firstdate, $account->period);
|
| 123 |
|
| 124 |
$header = carbon_stamp_header();
|
| 125 |
|
| 126 |
for ($i = count($reportDates) - 2 ; $i >= 0 ; $i--)
|
| 127 |
{
|
| 128 |
$firstdate_s = CarbonDate::asYYYYMMDD($reportDates[$i]);
|
| 129 |
|
| 130 |
$annual_report = "" ;
|
| 131 |
|
| 132 |
$sector_q = sprintf("q=carbon/report/&report=sector&firstdate=%s&period=%d&org=%s",
|
| 133 |
$firstdate_s,$account->period,$account->organization);
|
| 134 |
|
| 135 |
$results = carbon_calculate_total($account, $account->stamp, $reportDates[$i], $reportDates[$i+1]);
|
| 136 |
|
| 137 |
$annual_report .= carbon_calculate_present_results($results);
|
| 138 |
|
| 139 |
if (isset($account->organization))
|
| 140 |
{
|
| 141 |
$annual_report .= theme("carbon_loadable_box",
|
| 142 |
$account->organization . " sector report", url("", array("query"=>$sector_q)));
|
| 143 |
}
|
| 144 |
$output .= theme("carbon_static_box",
|
| 145 |
t("Emissions from "). _format_date_range($reportDates[$i], $reportDates[$i+1]), $annual_report);
|
| 146 |
|
| 147 |
}
|
| 148 |
return $output ;
|
| 149 |
}
|
| 150 |
|
| 151 |
|
| 152 |
/**
|
| 153 |
*
|
| 154 |
* @param $account
|
| 155 |
* @param $tablename
|
| 156 |
* @param $nid if specified, restrict table 1 stamp presented in 1 row
|
| 157 |
* @return html
|
| 158 |
*/
|
| 159 |
|
| 160 |
|
| 161 |
function carbon_view_stamp_one($account, $tablename, $nid, $singleRow = "")
|
| 162 |
{
|
| 163 |
$meterPage = $tablename == "meter" ? true : false ;
|
| 164 |
|
| 165 |
$sql = carbon_stamp_query($account->nid, null, $nid, $meterPage);
|
| 166 |
$get_header = "_get_".$tablename."_header";
|
| 167 |
$sql .= tablesort_sql($header = $get_header());
|
| 168 |
|
| 169 |
// get the links (no range limit here)
|
| 170 |
$queryResult = db_query($sql);
|
| 171 |
|
| 172 |
$stamps = array();
|
| 173 |
|
| 174 |
$titles = array();
|
| 175 |
while ($node = db_fetch_object($queryResult))
|
| 176 |
{
|
| 177 |
$node->protection = _log2($node->protect2);
|
| 178 |
$ro_access = carbon_access("view", $node);
|
| 179 |
if ($ro_access)
|
| 180 |
{
|
| 181 |
$stamps[] = $node ;
|
| 182 |
}
|
| 183 |
}
|
| 184 |
|
| 185 |
$output = $meterPage ? _meter_create_table($account, $tablename, $header, $stamps, $singleRow)
|
| 186 |
: _stamp_create_table($account, $tablename, $header, $stamps, $singleRow);
|
| 187 |
|
| 188 |
return $output ;
|
| 189 |
}
|
| 190 |
|
| 191 |
/**
|
| 192 |
* return html for table of meter readings
|
| 193 |
*
|
| 194 |
* @param $stamps array of meter readings
|
| 195 |
* @param $titles array of titles; each represents a a different meter
|
| 196 |
* @return html
|
| 197 |
*/
|
| 198 |
|
| 199 |
|
| 200 |
function _meter_create_table($account, $tablename, $header, $stamps, $singleRow)
|
| 201 |
{
|
| 202 |
$output = '' ;
|
| 203 |
$header = _get_meter_header();
|
| 204 |
|
| 205 |
$titles = array();
|
| 206 |
foreach ($stamps as $stamp)
|
| 207 |
$titles[$stamp->title] = "";
|
| 208 |
|
| 209 |
$unique_title = array_keys($titles); // establish the meter names
|
| 210 |
|
| 211 |
// for each unique meter name
|
| 212 |
$output = "" ;
|
| 213 |
foreach ($unique_title as $title)
|
| 214 |
{
|
| 215 |
$selectedStamps = array();
|
| 216 |
foreach($stamps as $stamp)
|
| 217 |
{
|
| 218 |
if ($stamp->title == $title)
|
| 219 |
$selectedStamps[] = $stamp ;
|
| 220 |
}
|
| 221 |
$meter_table = _stamp_create_table($account, $tablename, $header, $selectedStamps, $singleRow);
|
| 222 |
$output .= theme("carbon_static_box", $title, $meter_table);
|
| 223 |
}
|
| 224 |
|
| 225 |
$attributes = array('title'=>'Add first reading for a meter not listed above. ',
|
| 226 |
'class'=>'new-stamp');
|
| 227 |
$meter_table = l("New meter", "node/add/carbon-stamp",
|
| 228 |
$options = array('attributes'=>$attributes,
|
| 229 |
'query' => 'destination=node/'.$account->nid.'/meter&scope=1',
|
| 230 |
'html'=>true))."<br/>" ;
|
| 231 |
|
| 232 |
$output .= theme("carbon_static_box", "", $meter_table, $class='not-collapsible');
|
| 233 |
|
| 234 |
return $output ;
|
| 235 |
}
|
| 236 |
|
| 237 |
|
| 238 |
/**
|
| 239 |
* return html for table of stamps (NOT meters)
|
| 240 |
*
|
| 241 |
* @param $stamps array of stamps
|
| 242 |
* @return html
|
| 243 |
*/
|
| 244 |
|
| 245 |
|
| 246 |
function _stamp_create_table($account, $pagename, $header, $stamps, $singleRow)
|
| 247 |
{
|
| 248 |
$rows = array() ; // updated when form created
|
| 249 |
foreach ($stamps as $stamp)
|
| 250 |
{
|
| 251 |
// we have to format each cell because there may be embedded links.
|
| 252 |
|
| 253 |
$cols = _format_stamp_cells($header, $stamp, $account, $class = $pagename);
|
| 254 |
$rows[] = array("data" => $cols, 'id'=> $stamp->nid, 'class'=> empty($singleRow) ? 'editable' : $singleRow) ;
|
| 255 |
$lastStamp = $stamp ;
|
| 256 |
}
|
| 257 |
|
| 258 |
if (empty($singleRow))
|
| 259 |
{
|
| 260 |
$cols = array(); $width = count($header);
|
| 261 |
for ($i = 0 ; $i < $width - 1 ; $i++)
|
| 262 |
$cols[] = "" ;
|
| 263 |
$cols[] = carbon_access("update", $account) ?
|
| 264 |
_format_link_insitu_add($account, $pagename, $lastStamp) : "" ;
|
| 265 |
$rows[] = array("data" => $cols, 'id'=> 0, 'class'=>'inSituEdit', 'class'=>'addable');
|
| 266 |
}
|
| 267 |
$output = theme("table_inside_form", $singleRow ? array() : $header, $rows);
|
| 268 |
|
| 269 |
return $output ;
|
| 270 |
}
|
| 271 |
|
| 272 |
/**
|
| 273 |
*
|
| 274 |
* @param $header defines each column for the desired view
|
| 275 |
* @param $stamp single stamp
|
| 276 |
* @param $account $account->nid needed for links
|
| 277 |
* @param $class
|
| 278 |
* @return unknown_type
|
| 279 |
*/
|
| 280 |
|
| 281 |
function _format_stamp_cells($header, $stamp, $account, $class )
|
| 282 |
{
|
| 283 |
$cols = array();
|
| 284 |
foreach ($header as $h)
|
| 285 |
{
|
| 286 |
switch($col = empty($h['col']) ? $h['field'] : $h['col'])
|
| 287 |
{
|
| 288 |
case 'title' : $cols[] = carbon_link_from_title($stamp, $account, ""); break ;
|
| 289 |
case 'startdate' :$cols[] = _format_date($stamp->startdate); break ;
|
| 290 |
case 'enddate' : $cols[] = _format_date($stamp->enddate); break ;
|
| 291 |
case 'datetime': $cols[] = _format_date_time($stamp->enddate); break ;
|
| 292 |
case 'reading' : $cols[] = $stamp->reading; break ; // right adjust
|
| 293 |
case 'code' : $cols[] = $stamp->code; break ;
|
| 294 |
/*
|
| 295 |
if (isset($emission->units))
|
| 296 |
$units = is_numeric($emission->units) ? sprintf("%.0f",$emission->units) : $emission->units ;
|
| 297 |
else
|
| 298 |
$units = $emission->end->amount ; // is this line used?
|
| 299 |
|
| 300 |
$model = $show_model ? $emission->end->sourcemodel."/" : "" ;
|
| 301 |
$units_col = isset($units) ? $units." ".$emission->end->units." of " : "" ;
|
| 302 |
|
| 303 |
$units_col .= l($model.$emission->end->sourcetitle, 'node/'.$emission->end->sourceid);
|
| 304 |
*/
|
| 305 |
case 'adjustment':$cols[] = $stamp->adjustment . " ". ( $stamp->scope == 2 ? "R" : "" ) ; break ;
|
| 306 |
case 'status' : $cols[] = ($stamp->status >= 1 ? 'pub ' : ' ') . $stamp->protection ; break ;
|
| 307 |
case 'operations':$cols[] = carbon_access("update", $account) ?
|
| 308 |
_format_link_insitu_edit($account, $class, $stamp) : "" ; break ;
|
| 309 |
default : $cols[] = $f." ?" ; break ;
|
| 310 |
}
|
| 311 |
}
|
| 312 |
return $cols ;
|
| 313 |
}
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
function _get_meter_header()
|
| 318 |
{
|
| 319 |
global $carbon_meter_header ; // create once
|
| 320 |
if (!isset($carbon_meter_header))
|
| 321 |
{
|
| 322 |
$carbon_meter_header = array(
|
| 323 |
// visible name, database column name, key in form table, column name
|
| 324 |
array("data" => t("date"), "field" => "enddate", "form" => "enddate_s", "col" => "datetime", "sort" => "asc"),
|
| 325 |
array("data" => t("reading"), "field" => "reading"), // todo need to distinguish between 2 vals
|
| 326 |
array("data" => t("code"), "field" => "code", "form" => "code_index"),
|
| 327 |
array("data" => t("adjust"), "field" => "adjustment"),
|
| 328 |
// array("data" => t("status"), "field" => "status"),
|
| 329 |
array("data" => t("operations"),"field" => "operations"));
|
| 330 |
}
|
| 331 |
return $carbon_meter_header ;
|
| 332 |
}
|
| 333 |
|
| 334 |
|
| 335 |
function _get_travel_header()
|
| 336 |
{
|
| 337 |
global $carbon_travel_header ; // create once
|
| 338 |
if (!isset($carbon_travel_header))
|
| 339 |
{
|
| 340 |
$carbon_travel_header = array(
|
| 341 |
// visible name, database column name, key in form table
|
| 342 |
array("data" => t("description"), "field" => "title" ),
|
| 343 |
array("data" => t("start"), "field" => "startdate", "form" => "startdate_s"),
|
| 344 |
array("data" => t("end"), "field" => "enddate", "form" => "enddate_s", "sort" => "asc"),
|
| 345 |
array("data" => t("amount"), "field" => "reading"),
|
| 346 |
array("data" => t("code"), "field" => "code", "form" => "code_index"),
|
| 347 |
array("data" => t("adjust"), "field" => "adjustment"),
|
| 348 |
// array("data" => t("status"), "field" => "status"),
|
| 349 |
array("data" => t("operations"),"field" => "operations"));
|
| 350 |
// the long length of 'operations' forces the icons to be on one row.
|
| 351 |
}
|
| 352 |
return $carbon_travel_header ;
|
| 353 |
}
|
| 354 |
|
| 355 |
|
| 356 |
/**
|
| 357 |
* Filter out fields that aren't explicitly specified
|
| 358 |
* @param $form
|
| 359 |
* @param $fieldset
|
| 360 |
* @return unknown_type
|
| 361 |
*/
|
| 362 |
|
| 363 |
function _filter_fields($form, $fieldset)
|
| 364 |
{
|
| 365 |
foreach($form as $key => $f)
|
| 366 |
{
|
| 367 |
if (!is_array($f))
|
| 368 |
continue ;
|
| 369 |
if ($f["#type"] == "hidden") // keep hidden fields
|
| 370 |
continue ;
|
| 371 |
if ($f["#type"] == "token") // keep token fields
|
| 372 |
continue ;
|
| 373 |
if (in_array($key,$fieldset))
|
| 374 |
continue; // this field is in the list
|
| 375 |
unset($form[$key]); // don't recognise it, get rid of it.
|
| 376 |
}
|
| 377 |
return $form ;
|
| 378 |
}
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
/**
|
| 383 |
* The submit handler invoked by drupal to add/update a carbon stamp
|
| 384 |
*
|
| 385 |
* @param $form
|
| 386 |
* @param $form_state
|
| 387 |
* @return unknown_type
|
| 388 |
*/
|
| 389 |
|
| 390 |
function carbon_ajax_submit(&$form, &$form_state)
|
| 391 |
{
|
| 392 |
$nid = $form_state['values']['nid'];
|
| 393 |
$cid = $form_state['values']['cid']; // set if we are cloning another node
|
| 394 |
$uid = $form_state['values']['uid'];
|
| 395 |
$aid = $form_state['values']['aid'];
|
| 396 |
|
| 397 |
if (empty($nid) && empty($cid))
|
| 398 |
{
|
| 399 |
$node = new Source(); // todo - use something else
|
| 400 |
$node->status = 1 ; // published
|
| 401 |
$node->promote = 0 ;
|
| 402 |
$node->sticky = 0;
|
| 403 |
$node->body = null ;
|
| 404 |
$node->comment = 0 ; // not permitted
|
| 405 |
$node->uid = $uid ;
|
| 406 |
$node->teaser = "" ;
|
| 407 |
$node->body = "" ;
|
| 408 |
$node->type = "carbon_stamp" ; // todo no hard coding
|
| 409 |
$node->scope = 0 ;
|
| 410 |
// attach stamp to the account that we are looking at
|
| 411 |
$node->aid_map = array();
|
| 412 |
$node->aid_map_updated = array($aid => null); // attach a new stamp to this account
|
| 413 |
$op = "saved" ;
|
| 414 |
}
|
| 415 |
else
|
| 416 |
{
|
| 417 |
if (!empty($cid))
|
| 418 |
{
|
| 419 |
$node = node_load($cid); unset($node->nid); // force a fresh save
|
| 420 |
$node->aid_map_updated = $node->aid_map ; $node->aid_map = array();
|
| 421 |
$op = "cloned" ;
|
| 422 |
}
|
| 423 |
else
|
| 424 |
{
|
| 425 |
$node = node_load($nid);
|
| 426 |
$node->aid_map_updated = $node->aid_map ;
|
| 427 |
$op = "saved" ;
|
| 428 |
}
|
| 429 |
}
|
| 430 |
$header = $form['#header']; // amazed this is kept intact
|
| 431 |
|
| 432 |
foreach ($header as $h) // note that carbon_stamp_submit has done all the conversions from string etc
|
| 433 |
{
|
| 434 |
$fieldname = $h['field'] ;
|
| 435 |
if (isset($form_state['values'][$fieldname]))
|
| 436 |
{
|
| 437 |
$v = $form_state['values'][$fieldname];
|
| 438 |
$node->$fieldname = $v ;
|
| 439 |
}
|
| 440 |
}
|
| 441 |
node_save($node);
|
| 442 |
$msg = l($form_state['values']['title'], 'node/'.$node->nid);
|
| 443 |
drupal_set_message($msg . " " . $op , "info");
|
| 444 |
$form_state['redirect'] = false ; // prevent form api from executing a redirect and exiting
|
| 445 |
global $nid_update ; $nid_update = $node->nid ;
|
| 446 |
}
|
| 447 |
|
| 448 |
/**
|
| 449 |
* Generate HTML for a collapsible panel.
|
| 450 |
*
|
| 451 |
* Uses standard collapsible fieldset javascript provided by drupal.
|
| 452 |
*
|
| 453 |
* @param string $title
|
| 454 |
* @param string $body
|
| 455 |
* @return none
|
| 456 |
*/
|
| 457 |
|
| 458 |
function theme_carbon_static_box($title, $body, $class = 'collapsible')
|
| 459 |
{
|
| 460 |
return '<fieldset class="'.$class.'">'.
|
| 461 |
' <legend class="header">'.$title.'</legend>'.
|
| 462 |
' <div class="body">'.$body.'</div>' .
|
| 463 |
'</fieldset>' ;
|
| 464 |
}
|
| 465 |
|
| 466 |
|
| 467 |
/**
|
| 468 |
* Generate HTML for a panel which can be loaded with data on demand.
|
| 469 |
*
|
| 470 |
* Uses standard javascript but content is loaded by carbon_view.js
|
| 471 |
*
|
| 472 |
* @param $title
|
| 473 |
* @param $url
|
| 474 |
* @return none
|
| 475 |
*/
|
| 476 |
|
| 477 |
|
| 478 |
function theme_carbon_loadable_box($title, $url)
|
| 479 |
{
|
| 480 |
return '<fieldset class="collapsible collapsed">'.
|
| 481 |
' <legend class="header">'.
|
| 482 |
' <a class="loadOnDemand" href="' . $url .'">' . $title . '</a>'.
|
| 483 |
' </legend>'.
|
| 484 |
' <div class="loadingZone"></div>' .
|
| 485 |
'</fieldset>' ;
|
| 486 |
}
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
?>
|