<?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');


$test = new Common();
$test->mysqlConnect();

require_once('php/email.php');




class arList 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>
			
			<!-- BL: this hidden input is to store the email address of the  selected row. 
			This will only work if the user is trying to edit one row. With multiple rows, only one email would be sent out. 
			<form id="emailForm" method="GET" action="ar_list.php">
				<input type="hidden" id="cbHolder" name="cbHolder" value="#">
				<input type="hidden" id="emailHolder" name="emailHolder" value="#">
			</form>
			-->
			
			<script type="text/javascript">
				trackHistory = false;
				var ajaxUrl = '<?php echo $_SERVER['PHP_SELF']; ?>';
				toAjaxTableEditor('update_html','');
				
				/*
				// BL: Script to listen to all row checkboxes in order to save the email address of the selected row. 
				
				
				var loaded = false;
				var editId = "";
				var editEmail = "";
				new PeriodicalExecuter(function(pe) {
					if(loaded == true){
						pe.stop();
					} else {
						var allRows = $$('.rowCheckBox');
						if(allRows.length > 0) {
							loaded = true;
							for(var i = 0; i < allRows.length; i++) {				
								allRows[i].observe('click', function(event){									
									var sId = this.id;
									var row = 'row_' + sId.charAt(3);
									editEmail = $$('#' + row + ' td')[13].innerHTML;
									editId = this.value;								
								});
							}							
						}	
							//$('emailSubmit').writeAttribute('onclick', "emailUpdates('" + $('emailAddress') + "'," + $('id') + ",'americaReads');");
					}
				}, .2);
				
				new PeriodicalExecuter(function(pe) {
					if($('emailSubmit') != null) {
						$('emailSubmit').writeAttribute('onclick', "emailUpdates('" + editEmail + "'," + editId + ",'americaReads');");
						pe.stop();
					}
				}, .2);
				*/
			</script>
		<?php
	}
      
	  function initiateEditor()
	  {
	  $tableColumns['id'] = array('display_text' => 'ID', 'perms' => 'EVCTAXQSHO');
	  $tableColumns['jobTitle'] = array('display_text' => 'Job Title', 'perms' => 'EVCTAXQSHO');
	  $tableColumns['formType'] = array('display_text' => 'Job Type', 'perms' => 'EVCTAXQSHO', 'select_array' => array('Public' => 'Public', 'Non-profit' => 'Non-profit'));	  
	  $tableColumns['schoolType'] = array('display_text' => 'School Type', 'perms' => 'EVCTAXQSHO', 'select_array' => array('Elementary' => 'Elementary', 'Middle' => 'Middle', 'K-8' => 'K-8'));
	  $tableColumns['schoolName'] = array('display_text' => 'School Name', 'perms' => 'EVCTAXQSHO');
	  $tableColumns['schoolDistrict'] = array('display_text' => 'School District', 'perms' => 'EVCTAXQSHO');
	  $tableColumns['organization'] = array('display_text' => 'Organization', 'perms' => 'EVCTAXQSHO');
	  $tableColumns['payRate'] = array('display_text' => 'Pay Rate', 'perms' => 'EVCTAXQSHO');
	  $tableColumns['website'] = array('display_text' => 'Web Site', 'perms' => 'EVCTAXQSHO');	  
	  $tableColumns['contactFName'] = array('display_text' => 'First Name', 'perms' => 'EVCTAXQSHO');
	  $tableColumns['contactLName'] = array('display_text' => 'Last Name', 'perms' => 'EVCTAXQSHO');
	  $tableColumns['emailAddress'] = array('display_text' => 'Email', 'perms' => 'EVCTAXQSHO');
	  $tableColumns['timestamp'] = array('display_text' => 'Last Modified', 'perms' => 'EVCTAXQSHO');
	  
	  $tableColumns['availability'] = array('display_text' => 'Availability', 'perms' => 'EVCAXQSH', 'textarea' => array('rows' => 10, 'cols' => 80));
	  $tableColumns['duties'] = array('display_text' => 'Duties', 'perms' => 'EVCAXQSH', 'textarea' => array('rows' => 10, 'cols' => 80));
	  $tableColumns['qualifications'] = array('display_text' => 'Qualifications', 'perms' => 'EVCAXQSH', 'textarea' => array('rows' => 10, 'cols' => 80));
	  $tableColumns['benefits'] = array('display_text' => 'Benefits', 'perms' => 'EVCAXQSH', 'textarea' => array('rows' => 10, 'cols' => 80));
	  $tableColumns['additional'] = array('display_text' => 'Additional', 'perms' => 'EVCAXQSH', 'textarea' => array('rows' => 10, 'cols' => 80));
	  
	  $tableColumns['jobNum'] = array('display_text' => 'Job Number', 'perms' => 'EVCTAXQSHO');
	  $tableColumns['status'] = array('display_text' => 'Status', 'perms' => 'EVCTAXQSHO', 'select_array' => array('Open' => 'Open', 'Closed' => 'Closed'));
	  $tableColumns['approved'] = array('display_text' => 'Approved', 'perms' => 'EVCTAXQSHO', 'select_array' => array('No' => 'No', 'Yes' => 'Yes'));
	  
	  $userColumns[] = array('title' => 'View PDF', 'call_back_fun' => array(&$this,'pdfColumn'));

	  
      $this->tableName = 'americaReads';
      $primaryCol = 'id';
      $errorFun = array(&$this,'logError');
      $permissions = 'EAVDQCSMXHUO';
	  //BL: need to be able to pass the "emailAddress field." 
	  // arac@u.washington.edu should be default 
	  $this->senderEmail = "blee429@gmail.com";

	  
	  $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', 'American Reads 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="../ar_report.php?id='. $tableColumns['id'] .'&table='. $this->tableName .'">PDF</a></td>';
        return $html;
	}
   
	function arList()
	{
		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 arList();
