| 1 |
<?php
|
| 2 |
// $Id: bookings_room.module,v 1.2 2008/04/04 22:23:10 fronbow Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file Room Booking System
|
| 6 |
* A contrib module for the Booking System API
|
| 7 |
*/
|
| 8 |
|
| 9 |
|
| 10 |
/*
|
| 11 |
* a placeholder for now
|
| 12 |
*/
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Implementation of hook_info().
|
| 16 |
*/
|
| 17 |
function bookings_room_node_info() {
|
| 18 |
return array(
|
| 19 |
'bookings_room' => array(
|
| 20 |
'name' => t('Room booking'),
|
| 21 |
'module' => 'bookings_room',
|
| 22 |
'description' => "This is a room node type for use with the Booking System API.",
|
| 23 |
)
|
| 24 |
);
|
| 25 |
}
|
| 26 |
|
| 27 |
/**
|
| 28 |
* Implementation of hook_perm().
|
| 29 |
*/
|
| 30 |
function bookings_room_perm() {
|
| 31 |
return array('create room booking', 'edit own room bookings');
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Implementation of hook_access().
|
| 36 |
*/
|
| 37 |
function bookings_room_access($op, $node) {
|
| 38 |
global $user;
|
| 39 |
|
| 40 |
if ($op == 'create') {
|
| 41 |
// Only users with permission to do so may create this node type.
|
| 42 |
return user_access('create room booking');
|
| 43 |
}
|
| 44 |
|
| 45 |
// Users who create a node may edit or delete it later, assuming they have the
|
| 46 |
// necessary permissions.
|
| 47 |
if ($op == 'update' || $op == 'delete') {
|
| 48 |
if (user_access('edit own room bookings') && ($user->uid == $node->uid)) {
|
| 49 |
return TRUE;
|
| 50 |
}
|
| 51 |
}
|
| 52 |
}
|
| 53 |
|
| 54 |
function bookings_room_load($node) {
|
| 55 |
$additions = db_fetch_object(db_query('SELECT nid, start_datetime, end_datetime FROM {bookings_room_node} WHERE nid = %d', $node->nid));
|
| 56 |
return $additions;
|
| 57 |
}
|
| 58 |
|
| 59 |
/**
|
| 60 |
* Implementation of hook_form().
|
| 61 |
*/
|
| 62 |
function bookings_room_form(&$node) {
|
| 63 |
//unixtime:
|
| 64 |
$unixtime = arg(3);
|
| 65 |
//dpm( arg(3) );
|
| 66 |
|
| 67 |
if ( arg(3) ) {
|
| 68 |
$default_start_date = date_convert($unixtime, DATE_UNIX, DATE_ISO);
|
| 69 |
$default_end_date = date_convert($unixtime, DATE_UNIX, DATE_ISO);
|
| 70 |
} else {
|
| 71 |
if (arg(2)=='edit') {
|
| 72 |
//dpm($node);
|
| 73 |
$default_start_date = date_convert($node->start_datetime, DATE_UNIX, DATE_DATETIME);
|
| 74 |
$default_end_date = date_convert($node->end_datetime, DATE_UNIX, DATE_DATETIME);
|
| 75 |
}
|
| 76 |
else {
|
| 77 |
$default_start_date = date_convert(date_make_date('now'), DATE_OBJECT, DATE_ISO);
|
| 78 |
//$default_start_time = date_format_date(date_make_date('now'), 'custom', 'H:i');
|
| 79 |
$default_end_date = date_convert(date_make_date('now'), DATE_OBJECT, DATE_ISO);
|
| 80 |
//$default_end_time = date_format_date(date_make_date('now'), 'custom', 'H:i');
|
| 81 |
}
|
| 82 |
}
|
| 83 |
|
| 84 |
$type = node_get_types('type', $node);
|
| 85 |
|
| 86 |
// We need to define form elements for the node's title and body.
|
| 87 |
$form['title'] = array(
|
| 88 |
'#type' => 'textfield',
|
| 89 |
'#title' => check_plain($type->title_label),
|
| 90 |
'#required' => TRUE,
|
| 91 |
'#default_value' => $node->title,
|
| 92 |
'#weight' => -5
|
| 93 |
);
|
| 94 |
|
| 95 |
$form['body_filter']['body'] = array(
|
| 96 |
'#type' => 'textarea',
|
| 97 |
'#title' => check_plain($type->body_label),
|
| 98 |
'#default_value' => $node->body,
|
| 99 |
'#required' => FALSE
|
| 100 |
);
|
| 101 |
$form['body_filter']['filter'] = filter_form($node->format);
|
| 102 |
|
| 103 |
// Now we define the form elements specific to our node type.
|
| 104 |
$form['start_date'] = array(
|
| 105 |
'#type' => 'fieldset',
|
| 106 |
'#title' => t('Start date'),
|
| 107 |
'#description' => t("This is the start date and time of the booking"),
|
| 108 |
);
|
| 109 |
|
| 110 |
$form['start_date']['start_date'] = array(
|
| 111 |
'#type' => 'date_popup',
|
| 112 |
'#title' => t('Start date'),
|
| 113 |
'#default_value' => isset($node->start_date) ? $node->start_date : $default_start_date,
|
| 114 |
'#size' => 10,
|
| 115 |
'#maxlength' => 10
|
| 116 |
);
|
| 117 |
|
| 118 |
$form['end_date'] = array(
|
| 119 |
'#type' => 'fieldset',
|
| 120 |
'#title' => t('End date'),
|
| 121 |
);
|
| 122 |
|
| 123 |
$form['end_date']['end_date'] = array(
|
| 124 |
'#type' => 'date_popup',
|
| 125 |
'#title' => t('End date'),
|
| 126 |
'#default_value' => isset($node->end_date) ? $node->end_date : $default_end_date,
|
| 127 |
'#size' => 10,
|
| 128 |
'#maxlength' => 10
|
| 129 |
);
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
return $form;
|
| 134 |
}
|
| 135 |
|
| 136 |
function bookings_room_view($node, $teaser = FALSE, $page = FALSE) {
|
| 137 |
$node = node_prepare($node, $teaser);
|
| 138 |
$node->content['myfield'] = array(
|
| 139 |
'#value' => theme('bookings_room_info', $node),
|
| 140 |
'#weight' => 1,
|
| 141 |
);
|
| 142 |
|
| 143 |
return $node;
|
| 144 |
}
|
| 145 |
|
| 146 |
function theme_bookings_room_info($node) {
|
| 147 |
$start_datetime = date_format_date(date_convert($node->start_datetime, DATE_UNIX, DATE_OBJECT));
|
| 148 |
$end_datetime = date_format_date(date_convert($node->end_datetime, DATE_UNIX, DATE_OBJECT));
|
| 149 |
|
| 150 |
$output = '<div class="bookings_room_info">';
|
| 151 |
//$output .= t('The order is for %quantity %color items.', array('%quantity' => check_plain($node->quantity), '%color' => check_plain($node->color)));
|
| 152 |
$output .= t('Start time: %start', array('%start' => $start_datetime));
|
| 153 |
$output .= '<br />';
|
| 154 |
$output .= t('End time: %end', array('%end' => $end_datetime));
|
| 155 |
|
| 156 |
//$output .= $node->start_datetime;
|
| 157 |
$output .= '</div>';
|
| 158 |
return $output;
|
| 159 |
}
|
| 160 |
|
| 161 |
function bookings_room_theme() {
|
| 162 |
return array(
|
| 163 |
'bookings_room_info' => array(
|
| 164 |
'arguments' => array('node'),
|
| 165 |
),
|
| 166 |
);
|
| 167 |
}
|
| 168 |
|
| 169 |
/**
|
| 170 |
* Implementation of hook_validate()
|
| 171 |
*/
|
| 172 |
function bookings_room_validate($node, &$form) {
|
| 173 |
if ($node->start_date == $node->end_date) {
|
| 174 |
form_set_error('end_date', t('Room bookings must have a start time and an end time.'));
|
| 175 |
}
|
| 176 |
if ($node->start_date > $node->end_date) {
|
| 177 |
form_set_error('end_date', t('A room booking may not start before it ends.'));
|
| 178 |
}
|
| 179 |
}
|
| 180 |
|
| 181 |
/**
|
| 182 |
* Implementation of hook_insert()
|
| 183 |
*/
|
| 184 |
function bookings_room_insert($node) {
|
| 185 |
//convert times from ISO to unix
|
| 186 |
|
| 187 |
$node->start_date = date_convert($node->start_date, DATE_DATETIME, DATE_UNIX);
|
| 188 |
$node->end_date = date_convert($node->end_date, DATE_DATETIME, DATE_UNIX);
|
| 189 |
|
| 190 |
db_query("INSERT INTO {bookings_room_node} (nid, start_datetime, end_datetime) VALUES (%d, %d, %d)", $node->nid, $node->start_date, $node->end_date);
|
| 191 |
}
|
| 192 |
|
| 193 |
/**
|
| 194 |
* Implementation of hook_update()
|
| 195 |
*/
|
| 196 |
function bookings_room_update($node) {
|
| 197 |
//convert times from ISO to unix
|
| 198 |
$node->start_date = date_convert($node->start_date, DATE_DATETIME, DATE_UNIX);
|
| 199 |
$node->end_date = date_convert($node->end_date, DATE_DATETIME, DATE_UNIX);
|
| 200 |
|
| 201 |
//if ($node->revision) {
|
| 202 |
// bookings_room_insert($node);
|
| 203 |
//}
|
| 204 |
//else {
|
| 205 |
db_query("UPDATE {bookings_room_node} SET nid = %d, start_datetime = %d, end_datetime = %d", $node->nid, $node->start_date, $node->end_date);
|
| 206 |
//}
|
| 207 |
}
|
| 208 |
|
| 209 |
/**
|
| 210 |
* Implementation of hook_delete()
|
| 211 |
*/
|
| 212 |
function bookings_room_delete($node) {
|
| 213 |
db_query('DELETE FROM {bookings_room_node} WHERE nid = %d', $node->nid);
|
| 214 |
}
|
| 215 |
|
| 216 |
|
| 217 |
/**
|
| 218 |
* Implementation of hook_help().
|
| 219 |
*/
|
| 220 |
function bookings_room_help($path) {
|
| 221 |
switch ($path) {
|
| 222 |
case 'admin/help#bookings_room':
|
| 223 |
return t('<p>This module allows room bookings to be created.</p>');
|
| 224 |
break;
|
| 225 |
}
|
| 226 |
}
|
| 227 |
/**
|
| 228 |
* Implementation of hook_menu().
|
| 229 |
*/
|
| 230 |
|
| 231 |
function bookings_room_menu() {
|
| 232 |
$items = array();
|
| 233 |
|
| 234 |
$items['admin/settings/bookings/bookings_room'] = array(
|
| 235 |
'title' => 'Room Bookings settings',
|
| 236 |
'description' => 'Settings for the room booking system',
|
| 237 |
'page callback' => 'drupal_get_form',
|
| 238 |
'page arguments' => array('bookings_room_admin'),
|
| 239 |
'access arguments' => array('access administration pages'),
|
| 240 |
'type' => MENU_LOCAL_TASK,
|
| 241 |
);
|
| 242 |
|
| 243 |
return $items;
|
| 244 |
}
|
| 245 |
|
| 246 |
/**
|
| 247 |
*
|
| 248 |
*/
|
| 249 |
function bookings_room_admin() {
|
| 250 |
|
| 251 |
if ($form_id=='bookings-admin') {
|
| 252 |
$mins = range(0,30, 5);
|
| 253 |
|
| 254 |
$form['bookings_room']['bookings_room_timeslot'] = array(
|
| 255 |
'#type' => 'select',
|
| 256 |
'#title' => t('Smallest time a room can be booked'),
|
| 257 |
'#description' => t("This controls what the default page is when a user clicks the main link"),
|
| 258 |
'#default_value' => variable_get('bookings_room_timeslot', 'year'),
|
| 259 |
'#options' => $mins,
|
| 260 |
'#required' => TRUE,
|
| 261 |
);
|
| 262 |
|
| 263 |
return system_settings_form($form);
|
| 264 |
}
|
| 265 |
}
|
| 266 |
|
| 267 |
/**
|
| 268 |
* Implmentation of hook_form_alter().
|
| 269 |
*/
|
| 270 |
function bookings_room_form_alter(&$form, $form_state, $form_id) {
|
| 271 |
if ($form_id=='bookings_admin') {
|
| 272 |
$form['bookings']['room'] = array(
|
| 273 |
'#type' => 'fieldset',
|
| 274 |
'#title' => t('Room booking settings'),
|
| 275 |
'#collapsible' => TRUE,
|
| 276 |
'#collapsed' => TRUE,
|
| 277 |
);
|
| 278 |
|
| 279 |
$form['bookings']['room']['bookings_room_unit'] = array(
|
| 280 |
'#type' => 'select',
|
| 281 |
'#title' => t('Unit of measure for timeslots'),
|
| 282 |
'#description' => t(''),
|
| 283 |
'#default_value' => variable_get('bookings_room_unit', 'mins'),
|
| 284 |
'#options' => array(
|
| 285 |
'mins' => t('minutes'),
|
| 286 |
'hours' => t('hours'),
|
| 287 |
'days' => t('days'),
|
| 288 |
),
|
| 289 |
'#required' => TRUE,
|
| 290 |
);
|
| 291 |
|
| 292 |
$measure = range(0, 60, 1);
|
| 293 |
|
| 294 |
$form['bookings']['room']['bookings_room_timeslot'] = array(
|
| 295 |
'#type' => 'select',
|
| 296 |
'#title' => t('Smallest time a room can be booked'),
|
| 297 |
'#description' => t("This controls the timeslot"),
|
| 298 |
'#default_value' => variable_get('bookings_room_timeslot', 15),
|
| 299 |
'#options' => $measure,
|
| 300 |
'#required' => TRUE,
|
| 301 |
);
|
| 302 |
|
| 303 |
}
|
| 304 |
}
|