| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* $Id: carbon_source.inc,v 1.18 2009/04/29 21:04:09 johnackers Exp $
|
| 4 |
*
|
| 5 |
* created on 11-Nov-2006
|
| 6 |
*
|
| 7 |
* @file
|
| 8 |
* definition of carbon_source node. Each carbon_source
|
| 9 |
* node contains parameters that allow the carbon dioxide
|
| 10 |
* emissions to be calulated from a particular source of
|
| 11 |
* emissions e.g. domestic gas supply, car miles etc.
|
| 12 |
*
|
| 13 |
* This is a very straightforward standard drupal node
|
| 14 |
* implementation.
|
| 15 |
*
|
| 16 |
*/
|
| 17 |
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Implementation of hook_access().
|
| 21 |
*
|
| 22 |
* Refer to carbon_access because it is easier to check
|
| 23 |
* access for all carbon types in one place.
|
| 24 |
*/
|
| 25 |
|
| 26 |
|
| 27 |
function carbon_source_access($op, $node)
|
| 28 |
{
|
| 29 |
return carbon_access($op, $node);
|
| 30 |
}
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Render a page listing a one line summary of all carbon sources
|
| 37 |
*
|
| 38 |
* TODO add edit operations
|
| 39 |
*/
|
| 40 |
|
| 41 |
function carbon_source_page($format) {
|
| 42 |
global $user;
|
| 43 |
|
| 44 |
$header = array(
|
| 45 |
// array("data" => t("created")),
|
| 46 |
array("data" => t("model"), "field" => "model"),
|
| 47 |
array("data" => t("sector"),"field" => "sector"),
|
| 48 |
array("data" => t("code"), "field" => "code"),
|
| 49 |
array("data" => t("title"), "field" => "title"),
|
| 50 |
array("data" => t("units"), "field" => "units"),
|
| 51 |
array("data" => t("class"), "field" => "classname")
|
| 52 |
);
|
| 53 |
|
| 54 |
$sql = _get_carbon_source_sql();
|
| 55 |
|
| 56 |
$sql .= tablesort_sql($header);
|
| 57 |
|
| 58 |
// get the links (no range limit here)
|
| 59 |
$queryResult = db_query($sql);
|
| 60 |
$rows = array();
|
| 61 |
$protection = _get_protection_options();
|
| 62 |
$source_map = array();
|
| 63 |
while ($node = db_fetch_object($queryResult))
|
| 64 |
{
|
| 65 |
$access_rw = carbon_access("update", $node, $node->uid);
|
| 66 |
$link = "node/".$node->nid .($access_rw ? "" : "") ; // go to view mode first
|
| 67 |
|
| 68 |
$source_map[] = array("code" => $node->code,
|
| 69 |
"title" => $node->title,
|
| 70 |
"units" => $node->units,
|
| 71 |
"model" => $node->model ) ;
|
| 72 |
$rows[] = array(
|
| 73 |
$node->model,
|
| 74 |
$node->sector,
|
| 75 |
l($node->code,$link),
|
| 76 |
$node->title,
|
| 77 |
$node->units,
|
| 78 |
$node->classname
|
| 79 |
);
|
| 80 |
}
|
| 81 |
|
| 82 |
if ($params["datatype"] == "json")
|
| 83 |
{
|
| 84 |
$output = drupal_json($output);
|
| 85 |
print($output);
|
| 86 |
exit(0);
|
| 87 |
}
|
| 88 |
|
| 89 |
$output = theme("table", $header, $rows);
|
| 90 |
$output .= theme("pager", NULL, 10, 0);
|
| 91 |
|
| 92 |
if (count($rows) == 0)
|
| 93 |
$output .= "<p>".carbon_source_missing()."</p>";
|
| 94 |
|
| 95 |
drupal_set_title(t("Carbon Sources"));
|
| 96 |
return $output;
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
* hold the list of available sources globally because
|
| 101 |
* different functions use this array during the same
|
| 102 |
* transaction.
|
| 103 |
*/
|
| 104 |
|
| 105 |
|
| 106 |
function carbon_source_fetch_array()
|
| 107 |
{
|
| 108 |
|
| 109 |
}
|
| 110 |
|
| 111 |
/**
|
| 112 |
* The administrator can select which carbon models to
|
| 113 |
* make available. Every select for a carbon source
|
| 114 |
* is appended with the system wide list of approved
|
| 115 |
* models.
|
| 116 |
*/
|
| 117 |
|
| 118 |
function _get_carbon_source_sql()
|
| 119 |
{
|
| 120 |
$sql = "SELECT r.*, n.* FROM {carbon_source} r " .
|
| 121 |
"LEFT JOIN {node} n ON r.nid = n.nid " .
|
| 122 |
"INNER JOIN {users} u on n.uid = u.uid " ;
|
| 123 |
|
| 124 |
$model = variable_get("carbon_models", "");
|
| 125 |
if (!(empty($model)))
|
| 126 |
{
|
| 127 |
$models = explode(",", $model);
|
| 128 |
|
| 129 |
// wrap each element with " "
|
| 130 |
for ($i = 0 ; $i < count($models) ; $i++) $models[$i] = "'".trim($models[$i])."'";
|
| 131 |
$model = implode(",", $models);
|
| 132 |
|
| 133 |
$sql .= " WHERE model in (".$model.") " ;
|
| 134 |
}
|
| 135 |
return $sql ;
|
| 136 |
}
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
/**
|
| 144 |
* @return a map of key=> value pairs where the
|
| 145 |
* key is the code in the database and
|
| 146 |
* and the value is text presented to the user
|
| 147 |
*
|
| 148 |
* note that adding sources might while a user
|
| 149 |
* is selecting might result in the wrong option.
|
| 150 |
*/
|
| 151 |
|
| 152 |
|
| 153 |
/**
|
| 154 |
* Implementation of hook_form().
|
| 155 |
*/
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
function carbon_source_form(&$node, &$param) {
|
| 160 |
global $user;
|
| 161 |
$form = array() ;
|
| 162 |
|
| 163 |
$form["source"] = array(
|
| 164 |
"#type" => "fieldset",
|
| 165 |
"#title" => "Carbon source",
|
| 166 |
"#weight" => -3,
|
| 167 |
"#collapsible" => TRUE,
|
| 168 |
"#collapsed" => FALSE);
|
| 169 |
|
| 170 |
$form["source"]["title"] = array(
|
| 171 |
"#type" => "textfield",
|
| 172 |
"#title" => t("title"),
|
| 173 |
"#required" => TRUE,
|
| 174 |
"#size" => 20,
|
| 175 |
"#maxlength" => 80,
|
| 176 |
"#weight" => -5,
|
| 177 |
"#default_value" => $node->title,
|
| 178 |
"#description" => t("Full descriptive name of this source"));
|
| 179 |
|
| 180 |
$form["source"]["units"] = array(
|
| 181 |
"#type" => "textfield",
|
| 182 |
"#title" => t("units"),
|
| 183 |
"#required" => TRUE,
|
| 184 |
"#weight" => -4,
|
| 185 |
"#default_value" => $node->units,
|
| 186 |
"#size" => 20,
|
| 187 |
"#maxlength" => 20,
|
| 188 |
"#description" => t("Should be fairly short e.g. kilowatt hours, more details can be provided in notes"));
|
| 189 |
|
| 190 |
|
| 191 |
$form["source"]["code"] = array(
|
| 192 |
"#type" => "textfield",
|
| 193 |
"#title" => t("code"),
|
| 194 |
"#required" => TRUE,
|
| 195 |
"#weight" => -3,
|
| 196 |
"#size" => 20,
|
| 197 |
"#maxlength" => 20,
|
| 198 |
"#default_value" => $node->code,
|
| 199 |
"#description" => t("Short unique code used to define this source e.g. kwh. " .
|
| 200 |
"This code must match the same code in other models if you want " .
|
| 201 |
"to compare the carbon footprint calculated by two models from " .
|
| 202 |
"the same inputtted values"));
|
| 203 |
|
| 204 |
$form["source"]["model"] = array(
|
| 205 |
"#type" => "textfield",
|
| 206 |
"#title" => t("model"),
|
| 207 |
"#required" => TRUE,
|
| 208 |
"#size" => 20,
|
| 209 |
"#maxlength" => 20,
|
| 210 |
"#weight" => -2,
|
| 211 |
"#default_value" => $node->model,
|
| 212 |
"#description" => t("The overall model e.g. crag, carbon coach. You can use any arbitrary name."));
|
| 213 |
|
| 214 |
$form["source"]["sector"] = array(
|
| 215 |
"#type" => "textfield",
|
| 216 |
"#title" => "sector",
|
| 217 |
"#required" => TRUE,
|
| 218 |
"#size" => 20,
|
| 219 |
"#maxlength" => 20,
|
| 220 |
"#weight" => -1,
|
| 221 |
"#default_value" => $node->sector,
|
| 222 |
"#description" => t("General category of energy use e.g. 'transport' or 'surface transport'. You can use any arbitrary name."));
|
| 223 |
|
| 224 |
$e0 = variable_get("carbon_energy_units_0", "");
|
| 225 |
|
| 226 |
$descrip = t('Conversion factor. Number of energy units expended by one unit. This field can be disabled in carbon settings.') ;
|
| 227 |
|
| 228 |
if (!empty($e0))
|
| 229 |
$form["source"]["tokwh0"] = array(
|
| 230 |
"#type" => "textfield",
|
| 231 |
"#title" => $e0,
|
| 232 |
"#required" => TRUE,
|
| 233 |
"#size" => 20,
|
| 234 |
"#maxlength" => 20,
|
| 235 |
"#weight" => 0,
|
| 236 |
"#default_value" => $node->tokwh0,
|
| 237 |
"#description" => $descrip);
|
| 238 |
|
| 239 |
$e1 = variable_get("carbon_energy_units_1", "");
|
| 240 |
|
| 241 |
if (!empty($e1))
|
| 242 |
$form["source"]["tokwh1"] = array(
|
| 243 |
"#type" => "textfield",
|
| 244 |
"#title" => $e1,
|
| 245 |
"#required" => TRUE,
|
| 246 |
"#size" => 20,
|
| 247 |
"#maxlength" => 20,
|
| 248 |
"#weight" => 1,
|
| 249 |
"#default_value" => $node->tokwh1,
|
| 250 |
"#description" => $descrip);
|
| 251 |
|
| 252 |
$form["source"]["toco2"] = array(
|
| 253 |
"#type" => "textfield",
|
| 254 |
"#title" => "CO2",
|
| 255 |
"#required" => TRUE,
|
| 256 |
"#size" => 20,
|
| 257 |
"#maxlength" => 20,
|
| 258 |
"#weight" => 2,
|
| 259 |
"#default_value" => $node->toco2,
|
| 260 |
"#description" => t("Conversion factor. Number of Kilograms of CO2 emitted by one unit."));
|
| 261 |
|
| 262 |
$form["source"]["classname_index"] = array(
|
| 263 |
"#type" => "select",
|
| 264 |
"#title" => "classname",
|
| 265 |
"#required" => TRUE,
|
| 266 |
"#size" => 3,
|
| 267 |
"#maxlength" => 20,
|
| 268 |
"#weight" => 3,
|
| 269 |
"#options" => _get_source_class_key(null),
|
| 270 |
"#default_value" => array_search($node->classname, _get_source_class_key(null)),
|
| 271 |
"#description" => t("PHP class that calculates the emissions"));
|
| 272 |
|
| 273 |
$form["source"]["params"] = array(
|
| 274 |
"#type" => "textarea",
|
| 275 |
"#title" => "params",
|
| 276 |
"#required" => false,
|
| 277 |
"#size" => 20,
|
| 278 |
"#maxlength" => 80,
|
| 279 |
"#weight" => 5,
|
| 280 |
"#height" => 2,
|
| 281 |
"#default_value" => $node->params,
|
| 282 |
"#description" => t("Additional parameters used by the class"));
|
| 283 |
|
| 284 |
|
| 285 |
$form["source"]["body"] = array(
|
| 286 |
"#type" => "textarea",
|
| 287 |
"#title" => "notes",
|
| 288 |
"#required" => false,
|
| 289 |
"#size" => 4,
|
| 290 |
"#maxlength" => 320,
|
| 291 |
"#weight" => 4,
|
| 292 |
"#default_value" => $node->body,
|
| 293 |
"#description" => t("Notes about this carbon source and how the conversion " .
|
| 294 |
"factors have been calculated"));
|
| 295 |
|
| 296 |
$form['#submit'] = array('carbon_source_submit');
|
| 297 |
|
| 298 |
return $form ;
|
| 299 |
}
|
| 300 |
|
| 301 |
|
| 302 |
/**
|
| 303 |
* Validate our forms
|
| 304 |
* @param string form id
|
| 305 |
* @param array form values
|
| 306 |
*/
|
| 307 |
function carbon_source_validate($form_id, $form_values)
|
| 308 |
{
|
| 309 |
|
| 310 |
}
|
| 311 |
|
| 312 |
|
| 313 |
/**
|
| 314 |
* Implemenation of hook_load
|
| 315 |
* @param node object to load additional information for
|
| 316 |
* @return object with carbon fields
|
| 317 |
*/
|
| 318 |
function carbon_source_load($node) {
|
| 319 |
|
| 320 |
$record = db_fetch_object(db_query("SELECT * " .
|
| 321 |
"FROM {carbon_source} WHERE nid = %d ", $node->nid));
|
| 322 |
return $record;
|
| 323 |
}
|
| 324 |
|
| 325 |
|
| 326 |
function carbon_source_submit(&$form, &$form_state)
|
| 327 |
{
|
| 328 |
$form_state['values']['classname'] = _get_source_class_key($form_state['values']['classname_index']);
|
| 329 |
}
|
| 330 |
|
| 331 |
|
| 332 |
/**
|
| 333 |
* Implementation of hook_insert, which saves carbon-specific information
|
| 334 |
* into the carbon table
|
| 335 |
* @param node object
|
| 336 |
*/
|
| 337 |
function carbon_source_insert(&$node) {
|
| 338 |
|
| 339 |
db_query("INSERT INTO {carbon_source} " .
|
| 340 |
"(nid, model, sector, code, units, classname, tokwh0, tokwh1, toco2, params)" .
|
| 341 |
"VALUES (%d, '%s', '%s', '%s', '%s', '%s', %f, %f, %f, '%s' )",
|
| 342 |
$node->nid, $node->model, $node->sector, $node->code, $node->units,
|
| 343 |
$node->classname, $node->tokwh0, $node->tokwh1, $node->toco2, $node->params);
|
| 344 |
}
|
| 345 |
|
| 346 |
|
| 347 |
/**
|
| 348 |
* Implementation of hook_update, which saves updated todo-specific
|
| 349 |
* information into the todo table
|
| 350 |
* @param node object
|
| 351 |
*/
|
| 352 |
function carbon_source_update(&$node) {
|
| 353 |
|
| 354 |
db_query("UPDATE {carbon_source} set model='%s',sector='%s',code='%s',units='%s'," .
|
| 355 |
"classname='%s', tokwh0=%f, tokwh1=%f, toco2=%f, params='%s' WHERE nid=%d",
|
| 356 |
$node->model, $node->sector, $node->code, $node->units, $node->classname,
|
| 357 |
$node->tokwh0, $node->tokwh1, $node->toco2, $node->params, $node->nid);
|
| 358 |
}
|
| 359 |
|
| 360 |
|
| 361 |
/**
|
| 362 |
* Implementation of hook_delete().
|
| 363 |
* (not certain that this is necessary)
|
| 364 |
*/
|
| 365 |
function carbon_source_delete($node) {
|
| 366 |
db_query("DELETE FROM {carbon_source} WHERE nid = %d", $node->nid);
|
| 367 |
}
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
/**
|
| 372 |
* Implementation of hook_view, add our node specific information
|
| 373 |
* @param node object to display
|
| 374 |
* @param boolean is this a teaser or full node?
|
| 375 |
* @param boolean is this displaying on its own page
|
| 376 |
*/
|
| 377 |
function carbon_source_view(&$node, $teaser = FALSE, $page = FALSE)
|
| 378 |
{
|
| 379 |
$node = node_prepare($node, $teaser);
|
| 380 |
$carbon_source_info = theme('carbon_source_table_view', $node); // try item_list
|
| 381 |
$node->content["source"] = array("#value" => $carbon_source_info);
|
| 382 |
$node->body .= $carbon_source_info;
|
| 383 |
$node->teaser .= $carbon_source_info;
|
| 384 |
return $node ;
|
| 385 |
}
|
| 386 |
|
| 387 |
/**
|
| 388 |
* Theme function to display additional node data
|
| 389 |
* @param node to display
|
| 390 |
* @return HTML string with additional node information
|
| 391 |
*/
|
| 392 |
|
| 393 |
|
| 394 |
// then create an array of column names
|
| 395 |
|
| 396 |
function theme_carbon_source_table_view($node)
|
| 397 |
{
|
| 398 |
$header = array(
|
| 399 |
array("data" => t("created")),
|
| 400 |
array("data" => t("model")),
|
| 401 |
array("data" => t("sector")),
|
| 402 |
array("data" => t("code")),
|
| 403 |
array("data" => t("units")),
|
| 404 |
array("data" => t("classname")),
|
| 405 |
array("data" => t("renewable energy"), "field" => "tokwh0"),
|
| 406 |
array("data" => t("non renewable energy"), "field" => "tokwh1"),
|
| 407 |
array("data" => t("co2")),
|
| 408 |
array("data" => t("params"))
|
| 409 |
);
|
| 410 |
|
| 411 |
|
| 412 |
$rows[] = array(
|
| 413 |
_format_date($node->created),
|
| 414 |
$node->model,
|
| 415 |
$node->sector,
|
| 416 |
$node->code,
|
| 417 |
$node->units,
|
| 418 |
$node->classname,
|
| 419 |
$node->tokwh0,
|
| 420 |
$node->tokwh1,
|
| 421 |
$node->toco2,
|
| 422 |
$node->params
|
| 423 |
);
|
| 424 |
|
| 425 |
$output = theme('table', $header, $rows);
|
| 426 |
// $output .= theme('pager', NULL, 50, 0);
|
| 427 |
|
| 428 |
return "<h1></h1>".$output; // the H1s make bluebreeze work$output;
|
| 429 |
}
|
| 430 |
|
| 431 |
|
| 432 |
/**
|
| 433 |
* @return the class for a given numeric index. If index is null, return keys (names) for all classes
|
| 434 |
*/
|
| 435 |
|
| 436 |
function _get_source_class_key($i)
|
| 437 |
{
|
| 438 |
global $source_class_keys ;
|
| 439 |
if (empty($source_class_keys))
|
| 440 |
$source_class_keys = array_keys(_source_class_map());
|
| 441 |
if (!isset($i)) return $source_class_keys ;
|
| 442 |
return $source_class_keys[$i] ;
|
| 443 |
}
|
| 444 |
|
| 445 |
|
| 446 |
/**
|
| 447 |
* Each carbon source can have a different classname.
|
| 448 |
* Invoked from carbon_source.inc
|
| 449 |
*
|
| 450 |
* @return a map of class names (keys) and descriptions (values). Descriptions (values) not used yet.
|
| 451 |
*/
|
| 452 |
|
| 453 |
function _source_class_map()
|
| 454 |
{
|
| 455 |
return array(
|
| 456 |
"simple" => t("simple multiplier"),
|
| 457 |
"climatecare" => t("model used by commercial offsetting company"),
|
| 458 |
"chooseclimate" => t("model created by climate mathematician")
|
| 459 |
);
|
| 460 |
}
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
/**
|
| 465 |
* @return the text to present to the user if no carbon sources are configured.
|
| 466 |
*/
|
| 467 |
|
| 468 |
|
| 469 |
function carbon_source_missing()
|
| 470 |
{
|
| 471 |
return t(
|
| 472 |
"<p class='message'>There are no available carbon sources. If you think you have already ".
|
| 473 |
"configured or installed them, then try removing any carbon source model filter at !model. <p>".
|
| 474 |
|
| 475 |
"<p class='message'>If you have never installed carbon sources, !create or !install.</p>",
|
| 476 |
|
| 477 |
array("!model"=>l(t("Home » Administer » Site configuration » Carbon >> Settings"),
|
| 478 |
"admin/settings/carbon/settings"),
|
| 479 |
"!create"=>l(t("create you own carbon sources"),
|
| 480 |
"node/add/carbon-source"),
|
| 481 |
"!install"=> l(t("install the default carbon sources"),
|
| 482 |
"admin/settings/carbon/database"))
|
| 483 |
);
|
| 484 |
}
|
| 485 |
|
| 486 |
|
| 487 |
/**
|
| 488 |
* This function is called backed from the menu (defined in carbon.module)
|
| 489 |
* On first entry, the form is set up. On second entry, the action
|
| 490 |
* is executed.
|
| 491 |
*/
|
| 492 |
|
| 493 |
function carbon_source_install() {
|
| 494 |
|
| 495 |
$edit = $_POST;
|
| 496 |
if ($edit["confirm"] && $edit["form_id"] = "carbon_source_install_confirm")
|
| 497 |
return carbon_source_install_default();
|
| 498 |
|
| 499 |
return confirm_form(array(),
|
| 500 |
t("Are you sure you want to install the default carbon sources"),
|
| 501 |
"admin/settings/carbon/source/install",
|
| 502 |
t("This action cannot be undone."),
|
| 503 |
t("Install"), t("Cancel"));
|
| 504 |
}
|
| 505 |
|
| 506 |
|
| 507 |
/**
|
| 508 |
* Provides utilities to access all possible source keys
|
| 509 |
*
|
| 510 |
*/
|
| 511 |
|
| 512 |
|
| 513 |
class CarbonSources
|
| 514 |
{
|
| 515 |
var $source = array() ;
|
| 516 |
var $map = array() ;
|
| 517 |
var $keys = array();
|
| 518 |
|
| 519 |
static function getDefault()
|
| 520 |
{
|
| 521 |
global $defaultSources ;
|
| 522 |
if (empty($defaultSources))
|
| 523 |
$defaultSources = new CarbonSources();
|
| 524 |
return ($defaultSources);
|
| 525 |
}
|
| 526 |
|
| 527 |
function CarbonSources()
|
| 528 |
{
|
| 529 |
$queryResult = db_query(_get_carbon_source_sql());
|
| 530 |
|
| 531 |
while ($node = db_fetch_object($queryResult))
|
| 532 |
{
|
| 533 |
$this->source[] = $node ;
|
| 534 |
}
|
| 535 |
|
| 536 |
foreach($this->source as $s)
|
| 537 |
{
|
| 538 |
$this->map[$s->code] = $s->title. ', ' . $s->units ;
|
| 539 |
}
|
| 540 |
ksort($this->map);
|
| 541 |
|
| 542 |
$this->keys = array_keys($this->map);
|
| 543 |
$this->values = array_values($this->map);
|
| 544 |
}
|
| 545 |
|
| 546 |
|
| 547 |
/**
|
| 548 |
* returns a map of key=> value pairs where the
|
| 549 |
* key is the code in the database and
|
| 550 |
* and the value is text presented to the user
|
| 551 |
*
|
| 552 |
* note that adding sources might while a user
|
| 553 |
* is selecting might result in the wrong option.
|
| 554 |
*/
|
| 555 |
|
| 556 |
function getMap()
|
| 557 |
{
|
| 558 |
return $this->map ;
|
| 559 |
}
|
| 560 |
|
| 561 |
function keys()
|
| 562 |
{
|
| 563 |
return $this->keys ;
|
| 564 |
}
|
| 565 |
|
| 566 |
function values()
|
| 567 |
{
|
| 568 |
return $this->values ;
|
| 569 |
}
|
| 570 |
|
| 571 |
function getModelMap()
|
| 572 |
{
|
| 573 |
$map = array();
|
| 574 |
$map[''] = '-no preference-' ;
|
| 575 |
foreach($this->source as $s)
|
| 576 |
{
|
| 577 |
$map[$s->model] = $s->model ;
|
| 578 |
}
|
| 579 |
ksort($map);
|
| 580 |
return $map ;
|
| 581 |
}
|
| 582 |
}
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
class Source
|
| 587 |
{
|
| 588 |
}
|
| 589 |
|
| 590 |
|
| 591 |
function carbon_source_install_default()
|
| 592 |
{
|
| 593 |
global $user ;
|
| 594 |
$sources = _get_sources();
|
| 595 |
$insert = $update = 0 ;
|
| 596 |
foreach ($sources as $source)
|
| 597 |
{
|
| 598 |
$model = $source[2];
|
| 599 |
$code = $source[3];
|
| 600 |
|
| 601 |
// this routine is unused for updates as well as inserts.
|
| 602 |
// get the nid for this particular source
|
| 603 |
$node = db_fetch_object(db_query("SELECT nid " .
|
| 604 |
"FROM {carbon_source} WHERE model = '%s' AND code = '%s' ", $model, $code));
|
| 605 |
|
| 606 |
// now get the whole object
|
| 607 |
if (!empty($node))
|
| 608 |
{
|
| 609 |
// fiddling around here so that only the nid is visible
|
| 610 |
// to node_load.
|
| 611 |
$node2 = new Source();
|
| 612 |
$node2->nid = $node->nid ;
|
| 613 |
$node = node_load($node2->nid);
|
| 614 |
$update++ ;
|
| 615 |
}
|
| 616 |
else
|
| 617 |
{
|
| 618 |
$node = new Source();
|
| 619 |
$node->nid = 0 ;
|
| 620 |
$insert++ ;
|
| 621 |
}
|
| 622 |
|
| 623 |
$i = 0 ;
|
| 624 |
$node->sector = $source[$i++];
|
| 625 |
$node->title = $source[$i++];
|
| 626 |
$node->model = $source[$i++];
|
| 627 |
$node->code = $source[$i++];
|
| 628 |
$node->units = $source[$i++];
|
| 629 |
$node->classname = $source[$i++];
|
| 630 |
$node->tokwh0 = $source[$i++];
|
| 631 |
$node->tokwh1 = $source[$i++];
|
| 632 |
$node->toco2 = $source[$i++];
|
| 633 |
$node->params = $source[$i++];
|
| 634 |
$node->type = 'carbon_source' ;
|
| 635 |
$node->status = 1 ; // published
|
| 636 |
$node->promote = 0 ;
|
| 637 |
$node->sticky = 0;
|
| 638 |
$node->body = null ;
|
| 639 |
$node->comment = 1 ;
|
| 640 |
$node->uid = $user->uid ;
|
| 641 |
$node->teaser = "" ;
|
| 642 |
$node->body = "" ;
|
| 643 |
node_save($node);
|
| 644 |
}
|
| 645 |
|
| 646 |
// Notify of changes
|
| 647 |
drupal_set_message("Carbon module: inserted ".$insert.", updated ".$update." sources of emissions", "info");
|
| 648 |
}
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
function _get_sources()
|
| 654 |
{
|
| 655 |
return array(
|
| 656 |
|
| 657 |
// CRAG numbers are based on COIN info - http://coinet.org.uk/solutions/carbon_rationing
|
| 658 |
|
| 659 |
array("heating","natural gas","crag","gas-m3","cubic metres","simple","0.0","0.0","2.2",""),
|
| 660 |
array("heating","natural gas","crag","gas-therms","therms","simple","0.0","0.0","6.2",""),
|
| 661 |
array("heating","natural gas","crag","gas-kwh","kWh","simple","0.0","0.0","0.2",""),
|
| 662 |
|
| 663 |
array("heating","heating oil","crag","oil-litres","litres","simple","0.0","0.0","3",""),
|
| 664 |
array("heating","heating oil","crag","oil-gallons","gallons","simple","0.0","0.0","13.6",""),
|
| 665 |
|
| 666 |
array("heating","anthracite coal","crag","coal-anthracite","25 Kg bags","simple","0.0","0.0","1.9",""),
|
| 667 |
array("heating","bituminous coal","crag","coal-bituminous","25 Kg bags","simple","0.0","0.0","2.5",""),
|
| 668 |
|
| 669 |
array("elec","conventional tariff electricity","crag","elec-kwh","kWh","simple","0.0","0.0","0.5",""),
|
| 670 |
array("elec","green tariff electricity","crag","elec-green-kwh","kWh","simple","0.0","0.0","0",""),
|
| 671 |
|
| 672 |
array("air","air travel","crag","air-miles","miles","simple","0.0","0.0","0.82",""),
|
| 673 |
array("air","air travel","crag","air-km","Km","simple","0.0","0.0","0.51",""),
|
| 674 |
array("air","air travel","crag","air-co2-kg","Kg CO2","simple","0.0","0.0","1",""),
|
| 675 |
|
| 676 |
array("car","small car","crag","car-small-km","Km","simple","0.0","0.0","0.17",""),
|
| 677 |
array("car","small car","crag","car-small-miles","miles","simple","0.0","0.0","0.28",""),
|
| 678 |
array("car","medium car","crag","car-med-km","Km","simple","0.0","0.0","0.22",""),
|
| 679 |
array("car","medium car","crag","car-med-miles","miles","simple","0.0","0.0","0.36",""),
|
| 680 |
array("car","large car","crag","car-large-km","Km","simple","0.0","0.0","0.27",""),
|
| 681 |
array("car","large car","crag","car-large-miles","miles","simple","0.0","0.0","0.43",""),
|
| 682 |
array("car","small diesel car","crag","car-d-small-km","Km","simple","0.0","0.0","0.12",""),
|
| 683 |
array("car","small diesel car","crag","car-d-small-miles","miles","simple","0.0","0.0","0.19",""),
|
| 684 |
array("car","large diesel car","crag","car-d-large-km","Km","simple","0.0","0.0","0.14",""),
|
| 685 |
array("car","large diesel car","crag","car-d-large-miles","miles","simple","0.0","0.0","0.22",""),
|
| 686 |
array("car","LPG car","crag","car-lpg-km","Km","simple","0.0","0.0","0.17",""),
|
| 687 |
array("car","LPG car","crag","car-lpg-miles","miles","simple","0.0","0.0","0.28",""),
|
| 688 |
|
| 689 |
array("car","petrol","crag","petrol-litres","litres","simple","0.0","0.0","2.3",""),
|
| 690 |
array("car","petrol","crag","petrol-gallons","gallons","simple","0.0","0.0","10.4",""),
|
| 691 |
array("car","diesel","crag","diesel-litres","litres","simple","0.0","0.0","2.7",""),
|
| 692 |
array("car","diesel","crag","diesel-gallons","gallons","simple","0.0","0.0","12.2",""),
|
| 693 |
|
| 694 |
array("surface","public transport","crag","pt-miles","miles","simple","0.0","0.0","0.11",""),
|
| 695 |
array("surface","public transport","crag","pt-km","Km","simple","0.0","0.0","0.07",""),
|
| 696 |
|
| 697 |
array("elec","electricity","carbon coach","elec-gbp","pounds","simple","0.0","0.0","5",""),
|
| 698 |
array("heating","natural gas","carbon coach","gas-gbp","pounds","simple","0.0","0.0","10",""),
|
| 699 |
array("car","petrol","carbon coach","petrol-gbp","pounds","simple","0.0","0.0","2.5",""),
|
| 700 |
array("surface","public transport","carbon coach","pt-miles","miles","simple","0.0","0.0","0.1",""),
|
| 701 |
array("air","air travel, short haul","carbon coach","air-short-hours","hours","simple","0.0","0.0","333",""),
|
| 702 |
array("air","air travel, long haul","carbon coach","air-long-hours","hours","simple","0.0","0.0","250",""),
|
| 703 |
|
| 704 |
// http://www.resurgence.org/carboncalculator/
|
| 705 |
|
| 706 |
array("elec","electricity","resurgence","elec-kwh","kWh","simple","0.0","0.0","0.43",""),
|
| 707 |
array("heating","natural gas","resurgence","gas-kwh","kWh","simple","0.0","0.0","0.19",""),
|
| 708 |
array("heating","natural gas","resurgence","gas-therms","therms","simple","0.0","0.0","5.5",""),
|
| 709 |
array("heating","natural gas","resurgence","gas-m3","cubic metres","simple","0.0","0.0","1.77",""),
|
| 710 |
array("heating","LPG","resurgence","lpg-litres","litres","simple","0.0","0.0","1.51",""),
|
| 711 |
array("heating","butane","resurgence","butane-4.5","4.5 Kg bottles","simple","0.0","0.0","11.82",""),
|
| 712 |
array("heating","butane","resurgence","butane-7","7 Kg bottles","simple","0.0","0.0","18.39",""),
|
| 713 |
array("heating","butane","resurgence","butane-15","15 Kg bottles","simple","0.0","0.0","39.41",""),
|
| 714 |
array("heating","propane","resurgence","propane-3.9","3.9 Kg bottles","simple","0.0","0.0","11.48",""),
|
| 715 |
array("heating","propane","resurgence","propane-6","6 Kg bottles","simple","0.0","0.0","17.67",""),
|
| 716 |
array("heating","propane","resurgence","propane-13","13 Kg bottles","simple","0.0","0.0","38.28",""),
|
| 717 |
array("heating","propane","resurgence","propane-19.5","19.5 Kg bottles","simple","0.0","0.0","57.42",""),
|
| 718 |
array("heating","heating oil","resurgence","oil-litres","litres","simple","0.0","0.0","2.68",""),
|
| 719 |
array("heating","wood","resurgence","wood-kg","Kg","simple","0.0","0.0","0.518",""),
|
| 720 |
array("heating","bituminous coal","resurgence","coal-bituminous","25 Kg bags","simple","0.0","0.0","2.41",""),
|
| 721 |
|
| 722 |
array("air","air travel","resurgence","air-hours","hours","simple","0.0","0.0","435",""),
|
| 723 |
|
| 724 |
array("car","petrol","resurgence","petrol-litres","litres","simple","0.0","0.0","2.31",""),
|
| 725 |
array("car","diesel","resurgence","diesel-litres","litres","simple","0.0","0.0","2.68",""),
|
| 726 |
array("car","kerosene","resurgence","kerosene-litres","litres","simple","0.0","0.0","2.52",""),
|
| 727 |
|
| 728 |
array("air","air travel (Boeing 747)","choose climate","air-747-km","Km","chooseclimate","0.0","0.0","1.0",""),
|
| 729 |
array("air","air travel (Boeing 747)","choose climate","air-747-miles","miles","chooseclimate","0.0","0.0","1.609",""),
|
| 730 |
|
| 731 |
array("air","air travel","climate care","air-km","Km","climatecare","0.0","0.0","1.0",""),
|
| 732 |
array("air","air travel","climate care","air-miles","miles","climatecare","0.0","0.0","1.609",""),
|
| 733 |
|
| 734 |
// this is for people in groups who prefer to use a speadsheet and work offline
|
| 735 |
|
| 736 |
array("mixed", "co2 calculated offline","crag","co2-kg","Kg","simple","0.0","0.0","1.0","")
|
| 737 |
);
|
| 738 |
}
|
| 739 |
|
| 740 |
?>
|