Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @file
- * Booking module file.
- *
- */
- /**
- * Implementation of hook_views_api().
- */
- function bookable_views_api() {
- return array(
- 'api' => 2,
- 'path' => drupal_get_path('module', 'bookable') . '/includes',
- );
- }
- /**
- * Implementation of hook_link().
- *
- * Used to dynamically generate bookable links for nodes
- * that are bookable so they can be booked from any page
- * or from search result where the node is present if the
- * user is logged in.
- */
- function bookable_link($type, $object, $teaser) {
- global $user;
- $bookable_types = variable_get('bookable_node_types', array('resource'));
- //Check to ensure that the node type is 'bookable' and the user is logged in
- if (in_array($object->type, $bookable_types, TRUE) && $user->uid != 0) {
- $links['booking_link'] = array('title' => t('Book this @type', array('@type' => $object->type)),
- 'href' => "node/add/booking/nid/$object->nid", );
- return $links;
- }
- }
- /**
- * Implementation of hook_menu().
- */
- function bookable_menu() {
- $items['admin/config/content/bookable'] = array(
- 'title' => 'Bookable settings',
- 'description' => 'Change how Bookable module behaves',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('bookable_admin_settings'),
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_NORMAL_ITEM,
- 'file' => 'includes/bookable.admin.inc',
- );
- return $items;
- }
- /**
- * Implementation of hook_form_alter().
- */
- function bookable_form_alter(&$form, &$form_state, $form_id) {
- global $user;
- $node = $form['#node'];
- switch ($form_id) {
- case 'booking_node_form':
- $form['#validate'][] = 'bookable_booking_node_form_validate';
- $form['ticketing'] = array(
- '#type' => 'fieldset',
- '#collapsible' => TRUE,
- '#title' => t('Ticketing Details'),
- '#collapsed' => FALSE,
- '#weight' => 10,
- '#group' => 'allocated_seating',
- );
- $form['ticketing']['number_of_tickets'] = array(
- '#type' => 'textfield',
- '#title' => t('Number of tickets'),
- '#default_value' => isset($form_state['values']['number_of_tickets']) ? $form_state['values']['number_of_tickets'] : $node->number_of_tickets,
- '#size' => 60,
- '#description' => t('Enter the number of tickets available for sale in this ticketing layout'),
- '#maxlength' => 10,
- '#required' => TRUE,
- );
- $allocated_ticketing_default = (isset($form_state['values']['allocated_ticketing']) ? $form_state['values']['allocated_ticketing']
- : (isset($node->allocated_ticketing) ? $node->allocated_ticketing : 0));
- $form['ticketing']['allocated_ticketing'] = array(
- '#type' => 'radios',
- '#default_value' => $allocated_ticketing_default,
- '#title' => t('Allocated Ticketing?'),
- '#description' => t('Choose the ticketing arrangement for this ticket layout'),
- '#options' => array(
- 1 => 'Yes',
- 0 => 'No'
- ),
- '#ajax' => array(
- 'callback' => 'ajax_allocated_ticketing_plan',
- 'wrapper' => 'ticket-layout-plan-wrapper',
- 'event' => 'change',
- 'method' => 'replace',
- 'effect' => 'fade',
- ),
- );
- if ($allocated_ticketing_default) {
- // Don't need this without allocated ticketing.
- $form['ticketing']['number_of_tickets']['#ajax'] = array(
- 'callback' => 'ajax_allocated_ticketing_plan',
- 'wrapper' => 'ticket-layout-plan-wrapper',
- 'event' => 'change',
- 'method' => 'replace',
- 'effect' => 'fade',
- );
- }
- $form['#cache'] = TRUE;
- $tickets_default = (isset($form_state['values']['number_of_tickets']) ? $form_state['values']['number_of_tickets'] : $node->number_of_tickets);
- if ($tickets_default && $allocated_ticketing_default) {
- $form['ticketing']['ticket_plan'] = array(
- '#prefix' => '<div class="clear-block" id="ticket-layout-plan-wrapper">',
- '#suffix' => '</div>',
- '#description' => t('Enter the number of rows and columns in your layout. You may drag and drop seats to reorder them. You
- may add another floor/section/level by clicking add floor. You can drag seats from one floor to another
- by dropping them on the tab of the floor you wish to drag them to. '),
- '#ajax_extra' => 'ticketing/',
- '#default_value' => (isset($form_state['values']['ticket_plan']) ? $form_state['values']['ticket_plan'] : $node->ticket_plan),
- '#type' => 'allocated_seating_seat_design',
- '#seats' => $tickets_default
- );
- }
- else {
- // Add these here in case ahah adds our plan.
- allocated_seating_setup();
- $form['ticketing']['ticket_plan'] = array(
- '#prefix' => '<div class="clear-block" id="ticket-layout-plan-wrapper">',
- '#suffix' => '</div>',
- '#value' => t('This ticket layout method does not have allocated ticketing or the number of tickets is not known')
- );
- }
- break;
- }
- }
- function ajax_allocated_ticketing_plan($form, &$form_state) {
- return $form['ticketing']['ticket_plan'];
- }
- /**
- * Validation function for booking_node_form.
- */
- function bookable_booking_node_form_validate($form, &$form_state) {
- if(isset($form_state['values']['op'])) {
- $op = $form_state['values']['op'];
- $from_date = $form_state['values']['field_bookingdate'][0]['value'];
- $to_date = $form_state['values']['field_bookingdate'][0]['value2'];
- $resource_nid = $form_state['values']['field_nid'][0]['nid'];
- $conflicts = false;
- //$conflicts = bookable_check_overlap($from_date, $to_date, $resource_nid);
- $errors = "";
- $nid = $form_state['values']['nid'];
- switch ($op) {
- case 'Delete':
- //Do not validate on deletes
- break;
- case 'Save':
- if ($conflicts) {
- if (is_numeric($nid)) {
- /* If there is only one conflict and it happens to be itself cancel validation.
- This occurs when a node is being edited. */
- if (count($conflicts) == 1 && $conflicts[0]['nid'] == $nid) {
- break;
- }
- foreach ($conflicts as $conflict) {
- if ($conflict && $conflict['nid'] != $nid) {
- $node = node_load($resource_nid);
- $errors .= t("This %booking_node_type has been booked between %from_booking_date and %to_booking_date <br/>",
- array('%booking_node_type' => $node->type,
- '%from_booking_date' => $conflict['field_bookingdate_value'],
- '%to_booking_date' => $conflict['field_bookingdate_value2'])) ;
- }
- }
- $errors .= "Please use the calendar (if present) to find an available date and time that best suits you";
- form_set_error('field_bookingdate', t($errors));
- }
- //Validation if the node is new
- else if (!isset($nid)) {
- foreach ($conflicts as $conflict) {
- $node = node_load($resource_nid);
- $errors .= t("This %booking_node_type has been booked between %from_booking_date and %to_booking_date <br/>",
- array('%booking_node_type' => $node->type,
- '%from_booking_date' => $conflict['field_bookingdate_value'],
- '%to_booking_date' => $conflict['field_bookingdate_value2'])) ;
- }
- $errors .= t("Please use the calendar (if present) to find an available date and time that best suits you");
- form_set_error('field_bookingdate', $errors);
- }
- }
- break;
- }
- }
- }
- /**
- * Ensure mutual exclusion so that a node may not be booked for the same time period
- * period
- *
- * @param String $from_date
- * From date of booking
- *
- * @param String $to_date
- * To date of booking
- *
- * @param int $nid
- * Id of the node being booked
- *
- * @return array $results
- * Returns an array containing the time that conflicts with requestime period
- */
- function bookable_check_overlap($from_date, $to_date, $nid) {
- $results = FALSE;
- $result = db_query("SELECT field_bookingdate_value, field_bookingdate_value2, nid
- FROM content_type_booking AS c
- WHERE (
- (
- '%s'
- BETWEEN c.field_bookingdate_value
- AND c.field_bookingdate_value2
- )
- OR (
- '%s'
- BETWEEN c.field_bookingdate_value
- AND c.field_bookingdate_value2
- )
- OR (
- c.field_bookingdate_value
- BETWEEN '%s'
- AND '%s'
- )
- OR (
- c.field_bookingdate_value2
- BETWEEN '%s'
- AND '%s'
- )
- )
- AND c.field_nid_nid = %d", $from_date, $to_date, $from_date, $to_date, $from_date, $to_date, $nid);
- while ($row = db_fetch_array($result)) {
- $results[] = $row;
- }
- return $results;
- }
- /**
- * Implementation of hook_insert().
- */
- function bookable_node_insert($node) {
- $type = $node->type;
- if($type != 'booking') return $node;
- $function = 'bookable_node_' . $type . '_insert';
- if (function_exists($function)) {
- $function($node);
- }
- }
- /**
- * Implementation of hook_update().
- */
- function bookable_node_update($node) {
- // If this is a new node or we're adding a new revision.
- $type = $node->type;
- if($type != 'booking') return $node;
- if ($node->revision) {
- bookable_insert($node);
- }
- else {
- $function = 'bookable_node_' . $type . '_update';
- if (function_exists($function)) {
- $function($node);
- }
- }
- }
- /**
- * Implementation of hook_delete().
- */
- function bookable_node_delete(&$node) {
- $type = $node->type;
- if($type != 'booking') return $node;
- $function = 'bookable_node_' . $type . '_delete';
- if (function_exists($function)) {
- $function($node);
- }
- }
- /**
- * Hook insert handler
- */
- function bookable_node_booking_insert($node) {
- // Insert into db.
- $node->ticket_plan = serialize($node->ticket_plan);
- drupal_write_record('bookable_ticket_layouts', $node);
- }
- /**
- * Hook update handler
- */
- function bookable_node_booking_update($node) {
- // Do the update.
- $node->ticket_plan = serialize($node->ticket_plan);
- //$result = drupal_write_record('bookable_ticket_layouts', $node, 'vid');
- $result = db_query("SELECT count(vid) FROM {bookable_ticket_layouts} WHERE vid = :vid && nid= :nid",array(':vid'=> $node->vid,':nid'=> $node->nid ))->fetchField();
- if($result) {
- drupal_write_record('bookable_ticket_layouts', $node,array('vid','nid'));
- } else {
- drupal_write_record('bookable_ticket_layouts', $node);
- }
- }
- /**
- * Hook delete handler
- */
- function bookable_node_booking_delete(&$node) {
- // Do the delete.
- db_query("DELETE FROM {bookable_ticket_layouts} WHERE vid = %d", $node->vid);
- }
- /**
- * Hook load handler
- */
- function bookable_node_booking_load($node) {
- $data = db_query("SELECT * FROM {bookable_ticket_layouts}
- WHERE vid = :vid limit 1", array(":vid" => $node->vid))->fetchObject();
- $data->ticket_plan = unserialize($data->ticket_plan);
- return $data;
- }
- /**
- * Implementation of hook_load().
- */
- function bookable_node_load($nodes, $types) {
- if($types[0] != 'booking') return false;
- foreach ($nodes as $nid => $node) {
- $function = 'bookable_node_' . $types[0] . '_load';
- if (function_exists($function)) {
- $return = $function($node);
- if(!empty($return)) {
- $node->number_of_tickets = $return->number_of_tickets;
- $node->allocated_ticketing = $return->allocated_ticketing;
- $node->ticket_plan = $return->ticket_plan;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement