Skip to content
Snippets Groups Projects
class.thread.php 36.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • Peter Rotich's avatar
    Peter Rotich committed
            return ThreadEntry::add($vars);
        }
    
        function lookup($id, $tid=0, $type='M') {
    
            return ($id
    
                    && is_numeric($id)
    
    Peter Rotich's avatar
    Peter Rotich committed
                    && ($m = new Message($id, $tid))
    
                    && $m->getId()==$id
                    )?$m:null;
    
        function lastByTicketId($ticketId) {
    
            return self::byTicketId($ticketId);
        }
    
        function firstByTicketId($ticketId) {
            return self::byTicketId($ticketId, false);
        }
    
        function byTicketId($ticketId, $last=true) {
    
    
            $sql=' SELECT thread.id FROM '.TICKET_THREAD_TABLE.' thread '
                .' WHERE thread_type=\'M\' AND thread.ticket_id = '.db_input($ticketId)
    
                .sprintf(' ORDER BY thread.id %s LIMIT 1', $last ? 'DESC' : 'ASC');
    
    
            if (($res = db_query($sql)) && ($id = db_result($res)))
    
                return Message::lookup($id);
    
    }
    
    /* Response - Ticket thread entry of type response */
    class Response extends ThreadEntry {
    
        function Response($id, $ticketId=0) {
            parent::ThreadEntry($id, 'R', $ticketId);
        }
    
        function getSubject() {
            return $this->getTitle();
        }
    
        function getRespondent() {
            return $this->getStaff();
        }
    
    
    Peter Rotich's avatar
    Peter Rotich committed
        function create($vars, &$errors) {
            return self::lookup(self::add($vars, $errors));
        }
    
        function add($vars, &$errors) {
    
            if(!$vars || !is_array($vars) || !$vars['ticketId'])
                $errors['err'] = 'Missing or invalid data';
            elseif(!$vars['response'])
                $errors['response'] = 'Response required';
    
            if($errors) return false;
    
            $vars['type'] = 'R';
            $vars['body'] = $vars['response'];
            if(!$vars['pid'] && $vars['msgId'])
                $vars['pid'] = $vars['msgId'];
    
    
    Peter Rotich's avatar
    Peter Rotich committed
            if (!$vars['poster']
                    && $vars['staffId']
                    && ($staff = Staff::lookup($vars['staffId'])))
                $vars['poster'] = (string) $staff->getName();
    
    
    Peter Rotich's avatar
    Peter Rotich committed
            return ThreadEntry::add($vars);
        }
    
    
        function lookup($id, $tid=0, $type='R') {
    
            return ($id
    
                    && is_numeric($id)
    
    Peter Rotich's avatar
    Peter Rotich committed
                    && ($r = new Response($id, $tid))
    
                    && $r->getId()==$id
                    )?$r:null;
    
        }
    }
    
    /* Note - Ticket thread entry of type note (Internal Note) */
    class Note extends ThreadEntry {
    
        function Note($id, $ticketId=0) {
            parent::ThreadEntry($id, 'N', $ticketId);
        }
    
        function getMessage() {
            return $this->getBody();
        }
    
    
    Peter Rotich's avatar
    Peter Rotich committed
        /* static */
        function create($vars, &$errors) {
            return self::lookup(self::add($vars, $errors));
        }
    
        function add($vars, &$errors) {
    
            //Check required params.
            if(!$vars || !is_array($vars) || !$vars['ticketId'])
                $errors['err'] = 'Missing or invalid data';
            elseif(!$vars['note'])
                $errors['note'] = 'Note required';
    
            if($errors) return false;
    
            //TODO: use array_intersect_key  when we move to php 5 to extract just what we need.
            $vars['type'] = 'N';
            $vars['body'] = $vars['note'];
    
            return ThreadEntry::add($vars);
        }
    
        function lookup($id, $tid=0, $type='N') {
    
            return ($id
    
                    && is_numeric($id)
    
    Peter Rotich's avatar
    Peter Rotich committed
                    && ($n = new Note($id, $tid))
    
                    && $n->getId()==$id
                    )?$n:null;
    
    
    class ThreadBody /* extends SplString */ {
    
    
        static $types = array('text', 'html');
    
    
        var $body;
        var $type;
    
        function __construct($body, $type='text') {
            $type = strtolower($type);
            if (!in_array($type, static::$types))
                throw new Exception($type.': Unsupported ThreadBody type');
    
            $this->body = (string) $body;
    
            $this->type = $type;
        }
    
        function convertTo($type) {
            if ($type === $this->type)
                return $this;
    
            $conv = $this->type . ':' . strtolower($type);
            switch ($conv) {
            case 'text:html':
                return new ThreadBody(sprintf('<pre>%s</pre>',
                    Format::htmlchars($this->body)), $type);
            case 'html:text':
    
                return new ThreadBody(Format::html2text((string) $this), $type);
    
        function stripQuotedReply($tag) {
    
            //Strip quoted reply...on emailed  messages
            if (!$tag || strpos($this->body, $tag) === false)
                return;
    
            if ((list($msg) = explode($tag, $this->body, 2)) && trim($msg))
                $this->body = $msg;
    
        }
    
    
        function __toString() {
            return $this->body;
        }
    }
    
    class TextThreadBody extends ThreadBody {
        function __construct($body) {
    
            parent::__construct(Format::stripEmptyLines($body), 'text');
    
        }
    }
    class HtmlThreadBody extends ThreadBody {
        function __construct($body) {
    
            $body = trim($body, " <>br/\t\n\r") ? Format::safe_html($body) : '';
            parent::__construct($body, 'html');