Advertisement
Guest User

Bookable Module

a guest
May 26th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.62 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @file
  4.  * Booking module file.
  5.  *
  6.  */
  7.  
  8.  
  9.  
  10.  
  11. /**
  12.  * Implementation of hook_views_api().
  13.  */
  14. function bookable_views_api() {
  15.   return array(
  16.     'api' => 2,
  17.     'path' => drupal_get_path('module', 'bookable') . '/includes',
  18.   );
  19. }
  20.  
  21. /**
  22.  * Implementation of hook_link().
  23.  *
  24.  * Used to dynamically generate bookable links for nodes
  25.  * that are bookable so they can be booked from any page
  26.  * or from search result where the node is present if the
  27.  * user is logged in.
  28.  */
  29. function bookable_link($type, $object, $teaser) {
  30.   global $user;
  31.   $bookable_types = variable_get('bookable_node_types', array('resource'));
  32.  
  33.   //Check to ensure that the node type is 'bookable' and the user is logged in
  34.   if (in_array($object->type, $bookable_types, TRUE) && $user->uid != 0) {
  35.     $links['booking_link'] = array('title' => t('Book this @type', array('@type' => $object->type)),
  36.       'href' => "node/add/booking/nid/$object->nid", );
  37.  
  38.     return $links;
  39.   }
  40. }
  41.  
  42. /**
  43.  * Implementation of hook_menu().
  44.  */
  45. function bookable_menu() {
  46.  
  47.   $items['admin/config/content/bookable'] = array(
  48.     'title'            => 'Bookable settings',
  49.     'description'      => 'Change how Bookable module behaves',
  50.     'page callback'    => 'drupal_get_form',
  51.     'page arguments'   => array('bookable_admin_settings'),
  52.     'access arguments' => array('administer site configuration'),
  53.     'type'             => MENU_NORMAL_ITEM,
  54.     'file' => 'includes/bookable.admin.inc',
  55.   );
  56.  
  57.   return $items;
  58. }
  59.  
  60. /**
  61.  * Implementation of hook_form_alter().
  62.  */
  63. function bookable_form_alter(&$form, &$form_state, $form_id) {
  64.   global $user;
  65.   $node = $form['#node'];
  66.   switch ($form_id) {
  67.     case 'booking_node_form':
  68.         $form['#validate'][] = 'bookable_booking_node_form_validate';
  69.     $form['ticketing'] = array(
  70.         '#type'         => 'fieldset',
  71.         '#collapsible'  => TRUE,
  72.         '#title'        => t('Ticketing Details'),
  73.         '#collapsed'    => FALSE,
  74.         '#weight'       => 10,
  75.         '#group'        => 'allocated_seating',
  76.     );
  77.     $form['ticketing']['number_of_tickets'] = array(
  78.         '#type'          => 'textfield',
  79.         '#title'         => t('Number of tickets'),
  80.         '#default_value' => isset($form_state['values']['number_of_tickets']) ? $form_state['values']['number_of_tickets'] : $node->number_of_tickets,
  81.         '#size'          => 60,
  82.         '#description'   => t('Enter the number of tickets available for sale in this ticketing layout'),
  83.         '#maxlength'     => 10,
  84.         '#required'      => TRUE,
  85.     );
  86.     $allocated_ticketing_default = (isset($form_state['values']['allocated_ticketing']) ? $form_state['values']['allocated_ticketing']
  87.                           : (isset($node->allocated_ticketing) ? $node->allocated_ticketing : 0));
  88.  
  89.     $form['ticketing']['allocated_ticketing'] = array(
  90.         '#type'          => 'radios',
  91.         '#default_value' => $allocated_ticketing_default,
  92.         '#title'       => t('Allocated Ticketing?'),
  93.         '#description' => t('Choose the ticketing arrangement for this ticket layout'),
  94.         '#options'    => array(
  95.             1 => 'Yes',
  96.             0 => 'No'
  97.         ),
  98.         '#ajax' => array(
  99.             'callback' => 'ajax_allocated_ticketing_plan',
  100.             'wrapper' => 'ticket-layout-plan-wrapper',
  101.             'event' => 'change',
  102.             'method' => 'replace',
  103.             'effect' => 'fade',
  104.         ),
  105.     );
  106.     if ($allocated_ticketing_default) {
  107.         // Don't need this without allocated ticketing.
  108.         $form['ticketing']['number_of_tickets']['#ajax'] = array(
  109.             'callback' => 'ajax_allocated_ticketing_plan',
  110.             'wrapper' => 'ticket-layout-plan-wrapper',
  111.             'event' => 'change',
  112.             'method' => 'replace',
  113.             'effect' => 'fade',
  114.         );
  115.     }
  116.     $form['#cache'] = TRUE;
  117.  
  118.     $tickets_default = (isset($form_state['values']['number_of_tickets']) ? $form_state['values']['number_of_tickets'] : $node->number_of_tickets);
  119.     if ($tickets_default && $allocated_ticketing_default) {
  120.         $form['ticketing']['ticket_plan'] = array(
  121.             '#prefix' => '<div class="clear-block" id="ticket-layout-plan-wrapper">',
  122.             '#suffix' => '</div>',
  123.             '#description' => t('Enter the number of rows and columns in your layout. You may drag and drop seats to reorder them. You
  124.                       may add another floor/section/level by clicking add floor. You can drag seats from one floor to another
  125.                       by dropping them on the tab of the floor you wish to drag them to. '),
  126.             '#ajax_extra' => 'ticketing/',
  127.             '#default_value' => (isset($form_state['values']['ticket_plan']) ? $form_state['values']['ticket_plan'] : $node->ticket_plan),
  128.             '#type' => 'allocated_seating_seat_design',
  129.             '#seats' => $tickets_default
  130.         );
  131.     }
  132.     else {
  133.         // Add these here in case ahah adds our plan.
  134.         allocated_seating_setup();
  135.         $form['ticketing']['ticket_plan'] = array(
  136.             '#prefix' => '<div class="clear-block" id="ticket-layout-plan-wrapper">',
  137.             '#suffix' => '</div>',
  138.             '#value' => t('This ticket layout method does not have allocated ticketing or the number of tickets is not known')
  139.         );
  140.     }
  141.       break;
  142.   }
  143. }
  144. function ajax_allocated_ticketing_plan($form, &$form_state) {
  145.     return $form['ticketing']['ticket_plan'];
  146. }
  147. /**
  148.  * Validation function for booking_node_form.
  149.  */
  150. function bookable_booking_node_form_validate($form, &$form_state) {
  151.   if(isset($form_state['values']['op'])) {
  152.       $op = $form_state['values']['op'];
  153.       $from_date = $form_state['values']['field_bookingdate'][0]['value'];
  154.       $to_date = $form_state['values']['field_bookingdate'][0]['value2'];
  155.       $resource_nid = $form_state['values']['field_nid'][0]['nid'];
  156.       $conflicts = false;
  157.       //$conflicts = bookable_check_overlap($from_date, $to_date, $resource_nid);
  158.       $errors = "";
  159.       $nid = $form_state['values']['nid'];
  160.  
  161.  
  162.       switch ($op) {
  163.         case 'Delete':
  164.           //Do not validate on deletes
  165.           break;
  166.  
  167.         case 'Save':
  168.           if ($conflicts) {
  169.         if (is_numeric($nid)) {
  170.           /* If there is only one conflict and it happens to be itself cancel validation.
  171.             This occurs when a node is being edited. */
  172.           if (count($conflicts) == 1 && $conflicts[0]['nid'] == $nid) {
  173.             break;
  174.           }
  175.           foreach ($conflicts as $conflict) {
  176.             if ($conflict && $conflict['nid'] != $nid) {
  177.               $node = node_load($resource_nid);
  178.               $errors .= t("This %booking_node_type has been booked between %from_booking_date and %to_booking_date <br/>",
  179.                   array('%booking_node_type' => $node->type,
  180.                       '%from_booking_date' => $conflict['field_bookingdate_value'],
  181.                       '%to_booking_date' => $conflict['field_bookingdate_value2'])) ;
  182.             }
  183.           }
  184.           $errors .= "Please use the calendar (if present) to find an available date and time that best suits you";
  185.           form_set_error('field_bookingdate', t($errors));
  186.         }
  187.  
  188.         //Validation if the node is new
  189.         else if (!isset($nid)) {
  190.           foreach ($conflicts as $conflict) {
  191.  
  192.             $node = node_load($resource_nid);
  193.  
  194.             $errors .= t("This %booking_node_type has been booked between %from_booking_date and %to_booking_date <br/>",
  195.                   array('%booking_node_type' => $node->type,
  196.                       '%from_booking_date' => $conflict['field_bookingdate_value'],
  197.                       '%to_booking_date' => $conflict['field_bookingdate_value2'])) ;
  198.  
  199.           }
  200.           $errors .= t("Please use the calendar (if present) to find an available date and time that best suits you");
  201.           form_set_error('field_bookingdate', $errors);
  202.         }
  203.           }
  204.           break;
  205.       }
  206.   }
  207. }
  208.  
  209. /**
  210.  * Ensure mutual exclusion so that a node may not be booked for the same time period
  211.  * period
  212.  *
  213.  * @param String $from_date
  214.  *  From date of booking
  215.  *
  216.  * @param String $to_date
  217.  *  To date of booking
  218.  *
  219.  * @param int $nid
  220.  *  Id of the node being booked
  221.  *
  222.  * @return array $results
  223.  *  Returns an array containing the time that conflicts with requestime period
  224.  */
  225. function bookable_check_overlap($from_date, $to_date, $nid) {
  226.   $results = FALSE;
  227.   $result = db_query("SELECT field_bookingdate_value, field_bookingdate_value2, nid
  228.              FROM content_type_booking AS c
  229.              WHERE (
  230.                      (
  231.                        '%s'
  232.                        BETWEEN c.field_bookingdate_value
  233.                        AND c.field_bookingdate_value2
  234.                      )
  235.                      OR (
  236.                        '%s'
  237.                        BETWEEN c.field_bookingdate_value
  238.                        AND c.field_bookingdate_value2
  239.                      )
  240.                      OR (
  241.                        c.field_bookingdate_value
  242.                        BETWEEN  '%s'
  243.                        AND  '%s'
  244.                      )
  245.                      OR (
  246.                        c.field_bookingdate_value2
  247.                        BETWEEN  '%s'
  248.                        AND  '%s'
  249.                      )
  250.                    )
  251.                    AND c.field_nid_nid = %d", $from_date, $to_date, $from_date, $to_date, $from_date, $to_date, $nid);
  252.  
  253.   while ($row = db_fetch_array($result)) {
  254.     $results[] = $row;
  255.   }
  256.  
  257.   return $results;
  258. }
  259.  
  260.  
  261.  
  262.  
  263.  
  264. /**
  265.  * Implementation of hook_insert().
  266.  */
  267. function bookable_node_insert($node) {
  268.   $type = $node->type;
  269.  
  270.   if($type != 'booking') return $node;
  271.   $function = 'bookable_node_' . $type . '_insert';
  272.   if (function_exists($function)) {
  273.     $function($node);
  274.   }
  275. }
  276.  
  277.  
  278. /**
  279.  * Implementation of hook_update().
  280.  */
  281. function bookable_node_update($node) {
  282.   // If this is a new node or we're adding a new revision.
  283.  
  284.   $type = $node->type;
  285.  
  286.   if($type != 'booking') return $node;
  287.  
  288.   if ($node->revision) {
  289.     bookable_insert($node);
  290.   }
  291.   else {
  292.  
  293.     $function = 'bookable_node_' . $type . '_update';
  294.     if (function_exists($function)) {
  295.       $function($node);
  296.     }
  297.   }
  298. }
  299.  
  300.  
  301. /**
  302.  * Implementation of hook_delete().
  303.  */
  304. function bookable_node_delete(&$node) {
  305.   $type = $node->type;
  306.   if($type != 'booking') return $node;
  307.   $function = 'bookable_node_' . $type . '_delete';
  308.   if (function_exists($function)) {
  309.     $function($node);
  310.   }
  311. }
  312.  
  313.  
  314. /**
  315.  * Hook insert handler
  316.  */
  317. function bookable_node_booking_insert($node) {
  318.   // Insert into db.
  319.   $node->ticket_plan = serialize($node->ticket_plan);
  320.   drupal_write_record('bookable_ticket_layouts', $node);
  321. }
  322.  
  323. /**
  324.  * Hook update handler
  325.  */
  326. function bookable_node_booking_update($node) {
  327.   // Do the update.
  328.   $node->ticket_plan = serialize($node->ticket_plan);
  329.  
  330.   //$result = drupal_write_record('bookable_ticket_layouts', $node, 'vid');
  331.   $result = db_query("SELECT count(vid) FROM {bookable_ticket_layouts} WHERE vid = :vid && nid= :nid",array(':vid'=> $node->vid,':nid'=> $node->nid ))->fetchField();
  332.   if($result) {
  333.     drupal_write_record('bookable_ticket_layouts', $node,array('vid','nid'));
  334.   } else {
  335.     drupal_write_record('bookable_ticket_layouts', $node);
  336.   }
  337. }
  338.  
  339. /**
  340.  * Hook delete handler
  341.  */
  342. function bookable_node_booking_delete(&$node) {
  343.   // Do the delete.
  344.   db_query("DELETE FROM {bookable_ticket_layouts} WHERE vid = %d", $node->vid);
  345. }
  346. /**
  347.  * Hook load handler
  348.  */
  349. function bookable_node_booking_load($node) {
  350.  
  351.   $data = db_query("SELECT * FROM {bookable_ticket_layouts}
  352.                                   WHERE vid = :vid limit 1", array(":vid" =>  $node->vid))->fetchObject();
  353.   $data->ticket_plan = unserialize($data->ticket_plan);
  354.  
  355.   return $data;
  356. }
  357.  
  358. /**
  359.  * Implementation of hook_load().
  360.  */
  361. function bookable_node_load($nodes, $types)  { 
  362.  
  363.   if($types[0] != 'booking') return false;
  364.   foreach ($nodes as $nid => $node) {
  365.       $function = 'bookable_node_' . $types[0] . '_load';
  366.       if (function_exists($function)) {
  367.         $return = $function($node);
  368.         if(!empty($return)) {
  369.         $node->number_of_tickets = $return->number_of_tickets;
  370.         $node->allocated_ticketing = $return->allocated_ticketing; 
  371.         $node->ticket_plan = $return->ticket_plan; 
  372.         }  
  373.       }
  374.   }
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement