<?php
	include_once("globals.php");
	include_once("mysql.php");
	include_once("FCKeditor/fckeditor.php");
	
	class Ticket
	
	{
		public $ID;
		public $created;
		public $owner;
		public $status;
		public $type;
		public $client;
		public $comment_id;
		public $subject;
		public $note;
		public $locked;
		public $hidden;
		
		private $link; //the mysql resource identifier
				
		function Ticket()
		{
			global $__database, $__host, $__password, $__user;
			$this->created = date("Ymd (H\:i)");
			$this->link = new mySQL($__host, $__user, $__password, $__database);
			$this->ID = md5($this->created);
			
			if (isset($_SESSION["userid"]))
			{
				$this->owner = $_SESSION["userid"];
			}
			
			/* echo "<p>New ticket: $this->created (ID: $this->ID)</p>\n"; */
		}
		
		function createEntry()
		{
			$this->link->query("INSERT INTO tickets SET ID=\"$this->ID\", client=$this->client, 
			owner=$this->owner, status=$this->status, type=$this->type, created=\"$this->created\", 
			note=\"$this->note\", subject=\"$this->subject\", locked=$this->locked");			
		}
	
		function updateEntry()
		{
			$this->link->query("UPDATE tickets SET client=$this->client, 
			owner=$this->owner, status=$this->status, type=$this->type, created=\"$this->created\", 
			note=\"$this->note\", subject=\"$this->subject\", locked=$this->locked, hidden=$this->hidden WHERE ID=\"$this->ID\"");			
		}
		
		function selectUser($fieldname)
		{
			$this->link->query("SELECT userid, name FROM users ORDER BY name ASC");
			echo "<select name=\"$fieldname\">\n";
			while($row=$this->link->getRow())
			{
				if ($this->owner == $row[0])
				{
					$selected = " selected=\"selected\"";
				}
				else 
				{
					$selected = "";
				}
				echo "\t<option value=\"$row[0]\"$selected>$row[1]</option>\n";
			}
			echo "</select>\n";
		}
		
		function selectStatus($fieldname)
		{
			$this->link->query("SELECT status, description FROM status_codes ORDER BY description DESC");
			echo "<select name=\"$fieldname\">\n";
			while($row=$this->link->getRow())
			{
				if ($this->status == $row[0])
				{
					$selected = " selected=\"selected\"";
				}
				else 
				{
					$selected = "";
				}
				echo "\t<option value=\"$row[0]\"$selected>$row[1]</option>\n";
			}
			echo "</select>\n";
		}
		
		function selectClient($fieldname)
		{
			$this->link->query("SELECT clientid, name FROM clients ORDER BY name ASC");
			echo "<select name=\"$fieldname\">\n";
			while($row=$this->link->getRow())
			{
				if ($this->client == $row[0])
				{
					$selected = " selected=\"selected\"";
				}
				else 
				{
					$selected = "";
				}
				echo "\t<option value=\"$row[0]\"$selected>$row[1]</option>\n";
			}
			echo "</select>\n";
		}
				
		function selectType($fieldname)
		{
			$this->link->query("SELECT type, description FROM type_codes ORDER BY type ASC");
			echo "<select name=\"$fieldname\">\n";
			while($row=$this->link->getRow())
			{
				if ($this->type == $row[0])
				{
					$selected = " selected=\"selected\"";
				}
				else 
				{
					$selected = "";
				}
				echo "\t<option value=\"$row[0]\"$selected>$row[1]</option>\n";
			}
			echo "</select>\n";
		}
		
		function editNote($fieldname)
		{
			$text="";
			$this->link->query("SELECT note FROM tickets WHERE ID=\"$this->ID\"");
			if ($this->link->result != null)
			{
				$row = $this->link->getRow();
				$text = $row[0];
			}
			/* $text = stripslashes(preg_replace('/<br\\s*?\/??>/i', '', $text)); 
			echo "<textarea cols=\"60\" rows=\"12\" name=\"$fieldname\">$text</textarea>\n"; */
			$editor = new FCKeditor($fieldname);
			$editor->BasePath = "/ticket/FCKeditor/";
			$editor->Value = $text;
			$editor->Create();	
		}
		
		function editSubject($fieldname)
		{
			$text="";
			$this->link->query("SELECT subject FROM tickets WHERE ID=\"$this->ID\"");
			if ($this->link->result != null)
			{
				$row = $this->link->getRow();
				$text = $row[0];
			}
			echo "<input type=\"text\" name=\"$fieldname\" value=\"$text\" size=\"60\" onkeypress=\"return event.keyCode!=13\">\n";			
		}
		
		function setFromForm()
		{
			/* Form elements _must_ be named the same as the class attributes to work */
			$this->ID = $_POST["ID"];
			$this->status = $_POST["status"];
			$this->owner = $_POST["owner"];
			$this->type = $_POST["type"];
			$this->client = $_POST["client"];
			$this->subject = $_POST["subject"];
			$this->note = ($_POST["note"]);
			$this->created = $_POST["created"];
			$this->locked = $_POST["locked"];
			$this->hidden = $_POST["hidden"];
		}
		
		function loadFromID($ID)
		{
			$this->link->query("SELECT * FROM tickets WHERE ID=\"$ID\"");
			$row = $this->link->getRow();
			$this->ID = $row["ID"];
			$this->owner = $row["owner"];
			$this->status = $row["status"];
			$this->type = $row["type"];
			$this->created = $row["created"];
			$this->comment_id = $row["comment_id"];
			$this->note = $row["note"];			
			$this->subject = $row["subject"];
			$this->client = $row["client"];
			$this->locked = $row["locked"];
			$this->hidden = $row["hidden"];
		}
		
		function getType()
		{
			$this->link->query("SELECT color, description FROM type_codes WHERE type=$this->type");
			$row = $this->link->getRow();
			return "<span style=\"color:$row[0]\">$row[1]</span>";
		}				
		function getStatus()
		{
			$this->link->query("SELECT color, description FROM status_codes WHERE status=$this->status");
			$row = $this->link->getRow();
			return "<span style=\"color:$row[0]\">$row[1]</span>";
		}	
		function getStatusClean()
		{
			$this->link->query("SELECT description FROM status_codes WHERE status=$this->status");
			$row = $this->link->getRow();
			return $row["description"];
		}	
		function getClient()
		{
			$this->link->query("SELECT name FROM clients WHERE clientid=$this->client");
			$row = $this->link->getRow();
			return $row[0];
		}		
		function getOwner()
		{
			$this->link->query("SELECT name FROM users WHERE userid=$this->owner");
			$row = $this->link->getRow();
			return $row[0];
		}
		
		function displayTicket()
		{
			$data = $this->getType();
			echo "Type: $data<br>";
			$data = $this->getClient();
			echo "Client: $data<br>";
			$data = $this->getStatus();
			echo "Status: $data<br>";
			$data = $this->getOwner();
			echo "Owner: $data<br>";
			echo "Note: $this->note<br>";
		}
		
		function insertIntoForum()
		{
			$comment_insert = "INSERT INTO sforum (post_id, title, text, email, date, name, clientid, status, type)"
			." VALUES (\"$this->ID\", \"$this->subject\", \"$this->note\", \"test\", now(), \"".$this->getOwner()."\", $this->client, \"".$this->status."\", \"".$this->type."\")";
			$this->link->query($comment_insert);
			$this->comment_id = mysql_insert_id();
			$this->link->query("UPDATE tickets SET comment_id=$this->comment_id WHERE ID=\"$this->ID\"");
			$this->link->query("UPDATE sforum SET topic_id=$this->comment_id WHERE post_id=\"$this->ID\"");			
		}
			
		function selectAttention($fieldname)
		{
			global $__database, $__host, $__password, $__user;
			$this->link->query("SELECT * FROM users ORDER BY name ASC");
			$count = 0;
			$tmplink = new mySQL($__host, $__user, $__password, $__database);
			while($row = $this->link->getRow())
			{
				$checked = "";
				if (mysql_num_rows($tmplink->query("SELECT user_id FROM flags WHERE ticket_id=\"$this->ID\" AND user_id=$row[0]"))) { $checked = "checked=\"checked\"";}
				echo "<input type=\"checkbox\" value=\"$row[0]\" name=\"$fieldname$count\" $checked>$row[1]\n";
				$count++;
			}
			unset($tmplink);
		}
		
		function showAlert($userid)
		{
			return mysql_num_rows($this->link->query("SELECT * FROM flags WHERE user_id=$userid AND ticket_id=\"$this->ID\""));
		}
		
		function getDate()
		{
			$d = $this->created;
			return substr($d, 4, 2)."/".substr($d, 6, 2)."/".substr($d, 2, 2)." (".substr($d, 10, 5).")";
		}
		
		function getLastPoster()
		{
			$query = "select name from sforum where id=(select max(id) from sforum where post_id=\"$this->ID\")";
			$this->link->query($query);
			$result = $this->link->getRow();
			return $result["name"];
		}
	
	}	
?>

syntax highlighted by Code2HTML, v. 0.9.1