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

Contents of /contributions/modules/bookings/bookings.module

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


Revision 1.7 - (show annotations) (download) (as text)
Fri Apr 11 14:58:27 2008 UTC (19 months, 2 weeks ago) by fronbow
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +23 -61 lines
File MIME type: text/x-php
bookings module: fixed menu titles
1 <?php
2 // $Id: bookings.module,v 1.5 2008/04/04 22:23:10 fronbow Exp $
3
4 /**
5 * @file Booking System API
6 * An API to facilitate booking systems natively for drupal
7 */
8
9 /*
10 * Just getting the basics down
11 */
12
13 /*
14 * Time in Seconds
15 1 Minute: 60 seconds
16 1 Hour: 3,600 seconds
17 1 Day: 86,400 seconds
18 1 Week: 604,800 seconds
19 4 Weeks: 2,419,200 seconds
20 1 Year: 31,536,000 seconds
21 1 Decade: 315,360,000 seconds
22 */
23
24 /**
25 * Notes to self!
26 *
27 http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/exhibit/exhibit.module?revision=1.1&view=markup
28 */
29
30 function bookings_menu() {
31 $items = array();
32
33 // Admin menus
34
35 $items['admin/settings/bookings'] = array(
36 'title' => 'Booking System API settings',
37 'description' => 'Global settings for the API',
38 'page callback' => 'drupal_get_form',
39 'page arguments' => array('bookings_admin'),
40 'access arguments' => array('access administration pages'),
41 'type' => MENU_DEFAULT_TASK,
42 );
43
44 // Static menus
45
46 $items['bookings'] = array(
47 'title' => 'Booking System',
48 'description' => 'The Booking System',
49 'page callback' => 'bookings_show',
50 'access arguments' => array('access content'),
51 'type' => MENU_NORMAL_ITEM,
52 );
53 $items['bookings/year'] = array(
54 'title' => 'View by year',
55 'page callback' => 'bookings_show_year',
56 'access callback' => 'user_access',
57 'access arguments' => array('access content'),
58 'type' => MENU_NORMAL_ITEM,
59 );
60 $items['bookings/month'] = array(
61 'title' => 'View by month',
62 'page callback' => 'bookings_show_month',
63 'access callback' => 'user_access',
64 'access arguments' => array('access content'),
65 'type' => MENU_NORMAL_ITEM,
66 );
67 $items['bookings/week'] = array(
68 'title' => 'View by week',
69 'page callback' => 'bookings_show_week',
70 'access callback' => 'user_access',
71 'access arguments' => array('access content'),
72 'type' => MENU_NORMAL_ITEM,
73 );
74 $items['bookings/day'] = array(
75 'title' => 'View by day',
76 'page callback' => 'bookings_show_day',
77 'access callback' => 'user_access',
78 'access arguments' => array('access content'),
79 'type' => MENU_NORMAL_ITEM,
80 );
81
82 // Callbacks
83
84 $items['bookings/day/%/%/%'] = array(
85 'title' => 'View by day',
86 'page callback' => 'bookings_show_day',
87 'page arguments' => array(2, 3, 4),
88 'access callback' => 'user_access',
89 'access arguments' => array('access content'),
90 );
91 $items['bookings/week/%/%/%'] = array(
92 'title' => 'View by week',
93 'page callback' => 'bookings_show_week',
94 'page arguments' => array(2, 3, 4),
95 'access callback' => 'user_access',
96 'access arguments' => array('access content'),
97 );
98 $items['bookings/month/%/%/%'] = array(
99 'title' => 'View by month',
100 'page callback' => 'bookings_show_month',
101 'page arguments' => array(2, 3, 4),
102 'access callback' => 'user_access',
103 'access arguments' => array('access content'),
104 );
105 $items['bookings/year/%/%/%'] = array(
106 'title' => 'View by year',
107 'page callback' => 'bookings_show_year',
108 'page arguments' => array(2, 3, 4),
109 'access callback' => 'user_access',
110 'access arguments' => array('access content'),
111 );
112
113 return $items;
114 }
115
116 /**
117 * Generate HTML for the MyBookings block
118 *
119 * @param op
120 * the operation from the URL
121 * @param delta
122 * offset
123 * @return
124 * block HTML
125 */
126 function bookings_block($op = 'list', $delta = 0) {
127 if ($op == "list") {
128 $block[0]["info"] = t('MyBookings');
129 $block[1]["info"] = t('Bookings Admin');
130 return $block;
131 } else if ($op == 'view') {
132 //show block content
133 }
134 }
135
136 function bookings_admin() {
137 $weekdays = array(
138 0 => t('Sunday'),
139 1 => t('Monday'),
140 );
141 $form['bookings_first_dow'] = array(
142 '#type' => 'select',
143 '#title' => t('First day of the week'),
144 '#default_value' => variable_get('bookings_first_dow', 1),
145 '#options' => $weekdays,
146 '#description' => t("Please enter the first day of the week"),
147 '#required' => TRUE,
148 );
149
150 $form['bookings_yearview'] = array(
151 '#type' => 'select',
152 '#title' => t('Year view display layout'),
153 '#description' => t("This will tell the year view page how many columns to use for the month display"),
154 '#default_value' => 4,
155 '#options' => array(
156 3 => t('Display in 3 columns'),
157 4 => t('Display in 4 columns'),
158 6 => t('Display in 6 columns'),
159 ),
160 '#required' => TRUE,
161 );
162
163 $form['bookings_default_display'] = array(
164 '#type' => 'select',
165 '#title' => t('Default page display'),
166 '#description' => t("This controls what the default page is when a user clicks the main link"),
167 '#default_value' => variable_get('bookings_default_display', 'week'),
168 '#options' => array(
169 'year' => 'year',
170 'month' => 'month',
171 'week' => 'week',
172 'day' => 'day',
173 ),
174 '#required' => TRUE,
175 );
176
177 $hours = range(0,23);
178 $form['bookings_start_time'] = array(
179 '#type' => 'select',
180 '#title' => t('Day start time'),
181 '#description' => t("Use this to tell the system when there is a start time for the bookings"),
182 '#default_value' => variable_get('bookings_start_time', 0),
183 '#options' => $hours,
184 '#required' => FALSE,
185 );
186 $form['bookings_end_time'] = array(
187 '#type' => 'select',
188 '#title' => t('Day end time'),
189 '#description' => t("Use this to tell the system when there is an end time for the bookings"),
190 '#default_value' => variable_get('bookings_end_time', 0),
191 '#options' => $hours,
192 '#required' => FALSE,
193 );
194 $measure = range(0, 60, 1);
195
196 $form['bookings_increment'] = array(
197 '#type' => 'select',
198 '#title' => t('Day view time increment'),
199 '#description' => t("What time increment should the day calendar display"),
200 '#default_value' => variable_get('bookings_increment', 15),
201 '#options' => $measure,
202 '#required' => TRUE,
203 );
204
205 $form['bookings'] = array(
206 '#type' => 'fieldset',
207 '#title' => t('Contrib settings'),
208 '#collapsible' => TRUE,
209 '#collapsed' => FALSE,
210
211 );
212
213 return system_settings_form($form);
214 }
215
216 /**
217 * Placeholder for collision detection routine
218 *
219 * @param $node
220 * pass in the node object so that we have all the data
221 * @return
222 * false if no collision or
223 * an array of the clashes
224 */
225
226 function bookings_collision_detection($node) {
227
228
229 return false;
230 }
231
232 /**
233 * some more placeholders
234 */
235
236 /**
237 * The main way to show the bookings calendar for now
238 */
239 function bookings_show() {
240 //$output = 'Placeholder!';
241
242 $output .= bookings_show_week();
243
244 return $output;
245 }
246
247 /**
248 * Generate a calendar for the booking nodes
249 * Internal helper functions will be used
250 *
251 * @param $what
252 * should be an array of either 2 values (start and end) for custom periods, or 1 value (year, month, week, day)
253 * @param $date
254 * a valid date in ISO format. This is used to draw the calendar
255 * @returns
256 * a calendar view
257 */
258 function bookings_showcal($what, $date) {
259 }
260
261 /**
262
263 notes to self/team
264
265 My idea is to have two sets of global perms:
266 can create bookings
267 can create reservations
268
269 In our intranet system, only special admins can create bookings. Everyone else can only make reservations, which
270 then have to be agreed by the admin.
271
272 */
273
274 function bookings_perm() {
275 return array('can create bookings', 'can create reservations','administer booking system');
276 }
277
278 function bookings_add_booking($node) {
279 }
280
281 function bookings_edit_booking($node) {
282 }
283
284 function bookings_del_booking($node) {
285 }
286
287
288 function bookings_help($path, $arg) {
289 switch ($path) {
290 case 'admin/help#bookings':
291 return '<p>'.t('Booking System API').'</p>';
292 break;
293 }
294 }
295
296 /**
297 * Helper function to show the current year depending on the date
298 *
299 * @param $date
300 * A fully qualified date in ISO format
301 *
302 * @return
303 * Returns a themed calendar display showing all the months of the year
304 *
305 */
306 function bookings_show_year($date='') {
307 //get the year from the date
308 $year = 2008;
309 $output = '';
310
311 return $output;
312 }
313
314 function bookings_show_month($date='') {
315
316 return '';
317 }
318
319 function bookings_show_week($date='') {
320 if ($date=='') {
321 $date = date_make_date('now');
322 }
323 $dow = variable_get('bookings_first_dow', 1);
324
325 $day_dates = _bookings_get_weekdates($date);
326 $day = date_day_of_week($date);
327 //dpm($dates);
328 $bformat = "D jS M";
329
330 $header = array(
331 '',
332 date_format_date( date_convert($day_dates[0], DATE_UNIX, DATE_OBJECT), 'custom', $bformat ),
333 date_format_date( date_convert($day_dates[1], DATE_UNIX, DATE_OBJECT), 'custom', $bformat ),
334 date_format_date( date_convert($day_dates[2], DATE_UNIX, DATE_OBJECT), 'custom', $bformat ),
335 date_format_date( date_convert($day_dates[3], DATE_UNIX, DATE_OBJECT), 'custom', $bformat ),
336 date_format_date( date_convert($day_dates[4], DATE_UNIX, DATE_OBJECT), 'custom', $bformat ),
337 date_format_date( date_convert($day_dates[5], DATE_UNIX, DATE_OBJECT), 'custom', $bformat ),
338 date_format_date( date_convert($day_dates[6], DATE_UNIX, DATE_OBJECT), 'custom', $bformat ),
339 );
340
341 $bookings_start_time = strtotime( variable_get('bookings_start_time', 0).':00');
342 $bookings_end_time = strtotime( variable_get('bookings_end_time', 0).':00');
343 /*
344 bookings_start_time should include the date
345 bookings_end_time should include the date
346
347 this would make it more accurate, and searching the db should be a bit easier!
348 */
349
350 $ts = variable_get('bookings_increment', 15);
351
352 $increment = $ts*60; //convert to seconds
353
354 //start basic first, without checking for other units!
355 $f = $bookings_start_time;
356
357 /* create array of bookable objects */
358 $bookings_types = array();
359 $result = db_query("SELECT * FROM {node_type} WHERE type LIKE 'bookings_%'");
360 while ($row = db_fetch_array($result)) {
361 $bookings_types[] = $row;
362 }
363
364 //dpm($bookings_types);
365 //$f needs to be the data and time
366 //
367 $td = $day_dates[0]+$f;
368 $bookings_startdate = date_convert($day_dates[0], DATE_UNIX, DATE_ARRAY);
369 $bookings_startdate['hour'] = variable_get('bookings_start_time', 2);
370 $bookings_startdate['minute'] = 0;
371 $bookings_startdate['second'] = 0;
372
373 $bookings_enddate = date_convert($day_dates[0], DATE_UNIX, DATE_ARRAY);
374 $bookings_enddate['hour'] = variable_get('bookings_end_time', 23);
375 $bookings_enddate['minute'] = 0;
376 $bookings_enddate['second'] = 0;
377
378 unset($bookings_startdate[0]);
379 unset($bookings_enddate[0]);
380
381 $f = date_convert($bookings_startdate, DATE_ARRAY, DATE_UNIX );
382 $bookings_end_time = date_convert($bookings_enddate, DATE_ARRAY, DATE_UNIX);
383
384
385 //dpm ($day_dates[0]);
386 //dpm ($bookings_startdate);
387 //dpm ($bookings_enddate);
388 //dpm ($f);
389
390
391 while ($f <= $bookings_end_time) {
392 /*
393 create links for all booking node types
394 */
395 $d1 = '';
396 $d2 = '';
397 $d3 = '';
398 $d4 = '';
399 $d5 = '';
400 $d6 = '';
401 $d7 = '';
402 foreach ($bookings_types as $key => $value) {
403 //$value_type = str_replace('_','-', $value['type']);
404 $value['type'] = str_replace('_','-', $value['type']);
405 $d1 .= l('add '.$value['name'], 'node/add/'.$value['type'].'/'.$f);
406 $d2 .= l('add '.$value['name'], 'node/add/'.$value['type'].'/'.($f+(86400*1)));
407 $d3 .= l('add '.$value['name'], 'node/add/'.$value['type'].'/'.($f+(86400*2)));
408 $d4 .= l('add '.$value['name'], 'node/add/'.$value['type'].'/'.($f+(86400*3)));
409 $d5 .= l('add '.$value['name'], 'node/add/'.$value['type'].'/'.($f+(86400*4)));
410 $d6 .= l('add '.$value['name'], 'node/add/'.$value['type'].'/'.($f+(86400*5)));
411 $d7 .= l('add '.$value['name'], 'node/add/'.$value['type'].'/'.($f+(86400*6)));
412 }
413
414 $fobject = date_convert($f, DATE_UNIX, DATE_OBJECT);
415 $date_view = date_format_date($fobject, 'custom', 'h:i a');
416 //$date_view = date_make_date($f, NULL, DATE_UNIX);
417
418 $rows[] = array(
419 $date_view,
420 $d1,
421 $d2,
422 $d3,
423 $d4,
424 $d5,
425 $d6,
426 $d7
427 );
428
429 $f = $f + $increment;
430 }
431
432 //return 'working on this again!';
433 return theme('table', $header, $rows);
434 }
435
436 /**
437 *
438 */
439
440 function bookings_show_day() {
441 //use arg to get dates from url methinks
442 $numargs = func_num_args();
443 $arg_list = func_get_args();
444
445 $year = 0;
446 $month = 0;
447 $day = 0;
448 $date = 0;
449
450 if ($numargs < 3) {
451 //need to set some defaults
452 $date = date_make_date('now');
453 } else {
454 //parse args
455 //$year = isset(func_get_arg(1)) ? func_get_arg(1) : 0;
456 $year = $arg_list[0];
457 $month = $arg_list[1];
458 $day = $arg_list[2];
459
460 $date = date_make_date("$year/$month/$day");
461
462 }
463
464 //dpm($numargs);
465 //dpm($arg_list);
466 //dpm(date_array($date));
467 $dow = variable_get('bookings_first_dow', 1);
468
469 $output = '';
470
471 //need to populate header with bookable objects
472
473 //$output .= theme('table', $header, $rows);
474
475 return $output;
476 }
477
478 function _bookings_show_day($date='') {
479 if ($date=='') {
480 $date = date_make_date('now');
481 }
482 $dow = variable_get('bookings_first_dow', 1);
483
484 $day_dates = _bookings_get_weekdates($date);
485 $day = date_day_of_week($date);
486 //dpm($dates);
487 $bformat = "D jS M";
488
489 $header = array(
490 '',
491 date_format_date( date_convert($day_dates[0], DATE_UNIX, DATE_OBJECT), 'custom', $bformat ),
492 date_format_date( date_convert($day_dates[1], DATE_UNIX, DATE_OBJECT), 'custom', $bformat ),
493 date_format_date( date_convert($day_dates[2], DATE_UNIX, DATE_OBJECT), 'custom', $bformat ),
494 date_format_date( date_convert($day_dates[3], DATE_UNIX, DATE_OBJECT), 'custom', $bformat ),
495 date_format_date( date_convert($day_dates[4], DATE_UNIX, DATE_OBJECT), 'custom', $bformat ),
496 date_format_date( date_convert($day_dates[5], DATE_UNIX, DATE_OBJECT), 'custom', $bformat ),
497 date_format_date( date_convert($day_dates[6], DATE_UNIX, DATE_OBJECT), 'custom', $bformat ),
498 );
499
500 $bookings_start_time = strtotime( variable_get('bookings_start_time', 0).':00');
501 $bookings_end_time = strtotime( variable_get('bookings_end_time', 0).':00');
502 /*
503 bookings_start_time should include the date
504 bookings_end_time should include the date
505
506 this would make it more accurate, and searching the db should be a bit easier!
507 */
508
509 $ts = variable_get('bookings_increment', 15);
510
511 $increment = $ts*60; //convert to seconds
512
513 //start basic first, without checking for other units!
514 $f = $bookings_start_time;
515
516 /* create array of bookable objects */
517 $bookings_types = array();
518 $result = db_query("SELECT * FROM {node_type} WHERE type LIKE 'bookings_%'");
519 while ($row = db_fetch_array($result)) {
520 $bookings_types[] = $row;
521 }
522
523 //dpm($bookings_types);
524 //$f needs to be the data and time
525 //
526 $td = $day_dates[0]+$f;
527 $bookings_startdate = date_convert($day_dates[0], DATE_UNIX, DATE_ARRAY);
528 $bookings_startdate['hour'] = variable_get('bookings_start_time', 2);
529 $bookings_startdate['minute'] = 0;
530 $bookings_startdate['second'] = 0;
531
532 $bookings_enddate = date_convert($day_dates[0], DATE_UNIX, DATE_ARRAY);
533 $bookings_enddate['hour'] = variable_get('bookings_end_time', 23);
534 $bookings_enddate['minute'] = 0;
535 $bookings_enddate['second'] = 0;
536
537 unset($bookings_startdate[0]);
538 unset($bookings_enddate[0]);
539
540 $f = date_convert($bookings_startdate, DATE_ARRAY, DATE_UNIX );
541 $bookings_end_time = date_convert($bookings_enddate, DATE_ARRAY, DATE_UNIX);
542
543
544 //dpm ($day_dates[0]);
545 //dpm ($bookings_startdate);
546 //dpm ($bookings_enddate);
547 //dpm ($f);
548
549
550 while ($f <= $bookings_end_time) {
551 /*
552 create links for all booking node types
553 */
554 $d1 = '';
555 $d2 = '';
556 $d3 = '';
557 $d4 = '';
558 $d5 = '';
559 $d6 = '';
560 $d7 = '';
561 foreach ($bookings_types as $key => $value) {
562 //$value_type = str_replace('_','-', $value['type']);
563 $value['type'] = str_replace('_','-', $value['type']);
564 $d1 .= l('add '.$value['name'], 'node/add/'.$value['type'].'/'.$f);
565 $d2 .= l('add '.$value['name'], 'node/add/'.$value['type'].'/'.($f+(86400*1)));
566 $d3 .= l('add '.$value['name'], 'node/add/'.$value['type'].'/'.($f+(86400*2)));
567 $d4 .= l('add '.$value['name'], 'node/add/'.$value['type'].'/'.($f+(86400*3)));
568 $d5 .= l('add '.$value['name'], 'node/add/'.$value['type'].'/'.($f+(86400*4)));
569 $d6 .= l('add '.$value['name'], 'node/add/'.$value['type'].'/'.($f+(86400*5)));
570 $d7 .= l('add '.$value['name'], 'node/add/'.$value['type'].'/'.($f+(86400*6)));
571 }
572
573 $fobject = date_convert($f, DATE_UNIX, DATE_OBJECT);
574 $date_view = date_format_date($fobject, 'custom', 'h:i a');
575 //$date_view = date_make_date($f, NULL, DATE_UNIX);
576
577 $rows[] = array(
578 $date_view,
579 $d1,
580 $d2,
581 $d3,
582 $d4,
583 $d5,
584 $d6,
585 $d7
586 );
587
588 $f = $f + $increment;
589 }
590
591 //return 'working on this again!';
592 return theme('table', $header, $rows);
593 }
594
595 function _bookings_show_period($sdate, $edate) {
596 }
597
598 /**
599 * Helper function to return the start and end date for the week containing the selected date
600 *
601 * @param $date
602 * the selected date
603 *
604 * @return
605 * an array with the start date and end date for the week or false
606 */
607 function _bookings_get_week($date='') {
608 if ($date=='') {
609 $date = date('Y-m-d', time());
610 }
611 $year = date('Y', $date);
612 $month = date('m', $date);
613 $day = date('d', $date);
614 $actual_day = date('w', $date);
615 //where are we in the week and what is the start day of the week
616 $bookings_first_dow = variable_get('bookings_first_dow', 1);
617 if ($bookings_first_dow == $actual_day) {
618 return array(
619 'start' => $year.'-'.$month.'-'.$day,
620 'end' => $year.'-'.$month.'-'.$day+6,
621 );
622 } else {
623 //find the start date
624
625 }
626 return false;
627 }
628
629 /**
630 * Helper function to return a day
631 *
632 * not sure whether this is needed, but hey we'll see
633 */
634 function _bookings_get_day($date='') {
635 if ($date=='') {
636 $date = date_make_date('now');
637 }
638 $date_day = date_day_of_week($date, DATE_OBJECT);
639 }
640
641 /**
642 * Helper function to create an array of the month and weekdays
643 */
644 function _bookings_get_weekdates($date='') {
645 if ($date=='') {
646 $date = date_make_date('now');
647 }
648 $date_day = date_day_of_week($date, DATE_OBJECT);
649
650 $dow = variable_get('bookings_first_dow', 1);
651 $today = date_day_of_week($date);
652 $date_copy = date_convert($date, DATE_OBJECT, DATE_ARRAY);
653
654 $d = date_convert($date, DATE_OBJECT, DATE_ISO);
655 $start = 'empty';
656
657 //dpm($dow, 'dow');
658
659 if ($dow == 0) {
660 //start = Sunday
661 if ($date_day == 0) {
662 //$start = strtotime($d, date_convert($date, DATE_OBJECT, DATE_DATETIME));
663 $start = date_convert($date, DATE_OBJECT, DATE_UNIX);
664 } else {
665 $start = strtotime($d.' -1 Sunday', date_convert($date, DATE_OBJECT, DATE_DATETIME));
666 //check for dst -> day = saturday
667 if ( date_day_of_week($start, DATE_UNIX) == 6 ) {
668 //add a day to account for dst
669 $start = $start + 86400;
670 }
671 }
672 } else if ($dow == 1) {
673 //start = Monday
674 if ($date_day == 1) {
675 //$start = strtotime($d, date_convert($date, DATE_OBJECT, DATE_DATETIME));
676 $start = date_convert($date, DATE_OBJECT, DATE_UNIX);
677 } else {
678 $start = strtotime($d.' -1 Monday', date_convert($date, DATE_OBJECT, DATE_DATETIME));
679 //dpm( date_day_of_week($start, DATE_UNIX), 'date_day_of_week');
680 if ( date_day_of_week($start, DATE_UNIX) == 6 ) {
681 //add a day to account for dst
682 $start = $start + 86400;
683 }
684 }
685 //dpm(date_convert($start,DATE_UNIX, DATE_ARRAY), 'start');
686 }
687
688 $month = array();
689 $dateunix = $start;
690 for ($f=0; $f<7; $f++) {
691 $month[$f] = $dateunix;
692 $dateunix = $dateunix + 86400;
693 }
694
695 /*
696 so now we have an array of dates, hopefully!
697 we can pass this back to the week view to display the headings
698 */
699 return $month;
700 }
701
702 /**
703 * API function to return an array of times for use in the time submission for node/add
704 *
705 * @param $start_time
706 * A string timestamp in the form 'H:i'
707 * @param $end_time
708 * A string timestamp in the form 'H:i'
709 * @param $increment
710 * This should be defined by the contrib module otherwise it will take on the value set in
711 * the API. It makes sure we only get valid bookings. It should be in minutes
712 *
713 * @return array
714 */
715 function bookings_get_obj_times($start_time=0, $end_time=0, $increment=0) {
716 if ($start_time==0 && $end_time==0 && $increment==0) {
717 return false;
718 }
719
720 }
721
722 /*
723 notes to self:
724
725 table setups::
726
727 node -> nid, vid, type, title, uid, status, ...
728 node_type -> type, name, module, description ...
729
730 booking_objects -> oid (object id), name, description, weight, ???
731
732 booking_node -> nid, oid, uid, start_date, end_date, ???
733
734 */
735
736

  ViewVC Help
Powered by ViewVC 1.1.2