Skip to content
Snippets Groups Projects
class.collaborator.php 3.79 KiB
Newer Older
  • Learn to ignore specific revisions
  • Peter Rotich's avatar
    Peter Rotich committed
    <?php
    /*********************************************************************
        class.collaborator.php
    
        Ticket collaborator
    
        Peter Rotich <peter@osticket.com>
        Copyright (c)  2006-2013 osTicket
        http://www.osticket.com
    
        Released under the GNU General Public License WITHOUT ANY WARRANTY.
        See LICENSE.TXT for details.
    
        vim: expandtab sw=4 ts=4 sts=4:
    **********************************************************************/
    require_once(INCLUDE_DIR . 'class.user.php');
    
    require_once(INCLUDE_DIR . 'class.client.php');
    
    class Collaborator
    extends VerySimpleModel
    implements EmailContact, ITicketUser {
    
        static $meta = array(
            'table' => THREAD_COLLABORATOR_TABLE,
            'pk' => array('id'),
    
            'select_related' => array('user'),
    
            'joins' => array(
                'thread' => array(
                    'constraint' => array('thread_id' => 'Thread.id'),
                ),
                'user' => array(
                    'constraint' => array('user_id' => 'User.id'),
                ),
            ),
        );
    
    Peter Rotich's avatar
    Peter Rotich committed
        function __toString() {
    
            return Format::htmlchars($this->toString());
        }
        function toString() {
            return sprintf('%s <%s>', $this->getName(), $this->getEmail());
    
    Peter Rotich's avatar
    Peter Rotich committed
        function getId() {
    
            return $this->id;
    
    Peter Rotich's avatar
    Peter Rotich committed
        }
    
        function isActive() {
    
            return $this->isactive;
    
            return $this->created;
    
        function getThreadId() {
            return $this->thread_id;
        }
    
    
    Peter Rotich's avatar
    Peter Rotich committed
        function getTicketId() {
    
            if ($this->thread->object_type == ObjectModel::OBJECT_TYPE_TICKET)
    
                return $this->thread->object_id;
    
    Peter Rotich's avatar
    Peter Rotich committed
        }
    
        function getTicket() {
    
            // TODO: Change to $this->thread->ticket when Ticket goes to ORM
    
            if ($id = $this->getTicketId())
                return Ticket::lookup($id);
    
        function getUser() {
            return $this->user;
    
        // EmailContact interface
        function getEmail() {
            return $this->user->getEmail();
        }
        function getName() {
            return $this->user->getName();
        }
    
        // VariableReplacer interface
        function getVar($what) {
            global $cfg;
    
            switch (strtolower($what)) {
    
            case 'ticket_link':
                return sprintf('%s/view.php?%s',
                    $cfg->getBaseUrl(),
                    Http::build_query(
                        // TODO: Chance to $this->getTicket when
                        array('auth' => $this->getTicket()->getAuthToken($this)),
                        false
                    )
                );
                break;
            }
    
        // ITicketUser interface
        var $_isguest;
    
        function isOwner() {
            return false;
        }
        function flagGuest() {
            $this->_isguest = true;
        }
        function isGuest() {
            return $this->_isguest;
        }
        function getUserId() {
            return $this->user_id;
        }
    
        function save($refetch=false) {
            if ($this->dirty)
                $this->updated = SqlFunction::NOW();
            return parent::save($refetch || $this->dirty);
    
    Peter Rotich's avatar
    Peter Rotich committed
        static function add($info, &$errors) {
    
    
            if (!$info || !$info['threadId'] || !$info['userId'])
    
                $errors['err'] = __('Invalid or missing information');
    
            elseif ($c = static::lookup(array(
                'thread_id' => $info['threadId'],
                'user_id' => $info['userId'],
            )))
    
                $errors['err'] = sprintf(__('%s is already a collaborator'),
    
    Peter Rotich's avatar
    Peter Rotich committed
                        $c->getName());
    
    
            $collab = static::create(array(
                'isactive' => isset($info['isactive']) ? $info['isactive'] : 0,
                'thread_id' => $info['threadId'],
                'user_id' => $info['userId'],
            ));
            if ($collab->save(true))
                return $collab;
    
            $errors['err'] = __('Unable to add collaborator. Internal error');
    
    Peter Rotich's avatar
    Peter Rotich committed
    
            return false;
        }
    
    
    Peter Rotich's avatar
    Peter Rotich committed
    ?>