Location: PHPKode > projects > HelpCORE > helpcore/module_helpcore_secure/incident_reports.php
<?php

/*
 * HelpCORE source file 
 * ====================
 * 
 * CVS:
 * ----
 * $header$
 * 
 * Purpose:
 * --------
 * Generates incident reports
 * 
 * Copyright:
 * ----------
 * Copyright (C) 2002-2003 Dennis Fleurbaaij <hide@address.com>
 * Copyright (C) 2002-2005 IO Software <hide@address.com>
 * 
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, 
 * Cambridge, MA 02139, USA.
 * 
 * Please note that this software is dual licensed. For the commercial use of this 
 * software you will need a Commercial License. Please see http://www.io-software.nl
 * for the terms and conditions.
 * 
 * For more information you can contact IO Software at http://www.io-software.nl
 */ 
 
// Sometimes we want to delay loading
if( ! ( isset($_POST['howdisplaytype']) && ($_POST['howdisplaytype'] == 'csv_file') ) ) {

	if (! include('../coreapm/coreapm.php')) {
	    die('Cannot include CORE APM');
	} 

	$GLOBALS['security']->secure(ADMIN);
	$coreapm->start();
}

// return a file
else {

	// We have our own initializer
	include( '../application_config.php' );
	
	include( '../coreapm/includes/global_defines.php' );
	include( '../coreapm/includes/time.php' );
	include( '../coreapm/includes/syslog.php' );
	$syslog = new class_syslog;

	include( '../coreapm/includes/error.php' );
	include( '../coreapm/database/db.php' );
	$db = new class_database; 
	
	
	include( '../coreapm/includes/security.php' );
	$security = new class_security;
	$security->secure( ADMIN );

	include( '../coreapm/includes/language.php' );
	$language = new class_language;
	$language->set( $language->selected_language );
	if ( ! include( LANGUAGE_DIR . $language->selected_language . '.lng' ) ) {
		die( 'language file could not be loaded !' );
	} 

	// Start a CSV table and destry the current table
	//unset( class_table );
	include( '../coreapm/gui/csv_table.php' );
	class class_table extends class_csv_table{}
	
	
	// Fake the box
	include( '../coreapm/gui/box.php' );
	$GLOBALS['box'] = new class_fake_box;
	
	ob_start();
}

include_once( 'configuration_functions.php' );
include_once( 'report_functions.php' );
include_once( 'incident_reports_custom.php' );
include_once( 'uls_rights_functions.php' );
include_once( 'uls_object_functions.php' );

$where = '';

// Create security elements
$uls_organisation_limiter = $uls_building_limiter = $uls_room_limiter = $uls_department_limiter = $uls_group_limiter = $uls_user_limiter = $uls_incident_limiter = $uls_hardware_limiter = $uls_hardware_limiter_with_and = '';
$users_uls_limiter = $organisations_uls_limiter = $buildings_uls_limiter = $rooms_uls_limiter = $departments_uls_limiter = $groups_uls_limiter = $hardware_uls_limiter = null;

$organisation_limiter_array 	= uls_secure_organisation_show();
$building_limiter_array 		= uls_secure_building_show();
$room_limiter_array 			= uls_secure_room_show();
$department_limiter_array 		= uls_secure_department_show();
$group_limiter_array 			= uls_secure_group_show();

if( $organisation_limiter_array != null ) 	$uls_organisation_limiter	= ' '.$organisation_limiter_array[0] . ' ' . $organisation_limiter_array[1] . ' ' . $organisation_limiter_array[2];
if( $building_limiter_array != null ) 		$uls_building_limiter	= ' '.$building_limiter_array[0] . ' ' . $building_limiter_array[1] . ' ' . $building_limiter_array[2];
if( $room_limiter_array != null ) 	$uls_room_limiter	= ' '.$room_limiter_array[0] . ' ' . $room_limiter_array[1] . ' ' . $room_limiter_array[2];
if( $department_limiter_array != null ) 	$uls_department_limiter	= ' '.$department_limiter_array[0] . ' ' . $department_limiter_array[1] . ' ' . $department_limiter_array[2];
if( $group_limiter_array != null ) 	$uls_group_limiter	= ' '.$group_limiter_array[0] . ' ' . $group_limiter_array[1] . ' ' . $group_limiter_array[2];

if( ! uls_secure_modify( 'users', 'read_ok', null, $users_uls_limiter, $organisations_uls_limiter, $buildings_uls_limiter, $rooms_uls_limiter, $departments_uls_limiter, $groups_uls_limiter, $hardware_uls_limiter ) )
	$uls_user_limiter = ' ( users.id='.$GLOBALS['security']->userid.' OR users.rooms_id ' . $rooms_uls_limiter[1] . ' ' . $rooms_uls_limiter[2] . ' OR users.building_id ' . $buildings_uls_limiter[1] . ' ' . $buildings_uls_limiter[2] . ' OR users.organisation_id ' . $organisations_uls_limiter[1] . ' ' . $organisations_uls_limiter[2] . ' OR users.group_id ' . $groups_uls_limiter[1] . ' ' . $groups_uls_limiter[2] . ' OR users.department_id ' . $departments_uls_limiter[1] . ' ' . $departments_uls_limiter[2] .' ) ';
			
if( ! uls_secure_modify( 'hardware', 'read_ok', null, $users_uls_limiter, $organisations_uls_limiter, $buildings_uls_limiter, $rooms_uls_limiter, $departments_uls_limiter, $groups_uls_limiter, $hardware_uls_limiter ) ) {
	$uls_hardware_limiter = ' ( hardware.rooms_id ' . $rooms_uls_limiter[1] . ' ' . $rooms_uls_limiter[2] . ' OR hardware.building_id ' . $buildings_uls_limiter[1] . ' ' . $buildings_uls_limiter[2] . ' OR hardware.organisation_id ' . $organisations_uls_limiter[1] . ' ' . $organisations_uls_limiter[2] . ' OR hardware.group_id ' . $groups_uls_limiter[1] . ' ' . $groups_uls_limiter[2] . ' OR hardware.department_id ' . $departments_uls_limiter[1] . ' ' . $departments_uls_limiter[2] .' ) ';
	$uls_hardware_limiter_with_and .= ' AND '.$uls_hardware_limiter;
}
if( ! uls_secure_modify( 'incidents', 'read_ok', null, $users_uls_limiter, $organisations_uls_limiter, $buildings_uls_limiter, $rooms_uls_limiter, $departments_uls_limiter, $groups_uls_limiter, $hardware_uls_limiter ) )
	$uls_incident_limiter = ' ( i.rooms_id ' . $rooms_uls_limiter[1] . ' ' . $rooms_uls_limiter[2] . ' OR i.building_id ' . $buildings_uls_limiter[1] . ' ' . $buildings_uls_limiter[2] . ' OR i.organisation_id ' . $organisations_uls_limiter[1] . ' ' . $organisations_uls_limiter[2] . ' OR i.group_id ' . $groups_uls_limiter[1] . ' ' . $groups_uls_limiter[2] . ' OR i.department_id ' . $departments_uls_limiter[1] . ' ' . $departments_uls_limiter[2] .' ) ';

	
$db2 = new class_database;

$tempcheck = 0;

// Default dates
//$from_date = array_to_db_time( array( '1', date( 'm' ), date( 'Y' ), '00', '00', '00' ) );
//$to_date = array_to_db_time( array( '1', date( 'm' ) + 1, date( 'Y' ), '00', '00', '00' ) );




// Create dataset
if( isset( $_POST['process'] ) && ( $_POST['process'] == 'true' ) )
{
	$where = '';
	$software_where = '';
	$hardwareconfig_where = '';
	$hardwarebrand_where = '';
	$hardwaretype_where = '';

	// Timefield too
	if( ! isset( $_POST[ 'timefield' ] ) ) {
		$GLOBALS['box']->add( 'ERROR', 'No timefield selected' );
		$GLOBALS['coreapm']->finish();
		die;
	}
	$timefield_name = $_POST[ 'timefield' ];

	
	// Set better dates
	$from_date = array_to_db_time( array( $_POST['from_dateday'], $_POST['from_datemonth'], $_POST['from_dateyear'], '00', '00', '00' ) );
	$to_date = array_to_db_time( array( $_POST['to_dateday'], $_POST['to_datemonth'], $_POST['to_dateyear'], '00', '00', '00' ) );

	// Decompile date
	$from_date_array 	= db_time_to_array( $from_date );
	$to_date_array 		= db_time_to_array( $to_date );
	$from_day	= $day = $from_date_array[0];
	$to_day		= $to_date_array[0];
	$from_month	= $month = $from_date_array[1];
	$to_month	= $to_date_array[1];
	$from_year	= $year = $from_date_array[2];
	$to_year	= $to_date_array[2];
	$from_week	= $week = date_to_weeknr( $from_date );
	$to_week	= date_to_weeknr( $to_date );
	$epoch_from = $epoch = dmy_array_to_days_from_epoch( $from_date_array );
	$epoch_to   = dmy_array_to_days_from_epoch( $to_date_array );

	// Now add the absolute limits from and to to the query
	$main_time_where = ' WHERE ( ' .$timefield_name. ' >= \''.array_to_db_date( array( $from_day, $from_month, $from_year ) ).'\' '.
					   'AND ' .$timefield_name. ' <= \''.array_to_db_date( array( $to_day, $to_month, $to_year  ) ).'\' ) ';

	
	// Create where options
	$organisation_where = report_create_sql_from_post( $where, 'whereorganisation', 'i.organisation_id', 'organisation.id', $uls_organisation_limiter );
	$building_where 	= report_create_sql_from_post( $where, 'wherebuildings', 'i.building_id', 'buildings.id', $uls_building_limiter );
	$room_where 		= report_create_sql_from_post( $where, 'whererooms', 'i.rooms_id', 'rooms.id', $uls_room_limiter );
	$department_where 	= report_create_sql_from_post( $where, 'wheredepartments', 'i.department_id', 'departments.id', $uls_department_limiter );
	
	$status_where 		= report_create_sql_from_post( $where, 'wherestatus', 'i.status_id', 'status.id' );
	$priority_where 	= report_create_sql_from_post( $where, 'wherepriority', 'i.priorities_id', 'priorities.id' );
	$incidentcategory_where = report_create_sql_from_post( $where, 'whereincidentcategory', 'i.incidentcategories_id', 'incidentcategories.id' );
	$reporttype_where 	= report_create_sql_from_post( $where, 'wherereporttype', 'i.report_type_id', 'report_type.id' );
	
	$contact_where 		= report_create_sql_from_post( $where, 'whocontact', 'i.contact_id', 'users.id', $uls_user_limiter );
	$owner_where 		= report_create_sql_from_post( $where, 'whoowner', 'i.owner_id', 'users.id', $uls_user_limiter );
	$group_where 		= report_create_sql_from_post( $where, 'whogroups', 'i.group_id', 'groups.id', $uls_group_limiter );

	$hardware_where 	= report_create_sql_from_post( $where, 'hardwarehardware', 'i.hardware', 'hardware.id', $uls_hardware_limiter );
	
	if( USE_SOFTWARE ) {
		$software_where = report_create_sql_from_post( $where, 'softwaresoftware', 'software', 'software.id' );
	}
	
	if( USE_HARDWARE_CONFIGS ) {
		
		// Also create special where options for hardwareconfig and hardwarebrand
		if( isset( $_POST[ 'hardwareconfig' ] ) ) {
			
			$hardware_configs = array();
			$hardwareconfig_where = '';
						
			while( list( $key, $value ) = each( $_POST[ 'hardwareconfig' ] ) ) {		
				
				$hardwareconfig_boom = explode( ':', $value ); //Decompile values table_name:id
				
				// Store id's per hardwareconfig
				if( ! isset( $hardware_configs[ $hardwareconfig_boom[0] ] ) ) {
					$hardware_configs[ $hardwareconfig_boom[0] ] = '';
				}
				$hardware_configs[ $hardwareconfig_boom[0] ] .=  $hardwareconfig_boom[1].',';
			}
	
			// Loop and create a nice SQL string from this
			$hardware_sql_from = '';
			while( list( $key, $value) = each( $hardware_configs ) ) {
				$hardware_sql_from .= OR_AND.' (  type='.$key.' AND hardware_type_config_id IN ('.substr($value,0,-1).') ) '; // Strip trailing comman and add
				
				// Get matiching hardware id's
				$GLOBALS['db']->query('SELECT id FROM hardware WHERE type='.$key.' '.$uls_hardware_limiter_with_and.' AND hardware_type_config_id IN ('.substr($value,0,-1).') ' );
				while( $row = $GLOBALS['db']->fetch_array() ) {
					$hardwareconfig_where .= $row['id'].',';
				}
			}
			
			// Create one string from it that is in incidents and add it to the global where
			if( $hardwareconfig_where != '' ) {
			
				$hardwareconfig_where = ' hardware IN ('.substr($hardwareconfig_where,0,-1).')';
			
				if( $where != '' ) {
					$where .= OR_AND.' ';
				}	
				
				$where .= $hardwareconfig_where.' ';
			}
			
			unset( $hardware_configs );
			unset( $hardwareconfig_where );
		}
		
		//  hardware brands
		if( isset( $_POST[ 'hardwarebrand' ] ) ) {
	
			// Create an SQL string
			$tempbrands = '';
			while( list( $key, $value ) = each( $_POST[ 'hardwarebrand' ] ) ) {
				$tempbrands .= $value.',';
			}
			$tempbrands = substr($tempbrands,0 ,-1 );
			
			// If there are brands, get the hardware configs
			if( $tempbrands != '' ) {
			
				$hardware_configs = array();
				
				$GLOBALS['db']->query( 'SELECT id, name FROM hardware_types ORDER by name' );
				while ( $row = $GLOBALS['db']->fetch_array() )
				{
					$nice_name = $row['name'];
					$name = secure_name( $nice_name );
					$id = $row['id'];
					
					if ( file_exists( 'config_' . $name . '.php' ) ) {
					
						$db2->query( 'SELECT id FROM config_'.$name.' WHERE brands_id IN (' . $tempbrands.')' );
						while( $row = $db2->fetch_array() ) {
							
							// Store id's per hardwareconfig
							if( ! isset( $hardware_configs[ $id ] ) ) {
								$hardware_configs[ $id ] = '';
							}
							$hardware_configs[ $id ] .=  $row['id'].',';
						}
					}
				}
			
				// Loop and create a nice SQL string from this
				$hardware_sql_from = '';
				while( list( $key, $value) = each( $hardware_configs ) ) {
					$hardware_sql_from .= OR_AND.' ( type='.$key.' AND  hardware_type_config_id IN ('.substr($value,0,-1).') ) '; // Strip trailing comman and add
					
					// Get matiching hardware id's
					$GLOBALS['db']->query('SELECT id FROM hardware WHERE type='.$key.' '.$uls_hardware_limiter_with_and.' AND hardware_type_config_id IN ('.substr($value,0,-1).') ' );
					while( $row = $GLOBALS['db']->fetch_array() ) {
						$hardwarebrand_where .= $row['id'].',';
					}
				}
				
				// Create one string from it that is in incidents and add it to the global where
				if( $hardwarebrand_where != '' ) {
				
					$hardwarebrand_where = ' hardware IN ('.substr($hardwarebrand_where,0,-1).')';
				
					if( $where != '' ) {
						$where .= OR_AND.' ';
					}	
					
					$where .= $hardwarebrand_where.' ';
				}
				
				unset( $hardware_configs );
			} // if( $tempbrands != '' )
		} // if( isset( $_POST[ 'hardwarebrand' ] ) )

		
		// Also create special where options for hardwareconfig and hardwarebrand
		if( isset( $_POST[ 'hardware_type' ] ) ) {
			
			// Create an SQL string
			$hardwaretypes = '';
			while( list( $key, $value ) = each( $_POST[ 'hardware_type' ] ) ) {
				$hardwaretypes .= $value.',';
			}
			$hardwaretypes = substr($hardwaretypes,0 ,-1 );
			
			// 
			if( $hardwaretypes != '' ) {
	
				// Get matiching hardware id's
				$hardwaretypetemp_where = '';
				$GLOBALS['db']->query('SELECT id, name FROM hardware_types WHERE id IN ('.$hardwaretypes.') ' );
				while( $row = $GLOBALS['db']->fetch_array() ) {
					$hardwaretypetemp_where .= ' ( type='.$row['id'].' AND hardware_type_config_id is NOT NULL ) OR ';
				}
				$hardwaretypetemp_where = substr( $hardwaretypetemp_where, 0, -3);
	
				//  
				if( $hardwaretypetemp_where != '' ) {
				
					$GLOBALS['db']->query('SELECT id FROM hardware WHERE '.$hardwaretypetemp_where.' '.$uls_hardware_limiter_with_and );
					while( $row = $GLOBALS['db']->fetch_array() ) {
						$hardwaretype_where .= $row['id'] . ',';
					}
				}
	
				// Create one string from it that is in incidents and add it to the global where
				if( $hardwaretype_where != '' ) {
				
					$hardwaretype_where = ' hardware IN ('.substr($hardwaretype_where,0,-1).')';
				
					if( $where != '' ) {
						$where .= OR_AND.' ';
					}	
					
					$where .= $hardwaretype_where.' ';
				}
			}	
		}
	}

	
	// Add ULS code
	if( ( $where != '' ) && ( stristr( $uls_incident_limiter, '( i.rooms_id OR i.building_id OR i.organisation_id OR i.group_id OR i.department_id )' ) ) ) {
		$where .= ' AND '.$uls_incident_limiter;
	}
	
	// Fix query
	if( $where != '' ) {
		$where = ' AND ('.$where.') ';
	}
	
	
	//
	// Show intervals
	//
	if( isset( $_POST[ 'intervalwhat' ] ) && (count(isset( $_POST[ 'intervalwhat' ] )>0  ) ) ) {
	
		$intervals = 0;	// Number of columns - 1
		$loop_prot = 0;
		$header_row = array( 'HelpCORE<br />Report' );
		$all_datafields = array(); // Array of databafields
		$name_query_limiter = '';
		$set_value = 0;
	
	
		// What is obligatory
		if( ! isset( $_POST[ 'intervalwhat' ] ) ) {
			$GLOBALS['box']->add( 'ERROR', 'No what action selected' );
			$GLOBALS['coreapm']->finish();
			die;
		}
	
		// Displaytype too
		if( ! isset( $_POST[ 'howdisplaytype' ] ) ) {
			$GLOBALS['box']->add( 'ERROR', 'No what action selected' );
			$GLOBALS['coreapm']->finish();
			die;
		}
	
		
		
		
		// Calculate the amount of columns and create the header columns
		// The header columns live from [0][1] to [0][n] and contain just a date, their position is easily calculateable below
		switch( $_POST['interval'] ) {
		
			case 'day':
				while( $epoch <= $epoch_to )
				{
					if( $loop_prot++ > HEADER_LOOP_PROTECTION ) __FATAL__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Day header creation loop protection activated, looped too many times ('.HEADER_LOOP_PROTECTION.'x).' );
					
					$time_array = epoch_to_dmy($epoch);
					array_push( $header_row, '<small>'.$time_array[0].'-'.$time_array[1].'-'.$time_array[2].'</small>' );
					$epoch++;
				}
				break;
	
			case 'week':
				while( true )
				{
					if( $loop_prot++ > HEADER_LOOP_PROTECTION ) __FATAL__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Week header creation loop protection activated, looped too many times ('.HEADER_LOOP_PROTECTION.'x).' );
					
					array_push( $header_row, '<small>'.$week.'<br />'.weeknr_to_date($week, $year).'</small>' );
					
					if( ( $week >= $to_week ) && ( $year >= $to_year ) ) {
						break;
					}
					
					$week++;
					if( $week > 53 ) { 
						$week=1;
						$year++;
					}
				}
				break;
	
			case 'month':
				while( true ) 
				{
					
					if( $loop_prot++ > HEADER_LOOP_PROTECTION ) __FATAL__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Month header creation loop protection activated, looped too many times ('.HEADER_LOOP_PROTECTION.'x).' );
				
					array_push( $header_row, '<small>'.$month.'<br />'.$year.'</small>' );
	
					if( ( $month >= $to_month ) && ( $year >= $to_year ) ) {
						break;
					}			
					
					$month++;
					if( $month > 12 ) { 
						$month=1;
						$year++;
					}
					
				}
				break;	
			
			case 'year':
				while(  $year <= $to_year )
				{
					if( $loop_prot++ > HEADER_LOOP_PROTECTION ) __FATAL__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Year header creation loop protection activated, looped too many times ('.HEADER_LOOP_PROTECTION.'x).' );
					array_push( $header_row, '<small>'.$year.'</small>' );
					$year++;
				}
				break;	
		}
	
		//
		if( isset( $_POST['howshowhorizontaltotals'] ) && (!isset( $_POST['howshowpct'])) ) {
			array_push( $header_row, text('totals') );
		}
		
		$intervals = count($header_row) - 1; //Interval equals the amount of columns minus the left one	
			
		
		// Default set value
		if( ! isset( $_POST['howshowzero'] ) ) {
			$set_value = '';
		}
		
		// Sometimes we just want to see only the rows that contain real values
		// we create a piece of SQL code that accomodates this
		if( isset($_POST['howshowonlyresults'] ) ) {
		
			if( isset( $_POST['use_incidents'] ) ) {
				$name_query_limiter .= '( count(incidents.id) > 0 )';
			}
	
			if( isset( $_POST['use_incidents_ready'] ) ) {
				if( $name_query_limiter != '' ) {
					$name_query_limiter .= ' OR ';
				}
				$name_query_limiter .= '( count(incidents_ready.id) > 0 )';
			}
	
			if( $name_query_limiter != '' ) {
				$name_query_limiter = 'HAVING ( '.$name_query_limiter.' )';
			}
		}
		
	
		// Loop all items that the user wants to see
		while( list( $what_key, $what_value ) = each( $_POST[ 'intervalwhat' ] ) ) {		
		
			$name_query = ''; // SQL query that gets all indexes and their names
			$name_query_where = ''; // As we run the name query build up a list of all the rows that we want to see, so we can limit the main query
			$mainq_group_by = '';
			$url_name = '';
			$nameq_to_mainq_link = '';
			$datafields = array( array() );	// Array of Array with all data that needs to be displayed inside it
			$used_index = 'name'; //The field from the name query that will be shown 
	
			// Set header row
			$datafields[0] = $header_row;
		
			// Now switch the what-specific options
			switch( $what_value ) {
	
				case 'status':
					$query_head = 'SELECT count( i.status_id ) AS amount, i.status_id AS idx';
					$name_query = 'SELECT status.id AS id, name
								   FROM status LEFT JOIN incidents ON ( incidents.status_id=status.id) LEFT JOIN incidents_ready ON ( incidents_ready.status_id=status.id)
					 			   '.$status_where.' GROUP BY status.id, status.name '.$name_query_limiter.' ORDER BY name';
					$nameq_to_mainq_link = 'i.status_id';
					$name = text('status');
					$url_name = 'status';
					break;
	
				case 'priority':
					$query_head = 'SELECT count( i.priorities_id ) AS amount, i.priorities_id AS idx';
					$name_query = 'SELECT priorities.id AS id, name
					                 FROM priorities LEFT JOIN incidents ON ( incidents.priorities_id=priorities.id) LEFT JOIN incidents_ready ON ( incidents_ready.priorities_id=priorities.id) 
									 '.$priority_where.' GROUP BY priorities.id, priorities.name '.$name_query_limiter.' ORDER BY name';
	
					$nameq_to_mainq_link = 'i.priorities_id';
					$name = text('priority');
					$url_name = 'priorities';
					break;
	
				case 'totals':
					$query_head = 'SELECT count( id ) AS amount, \'idx\' as idx ';
					$name = text('totals');
					break;
					
				case 'report_type':
					$query_head = 'SELECT count( i.report_type_id ) AS amount, i.report_type_id AS idx';
					$name_query = 'SELECT report_type.id AS id, name
									FROM report_type LEFT JOIN incidents ON ( incidents.report_type_id=report_type.id) LEFT JOIN incidents_ready ON ( incidents_ready.report_type_id=report_type.id) 
									'.$reporttype_where.' GROUP BY report_type.id, report_type.name '.$name_query_limiter.' ORDER BY name';
					$nameq_to_mainq_link = 'i.report_type_id';
					$name = text('report_type');
					$url_name = 'report_type';
					break;
	
				case 'incident_category':
					$query_head = 'SELECT count( i.incidentcategories_id ) AS amount, i.incidentcategories_id AS idx';
					$name_query = 'SELECT incidentcategories.id AS id, name
									FROM incidentcategories LEFT JOIN incidents ON ( incidents.incidentcategories_id=incidentcategories.id) LEFT JOIN incidents_ready ON ( incidents_ready.incidentcategories_id=incidentcategories.id) 
									'.$incidentcategory_where.' GROUP BY incidentcategories.id, incidentcategories.name '.$name_query_limiter.' ORDER BY name';
					$nameq_to_mainq_link = 'i.incidentcategories_id';
					$name = text('incidentcategory');
					$url_name = 'incidentcategories';
					break;
	
				case 'organisation':
					$query_head = 'SELECT count( i.organisation_id ) AS amount, i.organisation_id AS idx';
					$name_query = 'SELECT organisation.id AS id, name
					     			FROM organisation LEFT JOIN incidents ON ( incidents.organisation_id=organisation.id) LEFT JOIN incidents_ready ON ( incidents_ready.organisation_id=organisation.id) 
									'.$organisation_where.' GROUP BY organisation.id, organisation.name '.$name_query_limiter.' ORDER BY name';
					$nameq_to_mainq_link = 'i.organisation_id';
					$name = text('organisations');
					$url_name = 'organisation';
					break;
	
				case 'building':
					$query_head = 'SELECT count( i.building_id ) AS amount, i.building_id AS idx';
					$name_query = 'SELECT buildings.id AS id, name
									FROM buildings LEFT JOIN incidents ON ( incidents.building_id=buildings.id) LEFT JOIN incidents_ready ON ( incidents_ready.building_id=buildings.id) 
									'.$building_where.' GROUP BY buildings.id, buildings.name '.$name_query_limiter.' ORDER BY name';
					$nameq_to_mainq_link = 'i.building_id';
					$name = text('buildings');
					$url_name = 'buildings';
					break;
					
				case 'room':
					$query_head = 'SELECT count( i.rooms_id ) AS amount, i.rooms_id AS idx';
					$name_query = 'SELECT rooms.id AS id, name
									FROM rooms LEFT JOIN incidents ON ( incidents.rooms_id=rooms.id) LEFT JOIN incidents_ready ON ( incidents_ready.rooms_id=rooms.id) 
									'.$room_where.' GROUP BY rooms.id, rooms.name '.$name_query_limiter.' ORDER BY name';
					$nameq_to_mainq_link = 'i.rooms_id';
					$name = text('rooms');
					$url_name = 'rooms';
					break;
					
				case 'department':
					$query_head = 'SELECT count( i.department_id ) AS amount, i.department_id AS idx';
					$name_query = 'SELECT departments.id AS id, name
									FROM departments LEFT JOIN incidents ON ( incidents.department_id=departments.id) LEFT JOIN incidents_ready ON ( incidents_ready.department_id=departments.id) 
									'.$department_where.' GROUP BY departments.id, departments.name '.$name_query_limiter.' ORDER BY name';
					$nameq_to_mainq_link = 'i.department_id';
					$name = text('departments');
					$url_name = 'departments';
					break;
	
				case 'contact':
					$query_head = 'SELECT count( i.contact_id ) AS amount, i.contact_id AS idx';
					$name_query = 'SELECT users.id AS id, surname
									FROM users LEFT JOIN incidents ON ( incidents.contact_id=users.id) LEFT JOIN incidents_ready ON ( incidents_ready.contact_id=users.id) 
									'.$contact_where.' GROUP BY users.id, users.surname '.$name_query_limiter.' ORDER BY surname';
					$nameq_to_mainq_link = 'i.contact_id';
					$name = text('reporter');
					$url_name = 'users';
					$used_index = 'surname';
					break;
	
				case 'owner':
					$query_head = 'SELECT count( i.owner_id ) AS amount, i.owner_id AS idx';
					$name_query = 'SELECT users.id AS id, surname
								 	FROM users LEFT JOIN incidents ON ( incidents.owner_id=users.id) LEFT JOIN incidents_ready ON ( incidents_ready.owner_id=users.id) 
									'.$owner_where.' GROUP BY users.id, users.surname '.$name_query_limiter.' ORDER BY surname';
					$nameq_to_mainq_link = 'i.owner_id';
					$name = text('owner');
					$url_name = 'users';
					$used_index = 'surname';				
					break;
					
				case 'group':
					$query_head = 'SELECT count( i.group_id ) AS amount, i.group_id AS idx';
					$name_query = 'SELECT groups.id AS id, name
									FROM groups LEFT JOIN incidents ON ( incidents.group_id=groups.id) LEFT JOIN incidents_ready ON ( incidents_ready.group_id=groups.id) 
									'.$group_where.' GROUP BY groups.id, groups.name '.$name_query_limiter.' ORDER BY name';
					$nameq_to_mainq_link = 'i.group_id';
					$name = text('group');
					$url_name = 'groups';
					break;
	
				case 'software':
					$query_head = 'SELECT count( i.software ) AS amount, i.software AS idx';
					$name_query = 'SELECT software.id AS id, name
									FROM software LEFT JOIN incidents ON ( incidents.software=software.id) LEFT JOIN incidents_ready ON ( incidents_ready.software=software.id) 
									'.$software_where.' GROUP BY software.id, software.name '.$name_query_limiter.' ORDER BY name';
					$nameq_to_mainq_link = 'i.software';
					$name = text('software');
					$url_name = 'software';
					break;
	
				case 'hardware':
					$query_head = 'SELECT count( i.hardware ) AS amount, i.hardware AS idx';
					$name_query = 'SELECT hardware.id AS id, name
									FROM hardware LEFT JOIN incidents ON ( incidents.hardware=hardware.id) LEFT JOIN incidents_ready ON ( incidents_ready.hardware=hardware.id) 
									'.$hardware_where.' GROUP BY hardware.id, hardware.name '.$name_query_limiter.' ORDER BY name';
					$nameq_to_mainq_link = 'i.hardware';
					$linked_id = 'i.hardware';
					$name = text('hardware');
					$url_name = 'hardware';
					break;
	
					
				//
				// This is hardware, and is therefore much, much more difficult
				//
				case 'hardware_type':
				case 'hardwareconfig':
				case 'hardwarebrand':
				default:
					__FATAL__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Illegal what action: "'.$what_value.'"' );
					break;
			}
			
			//
			if( $nameq_to_mainq_link != '' ) {
				$mainq_group_by = ' GROUP BY '.$nameq_to_mainq_link;
			}
			
			
			// Run the name query and fill up the $datafields
			if( $name_query != '' ) {
				$keyset = array();
				$GLOBALS['db']->query( $name_query );
				
				// If there are none, abort now
				if( $GLOBALS['db']->num_rows() == 0 ) {
					array_push( $all_datafields, array( $datafields, $name ) );
					unset( $datafields );
					continue;
				}
				
	//			$GLOBALS['box']->add(  'NAMEQUERY', $GLOBALS['db']->last_query );
				
				while( $row = $GLOBALS['db']->fetch_array() ) {
				
					// Add to keyset
					array_push( $keyset, (int)$row['id'] );
				
					// Add name
					if( $url_name != '' ) {
						$row[ $used_index ] = '<a href="'.$url_name.'.php?'.$url_name.'action=show&'.$url_name.'showid='.$row['id'].'">'.$row[ $used_index ].'</a>';
					}
					
					// Add index column
					$datafields[ $row['id'] ][0] = $row[ $used_index ];
	
					// Initialize all fields
					for( $i=1 ; $i<=$intervals ; $i++ ) {
						$datafields[ $row['id'] ][ $i ] = $set_value;
					}
					
					$name_query_where .= $row['id'].',';
				}		
			}
			
			// No name query, just one resultset, fill in the name here
			else {
				$datafields[ 'idx' ][ 0 ] = $name;
				$keyset = array( 'idx' );
				
				// Initialize all fields
				for( $i=1 ; $i<=$intervals ; $i++ ) {
					$datafields[ 'idx'  ][ $i ] = $set_value;
				}
			}
			
			
			// Fix and create SQL code
			$name_query_where = substr( $name_query_where, 0, -1 );
			if( $name_query_where != '' ) {
				$name_query_where = ' AND '.$nameq_to_mainq_link.' IN ('.$name_query_where.') ';
			}
			
			
			// reset the time vars that change
			$day	= $from_day;
			$month	= $temp_to_month = $from_month;
			$year	= $temp_to_year = $from_year;
			$week	= $temp_to_week = $from_week;
			$epoch	= $epoch_from;
			
			if( isset( $_POST['howshowverticaltotals'] )  && ($what_value!='totals') ) {
				$datafields[ 'totals'  ][ 0 ] = text('totals');
			}
	
			// Now loop the intervals, and query once per column
			// $interval_i starts at one ( we want to avoid hitting column zero ) and it's value is the column index of the currently running date
			for( $intererval_i=1 ; $intererval_i <= $intervals ; $intererval_i++ ) {
			
				$time_where = $main_time_where;
	
				// calculate the next time interval and create an SQL string to match
				switch( $_POST['interval'] ) {
		
					case 'day':
						$time_where .= ' AND ' .$timefield_name. db_like( array_to_db_date( epoch_to_dmy($epoch) ) );
						$epoch++;
						break;
		
					case 'week':
						$temp_to_week = $week + 1;
						if( $temp_to_week > 53 ) {
							$temp_to_week = 1;
							$temp_to_year++;
						}
						$time_where .= ' AND ( ' .$timefield_name. ' >= \''.weeknr_to_date($week, $year).'\' '.
						                      'AND ' .$timefield_name. ' < \''.weeknr_to_date( $temp_to_week, $temp_to_year).'\' ) ';
						$week++;						
						if( $week > 53 ) {
							$week = 1;
							$year++;
							$temp_to_year = $year;						
						}
						break;
		
					case 'month':
						$temp_to_month = $month + 1;
						if( $temp_to_month > 12 ) {
							$temp_to_month = 1;
							$temp_to_year++;
						}
						
						if( $month < 10 ) $month = '0' . (int)$month;
						if( $temp_to_month < 10 ) $temp_to_month = '0' . (int)$temp_to_month;
						
						$time_where .= ' AND ( ' .$timefield_name. ' >= \''.array_to_db_date( array( '01', $month, $year ) ).'\' '.
						                      'AND ' .$timefield_name. ' < \''.array_to_db_date( array( '01', $temp_to_month, $temp_to_year ) ).'\' ) ';
						$month++;						
						if( $month > 12 ) {
							$month = 1;
							$year++;
							$temp_to_year = $year;	
						}
						break;
						
					case 'year':
						$time_where .= ' AND ( ' .$timefield_name. ' >= \''.array_to_db_date( array( '01', '01', $year ) ).'\' '.
						                      'AND ' .$timefield_name. ' <= \''.array_to_db_date( array( '31', '12', $year ) ).'\' ) ';
						$year++;
						break;
				}
		
				$vertical_total = 0;
	
				// Do query on incidents
				if( isset( $_POST['use_incidents'] ) ) {
				
					$GLOBALS['db']->query( $query_head .' FROM incidents AS i '.$time_where.' ' . $where . ' ' . $name_query_where. ' ' .$mainq_group_by );
					
					// Nicer
					if( ! isset( $_POST['howshowzero']) ) {
						
						while( $row = $GLOBALS['db']->fetch_array() ) {
							if( $row['amount'] == 0 ) continue;
						
							if( ! is_numeric($datafields[ $row['idx'] ][$intererval_i]) ) {
								$datafields[ $row['idx'] ][$intererval_i] = 0;
							}
							$datafields[ $row['idx'] ][$intererval_i] += $row['amount'];
							$vertical_total += $row['amount'];
						}
					}
					
					// Faster
					else {
						while( $row = $GLOBALS['db']->fetch_array() ) {
							$datafields[ $row['idx'] ][$intererval_i] += $row['amount'];
							$vertical_total += $row['amount'];
						}
					}
				}
	
				// Do query on incidents_ready
				if( isset( $_POST['use_incidents_ready'] ) ) {
				
					$GLOBALS['db']->query( $query_head .' FROM incidents_ready AS i '.$time_where.' ' . $where . ' ' . $name_query_where. ' ' .$mainq_group_by );
					
					// Nicer
					if( ! isset( $_POST['howshowzero']) ) {
						
						while( $row = $GLOBALS['db']->fetch_array() ) {
							if( $row['amount'] == 0 ) continue;
						
							if( ! is_numeric($datafields[ $row['idx'] ][$intererval_i]) ) {
								$datafields[ $row['idx'] ][$intererval_i] = 0;
							}
							$datafields[ $row['idx'] ][$intererval_i] += $row['amount'];
							$vertical_total += $row['amount'];
						}
					}
					
					// Faster
					else {
						while( $row = $GLOBALS['db']->fetch_array() ) {
							$datafields[ $row['idx'] ][$intererval_i] += $row['amount'];
							$vertical_total += $row['amount'];
						}
					}
				}
				
				// If we want to show pct calc here
				if( isset( $_POST['howshowpct'] ) ) {
				
					$one_pct = $vertical_total/100;
					reset( $keyset );
					while( list($ignore, $key) = each( $keyset ) ) {
						
						if( ! ( isset( $datafields[ $key ] ) || isset($datafields[ $key ][$intererval_i] ) ) ) continue;
	
						if( $datafields[ $key ][$intererval_i] != '' ) {
							if( $one_pct != 0 ) {
								if( isset( $_POST['howshowabsolute'] ) ) {
									$datafields[ $key ][$intererval_i] = '<strong>' . $datafields[ $key ][$intererval_i] . '</strong> <br /><em>'. number_format( ($datafields[ $key ][$intererval_i] / $one_pct ), 2 ).' %</em>';
								}
								else {
									$datafields[ $key ][$intererval_i] = number_format( ($datafields[ $key ][$intererval_i] / $one_pct ), 2 ).' %';
								}
							}
						}
					}
				}
	
				// Show vert totals
				if( isset( $_POST['howshowverticaltotals'] ) && ($what_value!='totals') ) {
					$datafields[ 'totals' ][$intererval_i] = $vertical_total;
				}
				
	//			if( ! defined('blabla') ) {
	//				echo $GLOBALS['box']->add( 'Incidents Query', $GLOBALS['db']->last_query );
	//				define( 'blabla', true );
	//			}
				
				//echo $GLOBALS['box']->add( 'Incidents Query', $GLOBALS['db']->last_query );
			} // End of interval loop
			
			
			// Calculate horizontal totals per row
			if( isset( $_POST['howshowhorizontaltotals'] ) && ( ! isset( $_POST['howshowpct'] ) ) ) {
			
				// Don't use first
				each( $datafields );
			
				// Create the totals 
				while( list( $key, $value ) = each( $datafields ) ) {
				
					$row_totals = 0;
				
					for( $column=1 ; $column < ( count($value)-1) ; $column++ ) {
						$row_totals += $value[$column];
					}
	
					$datafields[ $key ][ ( count($value) - 1 ) ] = $row_totals;
				}
				
				reset( $datafields );
			}
			
			array_push( $all_datafields, array( $datafields, $name ) );
			unset( $datafields );
			
		} // End of what loop
		
		
		// Reverse array
		reset( $all_datafields );
		$all_datafields = array_reverse( $all_datafields );
		reset( $all_datafields );
		
		// Creation
		switch( $_POST[ 'howdisplaytype' ] ) {
		
			case 'csv_file':
			
				$file_contents = '';
				
				while( $datafields_and_name = array_pop( $all_datafields ) ) {
				
					$datafields = $datafields_and_name[0];
					$file_contents .= $datafields_and_name[1]."\r\n";
			
					// Create the csv file
					while( list( $key, $value ) = each( $datafields ) ) {
						for( $column=0 ; $column<count( $value) ; $column++ ) {
							$file_contents .= $value[$column].FIELD_SEPERATOR;
						}
						$file_contents = substr( $file_contents, 0, -1 )."\r\n";
					}
				
					$file_contents .= "\r\n";
				}
				
				$file_contents = strip_tags( str_replace( '<br />', ', ', $file_contents ) );
				
				// Alter the header
				header( 'Content-type: text/comma-separated-values' );
				header( 'Content-Disposition: attachment; filename=HelpCORE_report.csv' );
				header( 'Content-length: ' . strlen( $file_contents ) );
				header( 'Content-Description: HelpCORE Report' );
				echo $file_contents;
				die;
			
				break;
				
			case 'table':
				
				while( $datafields_and_name = array_pop( $all_datafields ) ) {
				
					$datafields = $datafields_and_name[0];
				
					$table = new class_table;
				
					// Shift first one off
					$table->add_header_row(  array_shift( $datafields ) );
					
					// Loop all others, first one is gone now
					while( list( $key, $value ) = each( $datafields ) ) {
					
						if( ( ! is_array( $value ) ) && ( is_string( $value ) ) ) {
							$table->add_header_line( $value );
							continue;
						}
					
						$table->add_row( $value );
					}
					
					$GLOBALS['box']->add( $datafields_and_name[1], $table->finish() );
					unset( $table );
				}
				break;
		}	
	} // END OF if( isset( $_POST[ 'intervalwhat' ] ) && (count(isset( $_POST[ 'intervalwhat' ] )>0  ) ) ) {


	//
	//  Custom reports
	//	
	if( isset( $_POST[ 'reportwhat' ] ) && (count(isset( $_POST[ 'reportwhat' ] )>0  ) ) ) {

		// Get the table that we need to query from
		$custom_table = 'incidents';
		$custom_id_fields = 'i.id AS dbid, i.id AS id';

		// Closed incidents
		if( isset($_POST[ 'customwheretable']) && ( $_POST[ 'customwheretable'] == 'incidents_ready' ) ) {
			$custom_table = 'incidents_ready';
			$custom_id_fields = 'i.old_id AS dbid, i.id AS id';
		}		
		
		// Loop all items that the user wants to see
		while( list( $what_key, $what_value ) = each( $_POST[ 'reportwhat' ] ) ) {		

			$function_name =  'report_'.$what_value;
			if( ! function_exists( $function_name) )  {
				__FATAL__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Unkown function: "'.$function_name.'"' );
			}

			$function_name();

			if( $_POST[ 'howdisplaytype' ] == 'csv_file' ) {

				// Get contents
				$file_contents = ob_get_clean();
				
				// Alter the header
				header( 'Content-type: text/comma-separated-values' );
				header( 'Content-Disposition: attachment; filename=HelpCORE_report.csv' );
				header( 'Content-length: ' . strlen( $file_contents ) );
				header( 'Content-Description: HelpCORE Report' );
				echo $file_contents;
				die;
		}

		}
	} // END OF if( isset( $_POST[ 'reportwhat' ] ) && (count(isset( $_POST[ 'reportwhat' ] )>0  ) ) ) {
		
		
	echo '<br /><a href="'.$_SERVER['PHP_SELF'].'"><small>'.text('create_another_report').'</small></a>';	
	
} // End of do-process if

else {
	
	//
	//
	// GRAPHICAL FUNCIONS FOR BUILDING UP INTERFACE -----------------------------------------------------------------------------------
	//
	//
	if( count($_POST) == 0 ) {
		$GLOBALS["override_values"]['use_incidents'] = 'on';
		$GLOBALS["override_values"]['use_incidents_ready'] = 'on';
		$GLOBALS["override_values"]['howshowonlyresults'] = 'on';
	//	$GLOBALS["override_values"]['howshowhorizontaltotals'] = 'on';
	//	$GLOBALS["override_values"]['howshowverticaltotals'] = 'on';
		$GLOBALS["override_values"]['howshownotset'] = 'on';
		$GLOBALS["override_values"]['howshowabsolute'] = 'on';
		
		
		// Set lowest from database
		$GLOBALS['db']->query( '(SELECT min(date_reported) AS dr FROM incidents_ready) UNION (SELECT min(date_reported) AS dr FROM incidents) order by dr asc' );
		$row = $GLOBALS['db']->fetch_array();
		$from_date = $row['dr'];
		
		// Set highest from database
		$GLOBALS['db']->query( '(SELECT max(date_reported) AS dr FROM incidents_ready) UNION (SELECT max(date_reported) AS dr FROM incidents) order by dr desc' );
		$row = $GLOBALS['db']->fetch_array();
		$to_date = $row['dr'];	
		
	}
	
	
	$intervalwhat_array = array( 'status'					=> text('status'),
							 'priority'					=> text('priorities'),
							 'report_type'				=> text('report_type'),
							 'incident_category'		=> text('incidentcategory'),
							 'organisation'				=> text('organisation'),
							 'building'					=> text('building'),
							 'room'						=> text('room'),
							 'department'				=> text('department'),
							 'contact'					=> text('reporter'),
							 'owner'					=> text('owner'),
							 'group'					=> text('group'),
							 'hardware'					=> text('hardware'),
							 'software'					=> text('software'),			 
							 'totals'					=> text('totals') );
	
	
	$display_array = array( array( 'table', text('table') ),
							array( 'csv_file', text('csv_file') ) );
							
	$interval_array = array( array( 'day', text('day') ),
							 array( 'week', text('week') ),
							 array( 'month', text('month') ),
							 array( 'year', text('year') ) );
							 
	$timefield_array = array( array( 'date_reported', text('date_reported')),
						 	  array( 'date_finished', text('date_finished') ),
							  array( 'date_started', text('date_started') ),
							  array( 'date_escalated', text('date_escalated') ),
							  array( 'date_last_status_change', text('date_status_change') ),
							  array( 'sla_date_solve_before', text('date_solve_before') ),
							  array( 'sla_date_solve_on', text('date_solve_on') ) );
	
	
							  
	
	echo form_start( 'reportForm', 'incident_reports.php', '', 'incidentReportForm' );
	echo form_hiddenfield( 'process', 'true' );
	
	
	
	//
	// Create a tab with all possible selectable items
	//
	$tab_contents = '';
	
	$table = new class_table;
	$table->table_columns = 2;
	$table->add_row( array( text('from_date'), form_datefield( 'from_date', $from_date ) ) );
	$table->add_row( array( text('to_date'), form_datefield( 'to_date', $to_date ) ) );
	$table->add_row( array( text('used_timefield'), form_select( 'timefield', $timefield_array, find_value( 'timefield' ) ) ) );
	$tab_contents .= $table->finish();
	
	$tab_contents .= '<table width="94%" border="0"><tr><td>'.text('report').'</td><td>'.text('interval_statistics').'</td></tr><tr><td>';
	
	// Creates custom reports
	$select_data = '';
	while( list( $key, $value) = each( $reportwhat_array ) ) {
		$select_data .= '<option value="'.$key.'">'.$value.'</option>'."\n";
	}
	$tab_contents .= form_multiple_select( 'reportwhat', $select_data );
	
	// Selected open or closed
	$tab_contents .= '<br />';
	$tab_contents .= form_radiobutton( 'customwheretable', 'incidents', true ) . text( 'incidents' ) . '&nbsp;&nbsp;&nbsp;' ;
	$tab_contents .= form_radiobutton( 'customwheretable', 'incidents_ready', false ) . text( 'incidents_ready' );
	
	$tab_contents .= "</td><td><table border=\"0\"><tr><td>";
	
	
	$select_data = '';
	while( list( $key, $value) = each( $intervalwhat_array ) ) {
		$select_data .= '<option value="'.$key.'">'.$value.'</option>'."\n";
	}
	$tab_contents .= form_multiple_select( 'intervalwhat', $select_data );
	
	$tab_contents .= '</td><td>';

	// Generate multiple select list
	$table = new class_table;
	$table->table_columns = 2;
	$table->add_row( array( text('time_interval'), form_select( 'interval', $interval_array, find_value( 'interval', 'week' ) ) ) );
	$table->add_row( array( text('use_open_incidents'), form_checkfield( 'use_incidents',  find_value( 'use_incidents' ) ) ) );
	$table->add_row( array( text('use_closed_incidents'), form_checkfield( 'use_incidents_ready',  find_value( 'use_incidents_ready' ) ) ) );
	$table->add_row( array( text('show_zero'), form_checkfield( 'howshowzero', find_value( 'howshowzero', find_value( 'howshowzero' ) ) ) ) );
	$table->add_row( array( text('show_only_rows_with_results'), form_checkfield( 'howshowonlyresults', find_value( 'howshowonlyresults' ) ) ) );
	$table->add_row( array( text('show_horizontal_totals'), form_checkfield( 'howshowhorizontaltotals', find_value( 'howshowhorizontaltotals' ) ) ) );
	$table->add_row( array( text('show_vertical_totals'), form_checkfield( 'howshowverticaltotals', find_value( 'howshowverticaltotals' ) ) ) );
	
	//$table->add_row( array( text('show_not_set'), form_checkfield( 'howshownotset', find_value( 'howshownotset' ) ) ) );
	$table->add_row( array( text('display_type'), form_select( 'howdisplaytype', $display_array, find_value( 'howdisplaytype' ) ) ) );
	$table->add_row( array( text('show_absolute'), form_checkfield( 'howshowabsolute', find_value( 'howshowabsolute' ) ) ) );
	$table->add_row( array( text('show_pct'), form_checkfield( 'howshowpct', find_value( 'howshowpct' ) ) ) );
	
	$tab_contents .= $table->finish().'</td></tr></table></td></tr></table><br />'.form_submit_button( 'submitForm', text( 'create_report' ) ) ;
	
	
	$GLOBALS['tabs']->addDataPanel( '(1) ' . text( 'report_select_from_when' ),  $GLOBALS['box']->data( '' , $tab_contents) );
	
	//
	// Create a tab that shows selectable users and groups
	//
	$tab_contents = '';
	$tab_contents .= '<table width="94%" border="0"><tr><td>'.text('reporter').'</td><td width="100" nowrap="nowrap" rowspan=2>&nbsp;</td><td>'.text('owner').'</td><td width="100" nowrap="nowrap" rowspan=2>&nbsp;</td><td>'.text('groups').'</td></tr><tr><td>';
	$tab_contents .= report_create_select('whocontact', 'users', array('surname','middlename','firstname'), array('','',$uls_user_limiter));
	$tab_contents .= '</td><td>';
	
	if($uls_user_limiter!='') $uls_user_limiter = ' AND '.$uls_user_limiter;
	
	$tab_contents .= report_create_select('whoowner', 'users', array('surname','middlename','firstname'), array( 'may_log_in', '=', database_boolean(true).' '.$uls_user_limiter ) );
	$tab_contents .= '</td><td>';
	$tab_contents .= report_create_select('whogroups', 'groups', array('name'), array('','',$uls_group_limiter));
	$tab_contents .= '</td></tr></table>';
	$tab_contents .= form_submit_button( 'submitFormusersgroups', text( 'create_report' ) );				   
	$GLOBALS['tabs']->addDataPanel( '(2) ' . text( 'report_who' ), $GLOBALS['box']->data( '' , $tab_contents) );
	
	
	//
	// Create a tab with statuses and prio's and such
	//
	$tab_contents = '<table width="94%" border="0"><tr><td>'.text('status').'</td><td width="50" nowrap="nowrap" rowspan=2>&nbsp;</td><td>'.text('priorities').'</td><td width="50" nowrap="nowrap" rowspan=2>&nbsp;</td><td>'.text('incidentcategory').'</td><td width="50" nowrap="nowrap" rowspan=2>&nbsp;</td><td>'.text('report_type').'</td></tr><tr><td>';
	$tab_contents .= report_create_select( 'wherestatus', 'status');
	$tab_contents .= '</td><td>';
	$tab_contents .= report_create_select( 'wherepriority', 'priorities');
	$tab_contents .= '</td><td>';
	$tab_contents .= report_create_select( 'whereincidentcategory', 'incidentcategories');
	$tab_contents .= '</td><td>';
	$tab_contents .= report_create_select( 'wherereporttype', 'report_type');
	$tab_contents .= '</td></tr></table>';
	$tab_contents .= form_submit_button( 'submitFormelements', text( 'create_report' ) );
	$GLOBALS['tabs']->addDataPanel( '(3) ' . text( 'report_elements' ),  $GLOBALS['box']->data( '' , $tab_contents));
	
	//
	// Create a tab with organisations, buildings, departments and rooms
	//
	$tab_contents = '';
	$tab_contents .= '<table width="94%" border="0"><tr><td>'.text('organisations').'</td><td width="50" nowrap="nowrap" rowspan=2>&nbsp;</td><td>'.text('buildings').'</td><td width="50" nowrap="nowrap" rowspan=2>&nbsp;</td><td>'.text('rooms').'</td><td width="50" nowrap="nowrap" rowspan=2>&nbsp;</td><td>'.text('departments').'</td></tr><tr><td>';
	$tab_contents .= report_create_select( 'whereorganisation', 'organisation', array('name'), array('','',$uls_organisation_limiter));
	$tab_contents .= '</td><td>';
	$tab_contents .= report_create_select( 'wherebuildings', 'buildings', array('name'), array('','',$uls_building_limiter));
	$tab_contents .= '</td><td>';
	$tab_contents .= report_create_select( 'whererooms', 'rooms', array('name'), array('','',$uls_room_limiter));
	$tab_contents .= '</td><td>';
	$tab_contents .= report_create_select( 'wheredepartments', 'departments', array('name'), array('','',$uls_department_limiter));
	$tab_contents .= '</td></tr></table>';
	$tab_contents .= form_submit_button( 'submitFormlocation', text( 'create_report' ) );
	$GLOBALS['tabs']->addDataPanel( '(4) ' . text( 'report_location' ),  $GLOBALS['box']->data( '' , $tab_contents));
	
	
	//
	// Create a tab with hardware
	//
	if( USE_HARDWARE_CONFIGS ) {
	
		$tab_contents = '';
		$tab_contents .= '<table width="94%" border="0"><tr><td>'.text('hardware').'</td><td width="50" nowrap="nowrap" rowspan=2>&nbsp;</td><td>'.text('hardware_types').'</td><td width="50" nowrap="nowrap" rowspan=2>&nbsp;</td><td>'.text('types').'</td><td width="50" nowrap="nowrap" rowspan=2>&nbsp;</td><td>'.text('brands').'</td></tr><tr><td>';
		$tab_contents .= report_create_select( 'hardwarehardware', 'hardware', array('name'), array('','',$uls_hardware_limiter) );
		$tab_contents .= '</td><td>';
		$tab_contents .= report_create_select( 'hardware_type', 'hardware_types');
		$tab_contents .= '</td><td>';
		
			$union_data = '';
			$GLOBALS['db']->query( 'SELECT id, name FROM hardware_types ORDER by name' );
			while ( $row = $GLOBALS['db']->fetch_array() )
			{
				$nice_name = $row['name'];
				$name = secure_name( $nice_name );
				$id = $row['id'];
				if ( file_exists( 'config_' . $name . '.php' ) ) {
				
					$db2->query( 'SELECT co.id AS id, co.name AS name, type, uid, brands.name AS brand FROM config_'.$name.' AS co LEFT JOIN brands ON ( brands.id = co.brands_id )' );
					while( $row = $db2->fetch_array() ) {
						$union_data .= '<option value="'.$id.':'.$row['id'].'">'.$nice_name.': '.$row['brand'].' '.$row['type'].' '.$row['name'].'</option>';
					}
				}
			}
			
			$tab_contents .= form_multiple_select( 'hardwareconfig', $union_data, 30 );
			
			$tab_contents .= '</td><td>';
	
		$tab_contents .= report_create_select( 'hardwarebrand', 'brands');
		$tab_contents .= '</td></tr></table>';
		$tab_contents .= form_submit_button( 'submitFormhardware', text( 'create_report' ) );
		$GLOBALS['tabs']->addDataPanel( '(5) ' . text( 'hardware' ),  $GLOBALS['box']->data( '' , $tab_contents));
	}
	
	
	//
	// Create a tab with organisations, buildings, departments and rooms
	//
	if( USE_SOFTWARE ) {
		$tab_contents = '';
		$tab_contents .= '<table width="94%" border="0"><tr><td>'.text('software').'</td></tr><tr><td>';
		$tab_contents .= report_create_select( 'softwaresoftware', 'software');
		$tab_contents .= '</td></tr></table>';
		$tab_contents .= form_submit_button( 'submitFormsoftware', text( 'create_report' ) );
		$GLOBALS['tabs']->addDataPanel( '(6) ' . text( 'software' ),  $GLOBALS['box']->data( '' , $tab_contents));
	}

	
	$GLOBALS['coreapm']->finish();
	echo form_finish();
	die;
}

$GLOBALS['coreapm']->finish();

?>
Return current item: HelpCORE