Permalink
Browse files

Merge ticket thread tables into one (1 of 2)

Replace the ticket_{message,response,note} tables with a single
ticket_thread table that will contain data for all three current message
types. This simplifies much of the ticket thread code and paves the way for
other types of messages in the future.
  • Loading branch information...
1 parent a3b6e39 commit 1899a6c624204f606ad8d18d97fbc355cc0c65f7 @greezybacon greezybacon committed Apr 9, 2012
View
@@ -75,18 +75,25 @@ function load($id=0) {
$sql='SELECT ticket.*, topic.topic as helptopic, lock_id, dept_name, priority_desc '
.' ,count(attach.attach_id) as attachments '
- .' ,count(DISTINCT message.msg_id) as messages '
- .' ,count(DISTINCT response.response_id) as responses '
- .' ,count(DISTINCT note.note_id) as notes '
+ .' ,count(DISTINCT message.id) as messages '
+ .' ,count(DISTINCT response.id) as responses '
+ .' ,count(DISTINCT note.id) as notes '
.' FROM '.TICKET_TABLE.' ticket '
.' LEFT JOIN '.DEPT_TABLE.' dept ON (ticket.dept_id=dept.dept_id) '
- .' LEFT JOIN '.TICKET_PRIORITY_TABLE.' pri ON (ticket.priority_id=pri.priority_id) '
- .' LEFT JOIN '.TOPIC_TABLE.' topic ON (ticket.topic_id=topic.topic_id) '
- .' LEFT JOIN '.TICKET_LOCK_TABLE.' tlock ON (ticket.ticket_id=tlock.ticket_id AND tlock.expire>NOW()) '
- .' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach ON (ticket.ticket_id=attach.ticket_id) '
- .' LEFT JOIN '.TICKET_MESSAGE_TABLE.' message ON (ticket.ticket_id=message.ticket_id) '
- .' LEFT JOIN '.TICKET_RESPONSE_TABLE.' response ON (ticket.ticket_id=response.ticket_id) '
- .' LEFT JOIN '.TICKET_NOTE_TABLE.' note ON (ticket.ticket_id=note.ticket_id ) '
+ .' LEFT JOIN '.TICKET_PRIORITY_TABLE.' pri ON ('
+ .'ticket.priority_id=pri.priority_id) '
+ .' LEFT JOIN '.TOPIC_TABLE.' topic ON ('
+ .'ticket.topic_id=topic.topic_id) '
+ .' LEFT JOIN '.TICKET_LOCK_TABLE.' tlock ON ('
+ .'ticket.ticket_id=tlock.ticket_id AND tlock.expire>NOW()) '
+ .' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach ON ('
+ .'ticket.ticket_id=attach.ticket_id) '
+ .' LEFT JOIN '.TICKET_THREAD_TABLE.' message ON ('
+ ."ticket.ticket_id=message.ticket_id AND message.thread_type = 'M') "
+ .' LEFT JOIN '.TICKET_THREAD_TABLE.' response ON ('
+ ."ticket.ticket_id=response.ticket_id AND response.thread_type = 'R') "
+ .' LEFT JOIN '.TICKET_THREAD_TABLE.' note ON ( '
+ ."ticket.ticket_id=note.ticket_id AND note.thread_type = 'N') "
.' WHERE ticket.ticket_id='.db_input($id)
.' GROUP BY ticket.ticket_id';
@@ -436,9 +443,10 @@ function getSLA() {
function getLastRespondent() {
$sql ='SELECT resp.staff_id '
- .' FROM '.TICKET_RESPONSE_TABLE.' resp '
+ .' FROM '.TICKET_THREAD_TABLE.' resp '
.' LEFT JOIN '.STAFF_TABLE. ' USING(staff_id) '
.' WHERE resp.ticket_id='.db_input($this->getId()).' AND resp.staff_id>0 '
+ .' AND resp.thread_type="R"'
.' ORDER BY resp.created DESC LIMIT 1';
if(!($res=db_query($sql)) || !db_num_rows($res))
@@ -456,8 +464,9 @@ function getLastMessageDate() {
return $this->lastmsgdate;
//for old versions...XXX: still needed????
- $sql='SELECT created FROM '.TICKET_MESSAGE_TABLE
+ $sql='SELECT created FROM '.TICKET_THREAD_TABLE
.' WHERE ticket_id='.db_input($this->getId())
+ ." AND thread_type = 'M'"
.' ORDER BY created DESC LIMIT 1';
if(($res=db_query($sql)) && db_num_rows($res))
list($this->lastmsgdate)=db_fetch_row($res);
@@ -474,8 +483,9 @@ function getLastResponseDate() {
if($this->lastrespdate)
return $this->lastrespdate;
- $sql='SELECT created FROM '.TICKET_RESPONSE_TABLE
+ $sql='SELECT created FROM '.TICKET_THREAD_TABLE
.' WHERE ticket_id='.db_input($this->getId())
+ .' AND thread_type="R"'
.' ORDER BY created DESC LIMIT 1';
if(($res=db_query($sql)) && db_num_rows($res))
list($this->lastrespdate)=db_fetch_row($res);
@@ -522,11 +532,12 @@ function getNotes($order='') {
$order='DESC';
$sql ='SELECT note.*, count(DISTINCT attach.attach_id) as attachments '
- .' FROM '.TICKET_NOTE_TABLE.' note '
+ .' FROM '.TICKET_THREAD_TABLE.' note '
.' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach
- ON (note.ticket_id=attach.ticket_id AND note.note_id=attach.ref_id AND ref_type="N") '
+ ON (note.ticket_id=attach.ticket_id AND note.id=attach.ref_id AND ref_type="N") '
.' WHERE note.ticket_id='.db_input($this->getId())
- .' GROUP BY note.note_id '
+ .' AND note.thread_type="N"'
+ .' GROUP BY note.id '
.' ORDER BY note.created '.$order;
$notes=array();
@@ -539,14 +550,17 @@ function getNotes($order='') {
function getMessages() {
- $sql='SELECT msg.msg_id, msg.created, msg.message '
- .' ,count(DISTINCT attach.attach_id) as attachments, count( DISTINCT resp.response_id) as responses '
- .' FROM '.TICKET_MESSAGE_TABLE.' msg '
- .' LEFT JOIN '.TICKET_RESPONSE_TABLE. ' resp ON(resp.msg_id=msg.msg_id) '
- .' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach
- ON (msg.ticket_id=attach.ticket_id AND msg.msg_id=attach.ref_id AND ref_type="M") '
+ $sql='SELECT msg.id, msg.created, msg.body '
+ .' ,count(DISTINCT attach.attach_id) as attachments '
+ .' ,count( DISTINCT resp.id) as responses '
+ .' FROM '.TICKET_THREAD_TABLE.' msg '
+ .' LEFT JOIN '.TICKET_THREAD_TABLE.' resp ON ('
+ .'resp.pid=msg.id AND resp.thread_type = "R") '
+ .' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach '
+ .'ON (msg.ticket_id=attach.ticket_id AND msg.id=attach.ref_id AND ref_type="M") '
.' WHERE msg.ticket_id='.db_input($this->getId())
- .' GROUP BY msg.msg_id '
+ .' AND msg.thread_type="M"'
+ .' GROUP BY msg.id '
.' ORDER BY msg.created ASC ';
$messages=array();
@@ -560,11 +574,12 @@ function getMessages() {
function getResponses($msgId) {
$sql='SELECT resp.*, count(DISTINCT attach.attach_id) as attachments '
- .' FROM '.TICKET_RESPONSE_TABLE. ' resp '
+ .' FROM '.TICKET_THREAD_TABLE. ' resp '
.' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach
- ON (resp.ticket_id=attach.ticket_id AND resp.response_id=attach.ref_id AND ref_type="R") '
+ ON (resp.ticket_id=attach.ticket_id AND resp.id=attach.ref_id AND ref_type="R") '
.' WHERE resp.ticket_id='.db_input($this->getId())
- .' GROUP BY resp.response_id '
+ .' AND resp.thread_type="R"'
+ .' GROUP BY resp.id '
.' ORDER BY resp.created';
$responses=array();
@@ -1272,25 +1287,34 @@ function release() {
}
//Insert message from client
- function postMessage($msg,$source='',$msgid=NULL,$headers='',$newticket=false){
+ function postMessage($msg,$source='',$emsgid=null,$headers='',$newticket=false){
global $cfg;
if(!$this->getId()) return 0;
# XXX: Refuse auto-response messages? (via email) XXX: No - but kill our auto-responder.
- $sql='INSERT INTO '.TICKET_MESSAGE_TABLE.' SET created=NOW() '
+ $sql='INSERT INTO '.TICKET_THREAD_TABLE.' SET created=NOW()'
+ .' ,thread_type="M" '
.' ,ticket_id='.db_input($this->getId())
- .' ,messageId='.db_input($msgid)
- .' ,message='.db_input(Format::striptags($msg)) //Tags/code stripped...meaning client can not send in code..etc
- .' ,headers='.db_input($headers) //Raw header.
+ # XXX: Put Subject header into the 'title' field
+ .' ,body='.db_input(Format::striptags($msg)) //Tags/code stripped...meaning client can not send in code..etc
.' ,source='.db_input($source?$source:$_SERVER['REMOTE_ADDR'])
.' ,ip_address='.db_input($_SERVER['REMOTE_ADDR']);
if(!db_query($sql) || !($msgid=db_insert_id())) return 0; //bail out....
$this->setLastMsgId($msgid);
+ if ($emsgid !== null) {
+ $sql='INSERT INTO '.TICKET_EMAIL_INFO_TABLE
+ .' SET msg_id='.db_input($msgid)
+ .', email_mid='.db_input($emsgid)
+ .', headers='.db_input($headers);
+
+ if (!db_query($sql)) return 0;
+ }
+
if($newticket) return $msgid; //Our work is done...
$autorespond = true;
@@ -1355,10 +1379,11 @@ function postReply($vars, $files, $errors, $alert = true) {
if($errors) return 0;
- $sql='INSERT INTO '.TICKET_RESPONSE_TABLE.' SET created=NOW() '
+ $sql='INSERT INTO '.TICKET_THREAD_TABLE.' SET created=NOW() '
+ .' ,thread_type="R"'
.' ,ticket_id='.db_input($this->getId())
- .' ,msg_id='.db_input($vars['msgId'])
- .' ,response='.db_input(Format::striptags($vars['response']))
+ .' ,pid='.db_input($vars['msgId'])
+ .' ,body='.db_input(Format::striptags($vars['response']))
.' ,staff_id='.db_input($thisstaff->getId())
.' ,staff_name='.db_input($thisstaff->getName())
.' ,ip_address='.db_input($thisstaff->getIP());
@@ -1432,7 +1457,7 @@ function logActivity($title,$note){
if(!$cfg || !$cfg->logTicketActivity())
return 0;
- return $this->postNote($title,$note,false,'system');
+ return $this->postNote($title,$note,false,'System');
}
// History log -- used for statistics generation (pretty reports)
@@ -1459,12 +1484,13 @@ function logEvent($state, $staff=null) {
function postNote($title,$note,$alert=true,$poster='') {
global $thisstaff,$cfg;
- $sql= 'INSERT INTO '.TICKET_NOTE_TABLE.' SET created=NOW() '.
+ $sql= 'INSERT INTO '.TICKET_THREAD_TABLE.' SET created=NOW() '.
+ ',thread_type="N"'.
',ticket_id='.db_input($this->getId()).
',title='.db_input(Format::striptags($title)).
- ',note='.db_input(Format::striptags($note)).
+ ',body='.db_input(Format::striptags($note)).
',staff_id='.db_input($thisstaff?$thisstaff->getId():0).
- ',source='.db_input(($poster || !$thisstaff)?$poster:$thisstaff->getName());
+ ',poster='.db_input(($poster || !$thisstaff)?$poster:$thisstaff->getName());
//echo $sql;
if(!db_query($sql) || !($id=db_insert_id()))
return false;
@@ -1566,9 +1592,7 @@ function delete(){
if(!db_query($sql) || !db_affected_rows())
return false;
- db_query('DELETE FROM '.TICKET_MESSAGE_TABLE.' WHERE ticket_id='.db_input($this->getId()));
- db_query('DELETE FROM '.TICKET_RESPONSE_TABLE.' WHERE ticket_id='.db_input($this->getId()));
- db_query('DELETE FROM '.TICKET_NOTE_TABLE.' WHERE ticket_id='.db_input($this->getId()));
+ db_query('DELETE FROM '.TICKET_THREAD_TABLE.' WHERE ticket_id='.db_input($this->getId()));
$this->deleteAttachments();
return true;
@@ -1684,8 +1708,9 @@ function getIdByMessageId($mid,$email) {
return 0;
$sql='SELECT ticket.ticket_id FROM '.TICKET_TABLE. ' ticket '.
- ' LEFT JOIN '.TICKET_MESSAGE_TABLE.' msg USING(ticket_id) '.
- ' WHERE messageId='.db_input($mid).' AND email='.db_input($email);
+ ' LEFT JOIN '.TICKE_THREAD_TABLE.' msg USING(ticket_id) '.
+ ' INNER JOIN '.TICKET_EMAIL_INFO_TABLE.' emsg ON (msg.id = emsg.message_id) '.
+ ' WHERE email_mid='.db_input($mid).' AND email='.db_input($email);
$id=0;
if(($res=db_query($sql)) && db_num_rows($res))
list($id)=db_fetch_row($res);
@@ -59,13 +59,12 @@
$queryterm=db_real_escape($_REQUEST['q'],false); //escape the term ONLY...no quotes.
$qwhere.=' AND ( '
." ticket.subject LIKE '%$queryterm%'"
- ." OR message.message LIKE '%$queryterm%'"
- ." OR response.response LIKE '%$queryterm%'"
+ ." OR thread.body LIKE '%$queryterm%'"
.' ) ';
$deep_search=true;
//Joins needed for search
- $qfrom.=' LEFT JOIN '.TICKET_MESSAGE_TABLE.' message ON (ticket.ticket_id=message.ticket_id )'
- .' LEFT JOIN '.TICKET_RESPONSE_TABLE.' response ON (ticket.ticket_id=response.ticket_id )';
+ $qfrom.=' LEFT JOIN '.TICKET_THREAD_TABLE.' thread ON ('
+ .'ticket.ticket_id=thread.ticket_id AND thread.thread_type IN ("M","R"))';
}
}
@@ -67,11 +67,11 @@
<tr><th><?php echo Format::db_datetime($message['created']); ?></th></tr>
- <tr><td><?php echo Format::display($message['message']); ?></td></tr>
+ <tr><td><?php echo Format::display($message['body']); ?></td></tr>
<?php
- if($message['attachments'] && ($links=$ticket->getAttachmentsLinks($message['msg_id'],'M'))) { ?>
+ if($message['attachments'] && ($links=$ticket->getAttachmentsLinks($message['id'],'M'))) { ?>
<tr><td class="info"><?php echo $links; ?></td></tr>
@@ -81,17 +81,17 @@
</table>
<?php
- if($message['responses'] && ($responses=$ticket->getResponses($message['msg_id']))) {
+ if($message['responses'] && ($responses=$ticket->getResponses($message['id']))) {
foreach($responses as $resp) {
$staff=$cfg->hideStaffName()?'staff':Format::htmlchars($resp['staff_name']);
?>
<table class="response" cellspacing="0" cellpadding="1" width="100%" border="0">
<tr>
<th><?php echo Format::db_datetime($resp['created']);?>&nbsp;-&nbsp;<?php echo $staff; ?></th>
</tr>
- <tr><td><?php echo Format::display($resp['response']); ?></td></tr>
+ <tr><td><?php echo Format::display($resp['body']); ?></td></tr>
<?php
- if($resp['attachments'] && ($links=$ticket->getAttachmentsLinks($resp['response_id'],'R'))) {?>
+ if($resp['attachments'] && ($links=$ticket->getAttachmentsLinks($resp['id'],'R'))) {?>
<tr><td class="info"><?php echo $links; ?></td></tr>
<?php
}?>
@@ -196,11 +196,11 @@
</tr>
<tr>
<td colspan="2">
- <?php echo Format::htmlchars($note['note']); ?>
+ <?php echo Format::htmlchars($note['body']); ?>
</td>
</tr>
<?php
- if($note['attachments'] && ($links=$ticket->getAttachmentsLinks($note['note_id'],'N'))) {?>
+ if($note['attachments'] && ($links=$ticket->getAttachmentsLinks($note['id'],'N'))) {?>
<tr>
<td class="info" colspan="2"><?php echo $links; ?></td>
</tr>
@@ -220,9 +220,9 @@
foreach($messages as $message) {?>
<table class="message" cellspacing="0" cellpadding="1" width="940" border="0">
<tr><th><?php echo Format::db_datetime($message['created']); ?></th></tr>
- <tr><td><?php echo Format::display($message['message']); ?></td></tr>
+ <tr><td><?php echo Format::display($message['body']); ?></td></tr>
<?php
- if($message['attachments'] && ($links=$ticket->getAttachmentsLinks($message['msg_id'],'M'))) {?>
+ if($message['attachments'] && ($links=$ticket->getAttachmentsLinks($message['id'],'M'))) {?>
<tr>
<td class="info"><?php echo $links; ?></td>
</tr>
@@ -231,17 +231,17 @@
</table>
<?php
/* --------- Responses ------------ */
- if($message['responses'] && ($responses=$ticket->getResponses($message['msg_id']))) {
+ if($message['responses'] && ($responses=$ticket->getResponses($message['id']))) {
foreach($responses as $resp) {?>
<table class="response" cellspacing="0" cellpadding="1" width="100%" border="0">
<tr>
<th><?php echo Format::db_datetime($resp['created']); ?>&nbsp;-&nbsp;<?php echo Format::htmlchars($resp['staff_name']); ?></th>
</tr>
<tr>
- <td><?php echo Format::display($resp['response']); ?></td>
+ <td><?php echo Format::display($resp['body']); ?></td>
</tr>
<?php
- if($resp['attachments'] && ($links=$ticket->getAttachmentsLinks($resp['response_id'],'R'))) {?>
+ if($resp['attachments'] && ($links=$ticket->getAttachmentsLinks($resp['id'],'R'))) {?>
<tr>
<td class="info"><?php echo $links; ?></td>
</tr>
@@ -251,7 +251,7 @@
<?php
}
}
- $msgId=$message['msg_id'];
+ $msgId=$message['id'];
}
} else {
echo '<p>Error fetching ticket thread - get technical help.</p>';
Oops, something went wrong.

0 comments on commit 1899a6c

Please sign in to comment.