| 1 |
<?php
|
| 2 |
/*
|
| 3 |
* Created on 30-May-2007
|
| 4 |
*
|
| 5 |
* To change the template for this generated file go to
|
| 6 |
* Window - Preferences - PHPeclipse - PHP - Code Templates
|
| 7 |
*/
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Render a page showing a table of carbon accounts and various columns
|
| 13 |
* The actual content is determined by a specfic class and the class
|
| 14 |
* name is derived from the name of the report.
|
| 15 |
*
|
| 16 |
* Note that the param $target_user is the user we are focused on, not necessarily
|
| 17 |
* the user that is making the query.
|
| 18 |
*
|
| 19 |
* Additional information is extracted from the query using GET.
|
| 20 |
*
|
| 21 |
* Called directly from drupal menu.
|
| 22 |
*
|
| 23 |
* @param $target_user select by $target_user; set to null to retrieve footprints from all users
|
| 24 |
* @param $map results returned here as array of $nid => $footprint
|
| 25 |
*/
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
function carbon_report_create($target_user, $displayQueryFormWanted)
|
| 30 |
{
|
| 31 |
global $user;
|
| 32 |
global $charts ; $charts = array(t("none"), t("bar"), t("pie"));
|
| 33 |
global $orgs ; $orgs = array(""); // add a blank field to represent all
|
| 34 |
|
| 35 |
$params = _gather(array("report"=>"accounts", "firstdate"=>"20080101", "period"=>12,
|
| 36 |
"org"=>null,"chart"=>"pie",
|
| 37 |
"target"=> null, "cost"=> 0.05,
|
| 38 |
"datatype"=>""));
|
| 39 |
|
| 40 |
$org = $params["org"] ;
|
| 41 |
$firstdate = CarbonDate::fromYYYYMMDD($params["firstdate"]);
|
| 42 |
$lastdate = _add_months($firstdate, $params["period"]);
|
| 43 |
|
| 44 |
$path = carbon_include_core_js();
|
| 45 |
require_once(CARBON_PATH ."/carbon_chart.inc"); // generate bar and pie PNGs
|
| 46 |
require_once(CARBON_PATH ."/carbon_calculate.inc");// co2 & kWh settings
|
| 47 |
|
| 48 |
$output = "" ;
|
| 49 |
|
| 50 |
if (variable_get("carbon_enable_crag_functions", 0) > 0) // these lines
|
| 51 |
require_once(CARBON_PATH ."/carbon_crag.inc"); // can be removed
|
| 52 |
|
| 53 |
$reportName = "Carbon".ucFirst($params["report"]) ;
|
| 54 |
|
| 55 |
if (!class_exists ($reportName))
|
| 56 |
{
|
| 57 |
return t("Report %report is unknown or unable to be loaded", array("%report" => $reportName));
|
| 58 |
}
|
| 59 |
|
| 60 |
$reporter = new $reportName();
|
| 61 |
$reporter->initialize($params);
|
| 62 |
$reporter->initHeader();
|
| 63 |
|
| 64 |
$uid = isset($target_user) ? $target_user->uid : null ;
|
| 65 |
|
| 66 |
$all_account_map = carbon_report_base($uid, null, $reporter->header);
|
| 67 |
$account_map = array(); $org_map = array();
|
| 68 |
// build a list of all known orgs (it is filtered - bug))
|
| 69 |
|
| 70 |
foreach ($all_account_map as $nid => $account)
|
| 71 |
{
|
| 72 |
if (empty($org) or $org == $account->organization)
|
| 73 |
$account_map[$nid] = $account ;
|
| 74 |
$org_map[$account->organization]= 0 ; // only interested in the keys
|
| 75 |
}
|
| 76 |
ksort($org_map); $orgs = array_keys($org_map);
|
| 77 |
|
| 78 |
// at this point we could be displaying a table or an image.
|
| 79 |
// if chart requested, transfer countrol to carbon_chart
|
| 80 |
//
|
| 81 |
if (carbon_chart_requested())
|
| 82 |
{
|
| 83 |
return carbon_report_page_chart($account_map, $uid, $org, $firstdate, $lastdate);
|
| 84 |
}
|
| 85 |
$output .= $reporter->writeOutput($account_map, $uid, $firstdate, $lastdate);
|
| 86 |
|
| 87 |
if ($params["datatype"] == "json")
|
| 88 |
{
|
| 89 |
$output = drupal_json($output);
|
| 90 |
print($output);
|
| 91 |
exit(0);
|
| 92 |
}
|
| 93 |
|
| 94 |
$reporter->setTitle($org, $target_user, $firstdate, $lastdate);
|
| 95 |
|
| 96 |
// include IMG links to chart/pie image(s) specified in the query as 'chart='
|
| 97 |
|
| 98 |
$output .= carbon_chart_embed(null, null, null);
|
| 99 |
|
| 100 |
if ($displayQueryFormWanted)
|
| 101 |
{
|
| 102 |
$output .= drupal_get_form("carbon_report_account_form", $params);
|
| 103 |
|
| 104 |
$output .= drupal_get_form("carbon_report_sector_form", $params);
|
| 105 |
|
| 106 |
if (variable_get("carbon_enable_crag_functions", 0))
|
| 107 |
$output .= drupal_get_form("carbon_crag_form", $params);
|
| 108 |
}
|
| 109 |
|
| 110 |
if (!empty($target_user))
|
| 111 |
{
|
| 112 |
$attributes = array('title'=>'Add first meter reading for a meter not listed above. ' .
|
| 113 |
'Make sure you select meter reading on next form.',
|
| 114 |
'class'=>'first-add');
|
| 115 |
$output .= l("New account", "node/add/carbon-account",array('attributes'=>$attributes));
|
| 116 |
}
|
| 117 |
|
| 118 |
$output .= theme('pager', array(), 2, 0);
|
| 119 |
|
| 120 |
return $output ;
|
| 121 |
}
|
| 122 |
|
| 123 |
/**
|
| 124 |
* The CarbonAccounts class
|
| 125 |
* creates a table with all the account names down on side
|
| 126 |
* and columns containing other misc. account information
|
| 127 |
*
|
| 128 |
* This is the default view as it uses just one (the
|
| 129 |
* original query and does not require any computation).
|
| 130 |
*
|
| 131 |
*/
|
| 132 |
|
| 133 |
|
| 134 |
class CarbonAccounts
|
| 135 |
{
|
| 136 |
var $header = null ;
|
| 137 |
var $sampleRecord = null ;
|
| 138 |
|
| 139 |
function initialize()
|
| 140 |
{
|
| 141 |
|
| 142 |
}
|
| 143 |
|
| 144 |
function initHeader()
|
| 145 |
{
|
| 146 |
$this->header = array(
|
| 147 |
array("data" => t("account name"),"field" => "title","sort" => "asc"),
|
| 148 |
array("data" => t("postcode"), "field" => "postcode"),
|
| 149 |
array("data" => t("group"), "field" => "organization"),
|
| 150 |
array("data" => t("first date"), "field" => "firstdate"),
|
| 151 |
|
| 152 |
array("data" => t("#stamps"), "field" => "numstamps"),
|
| 153 |
array("data" => t("last change"), "field" => "last_changed")
|
| 154 |
);
|
| 155 |
}
|
| 156 |
|
| 157 |
function writeOutput($account_map, $uid, $firstdate, $lastdate)
|
| 158 |
{
|
| 159 |
$rows = array();
|
| 160 |
|
| 161 |
foreach ($account_map as $nid => $account)
|
| 162 |
{
|
| 163 |
$rows[] = array($account->account_link,
|
| 164 |
$account->postcode,
|
| 165 |
l($account->organization,
|
| 166 |
"carbon/report",
|
| 167 |
array("query"=> array("org"=>$account->organization))),
|
| 168 |
empty($account->firstdate) ? '' : _format_date($account->firstdate),
|
| 169 |
$account->numstamps <= 1 ? (isset($account->fid) ? 1 : 0) : $account->numstamps,
|
| 170 |
_format_date_time($account->last_changed));
|
| 171 |
}
|
| 172 |
$output = theme("table", $this->header, $rows);
|
| 173 |
$output .= theme("pager", NULL, 50, 0);
|
| 174 |
return $output ;
|
| 175 |
}
|
| 176 |
|
| 177 |
function setTitle($org, $user, $firstdate, $lastdate)
|
| 178 |
{
|
| 179 |
|
| 180 |
if (!empty($org))
|
| 181 |
$title = t("Carbon Accounts for !org", array("!org" => $org));
|
| 182 |
|
| 183 |
elseif (!empty($user))
|
| 184 |
$title = t("Carbon Accounts owned by !name", array("!name" => $user->name));
|
| 185 |
|
| 186 |
else
|
| 187 |
$title = t("Carbon Accounts");
|
| 188 |
|
| 189 |
drupal_set_title(check_plain(t($title)));
|
| 190 |
}
|
| 191 |
|
| 192 |
function attachForm()
|
| 193 |
{
|
| 194 |
return "";
|
| 195 |
}
|
| 196 |
}
|
| 197 |
|
| 198 |
/**
|
| 199 |
* The CarbonAccounts class creates a spreadsheet that shows
|
| 200 |
* account names on the y axis and energy use by sector on
|
| 201 |
* the x axis.
|
| 202 |
*/
|
| 203 |
|
| 204 |
|
| 205 |
class CarbonSector
|
| 206 |
{
|
| 207 |
var $header = null ;
|
| 208 |
|
| 209 |
function initialize()
|
| 210 |
{
|
| 211 |
|
| 212 |
}
|
| 213 |
|
| 214 |
function initHeader()
|
| 215 |
{
|
| 216 |
$this->header = array(array(
|
| 217 |
"data" => t("account name"),"field" => "title","sort" => "asc"));
|
| 218 |
}
|
| 219 |
|
| 220 |
function writeOutput($account_map, $uid, $firstdate, $lastdate)
|
| 221 |
{
|
| 222 |
global $orgs ;
|
| 223 |
|
| 224 |
// for each item in the map, change the key and value to title and amount ;
|
| 225 |
$footprint_by_title = array();
|
| 226 |
|
| 227 |
$results = array();
|
| 228 |
$sectors = array();
|
| 229 |
|
| 230 |
foreach ($account_map as $nid => $account)
|
| 231 |
{
|
| 232 |
$stamps = carbon_stamp_array_load($nid, null, null);
|
| 233 |
$footprint = carbon_calculate_total($account, $stamps, $firstdate, $lastdate);
|
| 234 |
|
| 235 |
foreach ($footprint->sector_co2 as $sector => $amount)
|
| 236 |
{
|
| 237 |
$sectors[$sector] = 1 ;
|
| 238 |
}
|
| 239 |
$results[] = $footprint ;
|
| 240 |
}
|
| 241 |
|
| 242 |
$rows = array();
|
| 243 |
foreach ($results as $footprint)
|
| 244 |
{
|
| 245 |
|
| 246 |
// add first column, title
|
| 247 |
$row = array($footprint->account->account_link);
|
| 248 |
|
| 249 |
// add subsequent columns to table
|
| 250 |
foreach ($sectors as $sector => $ignore)
|
| 251 |
{
|
| 252 |
if (!empty($footprint->sector_co2[$sector]))
|
| 253 |
$row[] = sprintf("%.0f",$footprint->sector_co2[$sector]) ;
|
| 254 |
else
|
| 255 |
$row[] = "0" ;
|
| 256 |
}
|
| 257 |
$row[] = sprintf("%.0f",$footprint->grand_co2) ;
|
| 258 |
$rows[] = $row ;
|
| 259 |
}
|
| 260 |
|
| 261 |
foreach ($sectors as $sector => $ignore)
|
| 262 |
{
|
| 263 |
$this->header[] = $sector ;
|
| 264 |
}
|
| 265 |
$this->header[] = "total" ;
|
| 266 |
|
| 267 |
$output .= theme("table", $this->header, $rows);
|
| 268 |
return $output ;
|
| 269 |
}
|
| 270 |
|
| 271 |
function setTitle($org, $user, $firstdate, $lastdate)
|
| 272 |
{
|
| 273 |
$title = empty($org) ? "" : "!org " ;
|
| 274 |
$title .= "CO2 emissions by sector for !dates" ;
|
| 275 |
$daterange = _format_date_range($firstdate, $lastdate);
|
| 276 |
drupal_set_title(t($title, array("!org"=>$org, "!dates"=>_format_date_range($firstdate, $lastdate))));
|
| 277 |
}
|
| 278 |
|
| 279 |
function attachForm()
|
| 280 |
{
|
| 281 |
return "";
|
| 282 |
}
|
| 283 |
}
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
function carbon_report_account_form(&$form_state, &$default_params)
|
| 288 |
{
|
| 289 |
$path = drupal_get_path("module", "carbon");
|
| 290 |
drupal_add_css($path . "/carbon.css");
|
| 291 |
|
| 292 |
$form = array();
|
| 293 |
|
| 294 |
$form["select"] = array(
|
| 295 |
"#type" => "fieldset",
|
| 296 |
"#title" => "Account report selection",
|
| 297 |
"#collapsible" => FALSE,
|
| 298 |
"#collapsed" => FALSE);
|
| 299 |
|
| 300 |
$form["select"]["report"] = array(
|
| 301 |
"#type" => "hidden",
|
| 302 |
"#value" => "accounts");
|
| 303 |
|
| 304 |
carbon_report_form_build($form["select"], $default_params, array("org"));
|
| 305 |
|
| 306 |
$form["select"]["submit"] = array(
|
| 307 |
"#type" => "submit",
|
| 308 |
"#value" => t("show accounts"),
|
| 309 |
"#prefix"=> "<DIV class='float-left'>",
|
| 310 |
"#suffix"=> "</DIV>",
|
| 311 |
"#weight" => 2);
|
| 312 |
|
| 313 |
return $form ;
|
| 314 |
}
|
| 315 |
|
| 316 |
|
| 317 |
/**
|
| 318 |
* this form allows the user to set the carbon price, target etc
|
| 319 |
* and then request the report that shows the calculations.
|
| 320 |
*/
|
| 321 |
|
| 322 |
|
| 323 |
function carbon_report_sector_form(&$form_state, &$default_params)
|
| 324 |
{
|
| 325 |
$path = drupal_get_path("module", "carbon");
|
| 326 |
drupal_add_css($path . "/carbon.css");
|
| 327 |
|
| 328 |
$form = array();
|
| 329 |
|
| 330 |
$form["select"] = array(
|
| 331 |
"#type" => "fieldset",
|
| 332 |
"#title" => "Sector report selection",
|
| 333 |
"#collapsible" => FALSE,
|
| 334 |
"#collapsed" => FALSE);
|
| 335 |
|
| 336 |
|
| 337 |
$form["select"]["report"] = array(
|
| 338 |
"#type" => "hidden",
|
| 339 |
"#value" => "sector");
|
| 340 |
|
| 341 |
carbon_report_form_build($form["select"], $default_params, array("org", "firstdate", "period", "chart"));
|
| 342 |
|
| 343 |
$form["select"]["submit"] = array(
|
| 344 |
"#type" => "submit",
|
| 345 |
"#value" => t("show CO2"),
|
| 346 |
"#prefix"=> "<DIV class='float-left'>",
|
| 347 |
"#suffix"=> "</DIV>",
|
| 348 |
"#weight" => 2);
|
| 349 |
|
| 350 |
$form["select"]["#title"] = "Sector report selection";
|
| 351 |
|
| 352 |
return $form ;
|
| 353 |
}
|
| 354 |
|
| 355 |
|
| 356 |
/**
|
| 357 |
* add specified fields to the form structure
|
| 358 |
*
|
| 359 |
* @param $form target form
|
| 360 |
* @param $params array of default parameters
|
| 361 |
* @param $fields desired fields
|
| 362 |
* @return unknown_type
|
| 363 |
*/
|
| 364 |
|
| 365 |
|
| 366 |
function carbon_report_form_build(&$form, $params, $fields)
|
| 367 |
{
|
| 368 |
global $charts, $orgs ;
|
| 369 |
$path = drupal_get_path("module", "carbon");
|
| 370 |
|
| 371 |
foreach($fields as $field)
|
| 372 |
{
|
| 373 |
switch ($field)
|
| 374 |
{
|
| 375 |
case "org" :
|
| 376 |
$form["org"] = array(
|
| 377 |
"#type" => "select",
|
| 378 |
"#title" => t("group"),
|
| 379 |
"#options" => $orgs,
|
| 380 |
"#default_value" => array_search($params["org"],$orgs),
|
| 381 |
"#required" => FALSE,
|
| 382 |
"#prefix"=> "<DIV class='float-left'>",
|
| 383 |
"#suffix"=> "</DIV>",
|
| 384 |
"#weight" => -4);
|
| 385 |
break;
|
| 386 |
|
| 387 |
|
| 388 |
case "firstdate" :
|
| 389 |
$form["firstdate"] = array(
|
| 390 |
"#type" => "textfield",
|
| 391 |
"#title" => t("starting date"),
|
| 392 |
"#required" => FALSE,
|
| 393 |
"#size" => 15,
|
| 394 |
"#maxlength" => 40,
|
| 395 |
"#description" => t("Format: %time", array("%time" => _format_date_only(time()))),
|
| 396 |
"#default_value" => _format_date(CarbonDate::fromYYYYMMDD($params["firstdate"])),
|
| 397 |
"#prefix"=> "<DIV class='float-left'>",
|
| 398 |
"#suffix"=> "</DIV>",
|
| 399 |
"#weight" => -3);
|
| 400 |
break;
|
| 401 |
|
| 402 |
case "period" :
|
| 403 |
$form["select"]["period"] = array(
|
| 404 |
"#type" => "textfield",
|
| 405 |
"#title" => t("period"),
|
| 406 |
"#description" => t("months"),
|
| 407 |
"#default_value" => $params["period"],
|
| 408 |
"#required" => FALSE,
|
| 409 |
"#size" => 10,
|
| 410 |
"#maxlength" => 40,
|
| 411 |
"#prefix"=> "<DIV class='float-left'>",
|
| 412 |
"#suffix"=> "</DIV>",
|
| 413 |
"#weight" => -2);
|
| 414 |
break;
|
| 415 |
|
| 416 |
case "chart" :
|
| 417 |
$form["chart"] = array(
|
| 418 |
"#type" => "select",
|
| 419 |
"#title" => t("chart"),
|
| 420 |
"#options" => $charts,
|
| 421 |
"#default_value" => array_search($params["chart"],$charts),
|
| 422 |
"#required" => FALSE,
|
| 423 |
"#maxlength" => 3,
|
| 424 |
"#prefix"=> "<DIV class='float-left'>",
|
| 425 |
"#suffix"=> "</DIV>",
|
| 426 |
"#weight" => 1);
|
| 427 |
break;
|
| 428 |
|
| 429 |
case "submit" :
|
| 430 |
$form["submit"] = array(
|
| 431 |
"#type" => "submit",
|
| 432 |
"#value" => t("show accounts"),
|
| 433 |
"#prefix"=> "<DIV class='float-left'>",
|
| 434 |
"#suffix"=> "</DIV>",
|
| 435 |
"#weight" => 2);
|
| 436 |
break;
|
| 437 |
}
|
| 438 |
}
|
| 439 |
}
|
| 440 |
|
| 441 |
/**
|
| 442 |
* redirect a post operation to a new URL
|
| 443 |
*/
|
| 444 |
|
| 445 |
function carbon_report_account_form_submit($form, $form_state)
|
| 446 |
{
|
| 447 |
return carbon_report_form_submit($form, $form_state);
|
| 448 |
}
|
| 449 |
function carbon_report_sector_form_submit($form, $form_state)
|
| 450 |
{
|
| 451 |
return carbon_report_form_submit($form, $form_state);
|
| 452 |
}
|
| 453 |
|
| 454 |
/**
|
| 455 |
* When the user submits a form request, an HTTP post is generated.
|
| 456 |
* We convert this into an HTTP get. The reason for doing this is
|
| 457 |
* so that users can bookmark interesting reports for subsequent use
|
| 458 |
* as well as editing them to change start date, change period.
|
| 459 |
*
|
| 460 |
* @param unknown_type $form
|
| 461 |
* @param unknown_type $form_values
|
| 462 |
*/
|
| 463 |
|
| 464 |
function carbon_report_form_submit($form, $form_state)
|
| 465 |
{
|
| 466 |
global $orgs, $charts ;
|
| 467 |
|
| 468 |
$link = "";
|
| 469 |
|
| 470 |
$params = array("report", "period");
|
| 471 |
$params[] = "target" ; $params[] = "cost" ; // used only for CRAG page
|
| 472 |
|
| 473 |
// extract all the parameters that we'd expect to find in the POST
|
| 474 |
$first = true ;
|
| 475 |
foreach ($params as $name)
|
| 476 |
{
|
| 477 |
if (isset($form_state['values'][$name])) $link .= ($first ? "" : "&").$name."=".$form_state['values'][$name];
|
| 478 |
$first = false ;
|
| 479 |
}
|
| 480 |
// parse date to 6 char string
|
| 481 |
if (isset($form_state['values']['firstdate']))
|
| 482 |
{
|
| 483 |
$firstdate_s = CarbonDate::asYYYYMMDD(strtotime($form_state['values']['firstdate']));
|
| 484 |
$link .= "&firstdate=".$firstdate_s ;
|
| 485 |
}
|
| 486 |
|
| 487 |
// have to handle org and chart fields differently because they are pull down boxes
|
| 488 |
if (isset($form_state['values']["org"]))
|
| 489 |
if (!empty($orgs[$form_state['values']["org"]]))
|
| 490 |
$link .= "&org=".$orgs[$form_state['values']["org"]];
|
| 491 |
|
| 492 |
if (isset($form_state['values']["chart"]))
|
| 493 |
if (!empty($charts[$form_state['values']["chart"]]))
|
| 494 |
$link .= "&chart=".$charts[$form_state['values']["chart"]];
|
| 495 |
|
| 496 |
drupal_goto($_GET['q'], $link);
|
| 497 |
}
|
| 498 |
|
| 499 |
|
| 500 |
/**
|
| 501 |
* Create a skeleton report page.
|
| 502 |
* It creates a set of rows each representing a carbon account
|
| 503 |
* There is only one mandatory column, the account name.
|
| 504 |
* Optional columns can be added depending on the report name.
|
| 505 |
*
|
| 506 |
* Protection and page selection are done here.
|
| 507 |
*/
|
| 508 |
|
| 509 |
function carbon_report_base($uid, $org, $header)
|
| 510 |
{
|
| 511 |
$account_map = array();
|
| 512 |
|
| 513 |
$queryResult = carbon_report_query($uid, $org, $header);
|
| 514 |
|
| 515 |
while ($account = db_fetch_object($queryResult))
|
| 516 |
{
|
| 517 |
$access = carbon_access("view", $account) or ($org == $account->organization) ;
|
| 518 |
|
| 519 |
$account->anonomized_title = $access ? $account->title : carbon_anonomize($account->title);
|
| 520 |
$account->account_link = carbon_link_from_title($account, $account, "");
|
| 521 |
$account->transfer_link = carbon_link_from_title($account, $account, "transfer");
|
| 522 |
$account_map[$account->nid] = $account ;
|
| 523 |
}
|
| 524 |
return $account_map ;
|
| 525 |
}
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
/**
|
| 530 |
* Account retrieval, used by several other functions
|
| 531 |
* There is an underlying simple query that only involves
|
| 532 |
* a single select query.
|
| 533 |
*/
|
| 534 |
|
| 535 |
function carbon_report_query($uid, $org, $header)
|
| 536 |
{
|
| 537 |
$sql = "SELECT an.nid, an.title, an.uid, an.type, a.*, u.name, " .
|
| 538 |
"count(*) as numstamps, max(sn.changed) as last_changed " .
|
| 539 |
",s.uid AS shared_uid, protection " .
|
| 540 |
"FROM {carbon_account} a " .
|
| 541 |
"INNER JOIN {node} an USING (nid) " .
|
| 542 |
|
| 543 |
"LEFT JOIN {carbon_stamp_account} j ON an.nid = j.fid " . // to get num stamps
|
| 544 |
"LEFT JOIN {node} sn ON j.sid = sn.nid " . // to get last entry
|
| 545 |
|
| 546 |
"LEFT JOIN {carbon_account_share} s ON a.nid = s.nid " . // to get perm to access
|
| 547 |
"INNER JOIN {users} u ON an.uid = u.uid " .
|
| 548 |
"WHERE (u.uid = %d " . (empty($uid) ? " OR 1) " : ") "). // opt select by user
|
| 549 |
"AND (a.organization = '%s' " . (empty($org) ? " OR 1) " : ") ") ; // opt select by org
|
| 550 |
|
| 551 |
//if og_carbon is present, filter by organic group - sql condition
|
| 552 |
if (module_exists('og_carbon') && module_exists('og_basic')) {
|
| 553 |
$sql = og_carbon_sql($sql, 'condition');
|
| 554 |
}
|
| 555 |
|
| 556 |
// non admin users only permitted to see shared footprints and
|
| 557 |
// footprints from same group or public footprints.
|
| 558 |
// have to filter in the SQL in case there are a lot of records
|
| 559 |
// Note. private footprints in same group are shown but not linked
|
| 560 |
|
| 561 |
$sql .= "AND ( (s.uid = %d) " . // ie shared with target user
|
| 562 |
" OR (an.status AND length(a.organization) > 1 and a.organization = '%s') " .
|
| 563 |
" OR (an.uid = %d) ". // should restrict anonymous user
|
| 564 |
" OR 1 " .
|
| 565 |
" ) " ;
|
| 566 |
$sql .= "GROUP BY an.nid " ; // all stamps with the same account
|
| 567 |
$sql .= tablesort_sql($header);
|
| 568 |
|
| 569 |
//if og_carbon is present, filter by organic group - sql rewrite
|
| 570 |
if (module_exists('og_carbon') && module_exists('og_basic')) {
|
| 571 |
$sql = og_carbon_sql($sql, 'rewrite');
|
| 572 |
}
|
| 573 |
|
| 574 |
// drupal_set_message(" my sql ". $sql . " org:". $org. " uid:" . $uid, 'info');
|
| 575 |
|
| 576 |
// get the links (no range limit here)
|
| 577 |
|
| 578 |
global $user ;
|
| 579 |
$queryResult = db_query($sql, $uid, $org, $uid, $org, $user->uid);
|
| 580 |
|
| 581 |
return $queryResult ;
|
| 582 |
}
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
/**
|
| 587 |
* Render an image showing multiple carbon footprints
|
| 588 |
*
|
| 589 |
* Note that the param $uid is the user we are focused on, not the user doing
|
| 590 |
* the browsing.
|
| 591 |
*
|
| 592 |
* @param $account_map an array of $nid => $footprint
|
| 593 |
* @param $uid select by uid; set to zero to retrieve footprints from all users
|
| 594 |
* @param $org select by organisation; set to '' or null to retrieve footprints
|
| 595 |
* from users in any group.
|
| 596 |
*/
|
| 597 |
|
| 598 |
function carbon_report_page_chart($account_map, $uid, $org, $firstdate, $lastdate){
|
| 599 |
global $user;
|
| 600 |
// for each item in the map, change the key and value to title and amount ;
|
| 601 |
$footprint_by_title = array();
|
| 602 |
foreach ($account_map as $nid => $account)
|
| 603 |
{
|
| 604 |
$stamps = carbon_stamp_array_load($nid, $uid, $org);
|
| 605 |
|
| 606 |
$daterange = _format_date_range($firstdate, $lastdate);
|
| 607 |
|
| 608 |
$results = carbon_calculate_total($account, $stamps, $firstdate, $lastdate);
|
| 609 |
|
| 610 |
// there is a potential problem here in that only one of a group
|
| 611 |
// of footprints of the same name will be shown.
|
| 612 |
|
| 613 |
$footprint_by_title[$account->anonomized_title]
|
| 614 |
= array("data" => $results->sector_co2, "comment" => $daterange);
|
| 615 |
}
|
| 616 |
return carbon_chart_generate($footprint_by_title, "CO2 emissions");
|
| 617 |
}
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
/**
|
| 624 |
* Provide support for account select boxes
|
| 625 |
*/
|
| 626 |
|
| 627 |
|
| 628 |
class CarbonAccountMap
|
| 629 |
{
|
| 630 |
var $account_map = null ;
|
| 631 |
var $title_array = array();
|
| 632 |
var $nid_array = array();
|
| 633 |
|
| 634 |
function init()
|
| 635 |
{
|
| 636 |
$this->account_map = carbon_report_base(null,null,array());
|
| 637 |
foreach ($this->account_map as $account)
|
| 638 |
{
|
| 639 |
$this->title_array[] = $account->title ;
|
| 640 |
$this->nid_array[] = $account->nid ;
|
| 641 |
}
|
| 642 |
return $this ;
|
| 643 |
}
|
| 644 |
|
| 645 |
function getArray()
|
| 646 |
{
|
| 647 |
return $this->title_array ;
|
| 648 |
}
|
| 649 |
|
| 650 |
function getArrayIndex($nid)
|
| 651 |
{
|
| 652 |
$position = array_search($nid, $this->nid_array);
|
| 653 |
return $position ;
|
| 654 |
}
|
| 655 |
|
| 656 |
function getAccountLink($nid)
|
| 657 |
{
|
| 658 |
$account = $this->account_map[$nid];
|
| 659 |
return $account->account_link ;
|
| 660 |
}
|
| 661 |
|
| 662 |
function getTransferLink($nid)
|
| 663 |
{
|
| 664 |
$account = $this->account_map[$nid];
|
| 665 |
return $account->transfer_link ;
|
| 666 |
}
|
| 667 |
|
| 668 |
function getTitle($nid)
|
| 669 |
{
|
| 670 |
$account = $this->account_map[$nid];
|
| 671 |
return $account->title ;
|
| 672 |
}
|
| 673 |
|
| 674 |
function getNid($index)
|
| 675 |
{
|
| 676 |
return $this->nid_array[$index];
|
| 677 |
}
|
| 678 |
|
| 679 |
function sortByRevisionDate()
|
| 680 |
{
|
| 681 |
$account_map_recent = array();
|
| 682 |
foreach ($this->account_map as $key=>$account)
|
| 683 |
{
|
| 684 |
$account_map_recent[$account->last_changed] = $account ;
|
| 685 |
}
|
| 686 |
$this->account_map = $account_map_recent;
|
| 687 |
krsort($this->account_map);
|
| 688 |
return $this->account_map ;
|
| 689 |
}
|
| 690 |
}
|
| 691 |
|
| 692 |
|
| 693 |
/**
|
| 694 |
* gather a range of parameters using GET
|
| 695 |
* used by carbon_report().
|
| 696 |
* There's probably a better way to do this.
|
| 697 |
*
|
| 698 |
* @param $names is array of keys / default values
|
| 699 |
* @return array of keys / values
|
| 700 |
* */
|
| 701 |
|
| 702 |
function _gather($names)
|
| 703 |
{
|
| 704 |
$q = array();
|
| 705 |
foreach ($names as $name => $default_value)
|
| 706 |
{
|
| 707 |
if (!empty($_GET[$name]))
|
| 708 |
$q[$name] = $_GET[$name] ;
|
| 709 |
else
|
| 710 |
$q[$name] = $default_value ;
|
| 711 |
}
|
| 712 |
return $q ;
|
| 713 |
}
|
| 714 |
|
| 715 |
function _gather_form($form, $names)
|
| 716 |
{
|
| 717 |
$q = array();
|
| 718 |
foreach ($names as $name => $default_value)
|
| 719 |
{
|
| 720 |
if (!empty($form[$name]))
|
| 721 |
$q[$name] = $form[$name] ;
|
| 722 |
else
|
| 723 |
$q[$name] = $default_value ;
|
| 724 |
}
|
| 725 |
return $q ;
|
| 726 |
}
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
?>
|