<?php
session_start();
$admin = 'contact'; 
global $ROOT, $PATH, $phpself;
$ROOT = $_SERVER['DOCUMENT_ROOT'];
$PATH = '../../';  
$phpself = basename(__FILE__);
require_once($PATH . 'globals/globals.php');
$adminAct = $_REQUEST['adminType']; 
$admin = 'contact';
$updateString = ''; 
$udID = ''; 

$fieldArray = array('contactID','lastname','firstname','company','title','email','phone_office','phone_mobile','fax','website','address1','address2','PObox','city','state','zip','contact_source','last_contact','last_contact_campaign','notes','isClient','date_added','date_updated','display');
$excludeArray = array('last_contact','date_added','date_updated');

if (!empty($_POST['op'])) {
	$op = $_POST['op'];
	
	if ($op == 'delete') {
		$editID = $_POST['editID'];
		$query = 'UPDATE marketing_contacts SET display=0 WHERE contactID='. $editID;
		$result = $con->query($query);
	} else {	
		$fieldString = '';
		$valueString = '';
		foreach($_POST as $key => $value) {
			if (in_array($key,$fieldArray)) {
				// Do Not Include in query
				if ($key == 'display') {
					if ($op == "delete") {
						$fieldString .= 'display,';
						$valueString .= '0,';
					} else  {
						if ($op == "react") {
							$fieldString .= 'display,';
							$valueString .= '1,';
						} else {
							$fieldString .= 'display,';
							$valueString .= $_POST['display'] .",";
						}
					}
				} elseif ($key == 'isClient') {
					$fieldString .= 'isCLient,';
					if (!empty($_REQUEST['create'])) 
						$valueString .= '1,';
					else
						$valueString .= "'". $value ."',";
				} else {
					$fieldString .= $key .',';
					if(in_array($key,$excludeArray))
						$valueString .= "'". $value ."',";
					else
						$valueString .= "'". sanitize_html_string($value) ."',";
				}
			}
		}
		
		// Remove Last comma from string to avoid mySQL errors
		$fieldString = substr($fieldString,0,strlen($fieldString)-1);
		$valueString = substr($valueString,0,strlen($valueString)-1);
			
		$query = 'REPLACE INTO marketing_contacts ('. $fieldString .') VALUES ('. $valueString .')';
		$result = $con->query($query);
		$editID = (!empty($udID) ? $udID : $con->insert_id); 
		
		if (!empty($_REQUEST['create'])) {
			$query = "INSERT INTO clients (clientID,subClientOf,clientName,clientAbbr,clientWebsite,ratePercent,rateHourly,currentRateID,rateHistory,clientContactName,clientContactEmail,clientContactPhone,clientContactMobile,clientContactFax,clientAddressStreet,clientAddress2,clientAddressPO,clientAddressCity,clientState,clientAddressZip,clientSince,clientStatus,clientLastUpdate,clientNotes) VALUES ('',130,'". $_REQUEST['company'] ."','','". $_REQUEST['website'] ."','','','','','". $_REQUEST['firstname'] ." ". $_REQUEST['lastname'] ."','". $_REQUEST['email'] ."','". $_REQUEST['phone_office'] ."','". $_REQUEST['phone_mobile'] ."','". $_REQUEST['fax'] ."','". $_REQUEST['address1'] ."','". $_REQUEST['address2'] ."','". $_REQUEST['PObox'] ."','". $_REQUEST['city'] ."','". $_REQUEST['state'] ."','". $_REQUEST['zip'] ."','". $today ."',2,'". $today ."','". $_REQUEST['notes'] ."')";
			$result = $con->query($query);
			$editID = $con->insert_id; 
		} 
	}
	$updateID = (($page == 'dash') ? 'contact-'. $editID : 'record-'. $editID);
	$output = (($result) ? array('update'=>$updateID,'updateOP'=>$op,'updateString'=>$updateString) : 'UPDATE ERROR');
} else {
	$output = 'ERROR'; 	
}

if ($con) $con->close(); 

header('Content-Type: application/json; charset=utf-8', true); 
echo json_encode(array("output"=>$output));

?>