<?php
/*
 * mysql Ajax Table Editor
 *
 * Copyright (c) 2008 Chris Kitchen <info@mysqlajaxtableeditor.com>
 * All rights reserved.
 *
 * See COPYING file for license information.
 *
 * Download the latest version from
 * http://www.mysqlajaxtableeditor.com
 */
 
require_once('Common.php');
require_once('php/lang/LangVars-en.php');
require_once('php/AjaxTableEditor.php');
class onCampus extends Common
{
	var $Editor;
	//var $tableName;
	//var $senderEmail;
	
	function displayHtml()
	{
		?>
			<br />
	
			<div align="left" style="position: relative;"><div id="ajaxLoader1"><img src="ajax_loader.gif" alt="Loading..." /></div></div>
			
			<br />
			
			<div id="historyButtonsLayer" align="left">
			</div>
			
			<p id="errors"></p>
			
			<div id="historyContainer">
				<div id="information">
				</div>
		
				<div id="titleLayer">
				</div>
		
				<div id="searchButtonsLayer" align="center">
				</div>
				
				<div id="recordLayer" align="center">
				</div>	
				
				<div id="tableLayer" align="center">
				</div>
			</div>
			
			<script type="text/javascript">
				trackHistory = false;
				var ajaxUrl = '<?php echo $_SERVER['PHP_SELF']; ?>';
				toAjaxTableEditor('update_html','');
			</script>
		<?php
	}
      
	function initiateEditor()
	{
	  
      $tableColumns['id'] = array('display_text' => 'ID', 'perms' => 'OTVQSX');
	  $tableColumns['jobNumber'] = array('display_text' => 'Job Number', 'perms' => 'TEVCAXQSHO');
	  $tableColumns['category'] = array('display_text' => 'Category', 'perms' => 'EVCAXQSH', 'select_array' => array('Office & Administrative' => 'Office & Administrative', 'Arts & Media' => 'Arts & Media', 'Grad Appt' => 'Grad Appt', 'Science & Health' => 'Science & Health', 'Technology' => 'Technology', 'Social Services & Research' => 'Social Services & Research', 'Student Services' => 'Student Services'));
      $tableColumns['jobClass'] = array('display_text' => 'Job Class Code', 'perms' => 'EVCAXQSH', 'select_array' => array('0875' => '0875', '0872' => '0872', '0881' => '0881', '0882' => '0882', '0883' => '0883', '0868' => '0868', '0869' => '0869', '0886' => '0886', '0887' => '0887'));
      $tableColumns['program'] = array('display_text' => 'Program', 'perms' => 'EVCAXQSH', 'select_array' => array('Federal' => 'Federal', 'State' => 'State'));
      $tableColumns['classified'] = array('display_text' => '51% Comp to Classified', 'perms' => 'EVCAXQSH', 'select_array' => array('y' => 'Yes', 'n' => 'No'));
	  $tableColumns['orgName'] = array('display_text' => 'Department', 'perms' => 'TEVCAXQSHO');
      $tableColumns['contactFName'] = array('display_text' => 'First Name', 'perms' => 'TEVCAXQSHO');
      $tableColumns['contactLName'] = array('display_text' => 'Last Name', 'perms' => 'TEVCAXQSHO');
 	  $tableColumns['jobTitle'] = array('display_text' => 'Job Title', 'perms' => 'TEVCAXQSHO');
      $tableColumns['timeStamp'] = array('display_text' => 'Last Modified', 'perms' => 'TVCTAXQSHO'); 
      $tableColumns['status'] = array('display_text' => 'Job Status', 'perms' => 'EVCTAXQSHO', 'select_array' => array('Open' => 'Open', 'Closed' => 'Closed'));
      $tableColumns['location'] = array('display_text' => 'Location', 'perms' => 'EVCAXQSH');
      $tableColumns['website'] = array('display_text' => 'Website', 'perms' => 'EVCAXQSH');
      $tableColumns['min'] = array('display_text' => 'Min $', 'perms' => 'EVCAXQSH');
      $tableColumns['max'] = array('display_text' => 'Max $', 'perms' => 'EVCAXQSH');
      $tableColumns['employmentPeriod'] = array('display_text' => 'Employment Period', 'perms' => 'EVCAXQSH', 'select_array' => array('Summer' => 'Summer', 'Academic Year' => 'Academic Year', 'Summer, Academic Year' => 'Summer, Academic Year'));
      $tableColumns['hoursPerWeek'] = array('display_text' => 'Hours Per Week', 'perms' => 'EVCAXQSH', 'select_array' => array('19 hrs/wk' => 'up to 19 hrs/wk', '40 hrs/wk' => '40 hrs/wk (Summer Only', '40 hrs/wk, 19 hrs/wk' => '40 hrs/wk, 19 hrs/wk'));
      $tableColumns['phoneNumber'] = array('display_text' => 'Phone Number', 'perms' => 'EVCAXQSH');
      $tableColumns['emailAddress'] = array('display_text' => 'Email Address', 'perms' => 'EVCAXQSH');
      $tableColumns['boxNumber'] = array('display_text' => 'Box Number', 'perms' => 'EVCAXQSH');
      $tableColumns['nature'] = array('display_text' => 'Nature of Organization', 'perms' => 'EVCAXQSH', 'textarea' => array('rows' => 10, 'cols' => 80));
      $tableColumns['duties'] = array('display_text' => 'Duties and Responsibilities', 'perms' => 'EVCAXQSH', 'textarea' => array('rows' => 10, 'cols' => 80));
      $tableColumns['qualifications'] = array('display_text' => 'Minimum Qualifications', 'perms' => 'EVCAXQSH', 'textarea' => array('rows' => 10, 'cols' => 80));
      $tableColumns['benefits'] = array('display_text' => 'Educational Benefits', 'perms' => 'EVCAXQSH', 'textarea' => array('rows' => 10, 'cols' => 80));
      $tableColumns['apply'] = array('display_text' => 'How to Apply', 'perms' => 'EVCAXQSH', 'textarea' => array('rows' => 10, 'cols' => 80));
	  $userColumns[] = array('title' => 'View PDF', 'call_back_fun' => array(&$this,'pdfColumn'));
	  
      $this->tableName = 'onCampus';
      $primaryCol = 'id';
      $errorFun = array(&$this,'logError');
      $permissions = 'OEAVDQCSMXHU';
	  $this->senderEmail = 'workstdy@uw.edu';
      
	  $records = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM $this->tableName");
	  $num_rows = mysqli_num_rows($records);
	  
	  $userButtons[] = array('label' => 'Email', 'call_back_fun' => array(&$this,'emailEdit'), 'no_update' => true, 'pass_id_array' => true);
	  
      require_once('php/AjaxTableEditor.php');
		$this->Editor = new AjaxTableEditor($this->tableName,$primaryCol,$errorFun,$permissions,$tableColumns, $this->senderEmail);
		$this->Editor->setConfig('tableInfo','cellpadding="1" width="1000" class="mateTable"');
		$this->Editor->setConfig('orderByColumn','id');
		$this->Editor->setConfig('ascOrDesc','desc');
		$this->Editor->setConfig('maxDispNum',$num_rows);
		$this->Editor->setConfig('userButtons', $userButtons);
		$this->Editor->setConfig('tableTitle', 'On Campus Jobs'); 
		$this->Editor->setConfig('userColumns',$userColumns);

   }  
   
	function emailEdit($idArr){
		$formHtml = '<form id="emailForm"><label for="to">To:</label><textarea name="to" id="to" rows="2" cols="60">';
		$idStr = implode(' OR id = ',$idArr);
		//$this->Editor->retArr[] = array('where' => 'javascript', 'value' => 'alert("'.$idStr.'")');
		$query = "select DISTINCT emailAddress from $this->tableName where id = $idStr";
		$result = $this->Editor->doQuery($query);
		$adresses = '';
		while($row = mysqli_fetch_assoc($result)){;
			$address = implode('',$row);
			$formHtml .= $address.', ';
			$adresses .= $address. ', ';
		}
		$formHtml .= '</textarea><br/><label for="from">From:</label><textarea name="from" id="from" rows="1" cols="60">'.$this->senderEmail.'</textarea><br/>
		<label for="subject">Subject:</label><textarea name="subject" id="subject" rows="1" cols="60"></textarea><br/>
		<label for="body">Body:</label><textarea name="body" id="body" rows="10" cols="60"></textarea><br/></form>
		<button id="emailSubmit" onclick="sendEmail();">Email</button>
		<button onclick="toAjaxTableEditor(\'update_html\',\'\');">'.$this->Editor->langVars->btnBack.'</button><br/>';
		$this->Editor->retArr[] = array('layer_id' => 'titleLayer', 'where' => 'innerHTML', 'value' => 'Email');
		$this->Editor->retArr[] = array('layer_id' => 'recordLayer', 'where' => 'innerHTML', 'value' => '');
		$this->Editor->retArr[] = array('layer_id' => 'tableLayer', 'where' => 'innerHTML', 'value' => $formHtml);
		$this->Editor->retArr[] = array('layer_id' => 'searchButtonsLayer', 'where' => 'innerHTML', 'value' => '');
		
	}
	
	function sanitizeMySQL($col,$val,$row){
			$val = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $val) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
			return $val;
	}
	
	function pdfColumn($tableColumns)
	{
		$html = '<td align="center"><a href="../report.php?id='. $tableColumns['id'] .'&table='. $this->tableName .'">PDF</a></td>';
        return $html;
	}
	
	function onCampus()
	{
		if(isset($_POST['json']))
		{
			session_start();
			// Initiating lang vars here is only necessary for the logError, and mysqlConnect functions in Common.php. 
			// If you are not using Common.php or you are using your own functions you can remove the following line of code.
			$this->langVars = new LangVars();
			$this->mysqlConnect();
			if(ini_get('magic_quotes_gpc'))
			{
				$_POST['json'] = stripslashes($_POST['json']);
			}
			if(function_exists('json_decode'))
			{
				$data = json_decode($_POST['json']);
			}
			else
			{
				require_once('php/JSON.php');
				$js = new Services_JSON();
				$data = $js->decode($_POST['json']);
			}
			if(empty($data->info) && strlen(trim($data->info)) == 0)
			{
				$data->info = '';
			}
			$this->initiateEditor();
			$this->Editor->main($data->action,$data->info);
			if(function_exists('json_encode'))
			{
				echo json_encode($this->Editor->retArr);
			}
			else
			{
				echo $js->encode($this->Editor->retArr);
			}
		}
		else if(isset($_GET['export']))
		{
            session_start();
            ob_start();
            $this->mysqlConnect();
            $this->initiateEditor();
            echo $this->Editor->exportInfo();
            header("Cache-Control: no-cache, must-revalidate");
            header("Pragma: no-cache");
            header("Content-type: application/x-msexcel");
            header('Content-Type: text/csv');
            header('Content-Disposition: attachment; filename="'.$this->Editor->tableName.'.csv"');
            exit();
        }
		else
		{
			$this->displayHeaderHtml();
			$this->displayHtml();
			$this->displayFooterHtml();
		}
	}
}
$lte = new onCampus();