<?php
// $Id$

/**
 *		@file
 *		sabreQMS Module for plugin to Drupal 7 Framework
 *
 *		QMS custom configuration window
 *    contains feature settings (on/off switches) for the QMS
 *    
*/

/**
 *		
 *		qms_config_form()
 *    
*/


function qms_config_form($form, $form_state) {
	
	// admin access only
	if ( user_access('administer sabreQMS') == FALSE ) {
		drupal_set_message( t('Unauthorized:  Permission required'), 'status');
		return;
  }

	$form['settings'] = array(
		'#type' => 'fieldset',
		'#title' => t('Settings'),
		'#collapsible' => False,
	);

	//get the current settings for the config screen
	$wysiwyg_title = t('Use WYSIWYG Editor');
	$wysiwyg_enabled = variable_get(QMS_VAR_WYSIWYG_EDITOR, 1);  //Default to ON(1)
	$wysiwyg_desc = t(
									  'Sets the ability to use the WYSIWYG editor tool for text fields that allow for it.  ' .
										'(discrepancy, shift log, engineering log, etc.).<br />' .
										'Enabled: text fields allow for formatted content through the full featured editor.<br />' .
										'Disabled: text fields allow for plain text content.'
										);

	$form['settings']['wysiwyg_editor'] = array(
		'#type' => 'checkbox',
		'#title' => '<strong>' . $wysiwyg_title . '</strong>',
		'#default_value' => $wysiwyg_enabled,
		'#suffix' => '<div class="qms-desc">' . $wysiwyg_desc . '</div><br />',
	);
	
	
	$jquery_title = t('Set JQuery UI Theme');
	$jquery_value = variable_get('jquery_ui_theme', '');  // if not set, will default
	$jquery_desc = t('Sets the path to the theme for JQuery widgets and tools used by QMS.<br />' .
									 'example:  "sites/all/themes/redmond/jquery-ui-1.8.16.custom.css"'	);
	
	$form['settings']['jquery_theme'] = array(
		'#type' => 'textfield',
		'#size' => 50,
		'#maxlength' => 255,
		'#title' => $jquery_title,
		'#default_value' => $jquery_value,
		'#suffix' => '<div class="qms-desc">' . $jquery_desc . '</div><br />',
	);
	
	$sort_opts = array(
		'asc' => 'asc',
		'desc' => 'desc',
	);
	
	$form['settings']['sort_table_markup1'] = array(
		'#type' => 'markup',
		'#markup' => '<table class="qms-plain-table" style="width:60%;">',
	);
	
	
	$dr_sort_col = variable_get(QMS_DISCREPANCY_DEFAULT_SORT_COLUMN, 'DR No.');  //Default to DR No desc
	$dr_sort = variable_get(QMS_DISCREPANCY_DEFAULT_SORT, 'desc');
	
	$dr_column_list = array(
		'DR No.' => 'DR No.',
		'Opened' => 'Opened',
		'Closed' =>'Closed',
		'Customer' => 'Customer',
		'Technician' =>'Technician',
		'Simulator' =>'Simulator',
	);
	
	$form['settings']['discrepancy_sort_col'] = array(
		'#type' => 'select',
		'#title' => t('Discrepancy Search Results - Sort Column'),
		'#options' => $dr_column_list,
		'#default_value' => $dr_sort_col,
		'#required' => True,
		'#attributes' => array('class' => array('qms-select')),
		'#prefix' => '<tr><td>',
		'#suffix' => '</td>',
	);
	
	$form['settings']['discrepancy_sort'] = array(
		'#type' => 'select',
		'#title' => t('Default Sort Order'),
		'#options' => $sort_opts,
		'#default_value' => $dr_sort,
		'#required' => True,
		'#prefix' => '<td>',
		'#suffix' => '</td></tr>',
	);
	
	$tc_sort_col = variable_get(QMS_TROUBLECALL_DEFAULT_SORT_COLUMN, 'TC No.');  //Default to TC No desc
	$tc_sort = variable_get(QMS_TROUBLECALL_DEFAULT_SORT, 'desc');
	
	$tc_column_list = array(
		'TC No.' => 'TC No.',
		'Opened' => 'Opened',
		'Closed' =>'Closed',
		'Customer' => 'Customer',
		'Technician' =>'Technician',
		'Simulator' =>'Simulator',
	);
	
	$form['settings']['troublecall_sort_col'] = array(
		'#type' => 'select',
		'#title' => t('Trouble Call Search Results - Sort Column'),
		'#options' => $tc_column_list,
		'#default_value' => $tc_sort_col,
		'#required' => True,
		'#attributes' => array('class' => array('qms-select')),
		'#prefix' => '<tr><td>',
		'#suffix' => '</td>',
	);
	
	$form['settings']['troublecall_sort'] = array(
		'#type' => 'select',
		'#title' => t('Default Sort Order'),
		'#options' => $sort_opts,
		'#default_value' => $tc_sort,
		'#required' => True,
		'#prefix' => '<td>',
		'#suffix' => '</td></tr>',
	);
	
	
	$eng_sort_col = variable_get(QMS_ENGINEERING_DEFAULT_SORT_COLUMN, 'Eng. No.');  
	$eng_sort = variable_get(QMS_ENGINEERING_DEFAULT_SORT, 'desc');
	
	$eng_column_list = array(
		'Eng. No.' => 'Eng. No.',
		'DR No.' => 'DR No.',
		'Date' => 'Date',
		'Technician' =>'Technician',
		'Simulator' =>'Simulator',
	);
	
	$form['settings']['engineering_sort_col'] = array(
		'#type' => 'select',
		'#title' => t('Engineering Search Results - Sort Column'),
		'#options' => $eng_column_list,
		'#default_value' => $eng_sort_col,
		'#required' => True,
		'#attributes' => array('class' => array('qms-select')),
		'#prefix' => '<tr><td>',
		'#suffix' => '</td>',
	);
	
	$form['settings']['engineering_sort'] = array(
		'#type' => 'select',
		'#title' => t('Default Sort Order'),
		'#options' => $sort_opts,
		'#default_value' => $eng_sort,
		'#required' => True,
		'#prefix' => '<td>',
		'#suffix' => '</td></tr>',
	);
	
	$sl_sort_col = variable_get(QMS_SHIFTLOG_DEFAULT_SORT_COLUMN, 'Date');  
	$sl_sort = variable_get(QMS_SHIFTLOG_DEFAULT_SORT, 'desc');
	
	$sl_column_list = array(
		'Technician' => 'Technician',
		'Date' => 'Date',
		'Simulator' => 'Simulator',
		'Comment' => 'Comment',
	);
	
	$form['settings']['shiftlog_sort_col'] = array(
		'#type' => 'select',
		'#title' => t('Shift Log Search Results - Sort Column'),
		'#options' => $sl_column_list,
		'#default_value' => $sl_sort_col,
		'#required' => True,
		'#attributes' => array('class' => array('qms-select')),
		'#prefix' => '<tr><td>',
		'#suffix' => '</td>',
	);
	
	$form['settings']['shiftlog_sort'] = array(
		'#type' => 'select',
		'#title' => t('Default Sort Order'),
		'#options' => $sort_opts,
		'#default_value' => $sl_sort,
		'#required' => True,
		'#prefix' => '<td>',
		'#suffix' => '</td></tr>',
	);
	
	$sd_sort_col = variable_get(QMS_SIMULATORDOWN_DEFAULT_SORT_COLUMN, 'SD No.');  
	$sd_sort = variable_get(QMS_SIMULATORDOWN_DEFAULT_SORT, 'desc');
	
	$sd_column_list = array(
		'SD No.' => 'SD No.',
		'DR No.' => 'DR No.',
		'Out Date' => 'Out Date',
		'In Date' => 'In Date',
		'Technician' => 'Technician',
		'Simulator' => 'Simulator',
	);
	
	$form['settings']['simulatordown_sort_col'] = array(
		'#type' => 'select',
		'#title' => t('Shift Log Search Results - Sort Column'),
		'#options' => $sd_column_list,
		'#default_value' => $sd_sort_col,
		'#required' => True,
		'#attributes' => array('class' => array('qms-select')),
		'#prefix' => '<tr><td>',
		'#suffix' => '</td>',
	);
	
	$form['settings']['simulatordown_sort'] = array(
		'#type' => 'select',
		'#title' => t('Default Sort Order'),
		'#options' => $sort_opts,
		'#default_value' => $sd_sort,
		'#required' => True,
		'#prefix' => '<td>',
		'#suffix' => '</td></tr>',
	);
  
  
  $qtg_sort_col = variable_get(QMS_QTG_DEFAULT_SORT_COLUMN, 'QTG No.');  
	$qtg_sort = variable_get(QMS_QTG_DEFAULT_SORT, 'desc');
	
	$qtg_column_list = array(
    'QTG No.' => 'QTG No.',
		'Opened' => 'Opened',
    'Closed' => 'Closed',
		'Test Code' => 'Test Code',
		'Simulator' => 'Simulator',
		'Group' => 'Group',
	);
	
	$form['settings']['qtg_sort_col'] = array(
		'#type' => 'select',
		'#title' => t('QTG Search Results - Sort Column'),
		'#options' => $qtg_column_list,
		'#default_value' => $qtg_sort_col,
		'#required' => True,
		'#attributes' => array('class' => array('qms-select')),
		'#prefix' => '<tr><td>',
		'#suffix' => '</td>',
	);
	
	$form['settings']['qtg_sort'] = array(
		'#type' => 'select',
		'#title' => t('Default Sort Order'),
		'#options' => $sort_opts,
		'#default_value' => $qtg_sort,
		'#required' => True,
		'#prefix' => '<td>',
		'#suffix' => '</td></tr>',
	);
	
	
	$form['settings']['sort_table_markup2'] = array(
		'#type' => 'markup',
		'#markup' => '</table>',
	);
  
  //get the current settings for the config screen
	$prefix_sims_title = t('Display Simulators as "Customer - Simulator"');
	$prefix_sims_enabled = variable_get(QMS_VAR_SIMS_USE_CUSTOMER_PREFIX, 1);  //Default to ON(1)
	$prefix_sims_desc = 
       t('Display simulator names with or without customer information' . 
         ' where applicable.');

	$form['settings']['prefix_sims'] = array(
		'#type' => 'checkbox',
		'#title' => '<strong>' . $prefix_sims_title . '</strong>',
		'#default_value' => $prefix_sims_enabled,
		'#suffix' => '<div class="qms-desc">' . $prefix_sims_desc . '</div><br />',
	);
	
	
	
	$form['settings']['actions'] = array('#type' => 'actions');
	$form['settings']['actions']['submit'] = array(
		'#type' => 'submit',
		'#value' => 'Submit',
		'#attributes' => array('class' => array('qms-btn-submit'),
                           'style' => 'font-weight:bolder;'),
	);
	
  $form['settings']['actions']['done'] = array(
		'#type' => 'button',
		'#value' => t('Done'),
		'#attributes' => array('class' => array('qms-btn-done', 'qms-btn-extra'),
                           'onclick' => 'window.location="' . 
                                url('qmssettings') . '"; return false;'),
	);
	
	
	
	return $form;
}

/**
 *		
 *		qms_config_form_submit()
 *    
*/

function qms_config_form_submit($form, $form_state) {
	
	$wysiwyg_enabled = (int)$form_state['values']['wysiwyg_editor'];
	//$assign_tech = (int)$form_state['values']['assign_tech'];
	$jquery_theme = trim($form_state['values']['jquery_theme']);
	
	$discrepancy_sort_col = $form_state['values']['discrepancy_sort_col'];
	$discrepancy_sort = $form_state['values']['discrepancy_sort'];
	$troublecall_sort_col = $form_state['values']['troublecall_sort_col'];
	$troublecall_sort = $form_state['values']['troublecall_sort'];
	$engineering_sort_col = $form_state['values']['engineering_sort_col'];
	$engineering_sort = $form_state['values']['engineering_sort'];
	$shiftlog_sort_col = $form_state['values']['shiftlog_sort_col'];
	$shiftlog_sort = $form_state['values']['shiftlog_sort'];
	$simulatordown_sort_col = $form_state['values']['simulatordown_sort_col'];
	$simulatordown_sort = $form_state['values']['simulatordown_sort'];
  $qtg_sort_col = $form_state['values']['qtg_sort_col'];
	$qtg_sort = $form_state['values']['qtg_sort'];
  $prefix_sims = (int)$form_state['values']['prefix_sims'];

	
	variable_set(QMS_VAR_WYSIWYG_EDITOR, $wysiwyg_enabled);
	//variable_set(QMS_VAR_ASSIGN_TECHNICIAN, $assign_tech);
	variable_set('jquery_ui_theme', $jquery_theme);
	
	variable_set(QMS_DISCREPANCY_DEFAULT_SORT_COLUMN, $discrepancy_sort_col);
	variable_set(QMS_DISCREPANCY_DEFAULT_SORT, $discrepancy_sort);
	
	variable_set(QMS_TROUBLECALL_DEFAULT_SORT_COLUMN, $troublecall_sort_col);
	variable_set(QMS_TROUBLECALL_DEFAULT_SORT, $troublecall_sort);
	
	variable_set(QMS_ENGINEERING_DEFAULT_SORT_COLUMN, $engineering_sort_col);
	variable_set(QMS_ENGINEERING_DEFAULT_SORT, $engineering_sort);
	
	variable_set(QMS_SHIFTLOG_DEFAULT_SORT_COLUMN, $shiftlog_sort_col);
	variable_set(QMS_SHIFTLOG_DEFAULT_SORT, $shiftlog_sort);
	
	variable_set(QMS_SIMULATORDOWN_DEFAULT_SORT_COLUMN, $simulatordown_sort_col);
	variable_set(QMS_SIMULATORDOWN_DEFAULT_SORT, $simulatordown_sort);
  
  variable_set(QMS_QTG_DEFAULT_SORT_COLUMN, $qtg_sort_col);
	variable_set(QMS_QTG_DEFAULT_SORT, $qtg_sort);
  
  
  variable_set(QMS_VAR_SIMS_USE_CUSTOMER_PREFIX, $prefix_sims);
	
	drupal_set_message('QMS Configuration Settings have been saved.');
	
	drupal_goto('qmssettings');  
}
