<?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 offCampus extends Common
{
	var $Editor;
	private $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['jobTitle'] = array('display_text' => 'Job Title', 'perms' => 'EVCTAXQSHO');
      $tableColumns['orgName'] = array('display_text' => 'Department', 'perms' => 'EVCTAXQSHO');
      $tableColumns['contactFName'] = array('display_text' => 'First Name', 'perms' => 'EVCTAXQSHO');
      $tableColumns['contactLName'] = array('display_text' => 'Last Name', 'perms' => 'EVCTAXQSHO');
      $tableColumns['timeStamp'] = array('display_text' => 'Last Modified', 'perms' => 'EVCTAXQSHO'); 
      $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['ein'] = array('display_text' => 'EIN', 'perms' => 'EVCAXQSH');
      $tableColumns['street'] = array('display_text' => 'Street', 'perms' => 'EVCAXQSH');
      $tableColumns['city'] = array('display_text' => 'City', 'perms' => 'EVCAXQSH');
      $tableColumns['state'] = array('display_text' => 'State', 'perms' => 'EVCAXQSH');
      $tableColumns['zip'] = array('display_text' => 'Zip', '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));
      $tableColumns['jobNumber'] = array('display_text' => 'Job Number', 'perms' => 'EVCTAXQSHO');
	  $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['program'] = array('display_text' => 'Program', 'perms' => 'EVCAXQSH', 'select_array' => array('Federal' => 'Federal', 'State' => 'State'));
	  $tableColumns['reimburseRate'] = array('display_text' => 'Reimbursement Rate', 'perms' => 'EVCAXQSH');
	  $userColumns[] = array('title' => 'View PDF', 'call_back_fun' => array(&$this,'pdfColumn'));

	  
      $this->tableName = 'offCampus';
      $primaryCol = 'id';
      $errorFun = array(&$this,'logError');
      $permissions = 'EAVDQCSMXHUO';
      $this->senderEmail = 'uworkstu@uw.edu';
	  
	  $records = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM $this->tableName");
	  $num_rows = mysqli_num_rows($records);
	  
	$userButtons[] = array('label' => '<span>Email</span>', '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="800" class="mateTable"');
		$this->Editor->setConfig('orderByColumn','id');
		$this->Editor->setConfig('maxDispNum',$num_rows);
		$this->Editor->setConfig('userButtons', $userButtons);
		$this->Editor->setConfig('tableTitle', 'Off 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>';
		$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 pdfColumn($tableColumns)
	{
        //$html = '<td align="center"><a href="curlTest.php?id=' . $tableColumns['ID'] . '">PDF</td>';
		$html = '<td align="center"><a href="../report.php?id='. $tableColumns['id'] .'&table='. $this->tableName .'">PDF</a></td>';
        return $html;
	}
   
	function offCampus()
	{
		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 offCampus();
