From 48f5b4aa0b314d7a105edec40980f48a9fe24a25 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Fri, 27 May 2016 09:15:04 +1000 Subject: [PATCH] initial work on mysql view --- booking.module | 10 ++++++ booking.regn_node.inc | 80 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/booking.module b/booking.module index caa5598..bb9b5a5 100644 --- a/booking.module +++ b/booking.module @@ -225,6 +225,16 @@ function booking_menu() { 'type' => MENU_CALLBACK, ); + $items['admin/booking/createview'] = array( + 'title' => 'Booking Test mysql view creation', + 'description' => 'Booking Test mysql view creation', + 'page callback' => '_booking_node_create_mysqlview', + //'access arguments' => array('edit bookings'), + //always allow access to this page - security risk! + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + //pages for attendees to fill out information $items['booking'] = array( 'title' => $bookingTitle . ' Booking Form', diff --git a/booking.regn_node.inc b/booking.regn_node.inc index 45c62c4..3b42b72 100644 --- a/booking.regn_node.inc +++ b/booking.regn_node.inc @@ -12,6 +12,86 @@ function booking_node_presave($node) { } } +/** + * Function to create a mysql view of a complete person + * This is still in test phase and does not return any value + * + * @param + * @return + */ +function _booking_node_create_mysqlview() +{ + global $event; + $studygroups = array(); + + $query = db_select('booking_person', 'p'); + //add price info + $query->join('booking_price', 'pr', 'p.booking_payment_id = pr.pid'); + //add travel form info if it exists + $query->leftJoin('booking_travel', 't', 'p.nid = t.booking_person_nid'); + + //add the joins for room allocation info if enabled + if (variable_get('booking_enable_roomallocations', 0) == 1) { + $query->leftJoin('booking_room_mapping', 'rm', 'p.nid = rm.booking_nodeid'); + $query->leftJoin('booking_room_definition', 'r', 'rm.booking_roomid = r.rid'); + $query->leftJoin('booking_room_locations', 'l', 'l.lid = r.booking_room_location_id'); + } + + //add the joins to flatten out study groups into columns + if (variable_get('booking_enable_studygroups', 0) == 1) { + //get details of the study groups defined for this event + //but when this function gets called we don't have the global $event variable populated, so do the join here ourselves + $studygroups_query = db_query("SELECT * FROM {booking_studygroup_list} l INNER JOIN {booking_event} e ON l.booking_eventid = e.eid WHERE e.booking_event_active=1"); + $studygroups = $studygroups_query->fetchAllAssoc('sid'); + + foreach ($studygroups as $studygroup) { + $id = $studygroup->sid; + $query->leftJoin('booking_studygroup_mapping', 's' . $id, 'p.nid = s' . $id . '.booking_node_id and s' . $id . '.booking_studygroup_id = ' . $id); + } + } + + //filter the results either by current active event + $query->condition('p.booking_eventid', $event->eid, '='); + + //add the database fields we always want to retrieve + $query->fields('p') + ->fields('t') + ->fields('pr', array('booking_price', 'booking_price_descrip','booking_late_price')); + + if (variable_get('booking_enable_roomallocations', 0) == 1) { + $query->fields('rm', array('booking_room_bedtype')) + ->fields('r', array('rid', 'booking_room_location_id', 'booking_room_number')) + ->fields('l'); + } + + //now add the study group fields if applicable + if (variable_get('booking_enable_studygroups', 0) == 1) { + + foreach ($studygroups as $studygroup) + { + $id = $studygroup->sid; + $query->addField('s' . $id, 'booking_session_id', 'session' . $id); + $query->addField('s' . $id, 'booking_studygroup_role', 'session' . $id . '_role'); + } + } + + //now that we have built the query as a SelectObject, turn it into a string we can use to create a view + $querystring=$query->__toString(); + $querystring=str_replace("{",'',$querystring); + $querystring=str_replace("}",'',$querystring); + foreach($query->getArguments() as $key=> $item) { + if(!$item) { + $item = 'NULL'; + } + $querystring=str_replace($key.')',$item.')',$querystring); + } + $querystring = "CREATE OR REPLACE VIEW booking_person_view AS " . $querystring; + watchdog('booking_debug', "
Booking Person View creation query string:\n@info
", array('@info' => print_r($querystring, true))); + $viewquery = db_query($querystring); + $result = $viewquery->fetchObject(); + watchdog('booking_debug', "
Booking Person View creation result:\n@info
", array('@info' => print_r($result, true))); +} + /** * Function to query the database for the complete object representing a registration *