/[drupal]/contributions/modules/carbon/carbon_account.inc
ViewVC logotype

Contents of /contributions/modules/carbon/carbon_account.inc

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


Revision 1.12 - (show annotations) (download) (as text)
Tue May 5 13:24:18 2009 UTC (6 months, 3 weeks ago) by johnackers
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, HEAD
Changes since 1.11: +9 -7 lines
File MIME type: text/x-php
enhanced handling of energy fields
1 <?php
2 /**
3 * $Id$
4 *
5 * Drupal carbon account module
6 *
7 * Created on 18-Oct-2006
8 *
9 * @file
10 * definition of carbon_account node. Each carbon_account
11 * node contains settings such as choice of preferred carbon
12 * model and privacy settings that control the presentation of
13 * a carbon footprint.
14 */
15
16
17
18 /**
19 * Implementation of hook_access().
20 *
21 * Defer to carbon_access because it is easier to check
22 * access for all carbon types in one place.
23 */
24
25
26 function carbon_account_access($op, $node)
27 {
28 return carbon_access($op, $node);
29 }
30
31
32 /**
33 * Return an array of protection options
34 * @return array
35 */
36 function _get_protection_options() {
37 return array(t("private"), t("share with others in my group"),
38 t("share with all logged in users"), t("public"));
39 }
40
41
42 /**
43 * Implementation of hook_form().
44 */
45
46
47 function carbon_account_form(&$node, &$param) {
48 global $user;
49
50 $form = array();
51
52 $form["account"] = array(
53 "#type" => "fieldset",
54 "#title" => "Carbon settings",
55 "#weight" => -2,
56 "#collapsible" => TRUE,
57 "#collapsed" => FALSE);
58
59 $form["account"]["title"] = array(
60 "#type" => "textfield",
61 "#title" => t("Title of this Account"),
62 "#required" => TRUE,
63 "#default_value" => empty($node->title) ? $node->name : $node->title,
64 "#size" => 20,
65 "#maxlength" => 40,
66 "#weight" => -1,
67 "#description" => t("Your name or someone else's name, name of a building etc. It can be different from your username."));
68
69 if (variable_get("carbon_use_postal_code", 1))
70
71 $form["account"]["postcode"] = array(
72 "#type" => "textfield",
73 "#title" => t("Postcode"),
74 "#required" => FALSE,
75 "#default_value" => empty($node->postcode) ? '' : $node->postcode,
76 "#size" => 10,
77 "#maxlength" => 20,
78 "#weight" => 1,
79 "#description" => t("Either some or all of your postcode or zipcode. Interesting to others " .
80 "if you share your carbon account."));
81
82 if (variable_get("carbon_use_organization", 1)
83 || user_access("administer accounts")
84 || user_access("administer carbon nodes"))
85
86 $form["account"]["organization"] = array(
87 "#type" => "textfield",
88 "#title" => t("Local Group"),
89 "#default_value" => empty($node->organization) ? '' : $node->organization,
90 "#size" => 20,
91 "#maxlength" => 20,
92 "#weight" => 2,
93 "#description" => t("Name of any local group, leave blank if none."),
94 "#required" => FALSE);
95
96 $model_map = CarbonSources::getDefault()->getModelMap();
97 $model_keys = array_keys($model_map);
98 if (variable_get("carbon_use_model_selection", 1))
99
100 $form["account"]["model_index"] = array(
101 "#type" => "select",
102 "#title" => t("Preferred emissions model"),
103 "#required" => FALSE,
104 "#options" => array_values($model_map),
105 "#default_value" => isset($node->model) ? array_search($node->model, $model_keys) : 0 ,
106 "#description" => "Select the preferred model that you want to use to calculate the CO2 emissions." .
107 " Not all the models support all possible <a href='?q=carbon/source'>carbon " .
108 " sources</a>; in which case substitutes will be made. Leave as -no preference- if you are not sure.",
109 "#weight" => 3);
110
111 $form["account"]["protection"] = array(
112 "#type" => "select",
113 "#title" => t("Privacy"),
114 "#required" => FALSE,
115 "#default_value" => !isset($node->protection) ? 2 : $node->protection,
116 "#description" => t("Who can see this carbon footprint?"),
117 "#options" => _get_protection_options(),
118 "#weight" => 4);
119
120
121 $form["account"]["firstdate_s"] = array(
122 "#type" => "textfield",
123 "#title" => t("First day of reporting period"),
124 "#required" => FALSE,
125 "#size" => 20,
126 "#maxlength" => 20,
127 "#default_value" => empty($node->firstdate) ? "" : _format_date($node->firstdate),
128 "#weight" => 5,
129 "#description" =>
130 t("Format: %time. Can be left blank. The latest 12 months of " .
131 "carbon stamps are used to calculate your footprint which will be " .
132 "displayed alongside others in charts et cetera (assuming your privacy " .
133 "settings allow this). You can override that date " .
134 "by setting this field to the end of the period you want to use " .
135 "and you can change the length of the period below. ",
136 array("%time" => _format_date_only(time()))));
137
138 $form["account"]["period"] = array(
139 "#type" => "textfield",
140 "#title" => t("Reporting period in months"),
141 "#required" => FALSE,
142 "#size" => 2,
143 "#maxlength" => 20,
144 "#default_value" => empty($node->period) ? 12 : $node->period,
145 "#weight" => 6,
146 "#description" =>
147 t("The length of the reporting period in months."));
148
149 $e0 = variable_get("carbon_energy_units_0", "");
150 if (!empty($e0))
151 $form["account"]["enablekwh0"] = array(
152 "#type" => "checkbox",
153 "#title" => t("Display !s in results", array("!s" => $e0)),
154 "#required" => false,
155 "#default_value" => isset($node->enablekwh0)? $node->enablekwh0 : false,
156 "#weight" => 10);
157
158 $e1 = variable_get("carbon_energy_units_1", "");
159 if (!empty($e1))
160 $form["account"]["enablekwh1"] = array(
161 "#type" => "checkbox",
162 "#title" => t("Display !s in results", array("!s" =>$e1)),
163 "#required" => false,
164 "#default_value" => isset($node->enablekwh1)? $node->enablekwh1 : false,
165 "#weight" => 11);
166
167 $form["account"]["enableco2"] = array(
168 "#type" => "checkbox",
169 "#title" => t("Display CO2 emissions in results"),
170 "#required" => false,
171 "#default_value" => isset($node->enableco2)? $node->enableco2 : true,
172 "#weight" => 12);
173
174 $form['#submit'] = array('carbon_account_submit');
175
176 return $form;
177 }
178
179 /**
180 * Validate our forms
181 * @param string form id
182 * @param array form values
183 */
184 function carbon_account_validate($form_id, $form_values)
185 {
186
187 }
188
189 /**
190 * Implementation of hook_view, add our node specific information
191 * @param node object to display
192 * @param boolean is this a teaser or full node?
193 * @param boolean is this displaying on its own page
194 */
195 function carbon_account_view(&$node, $teaser = FALSE, $page = FALSE)
196 {
197 $node->content["account_view"] = array("#weight"=>0, "#value" => carbon_view($node, 'view'));
198 return $node ;
199 }
200
201
202 /**
203 * Implemenation of hook_load
204 * @param node object to load additional information for
205 * @return object with carbon fields
206 */
207 function carbon_account_load($node) {
208 global $user ;
209 $query = db_query("SELECT f.*, s.uid as friends_uid " .
210 "FROM {carbon_account} f " .
211 "LEFT JOIN {carbon_account_share} s ON f.nid = s.nid " .
212 "WHERE f.nid = %d ",
213 $node->nid);
214
215 // most user will not be sharing records. However if they are
216 // we pick up their friends uid's while loading the footprint
217 $first_record = null ;
218 while ($record = db_fetch_object($query))
219 {
220 if (!isset($first_record))
221 $first_record = $record ;
222 $first_record->friends = array();
223
224 if (isset($friends_uid))
225 $first_record->friends[] = $friends_uid ;
226 }
227 return $first_record;
228 }
229
230 /**
231 * This function is declared in carbon_account_form and called prior
232 * to node_save.
233 *
234 * @param $form
235 * @param $form_state discovered that there is a ['values'] field.
236 *
237 */
238 function carbon_account_submit(&$form, &$form_state)
239 {
240 $model_keys = array_keys(CarbonSources::getDefault()->getModelMap());
241
242 $f = &$form_state['values'];
243
244 if (!empty($f['firstdate_s']))
245 {
246 $f['firstdate'] = strtotime($f['firstdate_s']) ;
247 }
248
249 if (isset($f['model_index']))
250 {
251 $f['model'] = $model_keys[$f['model_index']] ;
252 }
253 }
254
255
256
257 /**
258 * Implementation of hook_insert, which saves carbon-specific information
259 * into the carbon table
260 * @param node object
261 */
262
263 function carbon_account_insert(&$node)
264 {
265 db_query("INSERT INTO {carbon_account} (postcode, organization, model, firstdate, period, " .
266 " enablekwh0, kwh0, enablekwh1, kwh1, enableco2, co2, " .
267 " filing_status, protection, nid) " .
268 " VALUES ('%s', '%s', '%s', %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)",
269 $node->postcode, $node->organization, $node->model, $node->firstdate, $node->period,
270 $node->enablekwh0, $node->kwh0, $node->enablekwh1, $node->kwh1, $node->enableco2, $node->co2,
271 $node->filing_status, $node->protection,$node->nid);
272 drupal_set_message(t("To retrieve your carbon account on a later " .
273 "date click on <em>my account</em>."));
274 }
275
276 /**
277 * Implementation of hook_update, which saves updated todo-specific
278 * information into the carbon_account table
279 *
280 * @param node object
281 */
282 function carbon_account_update(&$node)
283 {
284 $result = db_query("UPDATE {carbon_account} " .
285 " SET postcode='%s', organization='%s', model='%s', firstdate=%d, period=%d, " .
286 " enablekwh0=%d, kwh0=%d, enablekwh1=%d, kwh1=%d, enableco2=%d, co2=%d, " .
287 " filing_status=%d, protection=%d WHERE nid=%d",
288 $node->postcode, $node->organization, $node->model, $node->firstdate, $node->period,
289 $node->enablekwh0, $node->kwh0, $node->enablekwh1, $node->kwh1, $node->enableco2, $node->co2,
290 $node->filing_status, $node->protection,$node->nid);
291 return $result ;
292 }
293
294
295 /**
296 * Implementation of hook_delete().
297 */
298 function carbon_account_delete($node) {
299 db_query("DELETE FROM {carbon_stamp_account} WHERE fid = %d", $node->nid);
300 db_query("DELETE FROM {carbon_account_share} WHERE nid = %d", $node->nid);
301 db_query("DELETE FROM {carbon_account} WHERE nid = %d", $node->nid);
302 }
303
304
305 ?>

  ViewVC Help
Powered by ViewVC 1.1.2