Skip to content
Snippets Groups Projects
class.lock.php 4.05 KiB
Newer Older
Jared Hancock's avatar
Jared Hancock committed
<?php
/*********************************************************************
    class.lock.php

    Ticket lock handle.

    Peter Rotich <peter@osticket.com>
    Copyright (c)  2006-2013 osTicket
Jared Hancock's avatar
Jared Hancock committed
    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:
**********************************************************************/

/*
 * Mainly used as a helper...
 */

class Lock extends VerySimpleModel {
    static $meta = array(
        'table' => LOCK_TABLE,
        'pk' => array('lock_id'),
        'joins' => array(
            'ticket' => array(
                'reverse' => 'TicketModel.lock',
                'list' => false,
            ),
            'task' => array(
                'reverse' => 'Task.lock',
                'list' => false,
            ),
            'staff' => array(
                'constraint' => array('staff_id' => 'Staff.staff_id'),
            ),
        ),
    );
Jared Hancock's avatar
Jared Hancock committed

    function getId() {
        return $this->lock_id;
Jared Hancock's avatar
Jared Hancock committed
    }

    function getStaffId() {
        return $this->staff_id;
Jared Hancock's avatar
Jared Hancock committed
    }

    function getStaffName() {
        return $this->staff->getName();
Jared Hancock's avatar
Jared Hancock committed
    }

    function getCreateTime() {
        return $this->created;
Jared Hancock's avatar
Jared Hancock committed
    }

    function getExpireTime() {
        return strtotime($this->expire);
Jared Hancock's avatar
Jared Hancock committed
    }
    //Get remaiming time before the lock expires
    function getTime() {
        return $this->isExpired()?0:($this->getExpireTime()-time());
Jared Hancock's avatar
Jared Hancock committed
    }

    //Should we be doing realtime check here? (Ans: not really....expiretime is local & based on loadtime)
    function isExpired() {
        return (time()>$this->getExpireTime());
    function getCode() {
        return $this->code;
    }

Jared Hancock's avatar
Jared Hancock committed
    //Renew existing lock.
    function renew($lockTime=0) {
Peter Rotich's avatar
Peter Rotich committed
        global $cfg;
Jared Hancock's avatar
Jared Hancock committed

        if(!$lockTime || !is_numeric($lockTime)) //XXX: test to  make it works.
            $lockTime = $cfg->getLockTime();

        $this->expire = SqlExpression::plus(
            SqlFunction::NOW(),
            SqlInterval::MINUTE($lockTime)
        );
        return $this->save(true);
Jared Hancock's avatar
Jared Hancock committed
    }

    //release aka delete a lock.
    function release() {
        return $this->delete();
Jared Hancock's avatar
Jared Hancock committed
    }

    /* ----------------------- Static functions ---------------------------*/
    static function lookup($id, $object=false) {
        if ($object instanceof Ticket)
            return parent::lookup(array('lock_id' => $id, 'ticket__ticket_id' => $object->getId()));
        elseif ($object instanceof Task)
            return parent::lookup(array('lock_id' => $id, 'task__id' => $object->getId()));
        else
            return parent::lookup($id);
    //Create a ticket lock...this function assumes the caller checked for access & validity of ticket & staff x-ship.
    static function acquire($staffId, $lockTime) {
        if (!$staffId or !$lockTime)
            return null;
        // Create the new lock.
        $lock = parent::create(array(
            'created' => SqlFunction::NOW(),
            'staff_id' => $staffId,
            'expire' => SqlExpression::plus(
                SqlFunction::NOW(),
                SqlInterval::MINUTE($lockTime)
            ),
            'code' => Misc::randCode(10)
        ));
        if ($lock->save(true))
            return $lock;
    }

    static function create($staffId, $lockTime) {
        if ($lock = self::acquire($staffId, $lockTime))
            return $lock;
    }

    // Simply remove ALL locks a user (staff) holds on a ticket(s).
    static function removeStaffLocks($staffId, $object=false) {
        $locks = static::objects()->filter(array(
            'staff_id' => $staffId,
        ));
        if ($object instanceof Ticket)
            $locks->filter(array('ticket__ticket_id' => $object->getId()));
        elseif ($object instanceof Task)
            $locks->filter(array('task__id' => $object->getId()));

        return $locks->delete();
    }

    static function cleanup() {
        return static::objects()->filter(array(
            'expire__lt' => SqlFunction::NOW()
        ))->delete();