PageRenderTime 66ms CodeModel.GetById 2ms app.highlight 52ms RepoModel.GetById 1ms app.codeStats 1ms

/trunk/manage/controllers/events/sessioncontroller.php

https://bitbucket.org/pooshonk/esw
PHP | 1370 lines | 1169 code | 155 blank | 46 comment | 168 complexity | 3fdc1061bd681561bdc149e7fa8bd5ac MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2
   3class Sessioncontroller {
   4	
   5	public function __construct( PeacockCarterFrameworkRegistry $registry, $directCall )
   6	{
   7		$this->registry = $registry;
   8		if( $this->registry->getObject('authenticate')->isAuthorised('events_sessions') == true )
   9		{
  10			$this->registry->getObject('adminmenubuilder')->buildMenu( 'events.sessions' );
  11			if( $this->registry->getURLBit(1) != '' )
  12			{
  13	
  14				switch( $this->registry->getURLBit(2) )
  15				{
  16					case'add':
  17						$this->addSession( intval( $this->registry->getURLBit(3) ) );
  18						break;
  19					case'create':
  20						$this->createSession( );
  21						break;
  22					case'edit':
  23						$this->editSessionByEvent( intval( $this->registry->getURLBit(3) ), intval( $this->registry->getURLBit(4) ));
  24						break;
  25					case'edit-current':
  26						$this->editSession( intval( $this->registry->getURLBit(3) ), 'current' );
  27						break;
  28					case'edit-previous':
  29						$this->editSession( intval( $this->registry->getURLBit(3) ), 'previous' );
  30						break;
  31					case'delete':
  32						$this->delete( intval( $this->registry->getURLBit(3) ), intval( $this->registry->getURLBit(4) ), intval( $this->registry->getURLBit(5) ) );
  33						break;
  34					case'previous':
  35						$this->listPrevious( intval( $this->registry->getURLBit(4) ) );
  36						break;
  37					case'current':
  38						$this->listCurrent( intval( $this->registry->getURLBit(4) ) );
  39						break;
  40					case'attendees':
  41						$this->listAttendees( intval( $this->registry->getURLBit(3) ), intval( $this->registry->getURLBit(5) ) );
  42						break;
  43					case 'cancel-attendee':
  44						$this->cancelAttendee( intval( $this->registry->getURLBit(3) ),  intval( $this->registry->getURLBit(4) ));
  45						break;
  46					case 'toggle-active':
  47						$this->toggleActiveSession( intval( $this->registry->getURLBit(3) ),  intval( $this->registry->getURLBit(4) ), intval( $this->registry->getURLBit(5) ));
  48						break;
  49					case 'toggle-pay-online':
  50						$this->togglePayOnline( intval( $this->registry->getURLBit(3) ),  intval( $this->registry->getURLBit(4) ), intval( $this->registry->getURLBit(5) ));
  51						break;	
  52					case 'toggle-bookable':
  53						$this->toggleBookable( intval( $this->registry->getURLBit(3) ),  intval( $this->registry->getURLBit(4) ), intval( $this->registry->getURLBit(5) ));
  54						break;
  55					case 'toggle-non-members':
  56						$this->toggleNonMembers( intval( $this->registry->getURLBit(3) ),  intval( $this->registry->getURLBit(4) ), intval( $this->registry->getURLBit(5) ));
  57						break;
  58					case 'toggle-featured':
  59						$this->toggleFeatured( intval( $this->registry->getURLBit(3) ),  intval( $this->registry->getURLBit(4) ));
  60						break;
  61					case 'toggle-active-session':
  62						$this->toggleActiveSessionFixture( intval( $this->registry->getURLBit(3) ),  intval( $this->registry->getURLBit(4) ));
  63						break;
  64					case 'list':
  65						$this->listSessions ( intval( $this->registry->getURLBit(3) ), intval( $this->registry->getURLBit(5) ) );
  66						break;
  67					case 'details-attendees':
  68						$this->detailsAttendees( intval( $this->registry->getURLBit(3) ) );
  69						break;
  70					default:
  71						$this->listCurrent( intval( $this->registry->getURLBit(4) ) );
  72						break;
  73				}
  74			}
  75			else
  76			{
  77				$this->listCurrent( intval( $this->registry->getURLBit(4) ) );
  78			}
  79		
  80		}
  81		else
  82		{
  83			$this->registry->getObject('authenticate')->redirectToAuthorised('primary');
  84		}
  85	}
  86	
  87	private function detailsAttendees( $sid )
  88	{
  89		$this->registry->getObject('template')->getPage()->removeTag( 'admin_menu_primary' );
  90		$this->registry->getObject('template')->getPage()->removeTag( 'admin_menu_sub' );
  91		/*
  92		$sql =  "SELECT s.cost_structure, s.cost as standard_cost, s.comment as extra_information, s.ID as session_ID, vv.name as venue, ev.name as event, ev.content as event_content, DATE_FORMAT(s.date, '%b') as date_month, DATE_FORMAT(s.date, '%d') as date_day, s.ID as session_ID, DATE_FORMAT(s.date, '%D %M %Y') as date, " .
  93				"DATE_FORMAT(start_time, '%H:%i') as start_time, DATE_FORMAT(end_time, '%H:%i') as end_time, " .
  94				"((s.places)-((SELECT COUNT(*) FROM event_session_attendees WHERE session_ID=s.ID AND cancelled = 0) + (SELECT COUNT(*) FROM eventbasket_attendees a, eventbasket b WHERE b.ID = a.basket_id AND b.event_session = s.ID ))) as spaces, " .
  95				"IF( ((s.places)-((SELECT COUNT(*) FROM event_session_attendees WHERE session_ID=s.ID AND cancelled = 0) + (SELECT COUNT(*) FROM eventbasket_attendees a, eventbasket b WHERE b.ID = a.basket_id AND b.event_session = s.ID ))) > 0, '', '<!--' ) as fullleft, IF( ((s.places)-((SELECT COUNT(*) FROM event_session_attendees WHERE session_ID=s.ID AND cancelled = 0) + (SELECT COUNT(*) FROM eventbasket_attendees a, eventbasket b WHERE b.ID = a.basket_id AND b.event_session = s.ID ))) > 0, '', '-->' ) as fullright " .
  96				"FROM event_sessions s, content_versions ev, content ec, content_versions vv, content vc " .
  97				"WHERE s.venue_id = vc.ID AND vc.current_revision = vv.ID AND s.event_id = ec.ID AND ec.current_revision = ev.ID AND s.active = 1 AND s.deleted = 0 AND s.date >= CURDATE() AND s.ID = {$sid} LIMIT 1";
  98		*/
  99		
 100		
 101		$sql =  "SELECT vv.name as venue, ev.name as event, DATE_FORMAT(s.date, '%D %M %Y') as date, " .
 102				"DATE_FORMAT(start_time, '%H:%i') as start_time, DATE_FORMAT(end_time, '%H:%i') as end_time " .
 103				"FROM event_sessions s, content_versions ev, content ec, content_versions vv, content vc " .
 104				"WHERE s.venue_id = vc.ID AND vc.current_revision = vv.ID AND s.event_id = ec.ID AND ec.current_revision = ev.ID AND s.ID = {$sid} LIMIT 1";
 105		$this->registry->getObject('db')->executeQuery( $sql );
 106		$data = $this->registry->getObject('db')->getRows();
 107		$this->registry->getObject('template')->dataToTags( $data, '' );
 108		
 109		
 110		$sql = "SELECT *, ID as aid FROM event_session_attendees WHERE session_id = {$sid} AND cancelled = 0 ORDER BY name ASC";
 111		$this->registry->getObject('db')->executeQuery( $sql );
 112		if( $this->registry->getObject('db')->numRows() > 0 )
 113		{
 114			$attendees = array();
 115			$event_sessions = array();
 116			$session_ids = '';
 117			while( $row = $this->registry->getObject('db')->getRows() )
 118			{	
 119				if( !in_array( $row['session_id'], ( $event_sessions ) ) )
 120				{
 121					$session_ids .= $row['session_id'] . ',';
 122					$event_sessions[] = $row['session_id'];
 123				}
 124							
 125				if( in_array( $row['aid'], array_keys( $attendees ) ) )
 126				{
 127					$attendees[ $row['aid'] ][] = $row;	
 128				}
 129				else
 130				{
 131					$attendees[ $row['aid'] ] = array();
 132					$attendees[ $row['aid'] ][] = $row;	
 133				}
 134			}
 135			$session_ids  = substr( $session_ids, 0, -1 );
 136		
 137			$cache = $this->registry->getObject('db')->cacheQuery( $sql );
 138			$this->registry->getObject('template')->getPage()->addTag( 'attendees', array( 'SQL', $cache ) );
 139				
 140			//get the time slots and workshops for this session
 141			$sql = "SELECT s.ID as slot_id, DATE_FORMAT(s.time, '%H:%i') as time, s.heading, s.description, s.session_id FROM event_sessions_slots s WHERE ( SELECT COUNT(*) FROM event_sessions_slots_workshops w WHERE s.ID = w.slot_id  ) > 0 AND s.session_id IN ( $session_ids ) ORDER BY s.ID ASC";
 142			$this->registry->getObject('db')->executeQuery( $sql );
 143			if( $this->registry->getObject('db')->numRows() > 0 )
 144			{
 145				$slots = array();
 146				while( $data = $this->registry->getObject('db')->getRows() )
 147				{
 148					if( in_array( $data['session_id'], array_keys( $slots ) ) )
 149					{
 150						$slots[ $data['session_id'] ][] = $data;	
 151					}
 152					else
 153					{
 154						$slots[ $data['session_id'] ] = array();
 155						$slots[ $data['session_id'] ][] = $data;	
 156					}
 157				}
 158				
 159				foreach( $attendees as $attendee => $details )
 160				{
 161					foreach( $details as $record )
 162					{
 163						if( in_array( $record ['session_id'], array_keys( $slots ) ) )
 164						{
 165							$this->registry->getObject('template')->addTemplateBit('workshops-' . $attendee, 'events/sessions/workshops.tpl.php', array('session_id' => $record['session_id'], 'aid' => $attendee ) );
 166						}
 167						else
 168						{
 169							$this->registry->getObject('template')->getPage()->addTag( 'workshops-' . $attendee, '' );
 170						}
 171					}
 172				}
 173				
 174				foreach( $attendees as $attendee => $details )
 175				{
 176					foreach( $slots as $session => $details )
 177					{
 178						$cache = $this->registry->getObject('db')->cacheData( $details );
 179						$this->registry->getObject('template')->getPage()->addPPTag( 'slots-' . $session . '-' . $attendee, array( 'DATA', $cache ) );	
 180					}
 181				}	
 182																				
 183				//workshops
 184				$workshops = array();
 185				$sql = "SELECT ID as wid, slot_id, name as workshop FROM event_sessions_slots_workshops WHERE session_id IN ( $session_ids )";
 186				$this->registry->getObject('db')->executeQuery( $sql );
 187				while( $row = $this->registry->getObject('db')->getRows() )
 188				{
 189					if( in_array( $row['slot_id'], array_keys( $workshops ) ) )
 190					{
 191						$workshops[ $row['slot_id'] ][] = $row;	
 192					}
 193					else
 194					{
 195						$workshops[ $row['slot_id'] ] = array();
 196						$workshops[ $row['slot_id'] ][] = $row;	
 197					}
 198				}
 199				foreach( $attendees as $attendee => $data )
 200				{
 201					foreach( $workshops as $record => $details )
 202					{
 203						$cache = $this->registry->getObject('db')->cacheData( $details );
 204						$this->registry->getObject('template')->getPage()->addPPTag( 'workshops-' . $record . '-' . $attendee , array( 'DATA', $cache ) );
 205					}
 206				}	
 207								
 208				//get attendee preferences
 209				$sql = "SELECT w.* FROM event_session_attendees_workshops w, event_session_attendees a WHERE w.attendee_id = a.ID AND a.session_id = {$sid}";
 210				$this->registry->getObject('db')->executeQuery( $sql );
 211				while( $row = $this->registry->getObject('db')->getRows() )
 212				{
 213					$this->registry->getObject('template')->getPage()->addPPTag( $row['attendee_id'] . $row['workshop_id'], $row['preference'] );		
 214				}
 215			
 216			}
 217			else
 218			{
 219				foreach( $attendees as $attendee => $data )
 220				{
 221					$this->registry->getObject('template')->getPage()->addTag( 'workshops-' . $attendee, '' );
 222				}
 223			}
 224		}
 225		$this->registry->getObject('template')->buildFromTemplates( 'events/sessions/details-attendees.tpl.php' );
 226	}
 227		
 228	private function toggleFeatured( $id, $eid )
 229	{
 230		$sql = "UPDATE event_sessions SET featured=1-featured WHERE ID={$id}";
 231		$this->registry->getObject('db')->executeQuery( $sql );
 232		header('Location: ' . $this->registry->buildURL(array('events', 'sessions', 'list', $eid), '', true ) );
 233		exit();
 234	}
 235				
 236	private function listSessions( $eid, $offset=0 )
 237	{
 238		$sql = "SELECT ver.name as event FROM content_versions ver, content con WHERE con.current_revision = ver.ID AND con.ID = {$eid}";
 239		$this->registry->getObject('db')->executeQuery($sql);
 240		$data = $this->registry->getObject('db')->getRows();
 241		$this->registry->getObject('template')->getPage()->addTag( 'event', $data['event'] );
 242		$this->registry->getObject('template')->getPage()->addTag( 'eid', $eid );
 243		
 244		$sql = "SELECT v.name, s.featured, s.venue_ID, s.ID, DATE_FORMAT(s.date, '%D %b %Y') as date, s.cost, s.places, s.active, s.bookable, s.non_members, s.pay_online, (SELECT COUNT(*) FROM event_session_attendees a WHERE a.cancelled=0 AND a.session_ID=s.ID) as booked FROM event_sessions s, content_versions v, content c WHERE v.ID=c.current_revision AND s.deleted=0 AND s.event_ID={$eid} AND c.ID=s.venue_ID AND s.date >= CURDATE() ORDER BY s.date ASC";
 245				
 246		$pagination = $this->registry->pagination( $sql, 10, $offset, 'cache' );
 247		$this->registry->getObject('template')->getPage()->addTag('sessions', array( 'SQL', $pagination['cache'] ) );
 248		$this->registry->getObject('template')->getPage()->addTag('page_number', $pagination['cur_page'] ); 
 249		$this->registry->getObject('template')->getPage()->addTag('num_pages', $pagination['num_pages'] ); 
 250				
 251		// pagination links
 252		if( $pagination['is_first'] == true )
 253		{
 254			$this->registry->getObject('template')->getPage()->addTag('first', '');	
 255			$this->registry->getObject('template')->getPage()->addTag('previous', '');			
 256		}
 257		else
 258		{
 259			$this->registry->getObject('template')->getPage()->addTag('first', "<li><a href='events/sessions/list/$eid/page/'>First Page</a></li>");	
 260			$this->registry->getObject('template')->getPage()->addTag('previous', "<li><a href='events/sessions/list/$eid/page/". ( $pagination['cur_page'] - 2 ) . "'>Previous Page</a></li>");	
 261		}
 262		if( $pagination['is_last'] == true )
 263		{
 264			$this->registry->getObject('template')->getPage()->addTag('next', '');	
 265			$this->registry->getObject('template')->getPage()->addTag('last', '');	
 266		}
 267		else
 268		{
 269			$this->registry->getObject('template')->getPage()->addTag('last', "<li><a href='events/sessions/list/$eid/page/". ( $pagination['num_pages'] - 1 ) ."'>Last Page</a></li>");	
 270			$this->registry->getObject('template')->getPage()->addTag('next', "<li><a href='events/sessions/list/$eid/page/". ( $pagination['cur_page'] ) . "'>Next Page</a></li>");	
 271				
 272		}
 273					
 274		$this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'events/sessions/list.tpl.php', 'footer.tpl.php');
 275		
 276	}
 277		
 278	private function listPrevious( $offset=0 )
 279	{
 280		$sql = "SELECT v.name, (SELECT ver.name FROM content_versions ver, content con WHERE s.event_ID = con.ID AND con.current_revision = ver.ID ) as event, s.venue_ID, s.event_ID, s.ID, DATE_FORMAT(s.date, '%D %b %Y') as date, s.cost, s.places, s.non_members, s.active, (SELECT COUNT(*) FROM event_session_attendees a WHERE a.cancelled=0 AND a.session_ID=s.ID) as booked FROM event_sessions s, content_versions v, content c WHERE v.ID=c.current_revision AND s.deleted=0 AND c.ID=s.venue_ID AND s.date < CURDATE() ORDER BY s.date ASC";
 281		$pagination = $this->registry->pagination( $sql, 10, $offset, 'cache' );
 282		
 283		$this->registry->getObject('template')->getPage()->addTag('sessions', array( 'SQL', $pagination['cache'] ) );
 284		$this->registry->getObject('template')->getPage()->addTag('page_number', $pagination['cur_page'] ); 
 285		$this->registry->getObject('template')->getPage()->addTag('num_pages', $pagination['num_pages'] ); 
 286				
 287		// pagination links
 288		if( $pagination['is_first'] == true )
 289		{
 290			$this->registry->getObject('template')->getPage()->addTag('first', '');	
 291			$this->registry->getObject('template')->getPage()->addTag('previous', '');			
 292		}
 293		else
 294		{
 295			$this->registry->getObject('template')->getPage()->addTag('first', "<li><a href='events/sessions/previous/page/'>First Page</a></li>");	
 296			$this->registry->getObject('template')->getPage()->addTag('previous', "<li><a href='events/sessions/previous/page/". ( $pagination['cur_page'] - 2 ) . "'>Previous Page</a></li>");	
 297		}
 298		if( $pagination['is_last'] == true )
 299		{
 300			$this->registry->getObject('template')->getPage()->addTag('next', '');	
 301			$this->registry->getObject('template')->getPage()->addTag('last', '');	
 302		}
 303		else
 304		{
 305			$this->registry->getObject('template')->getPage()->addTag('last', "<li><a href='events/sessions/previous/page/". ( $pagination['num_pages'] - 1 ) ."'>Last Page</a></li>");	
 306			$this->registry->getObject('template')->getPage()->addTag('next', "<li><a href='events/sessions/previous/page/". ( $pagination['cur_page'] ) . "'>Next Page</a></li>");	
 307				
 308		}
 309			    
 310		$this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'events/sessions/previous.tpl.php', 'footer.tpl.php');
 311		
 312	}
 313	
 314	private function listCurrent( $offset=0 )
 315	{
 316		$sql = "SELECT v.name, (SELECT ver.name FROM content_versions ver, content con WHERE s.event_ID = con.ID AND con.current_revision = ver.ID ) as event, s.venue_ID, s.event_ID, s.ID, DATE_FORMAT(s.date, '%D %b %Y') as date, s.cost, s.places, s.active, s.non_members, s.bookable, s.pay_online, (SELECT COUNT(*) FROM event_session_attendees a WHERE a.cancelled=0 AND a.session_ID=s.ID) as booked FROM event_sessions s, content_versions v, content c WHERE v.ID=c.current_revision AND s.deleted=0 AND c.ID=s.venue_ID AND s.date >= CURDATE() ORDER BY s.date ASC";
 317			
 318		$pagination = $this->registry->pagination( $sql, 10, $offset, 'cache' );
 319		
 320		$this->registry->getObject('template')->getPage()->addTag('sessions', array( 'SQL', $pagination['cache'] ) );
 321		$this->registry->getObject('template')->getPage()->addTag('page_number', $pagination['cur_page'] ); 
 322		$this->registry->getObject('template')->getPage()->addTag('num_pages', $pagination['num_pages'] ); 
 323				
 324		// pagination links
 325		if( $pagination['is_first'] == true )
 326		{
 327			$this->registry->getObject('template')->getPage()->addTag('first', '');	
 328			$this->registry->getObject('template')->getPage()->addTag('previous', '');			
 329		}
 330		else
 331		{
 332			$this->registry->getObject('template')->getPage()->addTag('first', "<li><a href='events/sessions/current/page/'>First Page</a></li>");	
 333			$this->registry->getObject('template')->getPage()->addTag('previous', "<li><a href='events/sessions/current/page/". ( $pagination['cur_page'] - 2 ) . "'>Previous Page</a></li>");	
 334		}
 335		if( $pagination['is_last'] == true )
 336		{
 337			$this->registry->getObject('template')->getPage()->addTag('next', '');	
 338			$this->registry->getObject('template')->getPage()->addTag('last', '');	
 339		}
 340		else
 341		{
 342			$this->registry->getObject('template')->getPage()->addTag('last', "<li><a href='events/sessions/current/page/". ( $pagination['num_pages'] - 1 ) ."'>Last Page</a></li>");	
 343			$this->registry->getObject('template')->getPage()->addTag('next', "<li><a href='events/sessions/current/page/". ( $pagination['cur_page'] ) . "'>Next Page</a></li>");	
 344				
 345		}
 346			    
 347		$this->registry->getObject('template')->buildFromTemplates('header.tpl.php', 'events/sessions/current.tpl.php', 'footer.tpl.php');
 348		
 349	}
 350			
 351	private function addSession( $eid )
 352	{
 353		if( isset( $_POST['createsession'] ) )
 354		{
 355			$insert = array();
 356			$insert['event_id'] = $this->registry->getObject('db')->sanitizeData( $eid );
 357			$insert['venue_id'] = $this->registry->getObject('db')->sanitizeData( $_POST['venue']);
 358			
 359			if( isset( $_POST['date'] ) && $_POST['date'] > 0 )
 360			{
 361				$date = explode( '/', $_POST['date']);
 362				$dt = $date[2] . '-' . $date[1] .'-'. $date[0];
 363				$insert['date'] = $this->registry->getObject('db')->sanitizeData( $dt );		
 364			}
 365			
 366			if( isset( $_POST['start_time'] ) && $_POST['start_time'] > 0 )
 367			{
 368			
 369			$time = explode( ':', $_POST['start_time'] );
 370			$t = $time[0].':'.$time[1];
 371			$insert['start_time'] = $this->registry->getObject('db')->sanitizeData( $t );
 372			
 373			}
 374			
 375			if( isset( $_POST['end_time'] ) && $_POST['end_time'] > 0 )
 376			{
 377				$time = explode( ':', $_POST['end_time'] );
 378				$t = $time[0].':'.$time[1];
 379				$insert['end_time'] = $this->registry->getObject('db')->sanitizeData( $t );	
 380			}
 381						
 382			$insert['places'] = $this->registry->getObject('db')->sanitizeData( $_POST['places']);
 383			$insert['cost_structure'] = $this->registry->getObject('db')->sanitizeData( $_POST['cost_structure']);
 384			$insert['cost'] = $this->registry->getObject('db')->sanitizeData( $_POST['standard_cost']);
 385			$insert['discount'] = $this->registry->getObject('db')->sanitizeData( $_POST['discount']);
 386			$insert['comment'] = $this->registry->getObject('db')->sanitizeData( $_POST['comment']);
 387			$insert['active'] = 1;
 388			$insert['non_members'] = 1;
 389			$insert['pay_online'] = 1;
 390			$this->registry->getObject('db')->insertRecords( 'event_sessions', $insert);
 391			$session_id = $this->registry->getObject('db')->lastInsertID();
 392								
 393			//costs
 394			if( isset( $_POST['cost'] ) && is_array( $_POST['cost'] ) && count( $_POST['cost'] ) > 0 )
 395			{
 396				$sql = "INSERT INTO event_sessions_costs ( `session_ID`, `cost`, `type` ) VALUES ";
 397				foreach( $_POST['cost'] as $cost_type => $cost )
 398				{
 399					if( $cost == '' )
 400					{
 401						$cost = 0;
 402					}
 403					$cost = $this->registry->getObject('db')->sanitizeData( $cost );
 404					$cost_type = $this->registry->getObject('db')->sanitizeData( $cost_type );
 405					$sql .= " ( {$session_id}, {$cost}, '{$cost_type}' ),";
 406				}
 407				$sql = substr_replace($sql ,"",-1);
 408				$this->registry->getObject('db')->executeQuery( $sql );	
 409			}
 410							
 411			//time slots and workshops
 412			if( isset( $_POST['times'] ) && is_array( $_POST['times'] ) && count( $_POST['times'] ) > 0 )
 413			{				
 414				foreach( $_POST['times'] as $value )
 415				{
 416					$insert = array();
 417					$insert['session_id'] = $this->registry->getObject('db')->sanitizeData( $session_id );
 418					$insert['time'] = $this->registry->getObject('db')->sanitizeData( $value['time'] );
 419					$insert['heading'] = $this->registry->getObject('db')->sanitizeData( $value['heading'] );
 420					$insert['description'] = $this->registry->getObject('db')->sanitizeData( $value['description'] );
 421					$this->registry->getObject('db')->insertRecords( 'event_sessions_slots', $insert );
 422					$slot_id = $this->registry->getObject('db')->lastInsertID();		
 423							
 424					if( isset( $value['workshops'] ) && is_array( $value['workshops'] ) && count( $value['workshops'] ) > 0 )
 425					{						
 426						$sql = "INSERT INTO event_sessions_slots_workshops ( `session_id`, `slot_id`, `name` ) VALUES ";
 427						foreach( $value['workshops'] as $data )
 428						{
 429							$slot_id = $this->registry->getObject('db')->sanitizeData( $slot_id );
 430							$name = $this->registry->getObject('db')->sanitizeData( $data );
 431							$sql .= " ( {$session_id}, {$slot_id}, '{$name}' ),";
 432						}
 433						$sql = substr_replace($sql ,"",-1);
 434						$this->registry->getObject('db')->executeQuery( $sql );	
 435					}
 436					
 437				}
 438				
 439			}	
 440					
 441			$this->registry->getObject('template')->getPage()->removeTag( 'venues' );
 442			$this->registry->redirectUser(array('events', 'sessions', 'list', $eid), 'Session created', 'The session has now been saved.',true);
 443		}
 444		else
 445		{
 446			$sql = "SELECT c.ID, v.name FROM content_versions v, content c, content_types ct WHERE v.ID=c.current_revision AND c.type=ct.ID AND ct.reference='venue' AND c.deleted=0 AND c.active=1";
 447			$cache = $this->registry->getObject('db')->cacheQuery( $sql );
 448			$this->registry->getObject('template')->getPage()->addTag( 'venues', array( 'SQL', $cache ) );
 449			$this->registry->getObject('template')->getPage()->addTag( 'eid', $eid );							
 450			$this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'events/sessions/add.tpl.php', 'footer.tpl.php');				
 451		}
 452						
 453	}
 454	
 455	private function createSession ()
 456	{
 457		if( isset( $_POST['createsession'] ) )
 458		{
 459			$insert = array();
 460			$insert['event_id'] = $this->registry->getObject('db')->sanitizeData( $_POST['event']);
 461			$insert['venue_id'] = $this->registry->getObject('db')->sanitizeData( $_POST['venue']);
 462			
 463			if( isset( $_POST['date'] ) && $_POST['date'] > 0 )
 464			{
 465				$date = explode( '/', $_POST['date']);
 466				$dt = $date[2] . '-' . $date[1] .'-'. $date[0];
 467				$insert['date'] = $this->registry->getObject('db')->sanitizeData( $dt );		
 468			}
 469			
 470			if( isset( $_POST['start_time'] ) && $_POST['start_time'] > 0 )
 471			{
 472			
 473			$time = explode( ':', $_POST['start_time'] );
 474			$t = $time[0].':'.$time[1];
 475			$insert['start_time'] = $this->registry->getObject('db')->sanitizeData( $t );
 476			
 477			}
 478			
 479			if( isset( $_POST['end_time'] ) && $_POST['end_time'] > 0 )
 480			{
 481				$time = explode( ':', $_POST['end_time'] );
 482				$t = $time[0].':'.$time[1];
 483				$insert['end_time'] = $this->registry->getObject('db')->sanitizeData( $t );	
 484			}
 485						
 486			$insert['places'] = $this->registry->getObject('db')->sanitizeData( $_POST['places']);
 487			$insert['cost_structure'] = $this->registry->getObject('db')->sanitizeData( $_POST['cost_structure']);
 488			$insert['cost'] = $this->registry->getObject('db')->sanitizeData( $_POST['standard_cost']);
 489			$insert['discount'] = $this->registry->getObject('db')->sanitizeData( $_POST['discount']);
 490			$insert['comment'] = $this->registry->getObject('db')->sanitizeData( $_POST['comment']);
 491			$insert['active'] = 1;
 492			$insert['non_members'] = 1;
 493			$insert['pay_online'] = 1;
 494			$this->registry->getObject('db')->insertRecords( 'event_sessions', $insert);
 495			$session_id = $this->registry->getObject('db')->lastInsertID();
 496								
 497			//costs
 498			if( isset( $_POST['cost'] ) && is_array( $_POST['cost'] ) && count( $_POST['cost'] ) > 0 )
 499			{
 500				$sql = "INSERT INTO event_sessions_costs ( `session_ID`, `cost`, `type` ) VALUES ";
 501				foreach( $_POST['cost'] as $cost_type => $cost )
 502				{
 503					if( $cost == '' )
 504					{
 505						$cost = 0;
 506					}
 507					$cost = $this->registry->getObject('db')->sanitizeData( $cost );
 508					$cost_type = $this->registry->getObject('db')->sanitizeData( $cost_type );
 509					$sql .= " ( {$session_id}, {$cost}, '{$cost_type}' ),";
 510				}
 511				$sql = substr_replace($sql ,"",-1);
 512				$this->registry->getObject('db')->executeQuery( $sql );	
 513			}
 514							
 515			//time slots and workshops
 516			if( isset( $_POST['times'] ) && is_array( $_POST['times'] ) && count( $_POST['times'] ) > 0 )
 517			{				
 518				foreach( $_POST['times'] as $value )
 519				{
 520					$insert = array();
 521					$insert['session_id'] = $this->registry->getObject('db')->sanitizeData( $session_id );
 522					$insert['time'] = $this->registry->getObject('db')->sanitizeData( $value['time'] );
 523					$insert['heading'] = $this->registry->getObject('db')->sanitizeData( $value['heading'] );
 524					$insert['description'] = $this->registry->getObject('db')->sanitizeData( $value['description'] );
 525					$this->registry->getObject('db')->insertRecords( 'event_sessions_slots', $insert );
 526					$slot_id = $this->registry->getObject('db')->lastInsertID();		
 527							
 528					if( isset( $value['workshops'] ) && is_array( $value['workshops'] ) && count( $value['workshops'] ) > 0 )
 529					{						
 530						$sql = "INSERT INTO event_sessions_slots_workshops ( `session_id`, `slot_id`, `name` ) VALUES ";
 531						foreach( $value['workshops'] as $data )
 532						{
 533							$slot_id = $this->registry->getObject('db')->sanitizeData( $slot_id );
 534							$name = $this->registry->getObject('db')->sanitizeData( $data );
 535							$sql .= " ( {$session_id}, {$slot_id}, '{$name}' ),";
 536						}
 537						$sql = substr_replace($sql ,"",-1);
 538						$this->registry->getObject('db')->executeQuery( $sql );	
 539					}
 540					
 541				}
 542				
 543			}	
 544					
 545			$this->registry->getObject('template')->getPage()->removeTag( 'venues' );
 546			$this->registry->getObject('template')->getPage()->removeTag( 'events' );
 547									
 548			$this->registry->redirectUser(array('events', 'sessions', '0', '0'), 'Session created', 'The session has now been saved.',true);
 549		}
 550		else
 551		{
 552			$sql = "SELECT c.ID, v.name FROM content_versions v, content c, content_types ct WHERE v.ID=c.current_revision AND c.type=ct.ID AND ct.reference='venue' AND c.deleted=0 AND c.active=1";
 553			$cache = $this->registry->getObject('db')->cacheQuery( $sql );
 554			$this->registry->getObject('template')->getPage()->addTag( 'venues', array( 'SQL', $cache ) );
 555			
 556			$sql = "SELECT c.ID, v.name FROM content_versions v, content c, content_types ct WHERE v.ID=c.current_revision AND c.type=ct.ID AND ct.reference='event' AND c.deleted=0 AND c.active=1";
 557			$cache = $this->registry->getObject('db')->cacheQuery( $sql );
 558			$this->registry->getObject('template')->getPage()->addTag( 'events', array( 'SQL', $cache ) );
 559				
 560			$this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'events/sessions/create.tpl.php', 'footer.tpl.php');				
 561		}
 562		
 563	}
 564	
 565	
 566	
 567	private function editSessionByEvent( $sid, $eid )
 568	{
 569		//session details
 570		$sql =  "SELECT s.venue_id, s.discount, s.cost_structure, s.cost as standard_cost, s.event_id, DATE_FORMAT(s.date, '%m/%d/%Y') as date, DATE_FORMAT(s.start_time, '%H:%i:%s') as start_time, DATE_FORMAT(s.end_time, '%H:%i:%s') as end_time, s.places, s.comment " .
 571				"FROM event_sessions s " .
 572				"WHERE s.ID={$sid} LIMIT 1";
 573		$this->registry->getObject('db')->executeQuery($sql);
 574		$data = $this->registry->getObject('db')->getRows();
 575		$this->registry->getObject('template')->dataToTags($data, 'session_');
 576		//print_r($data);
 577		if( $data['cost_structure'] == 'standard' )
 578		{
 579			$this->registry->getObject('template')->getPage()->addTag( 'nbd_cost_structure_standard', 'checked="checked"' );
 580		}
 581		else
 582		{
 583			$this->registry->getObject('template')->getPage()->addTag( 'nbd_cost_structure_type', 'checked="checked"' );	
 584		}
 585				
 586		//venues
 587		$sql = "SELECT c.ID, v.name FROM content_versions v, content c, content_types t WHERE v.ID=c.current_revision AND c.type=t.ID AND t.reference='venue' AND c.deleted=0 AND c.active=1";
 588		$cache = $this->registry->getObject('db')->cacheQuery( $sql );
 589		$this->registry->getObject('template')->getPage()->addTag( 'venues', array( 'SQL', $cache ) );									
 590		
 591		//events
 592		$sql = "SELECT c.ID, v.name FROM content_versions v, content c, content_types t WHERE v.ID=c.current_revision AND c.type=t.ID AND t.reference='event' AND c.deleted=0 AND c.active=1";
 593		$cache = $this->registry->getObject('db')->cacheQuery( $sql );
 594		$this->registry->getObject('template')->getPage()->addTag( 'events', array( 'SQL', $cache ) );
 595		
 596		//costs
 597		$sql = "SELECT * FROM event_sessions_costs WHERE session_ID = {$sid}";
 598		$this->registry->getObject('db')->executeQuery($sql);
 599		$costs = array();
 600		while( $data = $this->registry->getObject('db')->getRows() )
 601		{
 602			$this->registry->getObject('template')->getPage()->addTag( 'nbd_costs_' . $data['type'], $data['cost'] );
 603		}
 604								
 605		//time slots
 606		$sql = "SELECT ID as slot_id, time, heading, description FROM event_sessions_slots WHERE session_id = {$sid} ORDER BY ID ASC";
 607		$this->registry->getObject('db')->executeQuery( $sql );
 608		$slots = array();
 609		$slot_id = '';
 610		while( $data = $this->registry->getObject('db')->getRows() )
 611		{
 612			$slots[] = $data;	
 613			$slot_id = $data['slot_id'];
 614		}
 615		
 616		$next_slot_id = $slot_id + 1;
 617		$cache = $this->registry->getObject('db')->cacheData( $slots );
 618		$this->registry->getObject('template')->getPage()->addTag( 'slots', array( 'DATA', $cache ) );	
 619		$this->registry->getObject('template')->getPage()->addTag( 'next_slot_id', $next_slot_id );
 620		
 621		//workshops
 622		$workshops = array();
 623		$workshop_id = '';
 624		$sql = "SELECT ID as wid, slot_id, name FROM event_sessions_slots_workshops WHERE session_id = {$sid} ORDER BY ID ASC";
 625		$this->registry->getObject('db')->executeQuery( $sql );
 626		while( $row = $this->registry->getObject('db')->getRows() )
 627		{
 628			if( in_array( $row['slot_id'], array_keys( $workshops ) ) )
 629			{
 630				$workshops[ $row['slot_id'] ][] = $row;	
 631			}
 632			else
 633			{
 634				$workshops[ $row['slot_id'] ] = array();
 635				$workshops[ $row['slot_id'] ][] = $row;	
 636			}
 637			
 638			$workshop_id = $row['wid'];
 639		}
 640		
 641		$next_workshop_id = $workshop_id + 1;				
 642		$this->registry->getObject('template')->getPage()->addTag( 'next_workshop_id', $next_workshop_id );
 643		
 644		foreach( $workshops as $record => $details )
 645		{
 646			$cache = $this->registry->getObject('db')->cacheData( $details );
 647			$this->registry->getObject('template')->getPage()->addPPTag( 'workshops-' . $record, array( 'DATA', $cache ) );
 648			$this->registry->getObject('template')->addTemplateBit('nbd_workshop-' . $record, 'events/sessions/workshop.tpl.php' );	
 649		}
 650				
 651		if( isset( $_POST['editsession'] ) )
 652		{
 653			$update = array();
 654			$update['venue_id'] = $this->registry->getObject('db')->sanitizeData( $_POST['venue']);
 655			
 656			if( isset( $_POST['date'] ) && $_POST['date'] > 0 )
 657			{
 658				$date = explode( '/', $_POST['date']);
 659				$dt = $date[2] . '-' . $date[1] .'-'. $date[0];
 660				$update['date'] = $this->registry->getObject('db')->sanitizeData( $dt );		
 661			}
 662			
 663			if( isset( $_POST['start_time'] ) && $_POST['start_time'] > 0 )
 664			{
 665			
 666			$time = explode( ':', $_POST['start_time'] );
 667			$t = $time[0].':'.$time[1];
 668			$update['start_time'] = $this->registry->getObject('db')->sanitizeData( $t );
 669			
 670			}
 671			
 672			if( isset( $_POST['end_time'] ) && $_POST['end_time'] > 0 )
 673			{
 674				$time = explode( ':', $_POST['end_time'] );
 675				$t = $time[0].':'.$time[1];
 676				$update['end_time'] = $this->registry->getObject('db')->sanitizeData( $t );	
 677			}
 678						
 679			$update['places'] = $this->registry->getObject('db')->sanitizeData( $_POST['places']);
 680			$update['cost_structure'] = $this->registry->getObject('db')->sanitizeData( $_POST['cost_structure']);
 681			$update['discount'] = $this->registry->getObject('db')->sanitizeData( $_POST['discount']);
 682			$update['cost'] = $this->registry->getObject('db')->sanitizeData( $_POST['standard_cost']);
 683			$update['comment'] = $this->registry->getObject('db')->sanitizeData( $_POST['comment']);
 684			$this->registry->getObject('db')->updateRecords( 'event_sessions', $update, 'ID=' . $sid );
 685						
 686			//costs
 687			$sql = "DELETE FROM event_sessions_costs WHERE session_ID = {$sid}";
 688			$this->registry->getObject('db')->executeQuery( $sql );
 689			if( isset( $_POST['cost'] ) && is_array( $_POST['cost'] ) && count( $_POST['cost'] ) > 0 )
 690			{
 691				$sql = "INSERT INTO event_sessions_costs ( `session_ID`, `cost`, `type` ) VALUES ";
 692				foreach( $_POST['cost'] as $cost_type => $cost )
 693				{
 694					if( $cost == '' )
 695					{
 696						$cost = 0;
 697					}
 698					$cost = $this->registry->getObject('db')->sanitizeData( $cost );
 699					$cost_type = $this->registry->getObject('db')->sanitizeData( $cost_type );
 700					$sql .= " ( {$sid}, {$cost}, '{$cost_type}' ),";
 701				}
 702				$sql = substr_replace($sql ,"",-1);
 703				$this->registry->getObject('db')->executeQuery( $sql );	
 704			}
 705				
 706			//time slots and workshops
 707			if( isset( $_POST['times'] ) && is_array( $_POST['times'] ) && count( $_POST['times'] ) > 0 )
 708			{				
 709				//delete slots that have been removed
 710				foreach( $slots as $slot )
 711				{
 712					if( !in_array( $slot['slot_id'], array_keys( $_POST['times'] ) ) )
 713					{
 714						$sql = "DELETE FROM event_sessions_slots WHERE ID = " . $this->registry->getObject('db')->sanitizeData( $slot['slot_id'] );
 715						$this->registry->getObject('db')->executeQuery( $sql );
 716						$sql = "DELETE FROM event_sessions_slots_workshops WHERE slot_id = " . $this->registry->getObject('db')->sanitizeData( $slot['slot_id'] );
 717						$this->registry->getObject('db')->executeQuery( $sql );
 718					}
 719				}
 720				
 721				//update or insert 											
 722				foreach( $_POST['times'] as $value )
 723				{
 724					if( isset( $slots ) && is_array($slots) && count( $slots ) > 0 )
 725					{
 726						$action = '';
 727						foreach( $slots as $slot )
 728						{
 729							if( $value['ID'] == $slot['slot_id'] )
 730							{
 731								$action = 'update';								
 732							}
 733						}
 734												
 735						if( $action == 'update' )
 736						{
 737							$update = array();
 738							$update['time'] = $this->registry->getObject('db')->sanitizeData( $value['time'] );
 739							$update['heading'] = $this->registry->getObject('db')->sanitizeData( $value['heading'] );
 740							$update['description'] = $this->registry->getObject('db')->sanitizeData( $value['description'] );
 741							$this->registry->getObject('db')->updateRecords( 'event_sessions_slots', $update, 'ID=' . $this->registry->getObject('db')->sanitizeData( $value['ID'] ) );	
 742														
 743							if( isset( $value['workshops'] ) && is_array( $value['workshops'] ) && count( $value['workshops'] ) > 0 )
 744							{	
 745								if( isset( $workshops ) && is_array($workshops) && count( $workshops ) > 0 )
 746								{
 747									//delete workshops that have been removed
 748									foreach( $workshops as $workshop => $details )
 749									{
 750									   foreach( $details as $record )
 751									   {									   	
 752									   		if( !in_array( $record['wid'], array_keys( $value['workshops'] ) ) )
 753											{
 754												 if( $value['ID'] == $record['slot_id'] )
 755												 {
 756												 	$sql = "DELETE FROM event_sessions_slots_workshops WHERE ID = " . $this->registry->getObject('db')->sanitizeData( $record['wid'] );
 757													$this->registry->getObject('db')->executeQuery( $sql );
 758												 }
 759											}
 760										}
 761									}
 762														
 763									//update or insert workshops
 764									foreach( $value['workshops'] as $workshop_values )
 765									{
 766										$workshop_action = '';	
 767										foreach( $workshops as $workshop => $details )
 768										{
 769										   foreach( $details as $record )
 770										   {
 771										   		if( $record['wid'] == $workshop_values['wid'] )
 772												{
 773													$workshop_action = 'update';
 774												}
 775											}
 776										}
 777																
 778										if( $workshop_action == 'update' )
 779										{
 780											$update = array();
 781											$update['name'] = $this->registry->getObject('db')->sanitizeData( $workshop_values['name'] );
 782											$this->registry->getObject('db')->updateRecords( 'event_sessions_slots_workshops', $update, 'ID=' . $this->registry->getObject('db')->sanitizeData( $workshop_values['wid'] ) );	
 783													
 784										}
 785										else
 786										{
 787											$insert = array();
 788											$insert['name'] = $this->registry->getObject('db')->sanitizeData( $workshop_values['name'] );
 789											$insert['session_id'] = $this->registry->getObject('db')->sanitizeData( $sid );
 790											$insert['slot_id'] = $this->registry->getObject('db')->sanitizeData( $value['ID'] );
 791											$this->registry->getObject('db')->insertRecords( 'event_sessions_slots_workshops', $insert );
 792										}
 793									}
 794																		
 795								}
 796								else
 797								{
 798									$sql = "INSERT INTO event_sessions_slots_workshops ( `session_id`, `slot_id`, `name` ) VALUES ";
 799									foreach( $value['workshops'] as $data )
 800									{
 801										$slot_id = $this->registry->getObject('db')->sanitizeData( $value['ID'] );
 802										$name = $this->registry->getObject('db')->sanitizeData( $data['name'] );
 803										$sql .= " ( {$sid}, {$slot_id}, '{$name}' ),";
 804									}
 805									$sql = substr_replace($sql ,"",-1);
 806									$this->registry->getObject('db')->executeQuery( $sql );	
 807								}
 808							}
 809							else
 810							{
 811								$sql = "DELETE FROM event_sessions_slots_workshops WHERE slot_id = " . $this->registry->getObject('db')->sanitizeData( $value['ID'] );
 812								$this->registry->getObject('db')->executeQuery( $sql );
 813							}	
 814						
 815						}
 816						else
 817						{
 818							$insert = array();
 819							$insert['session_id'] = $this->registry->getObject('db')->sanitizeData( $sid );
 820							$insert['time'] = $this->registry->getObject('db')->sanitizeData( $value['time'] );
 821							$insert['heading'] = $this->registry->getObject('db')->sanitizeData( $value['heading'] );
 822							$insert['description'] = $this->registry->getObject('db')->sanitizeData( $value['description'] );
 823							$this->registry->getObject('db')->insertRecords( 'event_sessions_slots', $insert );
 824							$slot_id = $this->registry->getObject('db')->lastInsertID();		
 825							
 826							if( isset( $value['workshops'] ) && is_array( $value['workshops'] ) && count( $value['workshops'] ) > 0 )
 827							{						
 828								$sql = "INSERT INTO event_sessions_slots_workshops ( `session_id`, `slot_id`, `name` ) VALUES ";
 829								foreach( $value['workshops'] as $data )
 830								{
 831									$slot_id = $this->registry->getObject('db')->sanitizeData( $value['ID']  );
 832									$name = $this->registry->getObject('db')->sanitizeData( $data['name'] );
 833									$sql .= " ( {$sid}, {$slot_id}, '{$name}' ),";
 834								}
 835								$sql = substr_replace($sql ,"",-1);
 836								$this->registry->getObject('db')->executeQuery( $sql );	
 837							}
 838						}
 839					
 840					}
 841					else
 842					{
 843						$insert = array();
 844						$insert['session_id'] = $this->registry->getObject('db')->sanitizeData( $sid );
 845						$insert['time'] = $this->registry->getObject('db')->sanitizeData( $value['time'] );
 846						$insert['heading'] = $this->registry->getObject('db')->sanitizeData( $value['heading'] );
 847						$insert['description'] = $this->registry->getObject('db')->sanitizeData( $value['description'] );
 848						$this->registry->getObject('db')->insertRecords( 'event_sessions_slots', $insert );
 849						$slot_id = $this->registry->getObject('db')->lastInsertID();
 850						
 851						if( isset( $value['workshops'] ) && is_array( $value['workshops'] ) && count( $value['workshops'] ) > 0 )
 852						{						
 853							$sql = "INSERT INTO event_sessions_slots_workshops ( `session_id`, `slot_id`, `name` ) VALUES ";
 854							foreach( $value['workshops'] as $data )
 855							{
 856								$slot_id = $this->registry->getObject('db')->sanitizeData( $value['ID']  );
 857								$name = $this->registry->getObject('db')->sanitizeData( $data['name'] );
 858								$sql .= " ( {$sid}, {$slot_id}, '{$name}' ),";
 859							}
 860							$sql = substr_replace($sql ,"",-1);
 861							$this->registry->getObject('db')->executeQuery( $sql );	
 862						}		
 863					}
 864				}
 865			}
 866			else
 867			{
 868				$sql = "DELETE FROM event_sessions_slots WHERE session_id = {$sid}";
 869				$this->registry->getObject('db')->executeQuery( $sql );
 870			}	
 871				
 872			$this->registry->getObject('template')->getPage()->removeTag( 'venues' );
 873			
 874			$url = array( 'events', 'sessions', 'list', $eid );
 875			$this->registry->redirectUser( $url, 'Session updated', 'Taking you to the course sessions screen', true );	
 876						
 877		}
 878		else
 879		{
 880			$this->registry->getObject('template')->getPage()->addAdditionalParsingData( 'venues', 'ID', $data['venue_id'], 'opt', "selected='selected'" );
 881			$this->registry->getObject('template')->getPage()->addTag( 'sid', $sid );
 882			$this->registry->getObject('template')->getPage()->addTag( 'eid', $eid );
 883			$this->registry->getObject('template')->buildFromTemplates( 'header.tpl.php', 'events/sessions/editsessionbyevent.tpl.php', 'footer.tpl.php');	
 884		}
 885		
 886	}
 887	
 888	private function editSession( $sid, $type )
 889	{
 890		//session details
 891		$sql =  "SELECT s.venue_id, s.discount, s.cost_structure, s.cost as standard_cost, s.event_id, DATE_FORMAT(s.date, '%m/%d/%Y') as date, DATE_FORMAT(s.start_time, '%H:%i:%s') as start_time, DATE_FORMAT(s.end_time, '%H:%i:%s') as end_time, s.places, s.comment " .
 892				"FROM event_sessions s " .
 893				"WHERE s.ID={$sid} LIMIT 1";
 894				//print_r($sql);
 895		$this->registry->getObject('db')->executeQuery($sql);
 896		$data = $this->registry->getObject('db')->getRows();
 897		$this->registry->getObject('template')->dataToTags($data, 'session_');
 898		//print_r($data);
 899		if( $data['cost_structure'] == 'standard' )
 900		{
 901			$this->registry->getObject('template')->getPage()->addTag( 'nbd_cost_structure_standard', 'checked="checked"' );
 902		}
 903		else
 904		{
 905			$this->registry->getObject('template')->getPage()->addTag( 'nbd_cost_structure_type', 'checked="checked"' );	
 906		}
 907				
 908		//venues
 909		$sql = "SELECT c.ID, v.name FROM content_versions v, content c, content_types t WHERE v.ID=c.current_revision AND c.type=t.ID AND t.reference='venue' AND c.deleted=0 AND c.active=1";
 910		$cache = $this->registry->getObject('db')->cacheQuery( $sql );
 911		$this->registry->getObject('template')->getPage()->addTag( 'venues', array( 'SQL', $cache ) );									
 912		
 913		//events
 914		$sql = "SELECT c.ID, v.name FROM content_versions v, content c, content_types t WHERE v.ID=c.current_revision AND c.type=t.ID AND t.reference='event' AND c.deleted=0 AND c.active=1";
 915		$cache = $this->registry->getObject('db')->cacheQuery( $sql );
 916		$this->registry->getObject('template')->getPage()->addTag( 'events', array( 'SQL', $cache ) );
 917		
 918		//costs
 919		$sql = "SELECT * FROM event_sessions_costs WHERE session_ID = {$sid}";
 920		$this->registry->getObject('db')->executeQuery($sql);
 921		$costs = array();
 922		while( $data = $this->registry->getObject('db')->getRows() )
 923		{
 924			$this->registry->getObject('template')->getPage()->addTag( 'nbd_costs_' . $data['type'], $data['cost'] );
 925		}
 926								
 927		//time slots
 928		$sql = "SELECT ID as slot_id, time, heading, description FROM event_sessions_slots WHERE session_id = {$sid} ORDER BY ID ASC";
 929		$this->registry->getObject('db')->executeQuery( $sql );
 930		$slots = array();
 931		$slot_id = '';
 932		while( $data = $this->registry->getObject('db')->getRows() )
 933		{
 934			$slots[] = $data;	
 935			$slot_id = $data['slot_id'];
 936		}
 937		
 938		$next_slot_id = $slot_id + 1;
 939		$cache = $this->registry->getObject('db')->cacheData( $slots );
 940		$this->registry->getObject('template')->getPage()->addTag( 'slots', array( 'DATA', $cache ) );	
 941		$this->registry->getObject('template')->getPage()->addTag( 'next_slot_id', $next_slot_id );
 942		
 943		//workshops
 944		$workshops = array();
 945		$workshop_id = '';
 946		$sql = "SELECT ID as wid, slot_id, name FROM event_sessions_slots_workshops WHERE session_id = {$sid} ORDER BY ID ASC";
 947		$this->registry->getObject('db')->executeQuery( $sql );
 948		while( $row = $this->registry->getObject('db')->getRows() )
 949		{
 950			if( in_array( $row['slot_id'], array_keys( $workshops ) ) )
 951			{
 952				$workshops[ $row['slot_id'] ][] = $row;	
 953			}
 954			else
 955			{
 956				$workshops[ $row['slot_id'] ] = array();
 957				$workshops[ $row['slot_id'] ][] = $row;	
 958			}
 959			
 960			$workshop_id = $row['wid'];
 961		}
 962		
 963		$next_workshop_id = $workshop_id + 1;				
 964		$this->registry->getObject('template')->getPage()->addTag( 'next_workshop_id', $next_workshop_id );
 965		
 966		foreach( $workshops as $record => $details )
 967		{
 968			$cache = $this->registry->getObject('db')->cacheData( $details );
 969			$this->registry->getObject('template')->getPage()->addPPTag( 'workshops-' . $record, array( 'DATA', $cache ) );
 970			$this->registry->getObject('template')->addTemplateBit('nbd_workshop-' . $record, 'events/sessions/workshop.tpl.php' );	
 971		}
 972				
 973		if( isset( $_POST['editsession'] ) )
 974		{
 975			$update = array();
 976			$update['event_id'] = $this->registry->getObject('db')->sanitizeData( $_POST['event']);
 977			$update['venue_id'] = $this->registry->getObject('db')->sanitizeData( $_POST['venue']);
 978			
 979			if( isset( $_POST['date'] ) && $_POST['date'] > 0 )
 980			{
 981				$date = explode( '/', $_POST['date']);
 982				$dt = $date[2] . '-' . $date[1] .'-'. $date[0];
 983				$update['date'] = $this->registry->getObject('db')->sanitizeData( $dt );		
 984			}
 985			
 986			if( isset( $_POST['start_time'] ) && $_POST['start_time'] > 0 )
 987			{
 988			
 989			$time = explode( ':', $_POST['start_time'] );
 990			$t = $time[0].':'.$time[1];
 991			$update['start_time'] = $this->registry->getObject('db')->sanitizeData( $t );
 992			
 993			}
 994			
 995			if( isset( $_POST['end_time'] ) && $_POST['end_time'] > 0 )
 996			{
 997				$time = explode( ':', $_POST['end_time'] );
 998				$t = $time[0].':'.$time[1];
 999				$update['end_time'] = $this->registry->getObject('db')->sanitizeData( $t );	
1000			}
1001						
1002			$update['places'] = $this->registry->getObject('db')->sanitizeData( $_POST['places']);
1003			$update['cost_structure'] = $this->registry->getObject('db')->sanitizeData( $_POST['cost_structure']);
1004			$update['discount'] = $this->registry->getObject('db')->sanitizeData( $_POST['discount']);
1005			$update['cost'] = $this->registry->getObject('db')->sanitizeData( $_POST['standard_cost']);
1006			$update['comment'] = $this->registry->getObject('db')->sanitizeData( $_POST['comment']);
1007			$this->registry->getObject('db')->updateRecords( 'event_sessions', $update, 'ID=' . $sid );
1008						
1009			//costs
1010			$sql = "DELETE FROM event_sessions_costs WHERE session_ID = {$sid}";
1011			$this->registry->getObject('db')->executeQuery( $sql );
1012			if( isset( $_POST['cost'] ) && is_array( $_POST['cost'] ) && count( $_POST['cost'] ) > 0 )
1013			{
1014				$sql = "INSERT INTO event_sessions_costs ( `session_ID`, `cost`, `type` ) VALUES ";
1015				foreach( $_POST['cost'] as $cost_type => $cost )
1016				{
1017					if( $cost == '' )
1018					{
1019						$cost = 0;
1020					}
1021					$cost = $this->registry->getObject('db')->sanitizeData( $cost );
1022					$cost_type = $this->registry->getObject('db')->sanitizeData( $cost_type );
1023					$sql .= " ( {$sid}, {$cost}, '{$cost_type}' ),";
1024				}
1025				$sql = substr_replace($sql ,"",-1);
1026				$this->registry->getObject('db')->executeQuery( $sql );	
1027			}
1028				
1029			//time slots and workshops
1030			if( isset( $_POST['times'] ) && is_array( $_POST['times'] ) && count( $_POST['times'] ) > 0 )
1031			{				
1032				//delete slots that have been removed
1033				foreach( $slots as $slot )
1034				{
1035					if( !in_array( $slot['slot_id'], array_keys( $_POST['times'] ) ) )
1036					{
1037						$sql = "DELETE FROM event_sessions_slots WHERE ID = " . $this->registry->getObject('db')->sanitizeData( $slot['slot_id'] );
1038						$this->registry->getObject('db')->executeQuery( $sql );
1039						$sql = "DELETE FROM event_sessions_slots_workshops WHERE slot_id = " . $this->registry->getObject('db')->sanitizeData( $slot['slot_id'] );
1040						$this->registry->getObject('db')->executeQuery( $sql );
1041					}
1042				}
1043				
1044				//update or insert 											
1045				foreach( $_POST['times'] as $value )
1046				{
1047					if( isset( $slots ) && is_array($slots) && count( $slots ) > 0 )
1048					{
1049						$action = '';
1050						foreach( $slots as $slot )
1051						{
1052							if( $value['ID'] == $slot['slot_id'] )
1053							{
1054								$action = 'update';								
1055							}
1056						}
1057												
1058						if( $action == 'update' )
1059						{
1060							$update = array();
1061							$update['time'] = $this->registry->getObject('db')->sanitizeData( $value['time'] );
1062							$update['heading'] = $this->registry->getObject('db')->sanitizeData( $value['heading'] );
1063							$update['description'] = $this->registry->getObject('db')->sanitizeData( $value['description'] );
1064							$this->registry->getObject('db')->updateRecords( 'event_sessions_slots', $update, 'ID=' . $this->registry->getObject('db')->sanitizeData( $value['ID'] ) );	
1065														
1066							if( isset( $value['workshops'] ) && is_array( $value['workshops'] ) && count( $value['workshops'] ) > 0 )
1067							{	
1068								if( isset( $workshops ) && is_array($workshops) && count( $workshops ) > 0 )
1069								{
1070									//delete workshops that have been removed
1071	

Large files files are truncated, but you can click here to view the full file