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

Contents of /contributions/modules/webcams/webcams.module

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


Revision 1.16 - (show annotations) (download) (as text)
Sat Apr 19 01:06:24 2008 UTC (19 months, 1 week ago) by moonray
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.15: +9 -7 lines
File MIME type: text/x-php
Views integration fix. Also fixed issue with multiple click events being attached when thickbox module is enabled and webcam times out.
1 <?php
2 // $Id: webcams.module,v 1.15 2008/04/16 21:16:14 moonray Exp $
3
4 /**
5 * @file
6 * Allows creation of dynamically updating webcam nodes, with one or multiple
7 * webcams per node.
8 */
9
10 if (module_exists('views')) {
11 include(drupal_get_path('module', 'webcams') .'/views.inc');
12 }
13
14
15 /**
16 * Implementation of hook_perm().
17 */
18 function webcams_perm() {
19 return array('access webcams', 'create webcams', 'edit webcams', 'edit own webcams');
20 }
21
22 /**
23 * Implementation of hook_node_info().
24 */
25 function webcams_node_info() {
26 return array(
27 'webcam' => array(
28 'name' => t('Webcam'),
29 'module' => 'webcams',
30 'description' => t("Create dynamically updating webcam nodes, with one or multiple webcams per node."),
31 )
32 );
33 }
34
35 /**
36 * Implementation of hook_access().
37 */
38 function webcams_access($op, $node) {
39 global $user;
40
41 if (!user_access('access webcams')) {
42 return FALSE;
43 }
44
45 if ($op == 'create') {
46 return user_access('create webcams');
47 }
48
49 if ($op == 'update' || $op == 'delete') {
50 if (user_access('edit webcams') || (user_access('edit own webcams') && $user->uid == $node->uid && $user->uid != 0)) {
51 return TRUE;
52 }
53 }
54 }
55
56 /**
57 * Implementation of hook_menu().
58 */
59 function webcams_menu($may_cache) {
60 $items = array();
61
62 if ($may_cache) {
63 $items[] = array(
64 'path' => 'node/add/webcam',
65 'title' => t('Webcam'),
66 'access' => user_access('create webcams'),
67 'type' => MENU_DYNAMIC_ITEM,
68 );
69 $items[] = array(
70 'path' => 'admin/settings/webcams',
71 'title' => t('Webcams'),
72 'description' => t('Configure webcam settings.'),
73 'callback' => 'drupal_get_form',
74 'callback arguments' => array('webcams_admin_settings'),
75 'access' => user_access('administer site configuration'),
76 );
77 }
78
79 return $items;
80 }
81
82 /**
83 * Administer settings form.
84 */
85 function webcams_admin_settings() {
86 $form['webcams_max_webcams_per_page'] = array(
87 '#type' => 'select',
88 '#title' => t('Maximum webcams per node'),
89 '#description' => t('Enter the maximum number of webcams a node can contain.'),
90 '#default_value' => variable_get('webcams_max_webcams_per_page', 4),
91 '#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4),
92 );
93 return system_settings_form($form);
94 }
95
96 /**
97 * Implementation of hook_form().
98 */
99 function webcams_form(&$node) {
100 drupal_add_css(drupal_get_path('module', 'webcams') .'/webcams.css');
101
102 if (count($node->webcams) < variable_get('webcams_max_webcams_per_page', 4)) {
103 $webcam = new StdClass;
104 $webcam->wid = 0;
105 $webcam->disabled = TRUE;
106 $webcam->name = '';
107 $webcam->default_url = '';
108 $webcam->url = '';
109 $webcam->width = 320;
110 $webcam->height = 240;
111 $webcam->thickbox = FALSE;
112 $webcam->link = '';
113 $webcam->delay = 30;
114 $webcam->timeout = 0;
115 $webcam->weight = 0;
116
117 for ($i = count($node->webcams); $i < variable_get('webcams_max_webcams_per_page', 4); $i++) {
118 $webcam->weight = -10 + $i;
119 $node->webcams[] = drupal_clone($webcam);
120 }
121 }
122
123 // Title
124 $form['title'] = array(
125 '#type' => 'textfield',
126 '#title' => t('Title'),
127 '#required' => TRUE,
128 '#default_value' => $node->title,
129 '#weight' => -10,
130 );
131
132 // Webcams
133 $form['webcams'] = array(
134 '#tree' => TRUE,
135 '#weight' => -9,
136 );
137 for ($i = 0; $i < count($node->webcams); $i++) {
138 $form['webcams'][$i] = array(
139 '#type' => 'fieldset',
140 '#tree' => 1,
141 '#title' => t('Webcam').' '.($i+1),
142 '#required' => $i == 0,
143 '#collapsible' => TRUE,
144 '#collapsed' => $i > 0 && isset($node->webcams[$i]->disabled) && $node->webcams[$i]->disabled == TRUE,
145 '#weight' => $i,
146 );
147 $form['webcams'][$i]['wid'] = array(
148 '#type' => 'hidden',
149 '#default_value' => $node->webcams[$i]->wid,
150 );
151 if ($i > 0) {
152 $form['webcams'][$i]['disabled'] = array(
153 '#type' => 'checkbox',
154 '#title' => t('Disable webcam'),
155 '#default_value' => isset($node->webcams[$i]->disabled) && $node->webcams[$i]->disabled == TRUE,
156 '#weight' => -10,
157 '#description' => t('Note that disabling this webcam will completely remove this webcam and its settings.'),
158 );
159 }
160 $form['webcams'][$i]['name'] = array(
161 '#type' => 'textfield',
162 '#title' => t('Name'),
163 '#required' => $i == 0,
164 '#default_value' => $node->webcams[$i]->name,
165 '#weight' => -9,
166 );
167 $form['webcams'][$i]['default_url'] = array(
168 '#type' => 'textfield',
169 '#title' => t('Default Image URL'),
170 '#required' => $i == 0,
171 '#default_value' => $node->webcams[$i]->default_url,
172 '#weight' => -8,
173 '#description' => t('This image is displayed while the webcam image is loading, and when an error occurs.'),
174 );
175 $form['webcams'][$i]['url'] = array(
176 '#type' => 'textfield',
177 '#title' => t('Webcam Image URL'),
178 '#required' => $i == 0,
179 '#default_value' => $node->webcams[$i]->url,
180 '#weight' => -7,
181 );
182
183 $form['webcams'][$i]['width'] = array(
184 '#type' => 'textfield',
185 '#title' => t('Size'),
186 '#required' => $i == 0,
187 '#size' => 4,
188 '#maxlength' => 4,
189 '#default_value' => $node->webcams[$i]->width,
190 '#weight' => -6,
191 '#prefix' => '<div class="webcams-form-size">',
192 );
193 $form['webcams'][$i]['times'] = array(
194 '#value' => '<div class="form-item">x</div>',
195 '#weight' => -5,
196 );
197 $form['webcams'][$i]['height'] = array(
198 '#type' => 'textfield',
199 '#size' => 4,
200 '#required' => $i == 0,
201 '#maxlength' => 4,
202 '#default_value' => $node->webcams[$i]->height,
203 '#weight' => -4,
204 '#suffix' => '</div>',
205 );
206
207 if (module_exists('thickbox')) {
208 $form['webcams'][$i]['thickbox'] = array(
209 '#type' => 'checkbox',
210 '#title' => t('Enlarge with Thickbox'),
211 '#default_value' => $node->webcams[$i]->thickbox,
212 '#weight' => -3,
213 '#description' => t('Enlarge the webcam to its original size using Thickbox. This will override the Link field.'),
214 );
215 }
216 else {
217 $form['webcams'][$i]['thickbox'] = array(
218 '#type' => 'hidden',
219 '#default_value' => $node->webcams[$i]->thickbox,
220 );
221 }
222 $form['webcams'][$i]['link'] = array(
223 '#type' => 'textfield',
224 '#title' => t('Link'),
225 '#default_value' => $node->webcams[$i]->link,
226 '#weight' => -2,
227 '#description' => t('Make your webcam image a link to another page, or leave blank if you don\'t wish to use this functionality. The link can be relative (e.g. <strong>node/23</strong>) or absolute (e.g. <strong>http://www.site.com/page.html</strong>).'),
228 );
229 $form['webcams'][$i]['delay'] = array(
230 '#type' => 'textfield',
231 '#title' => t('Delay'),
232 '#size' => 4,
233 '#required' => $i == 0,
234 '#maxlength' => 3,
235 '#default_value' => $node->webcams[$i]->delay,
236 '#weight' => -1,
237 '#attributes' => array('class' => 'webcams-form-delay'),
238 '#description' => t('The amount of seconds to wait before refreshing the webcam image.'),
239 );
240 $form['webcams'][$i]['timeout'] = array(
241 '#type' => 'textfield',
242 '#title' => t('Timeout'),
243 '#size' => 4,
244 '#required' => $i == 0,
245 '#maxlength' => 3,
246 '#default_value' => $node->webcams[$i]->timeout,
247 '#weight' => 0,
248 '#attributes' => array('class' => 'webcams-form-timeout'),
249 '#description' => t('The amount of times the webcam is allowed to refresh before timing out, and no longer update itself. Set to <strong>0</strong> to disable timing out.'),
250 );
251 $form['webcams'][$i]['weight'] = array(
252 '#type' => 'weight',
253 '#title' => t('Weight'),
254 '#default_value' => $node->webcams[$i]->weight,
255 '#delta' => 15,
256 '#weight' => 1,
257 '#description' => t('The order by which the webcams appear on the page.'),
258 );
259 }
260
261 // Body
262 $form['bodyparts'] = array(
263 '#weight' => -1,
264 );
265 $form['bodyparts']['body'] = array(
266 '#type' => 'textarea',
267 '#title' => t('Body'),
268 '#default_value' => $node->body,
269 '#rows' => 20,
270 '#required' => TRUE,
271 '#weight' => -5,
272 );
273 $form['bodyparts']['format'] = filter_form($node->format, -4, array('format'));
274
275 return $form;
276 }
277
278 /**
279 * Implementation of hook_insert().
280 */
281 function webcams_insert(&$node) {
282 for ($i = 0; $i < count($node->webcams); $i++) {
283 $webcam = (object) $node->webcams[$i];
284
285 // Only insert if webcam has a url
286 if ($i == 0 || ($webcam->name || $webcam->default_url || $webcam->url)) {
287 _webcams_insert_webcam($node->nid, $node->vid, $webcam);
288 }
289 }
290 }
291
292 function _webcams_insert_webcam($nid, $vid, &$webcam) {
293 $webcam->wid = db_next_id('{webcams}_wid');
294
295 db_query("INSERT INTO {webcams} (wid, nid, vid, name, default_url, url, width, height, thickbox, link, delay, timeout, weight) VALUES (%d, %d, %d, '%s', '%s', '%s', %d, %d, %d, '%s', %d, %d, %d)", $webcam->wid, $nid, $vid, $webcam->name, $webcam->default_url, $webcam->url, $webcam->width, $webcam->height, $webcam->thickbox, $webcam->link, $webcam->delay, $webcam->timeout, $webcam->weight);
296 }
297
298 /**
299 * Implementation of hook_update().
300 */
301 function webcams_update(&$node) {
302 if ($node->revision) {
303 webcams_insert($node);
304 }
305 else {
306 // Update statement
307 for ($i = 0; $i < count($node->webcams); $i++) {
308 $webcam = (object) $node->webcams[$i];
309
310 if ($webcam->wid > 0) {
311 if ($i == 0 || !(isset($webcam->disabled) && $webcam->disabled == TRUE)) {
312 _webcams_update_webcam($webcam);
313 }
314 else {
315 _webcams_delete_webcam($webcam);
316 }
317 }
318 elseif ($i == 0 || !(isset($webcam->disabled) && $webcam->disabled == TRUE)) {
319 _webcams_insert_webcam($node->nid, $node->vid, $webcam);
320 }
321 }
322 }
323 }
324
325 function _webcams_update_webcam(&$webcam) {
326 db_query("UPDATE {webcams} SET name = '%s', default_url = '%s', url = '%s', width = %d, height = %d, thickbox = %d, link = '%s', delay = %d, timeout = %d, weight = %d WHERE wid = %d", $webcam->name, $webcam->default_url, $webcam->url, $webcam->width, $webcam->height, $webcam->thickbox, $webcam->link, $webcam->delay, $webcam->timeout, $webcam->weight, $webcam->wid);
327 }
328
329 /**
330 * Implementation of hook_nodeapi().
331 *
332 * When a node revision is deleted, we need to remove the corresponding record
333 * from our table. The only way to handle revision deletion is by implementing
334 * hook_nodeapi().
335 */
336 function webcams_nodeapi(&$node, $op, $teaser, $page) {
337 switch ($op) {
338 case 'delete revision':
339 // Notice that we're matching a single revision based on the node's vid.
340 db_query('DELETE FROM {webcams} WHERE vid = %d', $node->vid);
341 break;
342 }
343 }
344
345 /**
346 * Implementation of hook_delete().
347 */
348 function webcams_delete(&$node) {
349 db_query("DELETE FROM {webcams} WHERE nid = %d", $node->nid);
350 }
351
352 function _webcams_delete_webcam(&$webcam) {
353 db_query("DELETE FROM {webcams} WHERE wid = %d", $webcam->wid);
354 }
355
356 /**
357 * Implementation of hook_load().
358 */
359 function webcams_load(&$node) {
360 $additions = new StdClass;
361 $additions->webcams = array();
362
363 $result = db_query('SELECT wid, nid, name, default_url, url, width, height, thickbox, link, delay, timeout, weight FROM {webcams} WHERE vid = %d ORDER BY weight', $node->vid);
364 while ($webcam = db_fetch_object($result)) {
365 $additions->webcams[] = $webcam;
366 }
367
368 return $additions;
369 }
370
371 /**
372 * Implementation of hook_view().
373 */
374 function webcams_view(&$node, $teaser, $page) {
375 static $loaded = array();
376
377 $node = node_prepare($node, $teaser);
378
379 if (!$teaser) {
380 $loaded[$node->nid] = FALSE;
381
382 drupal_add_js(drupal_get_path('module', 'webcams') .'/webcams.js');
383 drupal_add_css(drupal_get_path('module', 'webcams') .'/webcams.css');
384
385 $js = '';
386
387 for ($i = 0; $i < count($node->webcams); $i++) {
388 $webcam = $node->webcams[$i];
389 $delay = $webcam->delay * 1000;
390 $errmsg = t('Webcam <em>@name</em> has timed out...', array('@name' => $webcam->name));
391
392 $js .= "\n$.webcam.init('webcams{$webcam->nid}-webcam{$webcam->wid}', '{$webcam->name}', '{$webcam->url}', '{$webcam->default_url}', {$webcam->width}, {$webcam->height}, {$delay}, {$webcam->timeout}, 'webcams{$webcam->nid}-content', '{$errmsg}');";
393
394 $node->content['webcams-webcam'.$i] = array(
395 '#value' => theme('webcams_webcam', $webcam),
396 '#weight' => $webcam->weight,
397 );
398 }
399 $node->content['body']['#value'] = '<div id="webcams'. $node->nid .'-content" class="webcams-content">'.$node->content['body']['#value'].'</div>'."\n";
400
401 // Need to avoid loading webcam initialization javascript multiple times...
402 if (!$loaded[$node->nid]) {
403 drupal_add_js($js, 'inline', 'footer', FALSE);
404 $loaded[$node->nid] = TRUE;
405 }
406 }
407
408 return $node;
409 }
410
411 /**
412 * Implementation of hook_diff().
413 */
414 function webcams_diff(&$old_node, &$new_node) {
415 global $form_values;
416 $op = isset($form_values['op']) ? $form_values['op'] : '';
417
418 $result = array();
419 if ($old_node->type == 'webcam') {
420 $old_webcams = array();
421 for ($i = 0; $i < max(count($old_node->webcams), variable_get('webcams_max_webcams_per_page', 4)); $i++) {
422 if ($i < count($old_node->webcams)) {
423 $webcam = $old_node->webcams[$i];
424 $old_webcams[] = array(
425 t('Name') .': '. $webcam->name,
426 t('Default Image URL') .': '. $webcam->default_url,
427 t('Webcam Image URL') .': '. $webcam->url,
428 t('Size') .': '. $webcam->width . " x " . $webcam->height,
429 t('Enlarge with Thickbox') .': '. ($webcam->thickbox ? t('Yes') : t('No')),
430 t('Link') .': '. $webcam->link,
431 t('Delay') .': '. $webcam->delay,
432 t('TimeOut') .': '. $webcam->timeout,
433 );
434 }
435 else {
436 $old_webcams[] = array();
437 }
438 }
439
440 $new_webcams = array();
441 for ($i = 0; $i < max(count($new_node->webcams), variable_get('webcams_max_webcams_per_page', 4)); $i++) {
442 if ($i < count($new_node->webcams)) {
443 if ($op == t('Preview changes')) {
444 $webcam = (object)$new_node->webcams[$i];
445 }
446 else {
447 $webcam = $new_node->webcams[$i];
448 }
449 if ($op == t('Preview changes') && isset($webcam->disabled) && $webcam->disabled == 1 && $i <= count($old_node->webcams) + 1) {
450 $new_webcams[] = array();
451 }
452 elseif ($op != t('Preview changes') || !isset($webcam->disabled) || (isset($webcam->disabled) && $webcam->disabled == 0)) {
453 $new_webcams[] = array(
454 t('Name') .': '. $webcam->name,
455 t('Default Image URL') .': '. $webcam->default_url,
456 t('Webcam Image URL') .': '. $webcam->url,
457 t('Size') .': '. $webcam->width . " x " . $webcam->height,
458 t('Enlarge with Thickbox') .': '. ($webcam->thickbox ? t('Yes') : t('No')),
459 t('Link') .': '. $webcam->link,
460 t('Delay') .': '. $webcam->delay,
461 t('TimeOut') .': '. $webcam->timeout,
462 );
463 }
464 else {
465 $new_webcams[] = array();
466 }
467 }
468 else {
469 $new_webcams[] = array();
470 }
471 }
472
473 $max = max(count($old_webcams), count($new_webcams));
474 for ($i = 0; $i < $max; $i++) {
475 $result[] = array(
476 'name' => t('Webcam') .' '. ($i + 1),
477 'old' => isset($old_webcams[$i]) ? $old_webcams[$i] : '',
478 'new' => isset($new_webcams[$i]) ? $new_webcams[$i] : '',
479 'format' => array(
480 'show_header' => false,
481 )
482 );
483 }
484 }
485 return $result;
486 }
487
488 function theme_webcams_webcam(&$webcam) {
489 $output = '<div class="webcams-webcam" id="webcams'. $webcam->nid .'-webcam'. $webcam->wid .'" style="width: '. $webcam->width .'px;">';
490
491 $img = '<img src="'. $webcam->default_url .'" width="'. $webcam->width .'" height="'. $webcam->height .'" class="webcams-image" />';
492 if (module_exists('thickbox') && $webcam->thickbox) {
493 $zoom = '<img src="'. base_path() . drupal_get_path('module', 'webcams') .'/zoom.png" width="32" height="32" class="webcams-zoom iefix" />';
494
495 $output .= l($zoom, $webcam->default_url, array('title' => $webcam->name, 'class' => 'thickbox'), NULL, NULL, FALSE, TRUE);
496 $output .= l($img, $webcam->default_url, array('title' => $webcam->name, 'class' => 'thickbox'), NULL, NULL, FALSE, TRUE);
497 }
498 elseif ($webcam->link) {
499 $output .= l($img, $webcam->link, array('title' => $webcam->name), NULL, NULL, FALSE, TRUE);
500 }
501 else {
502 $output .= $img;
503 }
504 $output .= '<span class="webcams-name">'. $webcam->name .'</span>';
505 $output .= '</div>'."\n";
506 return $output;
507 }

  ViewVC Help
Powered by ViewVC 1.1.2