/[drupal]/contributions/modules/spaces/spaces_calendar/spaces_calendar.module
ViewVC logotype

Contents of /contributions/modules/spaces/spaces_calendar/spaces_calendar.module

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


Revision 1.11 - (show annotations) (download) (as text)
Mon Oct 6 21:56:40 2008 UTC (13 months, 3 weeks ago) by yhahn
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +24 -23 lines
File MIME type: text/x-php
Beginning merge of Spaces 2 to DRUPAL-6 branch
1 <?php
2 // $Id: spaces_calendar.module,v 1.10.6.2 2008/09/28 15:47:21 yhahn Exp $
3
4
5 /**
6 * TODO Make setup easier. Currenly you need to manually create three content types 'event',
7 * 'feed_ical', 'feed_ical_item' that has a field called 'field_date' for this module to work.
8 * We need to document the feed api integration as well.
9 */
10
11 /**
12 * Implementation of hook_menu()
13 */
14 function spaces_calendar_menu($may_cache) {
15 $items = array();
16 if ($may_cache) {
17 $items[] = array(
18 'path' => 'admin/settings/spaces/calendar',
19 'title' => t('Spaces calendar settings'),
20 'description' => t('Spaces calendar feature defaults.'),
21 'callback' => 'drupal_get_form',
22 'callback arguments' => array('spaces_calendar_settings_form'),
23 'access' => user_access('administer group features'),
24 'type' => MENU_LOCAL_TASK,
25 'weight' => 1,
26 );
27 }
28 return $items;
29 }
30
31 /**
32 * Implementation of hook_help()
33 */
34 function spaces_calendar_help($page) {
35 if (context_get('spaces', 'feature') == 'calendar') {
36 return "<p>". t('The calendar displays all events in this group. You can add events directly to the calendar or aggregate additional items using iCal feeds.') ."</p>";
37 }
38 }
39
40 /**
41 * Implementation of hook_context_define()
42 */
43 function spaces_calendar_context_define() {
44 $items = array();
45 if (_spaces_calendar_enabled()) {
46 $items['calendar'] = array(
47 'namespace' => 'spaces',
48 'attribute' => 'feature',
49 'value' => 'calendar',
50 'views' => array('spaces_calendar_upcoming', 'spaces_calendar'),
51 'node' => array(
52 variable_get('spaces_calendar_nodetype', ''),
53 ),
54 'block' => array(
55 array(
56 'module' => 'views',
57 'delta' => 'spaces_calendar_upcoming',
58 'region' => 'right',
59 'weight' => -11,
60 ),
61 ),
62 'spaces' => array(
63 'label' => t('Calendar'),
64 'description' => t('An event calendar with optional iCal integration.'),
65 'menu' => array(
66 'calendar' => array('title' => t('Calendar')),
67 ),
68 ),
69 );
70 }
71 if (_spaces_calendar_ical_enabled()) {
72 $items['calendar']['node'][] = variable_get('spaces_calendar_feed_nodetype', '');
73 $items['calendar']['node'][] = variable_get('spaces_calendar_feed_itemtype', '');
74 $items['calendar']['spaces']['menu']['calendar/feeds'] = array('title' => t('Feeds'));
75 $items['calendar']['views'][] = 'spaces_calendar_feeds';
76 }
77 return $items;
78 }
79
80 /**
81 * Implementation of hook_nodeapi()
82 */
83 function spaces_calendar_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
84 if ($op == 'view' && !$teaser && $page) {
85 _spaces_calendar_alter_links();
86 if ($node->type == variable_get('spaces_calendar_feed_nodetype', '')) {
87 $view = views_get_view('spaces_calendar_ical_items');
88 $view = views_build_view('embed', $view, array($node->nid), $view->pager, $view->nodes_per_page);
89 if ($view) {
90 $node->content['item_list'] = array(
91 '#value' => $view,
92 '#weight' => 10,
93 );
94 }
95 $node->content['buttons'] = array(
96 '#value' => "<div class='buttons'>". l(t('View calendar'), 'calendar', array('class' => 'button')) ."</div>",
97 '#weight' => 5,
98 );
99 }
100 }
101 }
102
103 /**
104 * Implementation of hook_form_alter()
105 */
106 function spaces_calendar_form_alter($form_id, &$form) {
107 if (_spaces_calendar_enabled()) {
108 _spaces_calendar_alter_links();
109 switch ($form_id) {
110 // TODO: use content types in features array to determine calendar node form id
111 case (variable_get('spaces_calendar_nodetype', '') .'_node_form'):
112 // add a submit handler to send user to view of submitted event
113 $form['#submit']['spaces_calendar_event_submit'] = array();
114 }
115 }
116 }
117
118 function spaces_calendar_settings_form() {
119 if (_spaces_calendar_ical_enabled()) {
120 $feedtype = variable_get('spaces_calendar_feed_nodetype', '');
121 if (!variable_get('feedapi_mapper_mapping_'. $feedtype, array())) {
122 $mapping = array(
123 serialize(array('raw', 'DTSTART', 'datetime')) => serialize(array('date', variable_get('spaces_calendar_datefield', ''), 'from')),
124 serialize(array('raw', 'DTEND', 'datetime')) => serialize(array('date', variable_get('spaces_calendar_datefield', ''), 'to')),
125 );
126 variable_set('feedapi_mapper_mapping_'. $feedtype, $mapping);
127 drupal_set_message(t('iCal feed mappings have been set up.'));
128 }
129 }
130
131 $form = array();
132 $types = array();
133 $nodetypes = node_get_types();
134 foreach ($nodetypes as $ntype => $nname) {
135 $types[$ntype] = $nname->name;
136 }
137
138 $fields = content_fields();
139 $fieldtypes = array();
140 foreach ($fields as $field) {
141 $fieldtypes[$field['field_name']] = t($field['widget']['label']) .' ('. $field['field_name'] .')';
142 }
143
144 $form['spaces_calendar_default_settings'] = array(
145 '#type' => 'fieldset',
146 '#title' => 'Spaces calendar default settings',
147 );
148
149 $form['spaces_calendar_default_settings']['spaces_calendar_nodetype'] = array(
150 '#type' => 'select',
151 '#title' => t('Event node types'),
152 '#description' => t('Select the designated node type for events.'),
153 '#options' => $types,
154 '#default_value' => variable_get('spaces_calendar_nodetype', ''),
155 '#multiple' => FALSE,
156 );
157
158 $form['spaces_calendar_default_settings']['spaces_calendar_datefield'] = array(
159 '#type' => 'select',
160 '#title' => t('Event date field'),
161 '#description' => t('Select the designated cck date field to use for events events.'),
162 '#options' => $fieldtypes,
163 '#default_value' => variable_get('spaces_calendar_datefield', ''),
164 '#multiple' => FALSE,
165 );
166
167 if (module_exists('feedapi') && module_exists('feedapi_mapper')) {
168
169 $form['spaces_calendar_feedapi_settings'] = array(
170 '#type' => 'fieldset',
171 '#title' => 'Spaces calendar feedapi/ical integration settings',
172 );
173
174 $form['spaces_calendar_feedapi_settings']['spaces_calendar_feed_nodetype'] = array(
175 '#type' => 'select',
176 '#title' => t('FeedAPI feed node type'),
177 '#description' => t('Select the type of node that will hold ical feeds.'),
178 '#options' => $types,
179 '#default_value' => variable_get('spaces_calendar_feed_nodetype', ''),
180 '#multiple' => FALSE,
181 );
182
183 $form['spaces_calendar_feedapi_settings']['spaces_calendar_feed_itemtype'] = array(
184 '#type' => 'select',
185 '#title' => t('FeedAPI node item type'),
186 '#description' => t('Select the type of node that events from ical feeds should be created into.'),
187 '#options' => $types,
188 '#default_value' => variable_get('spaces_calendar_feed_itemtype', ''),
189 '#multiple' => FALSE,
190 );
191 }
192
193 return system_settings_form($form);
194 }
195
196 /**
197 * Implementation of hook_default_views
198 */
199 function spaces_calendar_views_default_views() {
200 $default_views = array();
201 if (variable_get('spaces_calendar_datefield', '') && variable_get('spaces_calendar_nodetype', '')) {
202 $default_views[] = '_spaces_calendar_views_calendar';
203 $default_views[] = '_spaces_calendar_views_calendar_upcoming';
204 }
205 if (_spaces_calendar_ical_enabled()) {
206 $default_views[] = '_spaces_calendar_views_calendar_feeds';
207 $default_views[] = '_spaces_calendar_views_calendar_ical_items';
208 }
209 foreach ($default_views as $v) {
210 $view = call_user_func($v);
211 if (is_object($view) && $view->name) {
212 $views[$view->name] = $view;
213 }
214 }
215 return $views;
216 }
217
218 // Implementation of hook_views_post_view().
219 function spaces_calendar_views_post_view(&$view) {
220 _spaces_calendar_alter_links();
221 }
222
223 // Push user to the calendar view of their submitted event
224 function spaces_calendar_event_submit($form_id, $form_values) {
225 $n = node_submit($form_values);
226 if ($date = $n->field_date[0]['value']) {
227 $date = strtotime($date);
228 $m = format_date($date, 'custom', 'm');
229 $y = format_date($date, 'custom', 'Y');
230 return "calendar/$y/$m";
231 }
232 }
233
234 /**
235 * Helper function to display feed links in calendar view footers
236 */
237 function _spaces_calendar_feed_links() {
238 $links = array();
239 $space = spaces_get_space();
240 $feedtype = variable_get('spaces_calendar_feed_nodetype', '');
241 if ($space && $feedtype) {
242 $results = db_query(
243 "SELECT nr.title, n.nid
244 FROM {node} n
245 LEFT JOIN {node_revisions} nr ON n.vid = nr.vid
246 JOIN {og_ancestry} og ON n.nid = og.nid
247 WHERE n.type = '%s'
248 AND n.status = 1
249 AND og.group_nid = %d
250 ORDER BY nr.title ASC",
251 $feedtype, $space->sid);
252 while ($feed = db_fetch_object($results)) {
253 $crayon = theme('crayon', $feed->nid);
254 $links[] = array(
255 'title' => "<span class='crayon-marker crayon-$crayon'></span> ". $feed->title,
256 'html' => true,
257 );
258 }
259 }
260 return $links;
261 }
262
263 /* VIEWS */
264
265 function _spaces_calendar_views_calendar() {
266 $view = new stdClass();
267 $view->name = 'spaces_calendar';
268 $view->description = t('Calendar view of events.');
269 $view->access = array();
270 $view->menu = TRUE;
271 $view->menu_title = t('Calendar');
272 $view->page = TRUE;
273 $view->page_title = 'Calendar';
274 $view->page_type = 'calendar';
275 $view->page_footer = (_spaces_calendar_ical_enabled() && module_exists('feedapi_node_views')) ? '<?php print theme("links", _spaces_calendar_feed_links(), array("class" => "clear-block calendar-key")); ?>' : '';
276 $view->page_footer_format = '2';
277 $view->view_args_php = '';
278 $view->url = 'calendar';
279 $view->use_pager = TRUE;
280 $view->nodes_per_page = '100';
281 $view->sort = array();
282 $view->argument = array(
283 array (
284 'type' => 'calendar_year',
285 'argdefault' => '2',
286 'title' => '',
287 'options' => '',
288 'wildcard' => '',
289 'wildcard_substitution' => '',
290 ),
291 array (
292 'type' => 'calendar_month',
293 'argdefault' => '2',
294 'title' => '',
295 'options' => '',
296 'wildcard' => '',
297 'wildcard_substitution' => '',
298 ),
299 );
300 $view->field = array (
301 array (
302 'tablename' => 'node',
303 'field' => 'title',
304 'label' => '',
305 'handler' => 'views_handler_field_nodelink',
306 'options' => 'link',
307 ),
308 array (
309 'tablename' => 'node_data_'. variable_get('spaces_calendar_datefield', ''),
310 'field' => variable_get('spaces_calendar_datefield', '') .'_value',
311 'label' => '',
312 'handler' => 'content_views_field_handler_ungroup',
313 'options' => 'short',
314 ),
315 );
316 if (_spaces_calendar_ical_enabled() && module_exists('feedapi_node_views')) {
317 $view->field[] = array (
318 'tablename' => 'feedapi_node_item_feed',
319 'field' => 'feed_nid',
320 'label' => '',
321 'options' => 'nolink',
322 );
323 }
324 $view->filter = array (
325 array (
326 'tablename' => 'node',
327 'field' => 'status',
328 'operator' => '=',
329 'options' => '',
330 'value' => '1',
331 ),
332 array (
333 'tablename' => 'node',
334 'field' => 'type',
335 'operator' => 'OR',
336 'options' => '',
337 'value' => array (
338 0 => variable_get('spaces_calendar_nodetype', ''),
339 1 => variable_get('spaces_calendar_feed_itemtype', ''),
340 ),
341 ),
342 array (
343 'tablename' => 'spaces',
344 'field' => 'type',
345 'operator' => 'all',
346 'options' => '',
347 'value' => 'all',
348 ),
349 );
350 $view->requires = array('node', 'node_data_'. variable_get('spaces_calendar_datefield', ''), 'og_ancestry');
351 return $view;
352 }
353
354 function _spaces_calendar_views_calendar_upcoming() {
355 $view = new stdClass();
356 $view->name = 'spaces_calendar_upcoming';
357 $view->description = t('Provides a listing of upcoming events');
358 $view->access = array();
359 $view->view_args_php = '';
360 $view->page = FALSE;
361 $view->block = TRUE;
362 $view->block_title = t('Upcoming Events');
363 $view->block_empty = '<p>'. t('No upcoming events found.') .'</p>';
364 $view->block_empty_format = '2';
365 $view->block_type = 'spaces_datetitle';
366 $view->nodes_per_block = '5';
367 $view->argument = array();
368 $view->sort = array (
369 array (
370 'tablename' => 'node_data_'. variable_get('spaces_calendar_datefield', ''),
371 'field' => variable_get('spaces_calendar_datefield', '') .'_value',
372 'sortorder' => 'ASC',
373 'options' => '',
374 ),
375 );
376 $view->field = array (
377 array (
378 'tablename' => 'node',
379 'field' => 'title',
380 'label' => '',
381 'handler' => 'views_handler_field_nodelink',
382 'options' => 'link',
383 ),
384 array (
385 'tablename' => 'node_data_'. variable_get('spaces_calendar_datefield', ''),
386 'field' => variable_get('spaces_calendar_datefield', ''). '_value',
387 'label' => '',
388 'handler' => 'content_views_field_handler_ungroup',
389 'options' => 'default',
390 ),
391 );
392 $view->filter = array (
393 array (
394 'tablename' => 'node',
395 'field' => 'status',
396 'operator' => '=',
397 'options' => '',
398 'value' => '1',
399 ),
400 array (
401 'tablename' => 'node',
402 'field' => 'type',
403 'operator' => 'OR',
404 'options' => '',
405 'value' => array (
406 0 => variable_get('spaces_calendar_nodetype', ''),
407 1 => variable_get('spaces_calendar_feed_itemtype', ''),
408 ),
409 ),
410 array (
411 'tablename' => 'node_data_'. variable_get('spaces_calendar_datefield', ''),
412 'field' => variable_get('spaces_calendar_datefield', '') .'_value_default',
413 'operator' => '>',
414 'options' => 'now',
415 'value' => '',
416 ),
417 array (
418 'tablename' => 'spaces',
419 'field' => 'type',
420 'operator' => 'all',
421 'options' => '',
422 'value' => 'all',
423 ),
424 );
425 $view->exposed_filter = array ();
426 $view->requires = array('node_data_'. variable_get('spaces_calendar_datefield', ''), 'node');
427 return $view;
428 }
429
430 function _spaces_calendar_views_calendar_feeds() {
431 $view = new stdClass();
432 $view->name = 'spaces_calendar_feeds';
433 $view->description = 'Displays ical feeds and allows users to administer them';
434 $view->access = array ();
435 $view->view_args_php = '';
436 $view->page = TRUE;
437 $view->page_title = 'Calendar feeds';
438 $view->page_type = 'table';
439 $view->url = 'calendar/feeds';
440 $view->use_pager = TRUE;
441 $view->nodes_per_page = '20';
442 $view->menu = TRUE;
443 $view->menu_title = 'Calendar feeds';
444 $view->menu_tab = FALSE;
445 $view->menu_tab_weight = '0';
446 $view->menu_tab_default = FALSE;
447 $view->menu_tab_default_parent = NULL;
448 $view->menu_tab_default_parent_type = 'tab';
449 $view->menu_parent_tab_weight = '0';
450 $view->menu_parent_title = '';
451 $view->sort = array (
452 array (
453 'tablename' => 'node',
454 'field' => 'title',
455 'sortorder' => 'ASC',
456 'options' => '',
457 ),
458 );
459 $view->argument = array (
460 );
461 $view->field = array (
462 array (
463 'tablename' => 'node',
464 'field' => 'title',
465 'label' => 'Feed',
466 'handler' => 'views_handler_field_nodelink',
467 'options' => 'link',
468 ),
469 array (
470 'tablename' => 'node',
471 'field' => 'created',
472 'label' => 'Created On',
473 'handler' => 'views_handler_field_date_custom',
474 'options' => 'M j, Y',
475 ),
476 array (
477 'tablename' => 'node',
478 'field' => 'edit',
479 'label' => '',
480 'handler' => 'views_handler_node_edit_destination',
481 ),
482 );
483 $view->filter = array (
484 array (
485 'tablename' => 'node',
486 'field' => 'type',
487 'operator' => 'OR',
488 'options' => '',
489 'value' => array (
490 0 => 'feed_ical',
491 ),
492 ),
493 array (
494 'tablename' => 'node',
495 'field' => 'status',
496 'operator' => '=',
497 'options' => '',
498 'value' => '1',
499 ),
500 array (
501 'tablename' => 'spaces',
502 'field' => 'type',
503 'operator' => 'all',
504 'options' => '',
505 'value' => 'all',
506 ),
507 );
508 $view->exposed_filter = array ();
509 $view->requires = array(node, og_ancestry);
510 return $view;
511 }
512
513 function _spaces_calendar_views_calendar_ical_items() {
514 $view = new stdClass();
515 $view->name = 'spaces_calendar_ical_items';
516 $view->description = 'Listing of ical items for the node view of feeds.';
517 $view->access = array ();
518 $view->view_args_php = '';
519 $view->page = TRUE;
520 $view->page_type = 'table';
521 $view->url = '';
522 $view->use_pager = TRUE;
523 $view->nodes_per_page = '10';
524 $view->sort = array (
525 array (
526 'tablename' => 'node_data_'. variable_get('spaces_calendar_datefield', ''),
527 'field' => variable_get('spaces_calendar_datefield', '') .'_value',
528 'sortorder' => 'DESC',
529 'options' => '',
530 ),
531 );
532 $view->argument = array (
533 array (
534 'type' => 'feed_nid',
535 'argdefault' => '1',
536 'title' => '',
537 'options' => '',
538 'wildcard' => '',
539 'wildcard_substitution' => '',
540 ),
541 );
542 $view->field = array (
543 array (
544 'tablename' => 'node',
545 'field' => 'title',
546 'label' => 'Title',
547 'handler' => 'views_handler_field_nodelink',
548 'options' => 'link',
549 ),
550 array (
551 'tablename' => 'node_data_'. variable_get('spaces_calendar_datefield', ''),
552 'field' => variable_get('spaces_calendar_datefield', '') .'_value',
553 'label' => 'Date',
554 'handler' => 'content_views_field_handler_group',
555 'options' => 'default',
556 ),
557 );
558 $view->filter = array (
559 array (
560 'tablename' => 'node',
561 'field' => 'status',
562 'operator' => '=',
563 'options' => '',
564 'value' => '1',
565 ),
566 array (
567 'tablename' => 'node',
568 'field' => 'type',
569 'operator' => 'OR',
570 'options' => '',
571 'value' => array (
572 0 => variable_get('spaces_calendar_feed_itemtype', ''),
573 ),
574 ),
575 );
576 $view->exposed_filter = array ();
577 $view->requires = array('node_data_'. variable_get('spaces_calendar_datefield', ''), 'node');
578 return $view;
579 }
580
581 function _spaces_calendar_enabled() {
582 return variable_get('spaces_calendar_nodetype', '') && variable_get('spaces_calendar_datefield', '');
583 }
584
585 function _spaces_calendar_ical_enabled() {
586 return module_exists('feedapi') && module_exists('feedapi_mapper') && variable_get('spaces_calendar_feed_itemtype', '') && variable_get('spaces_calendar_feed_nodetype', '');
587 }
588
589 function _spaces_calendar_alter_links() {
590 if (context_get('spaces', 'feature') == 'calendar') {
591 $links = array();
592 $links['feed_ical_item'] = null;
593 context_set('spaces', 'links', $links);
594 }
595 }

  ViewVC Help
Powered by ViewVC 1.1.2