/[drupal]/contributions/modules/carbon/carbon.module
ViewVC logotype

Contents of /contributions/modules/carbon/carbon.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.34 - (show annotations) (download) (as text)
Tue May 12 13:48:37 2009 UTC (6 months, 2 weeks ago) by johnackers
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, HEAD
Changes since 1.33: +12 -11 lines
File MIME type: text/x-php
changed text and legth of some settings fields. Remove superflous spaces generated by _format_date
1 <?php
2 /**
3 * $Id: carbon.module,v 1.33 2009/05/05 14:45:41 johnackers Exp $
4 *
5 * Drupal carbon module
6 *
7 * Created on 6-Sep-2006
8 *
9 * @file
10 * This file implements the drupal module hooks including
11 * _help, _menu, _user and _block. It also implements
12 * _access for all the carbon nodes.
13 */
14
15
16
17 define("CARBON_PATH", drupal_get_path("module", "carbon"));
18 define("CARBON_PALETTE", "heating=AA0000, elec=0000AA, car=AA00AA, surface=00AA00, air=5555FF, mixed=808080");
19
20 // core routines that handle basic database ops
21
22 include(CARBON_PATH ."/carbon_account.inc"); // create/delete carbon account
23 include(CARBON_PATH ."/carbon_stamp.inc"); // create/edit/list meter readings etc
24 include(CARBON_PATH ."/carbon_source.inc"); // create/edit/list sources eg. gas, elec
25 include(CARBON_PATH ."/carbon_transfer.inc"); // create/edit/list carbon transfers
26
27 include(CARBON_PATH ."/carbon_view.inc"); // generates overview/meter/travel pane
28 include(CARBON_PATH ."/carbon_edit.inc"); // handles in situ editing
29
30 // included dynamically by drupal's menu mechanism or using require_once():
31
32 //include(CARBON_PATH ."/carbon_admin.inc"); // settings and database integrity check
33 //include(CARBON_PATH ."/carbon_report.inc"); // group and sector reports
34 //include(CARBON_PATH ."/carbon_crag.inc"); // crag reports
35
36
37 //include(CARBON_PATH ."/carbon_calculate.inc"); // co2 & kWh settings
38 //include(CARBON_PATH ."/carbon_chart.inc"); // generate bar and pie PNGs
39
40
41 /**
42 * Display help and module information
43 * @param section which section of the site we're displaying help
44 * @return help text for section
45 */
46 /*
47 function carbon_help($section='', $arg) {
48
49 $output = '';
50
51 switch ($section) {
52 case "admin/modules#description":
53 $output = t("Allows users to create and maintain online carbon accounts. " .
54 "By entering utility meter readings, gas/petrol consumption and trips " .
55 "by air, users can build an accurate record of CO2 emissions about " .
56 "which they have some influence. Users can also view records submitted " .
57 "by others. Administrators can add, adjust and remove different sources " .
58 "of carbon emissions.");
59 break ;
60
61 case "help#carbon_source":
62 return t("Display a list of all carbon sources.");
63
64
65 case "help#carbon_account":
66 return t("Display all public accounts.");
67
68
69 case "user/help#carbon":
70 return t("<p>The carbon module allows you to create a personal <em>carbon account</em>.</p>" .
71 "<p>You can</p>" .
72 "<ul>" .
73 "<li>Submit a carbon account at <a href='%node-add-carbon-account'>create content &gt;&gt; carbon account</a>. " .
74 "You can create more than one account.</li>" .
75 "<li>Look at your account at <a href='%node-my-carbon-account'>carbon &gt;&gt; account &gt;&gt; my</a>.</li>" .
76 "<li>Each account contains carbon stamps which can be viewed at <a href='%node-my-carbon-stamp'>carbon &gt;&gt; stamp &gt;&gt; my </a> </li>" .
77 "<li>Display other carbon accounts that others want to share with you <a href='%node-list-carbon-account'>carbon &gt;&gt; account</a></li>" .
78 "<li>Display a list of carbon sources at <a href='%node-list-carbon-source'>carbon &gt;&gt; stamp</a> </li>" .
79 "<li>configure carbon accounts at <a href='%admin-node-configure-types'> administer &gt;&gt; content &gt;&gt; " .
80 "configure types &gt;&gt; carbon accounts configure</a>.</li>" .
81 "</ul>", array(
82 "%node-add-carbon-account" => url("node/add/carbon-account"),
83 "%node-my-carbon-account" => url("carbon/account/my"),
84 "%node-my-carbon-stamp" => url("carbon/stamp/my"),
85 "%node-list-carbon-account" => url("carbon/account"),
86 "%node-list-carbon-source" => url("carbon/source"),
87 "%admin-node-configure-types" => url("admin/node/configure/types")
88 ));
89
90
91 case "admin/help#carbon":
92 return t("<p>The carbon module allows users to create a personal <em>carbon account</em>.</p>" .
93 "<p>The optional menu entries for users are not all enabled by default. " .
94 "You might want to add them but they are not necessary." .
95 "<p/> " .
96 "<p>You can add and remove individual carbon sources by editing it and checking " .
97 "or unchecking the 'published' box in the 'Publishing Options' group ." .
98 "<p/> " .
99 "",
100 array(
101 "%node-add-carbon-account" => url("node/add/carbon-account"),
102 "%node-list-carbon-stamp" => url("admin/carbon/stamp"),
103 "%node-list-carbon-account" => url("admin/carbon/account"),
104 "%node-list-carbon-source" => url("admin/carbon/source"),
105 "%admin-node-configure-types" => url("admin/node/configure/types"),
106 "%admin-carbon-settings" => url("admin/carbon/settings")
107 ));
108
109 }
110 return $output;
111
112 }
113 */
114
115
116 function carbon_include_core_js()
117 {
118 // load the basic javascript routines
119 $path = drupal_get_path("module", 'carbon');
120 drupal_add_js($path . "/carbon.js", 'module');
121
122 // enhance fieldset operations
123 drupal_add_js('misc/collapse.js', 'core', 'header', FALSE, TRUE);
124
125 // all css in this file, leading slash needed
126 drupal_add_css($path . "/misc/carbon.css");
127
128 return $path ;
129 }
130
131
132 /**
133 * Implementation of hook_node_info().
134 * Define the node type 4.7?
135 */
136
137 function carbon_node_info() {
138 return array(
139 "carbon_account" => array("name" => t("Carbon Account"),
140 "module" => "carbon_account",
141 "description"=> t("A record of personal carbon emissions covering multiple " .
142 "years. The account can be appended and updated at at any time " .
143 "future. Previous trips etc can be added to build a account for " .
144 "previous years. A registered user can create and update accounts for " .
145 "many others. Accounts can be made public so they can be shared with " .
146 "other logged in users. " .
147 "")),
148
149 "carbon_stamp" => array("name" => t("Carbon Stamp"),
150 "title_label" => t("Description"),
151 "has_body" => false,
152 "module" => "carbon_stamp",
153 "description"=> t("A single record of carbon emissions such as ongoing household electricity ".
154 "use or a daily commute or a single air trip." )),
155 /*
156 "carbon_meter" => array("name" => t("Meter"),
157 "module" => "carbon_meter",
158 "description"=> t("A utility meter e.g. gas or electricity or car mileometer.")),
159
160 "carbon_reading" => array("name" => t("Meter reading"),
161 "module" => "carbon_reading",
162 "description"=> t("A reading taken from a utility meter or a car mileometer.")),
163 */
164 "carbon_transfer" => array("name" => t("Carbon Transfer"),
165 "title_label" => t("Description"),
166 "has_body" => false,
167 "module" => "carbon_transfer",
168 "description"=> t("Transfer of carbon credit from one carbon account to another ".
169 "account"),
170 'has_title' => true,
171 'has_body' => false),
172
173 "carbon_source" => array("name" => t("Carbon Source"),
174 "module" => "carbon_source",
175 "description"=> t("A source of emissions such as a new type of fuel or a type of " .
176 "travel that is not already held in the database. All the usual sources of " .
177 "emissions such as gas, electricity and car are normally configured by the " .
178 "administrator or <em>carbon accountant</em> at installation time.")));
179 }
180
181 /**
182 * only carbon_account has extra tabs
183 *
184 * @param $op
185 * @param $node may or may not be a carbon_account
186 * @return boolean
187 */
188
189 function carbon_tab_access($op, $node)
190 {
191 $ab = $op ;
192 return $node->type == "carbon_account";
193 }
194
195
196 /**
197 * Valid permissions for this module. Note that these
198 * permissions are accessed principally by the menu
199 * system to determine which options should be presented
200 * to the user.
201 *
202 * Access to specific carbon accounts is determined by the
203 * user under preferences under the account node.
204 *
205 * @return An array of valid permissions this module
206 */
207 function carbon_perm() {
208
209 return array(
210 //"access content" // gives access to node/12xx34, implemented by drupal core
211 "access accounts", // give access to carbon/acccount and carbon/stamp pages
212 "create/edit account",
213 "administer accounts", // carbon accountant?
214 "administer carbon nodes", // can do anything
215 );
216 }
217
218
219 /**
220 * Implementation of hook_access().
221 *
222 * The carbon accounts are protected because we dont want personal data to be
223 * displayed to other users without the permission of that user, same goes for
224 * carbon_stamps. Carbon sources do not matter as the data is impersonal,
225 * just need to be granted 'access content'.
226 *
227 * Note that $node->protection, $node->type and $node->uid are required
228 *
229 * @param create | view | update | delete
230 * @param node->type OR the node itself (crazy API)
231 */
232
233 function carbon_access($op, $node) {
234 global $user;
235
236 $node_type = is_object($node) ? $node->type : $node ;
237
238 switch ($op) {
239 case 'meter' :
240 case 'travel' :
241 if ($node_type != "carbon_account") return false ;
242 }
243
244 // let annonymous users view carbon sources if node access permitted
245
246 if ($op == "view" && $node_type == "carbon_source")
247 return (user_access("access content")) ;
248
249 $node_protection = is_object($node) ? $node->protection : 0 ;
250 $uid = is_object($node) ? $node->uid : 0 ;
251
252 $carbon_data = $node_type == "carbon_account"
253 || $node_type == "carbon_stamp"
254 || $node_type == "carbon_source"
255 || $node_type == "carbon_transfer" ;
256
257 if ($op == "view")
258 {
259 if ($carbon_data)
260 {
261 // public nodes viewable to all
262 if ($node_protection >= 3)
263 return TRUE;
264
265 // public nodes viewable to logged in users
266 if ($user->uid != 0)
267 if ($node_protection >= 2)
268 return TRUE;
269
270 // my own node
271 if (user_access("create/edit account") && ($user->uid == $uid))
272 return TRUE;
273
274 if ($node_protection >= 1)
275 if (!empty($node->shared_uid))
276 if ($node->shared_uid == $user->uid)
277 return TRUE;
278
279 if (user_access("administer accounts"))
280 return TRUE;
281 }
282 }
283
284 if ($op == "create" || $op == "update" || $op == "delete")
285 {
286 if ($carbon_data)
287 {
288 if (user_access("create/edit account") && ($op == "update") && ($user->uid == $uid))
289 return TRUE;
290
291 if (user_access("create/edit account") && ($op == "delete") && ($user->uid == $uid))
292 return TRUE;
293
294 if (user_access("create/edit account") && ($op == "create"))
295 return TRUE;
296
297 if (user_access("administer accounts"))
298 return TRUE;
299 }
300 }
301
302 if (user_access("administer carbon nodes"))
303 if ($carbon_data)
304 return TRUE;
305
306 return FALSE ;
307 }
308
309
310 /**
311 * menu hook
312 * @return array of menu items
313 */
314 function carbon_menu()
315 {
316 global $user ;
317 $items = array();
318 /*
319 $items['carbon/help'] = array(
320 'title' => 'Carbon', // does not work
321 'description' => 'Carbon help',
322
323 'access callback' => 'user_access',
324 'access arguments' => array('access accounts'),
325
326 'page callback' => 'carbon_help',
327 'page arguments' => array("user/help#carbon",2)
328 );
329 */
330
331 //--------------------------------------------------------------
332 // set tabs around the account node
333 // these provide the main access to
334 // the carbon account
335 //--------------------------------------------------------------
336
337
338 $items["node/%node/meter"] = array(
339 'title' => 'Meters',
340
341 'access callback' => 'carbon_tab_access',
342 'access arguments' => array(2,1),
343
344 'file path' => CARBON_PATH,
345 'file' => 'carbon_view.inc',
346
347 'page callback' => 'carbon_view',
348 'page arguments' => array(1,2),
349 'weight' => -2,
350 'type' => MENU_LOCAL_TASK
351 );
352
353 $items["node/%node/travel"] = array(
354 'title' => 'Travel and other',
355
356 'file path' => CARBON_PATH,
357 'file' => 'carbon_view.inc',
358
359 'access callback' => 'carbon_tab_access',
360 'access arguments' => array(2,1),
361
362 'page callback' => 'carbon_view',
363 'page arguments' => array(1,2),
364 'weight' => -1,
365 'type' => MENU_LOCAL_TASK
366 );
367
368 //--------------------------------------------------------------
369 // add another tab to the user's profile
370 // users can have multiple accounts,
371 // we list them here.
372 //--------------------------------------------------------------
373
374 $items["user/%user/carbon"] = array(
375 'title' => 'Carbon acounts',
376 'description' => 'Carbon accounts owned by user',
377 'access callback' => 'user_access',
378 'access arguments' => array('access accounts'),
379
380 'file path' => CARBON_PATH,
381 'file' => 'carbon_report.inc',
382
383 'page callback' => 'carbon_report_create',
384 'page arguments' => array(1, false), // $user, no display query
385 'weight' => 10,
386 'type' => MENU_LOCAL_TASK
387 );
388
389 $items["user/%user/stamp"] = array(
390 'title' => 'Carbon stamps',
391 'description' => 'Carbon stamps owned by user',
392 'access callback' => 'user_access',
393 'access arguments' => array('access accounts'),
394
395 'page callback' => 'carbon_stamp_page',
396 'page arguments' => array(null,1,null),
397
398 'weight' => 10,
399 'type' => MENU_LOCAL_TASK
400 );
401
402 //--------------------------------------------------------------
403 // accessing groups of accounts, sector reports
404 // supports ajax calls
405 //--------------------------------------------------------------
406
407 $items["carbon/report"] = array(
408 'title' => "Carbon account summary",
409 'description' => 'summary of accounts, parameters specified in GET parameters',
410 'access callback' => 'user_access',
411 'access arguments' => array('access accounts'),
412
413 'file path' => CARBON_PATH,
414 'file' => 'carbon_report.inc',
415
416 'page callback' => 'carbon_report_create',
417 'page arguments' => array(null, true), // $target_user, displayForm
418 'type' => MENU_CALLBACK
419 );
420
421
422 $items['carbon/%node/transfer'] = array(
423 'title' => 'Carbon trades',
424 'description' => 'movement between accounts',
425
426 'access callback' => 'user_access',
427 'access arguments' => array('access accounts'),
428
429 // 'file path' => CARBON_PATH,
430 // 'file' => 'carbon_transfer.inc',
431
432 'page callback' => 'carbon_transfer_page',
433 'page arguments' => array(1),
434
435 'type' => MENU_CALLBACK
436 );
437
438 $items['carbon/transfer'] = array(
439 'title' => 'Carbon trades',
440 'description' => 'movement between accounts',
441
442 'access callback' => 'user_access',
443 'access arguments' => array('access accounts'),
444
445 // 'file path' => CARBON_PATH,
446 // 'file' => 'carbon_transfer.inc',
447
448 'page callback' => 'carbon_transfer_page',
449 'page arguments' => array(null),
450
451 'type' => MENU_CALLBACK
452 );
453
454
455
456
457
458
459 $items['carbon/share'] = array(
460 'title' => 'Carbon Account sharing',
461 'description' => 'Adjust how i want to share?',
462
463 'access callback' => 'user_access',
464 'access arguments' => array('access accounts'),
465
466 'page callback' => 'carbon_view_consolidated',
467 'page arguments' => array($user->uid),
468
469 'type' => MENU_CALLBACK
470 );
471
472 // admin type functions which are not put into the
473 // drupal menu structure. Perhaps they should
474
475 $items["carbon/source"] = array(
476 'title' => "Carbon sources",
477 'description' => "list/edit all sources of carbon emissions",
478 'access callback' => 'carbon_access',
479 'access arguments' => array('view', 'carbon_source'),
480
481 'page callback' => 'carbon_source_page',
482 'page arguments' => array(null),
483
484 'type' => MENU_NORMAL_ITEM
485 );
486
487
488 $items["carbon/source/install"] = array(
489 'title' => "Install default carbon sources",
490
491 'access callback' => "user_access",
492 'access arguments' => array("administer carbon nodes"),
493
494 'page callback' => "carbon_source_install",
495 'page arguments' => array(),
496
497 'type' => MENU_CALLBACK
498 );
499
500
501 $items["admin/settings/carbon"] = array(
502 'title' => "Carbon settings",
503 'description' => "Settings, administrative functions and database checks.",
504
505 'access callback' => "user_access",
506 'access arguments' => array("administer carbon nodes"),
507
508 'page callback' => "drupal_get_form",
509 'page arguments' => array("carbon_admin_settings"),
510 'type' => MENU_NORMAL_ITEM
511 );
512
513
514 $items["admin/settings/carbon/settings"] = array(
515 'title' => "Settings",
516
517 'access callback' => "user_access",
518 'access arguments' => array("administer carbon nodes"),
519
520 'page callback' => "drupal_get_form",
521 'page arguments' => array("carbon_admin_settings"),
522 'type' => MENU_DEFAULT_LOCAL_TASK
523 );
524
525
526 $items["admin/settings/carbon/database"] = array(
527 "title" => "Database check and repair",
528
529 'access callback' => "user_access",
530 'access arguments' => array("administer carbon nodes"),
531
532 'page callback' => "carbon_admin",
533 'page arguments' => array(),
534
535 'file path' => drupal_get_path("module", "carbon"),
536 'file' => 'carbon_admin.inc',
537 'weight' => +10,
538 'type' => MENU_LOCAL_TASK
539 );
540
541 // load a form as a row at the bottom of an
542 // existing page so that it can be edited insitu
543
544 // %node/table-name/add
545
546 $items["carbon/%node/%/add"] = array(
547 "title" => "Add carbon node",
548
549 'access callback' => 'node_access',
550 'access arguments' => array('view', 1),
551
552 'file path' => drupal_get_path("module", "carbon"),
553 'file' => 'carbon_edit.inc',
554
555 'page callback' => "carbon_ajax_edit",
556 'page arguments' => array(1,2,3,null), // note arguments
557 );
558
559 // load a form onto a row on an existing page so
560 // that it can be edited insitu
561
562 // %node/table-name/edit|clone/%node
563
564 $items["carbon/%node/%/%/%node"] = array(
565 "title" => "Edit carbon node",
566
567 'access callback' => 'node_access',
568 'access arguments' => array('view', 4),
569
570 'file path' => drupal_get_path("module", "carbon"),
571 'file' => 'carbon_edit.inc',
572
573 'page callback' => "carbon_ajax_edit",
574 'page arguments' => array(1,2,3,4),
575 );
576 return $items;
577 }
578
579
580 /**
581 * Implementation of hook_block().
582 * Declare the name of this block, later we will generate HTML for the carbon block
583 * @param op the operation from the URL
584 * @param delta offset
585 * @returns block HTML
586 */
587
588 function carbon_block($op='list', $delta=0, $edit = array()) {
589 global $user ;
590
591 // variable_set('menu_rebuild_needed', true); // for debugging
592
593 // listing of blocks, such as on the admin/block page
594 if ($op == "list") {
595 $block[0]["info"] = t(" Carbon accounts");
596 return $block;
597
598 }
599 else if ($op == "view")
600 {
601 require_once(CARBON_PATH ."/carbon_report.inc");
602 $block_size = variable_get("carbon_account_block_items",5);
603 $accountMap = new CarbonAccountMap();
604 $accountMap->init()->sortByRevisionDate();
605
606 foreach ($accountMap->account_map as $account) {
607 if ($block_size-- <= 0 ) break ;
608 $block_content .= carbon_link_from_title($account, $account, "") . '<br />';
609 }
610
611 // check to see if there was any content before setting up the block
612 if (empty($block_content)) {
613 return;
614 }
615 // set up the block
616 $block["subject"] = "Active carbon accounts";
617 $block["content"] = $block_content;
618 return $block;
619 }
620 else if ($op == "configure" && $delta == 0) {
621 $form["items"] = array(
622 "#type" => "select",
623 "#title" => t("Number of items"),
624 "#default_value" => variable_get("carbon_account_block_items", 10),
625 "#options" => drupal_map_assoc(array(2, 3, 5, 8, 10, 12, 15, 20, 25, 30, 40, 50, 70, 100))
626 );
627 return $form ;
628 }
629 else if ($op == "save" && $delta == 0) {
630 variable_set("carbon_account_block_items", $edit["items"]);
631 }
632 }
633
634
635 /*
636 * implementation of the (rather annoying) hook_theme
637 * These declarations are required for drupal 6
638 *
639 * They are only read when the module is enabled (i think)
640 */
641
642 function carbon_theme($existing, $type, $theme, $path)
643 {
644 return array(
645 'carbon_source_table_view' => array(
646 'arguments' => array('node' => NULL)),
647 'carbon_stamp_view' => array(
648 'arguments' => array('node' => NULL)),
649 'table_inside_form' => array( // not sure this is useful
650 'arguments' => array('header' => NULL, 'rows')),
651 'carbon_editable_row_form' => array(
652 'arguments' => array('form'=>NULL)),
653 'carbon_static_box' => array(
654 'arguments' => array('title' => NULL, 'body' => NULL)),
655 'carbon_loadable_box' => array(
656 'arguments' => array('title' => NULL, 'url' => NULL))
657 );
658 }
659
660
661 /**
662 * create a set of links that can be appended to a table of stamps,
663 * accounts or sources.
664 *
665 * Note that $node->type, $node->organization, $node->uid, $node->name
666 * and other properties must be set for links to be created correctly.
667 */
668
669 function carbon_link($type, $node = 0, $main = 0)
670 {
671 global $user ;
672
673 $links = array();
674
675 if ($type != "node")
676 return $links ;
677
678 if (empty($user->uid))
679 return $links;
680
681 if (($node->type == "carbon_stamp") || ($node->type == "carbon_account") || ($node->type == "carbon_stamp"))
682 $links["my_account"] = array("title"=>"My carbon accounts", "href"=> "user/".$node->uid."/carbon",
683 "attributes" => array("title" => t("Accounts owned by !name", array("!name"=>$node->name))));
684
685 if (($node->type == "carbon_stamp") || ($node->type == "carbon_account"))
686 {
687 $links["my_stamps"] = array("title" => "My stamps", "href"=>"user/".$node->uid."/stamp",
688 "attributes" => array("title" => t("All stamps owned by !name", array("!name"=>$node->name))));
689
690 if (!empty($node->organization))
691 {
692 $links["group_accounts"] = array("title"=>"Group accounts", "href"=>"carbon/report/",
693 "query"=> "org=".$node->organization,
694 "attributes" => array("title" => t("Accounts of all users in !name", array("!name"=>$node->organization))));
695 }
696 else
697 {
698 $links["all_accounts"] = array("title"=>"All accounts", "href"=>"carbon/report",
699 "attributes"=> array("title" => t("All accounts.")));
700 }
701
702 $links["carbon_sources"] = array("title" => t("All carbon sources"), "href"=>"carbon/source",
703 "attributes" => array("title" => t("All sources of carbon emissions.")));
704 }
705 elseif ($node->type == "carbon_source")
706 {
707 $links["carbon_sources"] = array("title" => t("All Carbon sources"), "href"=>"carbon/source",
708 "attributes" => array("title" => t("All sources of carbon emissions.")));
709
710 $links["add_new_source"] = array("title"=>"Create new source", "href" => "node/add/carbon-source",
711 "attributes" => array("title"=> t("Add new source of carbon emissions")));
712 // $links["xml"] = l(t("view as XML"), "carbon/account/xml", array('title' => t("view/export as XML (todo)")));
713 }
714 return $links;
715 }
716
717
718
719 function carbon_share()
720 {
721 return "Sharing not supported yet";
722 }
723
724
725 /**
726 * revamped for drupal 5.0
727 */
728
729 function carbon_admin_settings() {
730
731 $form["carbon_models"] = array(
732 "#type" => "textfield",
733 "#title" => t("Carbon source model filter"),
734 "#default_value" => variable_get("carbon_models", ""),
735 "#description" => t("Specify each carbon source model you want users and admins to see. " .
736 " Each model name should be separated with a comma." .
737 " Existing source selections made by users are unaffected. " .
738 " Leave blank to include all models."),
739 "#maxlength" => "80",
740 "#size" => "80");
741
742 $form["carbon_energy_units_0"] = array(
743 "#type" => "textfield",
744 "#title" => t("Units of energy field 0"),
745 "#default_value" => variable_get("carbon_energy_units_0", ""),
746 "#description" => t("Set description to match what is provided in carbon sources ".
747 "e.g. non renewable kWh. This field is not shown to users until units are specified above."),
748 "#maxlength" => "30",
749 "#size" => "30");
750
751 $form["carbon_energy_units_1"] = array(
752 "#type" => "textfield",
753 "#title" => t("Units of energy field 1"),
754 "#default_value" => variable_get("carbon_energy_units_1", ""),
755 "#description" => t("Set description to match what is provided in carbon sources ".
756 "e.g. renewable kWh. This field is not shown to users until units are specified above."),
757 "#maxlength" => "30",
758 "#size" => "30");
759
760
761 $form["carbon_units"] = array(
762 "#type" => "textfield",
763 "#title" => t("Units of carbon dioxide"),
764 "#default_value" => variable_get("carbon_units", "Kg of C02"),
765 "#description" => t("The default carbon sources calculate carbon emissions in Kg. " .
766 "However in some countries, you might want to use pounds. ".
767 "No conversions are done within the software."),
768 "#maxlength" => "30",
769 "#size" => "30");
770
771 $form["carbon_sectors"] = array(
772 "#type" => "textfield",
773 "#title" => t("Sector color palette"),
774 "#default_value" => variable_get("carbon_sectors", CARBON_PALETTE),
775 "#description" => t("You can use any sector names in carbon sources. ".
776 "However you can associate sectors with particular colors e.g. ".CARBON_PALETTE."."),
777 "#maxlength" => "255",
778 "#size" => "80");
779
780 /*
781 $form["carbon_chart_provider"] = array(
782 "#type" => "select",
783 "#options" => _get_carbon_chart_providers(),
784 "#title" => t("Chart provider"),
785 "#default_value" => variable_get("carbon_chart_provider", 'internal'),
786
787 // "#description" => t("The drupal charts module (%is_enabled) is required for all options except internal.",
788 // array("%is_enabled"=>module_exists('charts') ? 'enabled' : 'disabled'))
789
790 */
791 $form["carbon_default_chart"] = array(
792 "#type" => "textfield",
793 "#title" => t("Default chart shown with a carbon account"),
794 "#default_value" => variable_get("carbon_default_chart", ""),
795 "#description" => t("Leave blank or specify 'bar' or 'pie'. Note that creating a bar or pie chart for each account by default " .
796 "puts extra load on the server."),
797 "#maxlength" => "10",
798 "#size" => "10");
799
800 $form["carbon_use_model_selection"] = array(
801 "#type" => "checkbox",
802 "#title" => t("Prompt user for preferred model"),
803 "#default_value" => variable_get("use_model_selection", 1),
804 "#description" => t("If users are not able to select a model make sure that " .
805 "are not a lot of similar carbon sources with the same code e.g. elec-kwh as this will " .
806 "confuse users. This could be done by just enabling a single model above."),
807 "#maxlength" => "10",
808 "#size" => "10");
809
810
811 $form["carbon_use_postal_code"] = array(
812 "#type" => "checkbox",
813 "#title" => t("Prompt user for postal code or zipcode and display"),
814 "#default_value" => variable_get("carbon_use_postal_code", 1),
815 "#maxlength" => "10",
816 "#size" => "10");
817
818 $form["carbon_use_organization"] = array(
819 "#type" => "checkbox",
820 "#title" => t("Prompt user for organizaton and display"),
821 "#default_value" => variable_get("carbon_use_organization", 1),
822 "#maxlength" => "10",
823 "#size" => "10");
824
825 $form["carbon_enable_crag_functions"] = array(
826 "#type" => "checkbox",
827 "#title" => t("Enable CRAG functions"),
828 "#default_value" => variable_get("carbon_enable_crag_functions", 0),
829 "#description" => t("Adds additional reporting functions"),
830 "#maxlength" => "10",
831 "#size" => "10");
832
833
834 $form["carbon_money_units"] = array(
835 "#type" => "textfield",
836 "#title" => t("Units of money"),
837 "#default_value" => variable_get("carbon_money_units", "$"),
838 "#description" => t("Used only for carbon trades."),
839 "#maxlength" => "10",
840 "#size" => "10");
841
842
843 return system_settings_form($form);
844 }
845
846
847 function _get_carbon_chart_providers()
848 {
849 return array('internal'); //,'google','openflashchart','fusioncharts');
850 }
851
852 function _set_title($account)
853 {
854 drupal_set_title($account->title);
855 }
856
857 /**
858 * This function hides most of an account name so users need not reveal their
859 * fullname but enough of it that their friends recognise the name.
860 *
861 * @param $name
862 * @return name which has been hideen
863 */
864
865 function carbon_anonomize($name)
866 {
867 return substr($name, 0, 5)."..." ;
868 }
869
870 /**
871 * Because carbon_stamps don't have their own protection (too
872 * difficult to manage), when we to show a link to a carbon_stamp,
873 * test the protection of the account supplied.
874 *
875 * In other words carbon stamps are private be default. But they
876 * can be made public if attached to a carbon_account.
877 *
878 * Also used by carbon_transfers.
879 *
880 * @param $node is the object that you want to link to (e.g. a carbon stamp)
881 * @param $account is the object that you want to evaluate protection (e.g. a carbon account)
882 */
883
884 function carbon_link_from_title($node, $account, $extra)
885 {
886 $access = carbon_access("view", $account); // $node->type must be set!
887 $title = empty($node->title) ? '('.$node->nid.')'
888 : ($access ? $node->title : carbon_anonomize($node->title)) ;
889 return $access ? l($title,"node/".$node->nid. "/". $extra) : $title;
890 }
891
892
893 /**
894 * format the edit clone buttons
895 *
896 * @param $node the node that might be editable
897 * @param $query the query to initiate edir
898 * @return html
899 */
900
901
902 function carbon_format_edit_ops($node, $query)
903 {
904 return
905 _fancy_link($node,"","edit all fields on stamp", "node/".$node->nid."/edit", $query, "edit");
906 }
907
908
909 function carbon_format_add_new_op($node, $query)
910 {
911 return _fancy_link($node, "new", "add new carbon stamp", "node/add/carbon_stamp", $query, "add");
912 }
913
914
915 /**
916 * Similar to carbon_link_from_title except that you can insert
917 * the link in colour and in reverse video.
918 */
919
920
921 function carbon_highlighted_link($node, $text, $title, $path, $query, $text_class)
922 {
923 return _fancy_link($node, $text, $title, $path, $query, $text_class);
924 }
925
926
927
928 function _fancy_link($node, $text, $title, $path, $query, $class)
929 {
930 // $access = carbon_access(is_object($node) ? "update":"create", $node_type); // $node->type must be set!
931 // if (!$access)
932 // return "" ;
933
934 $attributes = array('title'=>$title, 'class'=>$class);
935 $html = l($text, $path, $options = array('attributes'=>$attributes, 'query'=> $query, $html=false));
936
937 return $html ;
938 }
939
940
941 /*
942 * hm probably not necessary to define this class
943 */
944
945 class Node
946 {
947
948 }
949
950
951 // Shared date functions
952
953
954 /**
955 * Format a date range. Show a date range using as few as
956 * characters as possible so that it takes little space
957 * on the screen but without being ambiguous.
958 */
959
960 function _format_date_range($firstdate, $lastdate)
961 {
962 $y1 = date('y', $firstdate); $y2 = date('y', $lastdate);
963 $m1 = date('m', $firstdate); $m2 = date('m', $lastdate);
964
965 if ($y1 == $y2)
966 {
967 if ($m1 == $m2)
968 return date("M Y", $firstdate);
969
970 return date("M", $firstdate)." to ". date("M Y", $lastdate);
971 }
972 return date("M Y", $firstdate)." to ". date("M Y", $lastdate);
973 }
974
975
976
977 /**
978 * Bypass Drupal's format_date() which deals only with offsets and
979 * appears to not understand daylight savings time.
980 *
981 * It is really important that dates are handled consistently.
982 * Otherwise you end up accepting dates with one offset e.g. BST
983 * and displaying them with another e.g. GMT which is what happened
984 * with early software versions.
985 *
986 * Print time if it's any value other than 00:00
987 *
988 * @param $timestamp
989 * @param $type
990 * @return unknown_type
991 */
992
993 function _format_date_only($timestamp)
994 {
995 if (empty($timestamp)) return '' ;
996 $datetimeformat = variable_get('date_format_short', 'm/d/Y');
997 // trim the format string down to just the date part see http://uk.php.net/manual/en/function.date.php
998 $len = strspn($datetimeformat, "-dDjlNSwz-W-FmMnt-LoyY-/");
999 $dateformat = substr($datetimeformat, 0 , $len);
1000 return date($dateformat, $timestamp);
1001 }
1002
1003 /**
1004 * Print time if it's any value other than 00:00
1005 * @param $timestamp
1006 * @return unknown_type
1007 */
1008
1009
1010 function _format_date($timestamp)
1011 {
1012 if (empty($timestamp)) return '' ;
1013
1014 if (idate('H', $timestamp) || idate('i', $timestamp))
1015 {
1016 return _format_date_time($timestamp); // time not midnight, so add hours
1017 }
1018 return _format_date_only($timestamp);
1019 }
1020
1021 /**
1022 * Format date time string and always show time
1023 *
1024 * @param $timestamp
1025 * @return unknown_type
1026 */
1027
1028 function _format_date_time($timestamp)
1029 {
1030 if (empty($timestamp)) return '' ;
1031 $format = variable_get('date_format_short', 'm/d/Y - H:i'); // drupal default date
1032 return date($format, $timestamp);
1033 }
1034
1035 /**
1036 * This class is used to convert between dates embedded in URLs
1037 * and the unix timestamp.
1038 *
1039 * @author johna
1040 *
1041 */
1042
1043
1044 class CarbonDate
1045 {
1046 /*
1047 * return a unix timestamp from a string
1048 */
1049 static function fromYYYYMMDD($s)
1050 {
1051 return mktime(0, 0, 0, substr($s,4,2), substr($s,6,2), substr($s,0,4));
1052 }
1053
1054 /*
1055 * return a string from a unix timestamp
1056 */
1057
1058 static function asYYYYMMDD($time)
1059 {
1060 return sprintf("%04d%02d%02d", date('Y', $time), date('m', $time), date('d', $time));
1061 }
1062
1063 /**
1064 * return time
1065 * @return unknown_type
1066 */
1067 static function today()
1068 {
1069 return mktime(0, 0, 0, idate('m'), idate('d'), idate('Y'));
1070 }
1071 }
1072
1073
1074 /**
1075 * Add whole years onto a date
1076 *
1077 * @param unix timestamp (secs from 1970)
1078 * @param integer number of months
1079 * @return the new date (secs from 1970)
1080 */
1081
1082 function _add_months($date, $num)
1083 {
1084 return strtotime($num." month", $date);
1085 }
1086
1087
1088
1089
1090 ?>

  ViewVC Help
Powered by ViewVC 1.1.2