Completed Code
To achieve this end, we'll need to tell MySql to retrieve the message table, join it with the ticket table, create a function to retrieve the message from the table, then make a new entry into the $search and $replace arrays. Sound like fun? Lets do it.
includes/class.ticket.php
Find this line (66 or so)...
$sql =' SELECT ticket.*,topic.topic_id as topicId,lock_id,dept_name,priority_desc FROM '.TICKET_TABLE.' ticket '.
replace it with this line...
$sql =' SELECT ticket.*,message.*,topic.topic_id as topicId,lock_id,dept_name,priority_desc FROM '.TICKET_TABLE.' ticket '.
Here we have added in *.message. This says get everything from the message table.
Now find this line (71 or so)...
' LEFT JOIN '.TICKET_LOCK_TABLE.' tlock ON ticket.ticket_id=tlock.ticket_id AND tlock.expire>NOW() '.
Directly below that, on the next line add...
' LEFT JOIN '.TICKET_MESSAGE_TABLE.' message ON ticket.ticket_id=message.ticket_id '.
On this part we "LEFT JOINed" the message table with the ticket table. Left join means to combine whatever table we are requesting (messages), with the one that was requested before it (tickets), using a common column (ticket_id) to pair up the entries.
We now add the function to get the message from the table..
Around line 80 or so add this...
$this->message =$row;
and a function call to actually return the data...
around line 165 or so add this...
function getMessage(){
return $this->message;
}
Now, let's take a look at the base variables replacement function.
function replaceTemplateVars($text){
global $cfg;
$dept = $this->getDept();
$staff= $this->getStaff();
$search = array('/%id/','/%ticket/','/%email/','/%name/','/%subject/','/%topic/','/%phone/','/%status/','/%priority/','/%dept/','/%assigned_staff/','/%createdate/','/%duedate/','/%closedate/','/%url/');
$replace = array($this->getId(),
$this->getExtId(),
$this->getEmail(),
$this->getName(),
$this->getSubject(),
$this->getHelpTopic(),
$this->getPhone(),
$this->getStatus(),
$this->getPriority(),
($dept?$dept->getName():''),
($staff?$staff->getName():''),
Format:($this->getCreateDate()),
Format:($this->getDueDate()),
Format:($this->getCloseDate()),
$cfg->getBaseUrl());
return preg_replace($search,$replace,$text);
}
We can see that we have two arrays within the function. $search and $replace. The first item in the $search array is '/%id/' and the first item in the replace array is $this->getId(). Basically it says if you find %id, run the function $this->getId() and replace %id with the result of the function.
Now, pay very close attention to the order of the items in the $search and $replace arrays. These two depend on each other, and many, many other functions depend on them both being in the proper order. This is the nature of paired arrays.
I cannot stress enough that mismatched pairs in the search and replace arrays will break the hell out of your osTicket.
That being said, let's carry on.
We can now clearly see that %message is nowhere to be found inside of our function. That is because at most times (as mentioned on the Emails->Templates Page) the usage depends on the context in question. Eff a bunch of context. We want it ALL THE TIME and were going to get it.
Replace the entire function with this code....
function replaceTemplateVars($text){
global $cfg;
$dept = $this->getDept();
$staff= $this->getStaff();
$search = array('/%id/','/%ticket/','/%email/','/%name/','/%subject/','/%topic/','/%phone/','/%status/','/%priority/','/%dept/','/%assigned_staff/','/%createdate/','/%duedate/','/%closedate/','/%url/','/%message/');
$replace = array($this->getId(),
$this->getExtId(),
$this->getEmail(),
$this->getName(),
$this->getSubject(),
$this->getHelpTopic(),
$this->getPhone(),
$this->getStatus(),
$this->getPriority(),
($dept?$dept->getName():''),
($staff?$staff->getName():''),
Format:($this->getCreateDate()),
Format:($this->getDueDate()),
Format:($this->getCloseDate()),
$cfg->getBaseUrl(),
$this->getMessage());
return preg_replace($search,$replace,$text);
}
Note that $cfg->getBaseUrl() is now followed by a comma, and $this->getMessage()); gets the closing parenthesis and the semi-colon.
Also note that '/%message/' is at the END of the $search array and we call $this->getMessage() at the END of the $replace array.
You should now have access to %message in pre-made replies.
: