Custom Requests

From FreeDESK
Revision as of 00:03, 22 August 2012 by Dave (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Custom request modules are the concrete classes which can be used for configured request types as the "concrete class method".

Requests classes are derived from the RequestBase class and this, along with the standard request implementation, are included below.

RequestBase class

<?php 
/* -------------------------------------------------------------
This file is part of FreeDESK
 
FreeDESK is (C) Copyright 2012 David Cutting
 
FreeDESK is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
 
FreeDESK is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with FreeDESK.  If not, see www.gnu.org/licenses
 
For more information see www.purplepixie.org/freedesk/
-------------------------------------------------------------- */
 
/**
 * Abstract Request base class for all request classes
**/
abstract class RequestBase
{
	/**
	 * Current FreeDESK instance
	**/
	protected $DESK = null;
 
	/**
	 * Current Request ID
	**/
	var $ID = 0;
 
	/**
	 * Entity Data
	**/
	protected $Entity = null;
 
	/**
	 * Updates
	**/
	protected $Updates = null;
 
	/**
	 * Constructor
	 * @param mixed &$freeDESK Current FreeDESK instance
	**/
	function RequestBase(&$freeDESK)
	{
		$this->DESK = &$freeDESK;
	}
 
	/**
	 * Create a request
	 * @param int $customer Customer ID
	 * @param string $update Initial Update
	 * @param int $class Request Class
	 * @param int $status Request status
	 * @param int $priority Priority Code (optional, default 0)
	 * @param int $group Request Group (optional, default 0)
	 * @param string $assign Assigned user (optional, default "")
	 * @return string Request ID
	**/
	abstract function Create($customer, $update, $class, $status, $priority=0, $group=0, $assign="");
 
	/**
	 * Update a request
	 * @param string $update Update text
	 * @param bool $public Public update (optional, default false)
	**/
	abstract function Update($update, $public=false);
 
	/**
	 * Change a request status
	 * @param int $status New status
	 * @param bool $public Public update (optional, default false)
	**/
	abstract function Status($status, $public=false);
 
	/**
	 * Assign a request
	 * @param int $group Group ID
	 * @param string $username Username (optional, default "")
	 * @param bool $public Public update (optional, default false)
	**/
	abstract function Assign($group, $username="", $public=false);
 
	/**
	 * Attach a file
	 * @param int $fileid File ID
	 * @param bool $public Public update (optional, default false)
	**/
	abstract function Attach($fileid, $public=false);
 
	/**
	 * Output XML
	 * @return string xml output
	 * @param bool $header Put XML header on output (optional, default false)
	**/
	abstract function XML($header=false);
 
	/**
	 * Set an entity value
	 * @param string $field Field ID
	 * @param string $value Value
	**/
	function Set($field, $value)
	{
		if ($this->Entity == null)
			$this->Entity = new Entity($this->DESK);
		$this->Entity->Set($field, $value);
	}
 
	/**
	 * Get an entity value
	 * @param string $field Field ID
	 * @return mixed Value
	**/
	function Get($field)
	{
		$data = $this->Entity->GetData();
		if (isset($data[$field]))
			return $data[$field];
		else
			return false;
	}
 
	/**
	 * Load updates for our ID
	**/
	function LoadUpdates()
	{
		$q="SELECT * FROM ".$this->DESK->Database->Table("update")." WHERE ";
		$q.=$this->DESK->Database->Field("requestid")."=".$this->DESK->Database->Safe($this->ID)." ";
		$q.="ORDER BY ".$this->DESK->Database->Field("updateid")." DESC";
 
		$this->Updates = array();
 
		$r=$this->DESK->Database->Query($q);
 
		while ($row=$this->DESK->Database->FetchAssoc($r))
			$this->Updates[] = $row;
 
		$this->DESK->Database->Free($r);
	}
 
	/**
	 * Get updates
	 * @return array Updates
	**/
	function GetUpdates()
	{
		if ($this->Updates == null)
			$this->LoadUpdates();
		return $this->Updates;
	}
 
}
?>

Inbuilt Standard Request Class

<?php 
/* -------------------------------------------------------------
This file is part of FreeDESK
 
FreeDESK is (C) Copyright 2012 David Cutting
 
FreeDESK is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
 
FreeDESK is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with FreeDESK.  If not, see www.gnu.org/licenses
 
For more information see www.purplepixie.org/freedesk/
-------------------------------------------------------------- */
 
/**
 * Request class for all standard requests
**/
class Request extends RequestBase
{
 
	/**
	 * Constructor
	 * @param mixed &$freeDESK Current FreeDESK instance
	**/
	function Request(&$freeDESK)
	{
		parent::RequestBase($freeDESK);
	}
 
	/**
	 * Create a request
	 * @param int $customer Customer ID
	 * @param string $update Initial Update
	 * @param int $class Request Class
	 * @param int $status Initial request status
	 * @param int $priority Priority code (optional, default 0)
	 * @param int $group Request Group (optional, default 0)
	 * @param string $assign Assigned user (optional, default "")
	 * @return string Request ID
	**/
	function Create($customer, $update, $class, $status, $priority=0, $group=0, $assign="")
	{
		if (!is_numeric($class))
			$class=1;
		$q="INSERT INTO ".$this->DESK->Database->Table("request");
		$q.="(".$this->DESK->Database->Field("customer").",";
		$q.=$this->DESK->Database->Field("assignteam").",";
		$q.=$this->DESK->Database->Field("assignuser").",";
		$q.=$this->DESK->Database->Field("class").",";
		$q.=$this->DESK->Database->Field("openeddt").",";
		$q.=$this->DESK->Database->Field("priority").",";
		$q.=$this->DESK->Database->Field("status").") ";
		$q.="VALUES(";
		$q.=$this->DESK->Database->Safe($customer).",";
		$q.=$this->DESK->Database->SafeQuote($group).",";
		$q.=$this->DESK->Database->SafeQuote($assign).",";
		$q.=$this->DESK->Database->SafeQuote($class).",";
		$q.="NOW(),";
		$q.=$this->DESK->Database->Safe($priority).",";
		$q.=$this->DESK->Database->Safe($status).")";
 
		$this->DESK->Database->Query($q);
 
		$this->ID = $this->DESK->Database->InsertID();
 
		$this->Update($update, true);
 
		return $this->ID;
	}
 
	/**
	 * Update a request
	 * @param string $update Update text
	 * @param bool $public Public update (optional, default false)
	**/
	function Update($update, $public=false)
	{
		if ($this->ID <= 0)
			return false;
		if ($this->DESK->ContextManager->Session == null)
			return false;
 
		$q="INSERT INTO ".$this->DESK->Database->Table("update")."(";
		$q.=$this->DESK->Database->Field("requestid").",";
		$q.=$this->DESK->Database->Field("update").",";
		$q.=$this->DESK->Database->Field("public").",";
		$q.=$this->DESK->Database->Field("updateby").",";
		$q.=$this->DESK->Database->Field("updatedt").") ";
		$q.="VALUES(";
		$q.=$this->DESK->Database->Safe($this->ID).",";
		$q.=$this->DESK->Database->SafeQuote($update).",";
		if ($public)
			$p=1;
		else
			$p=0;
		$q.=$p.",";
		$q.="\"".$this->DESK->ContextManager->Session->NiceName()."\",";
		$q.="NOW())";
 
		$this->DESK->Database->Query($q);
	}
 
	/**
	 * Change a request status
	 * @param int $status New status
	 * @param bool $public Public update (optional, default false)
	**/
	function Status($status, $public=false)
	{
		$list = $this->DESK->RequestManager->StatusList();
 
		if (isset($list[$status]))
		{
			$q="UPDATE ".$this->DESK->Database->Table("request")." SET ";
			$q.=$this->DESK->Database->Field("status")."=".$this->DESK->Database->Safe($status);
			if ($status == 0)
				$q.=",".$this->DESK->Database->Field("closeddt")."=NOW()";
			$q.=" WHERE ".$this->DESK->Database->Field("requestid")."=".$this->DESK->Database->Safe($this->ID);
			$this->DESK->Database->Query($q);
 
			$update="Status changed to ".$list[$status];
			$this->Update($update, $public);
		}
	}
 
	/**
	 * Assign a request
	 * @param int $group Group ID
	 * @param string $username Username (optional, default "")
	 * @param bool $public Public update (optional, default false)
	**/
	function Assign($group, $username="", $public=false)
	{
		$q="UPDATE ".$this->DESK->Database->Table("request")." SET ";
		$q.=$this->DESK->Database->Field("assignteam")."=".$this->DESK->Database->Safe($group).",";
		$q.=$this->DESK->Database->Field("assignuser")."=".$this->DESK->Database->SafeQuote($username)." WHERE ";
		$q.=$this->DESK->Database->Field("requestid")."=".$this->DESK->Database->Safe($this->ID);
		$this->DESK->Database->Query($q);
 
		$teams = $this->DESK->RequestManager->TeamList();
		$users = $this->DESK->RequestManager->UserList();
		$assign = "";
 
		if (isset($teams[$group]))
			$assign.=$teams[$group];
		else if ($group == 0)
			$assign.=$this->DESK->Lang->Get("unassigned");
 
		if ($username != "" && isset($users[$username]))
		{
			if ($assign != "")
				$assign.=" &gt; ";
			$assign.=$users[$username];
		}
 
		$update = $this->DESK->Lang->Get("assigned_to")." ".$assign;
 
		$this->Update($update, $public);
	}
 
	/**
	 * Attach a file
	 * @param int $fileid File ID
	 * @param bool $public Public update (optional, default false)
	**/
	function Attach($fileid, $public=false)
	{
		//
	}
 
	/**
	 * Output XML
	 * @return string xml output
	 * @param bool $header Put XML header on output (optional, default false)
	**/
	function XML($header=false)
	{
		$data = $this->Entity->GetData();
		$xml = new xmlCreate();
		$xml->startElement("request");
		$xml->xmlArray($data);
		$xml->endElement("request");
		return $xml->getXML($header);
	}
 
 
 
}
?>
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox