vendor/pimcore/pimcore/models/Element/Editlock.php line 141

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model\Element;
  15. use Pimcore\Model;
  16. use Pimcore\Tool\Session;
  17. /**
  18.  * @internal
  19.  *
  20.  * @method \Pimcore\Model\Element\Editlock\Dao getDao()
  21.  * @method void delete()
  22.  * @method void save()
  23.  */
  24. final class Editlock extends Model\AbstractModel
  25. {
  26.     /**
  27.      * @var int
  28.      */
  29.     protected $id;
  30.     /**
  31.      * @var int
  32.      */
  33.     protected $cid;
  34.     /**
  35.      * @var string
  36.      */
  37.     protected $ctype;
  38.     /**
  39.      * @var int
  40.      */
  41.     protected $userId;
  42.     /**
  43.      * @var string
  44.      */
  45.     protected $sessionId;
  46.     /**
  47.      * @var int
  48.      */
  49.     protected $date;
  50.     /**
  51.      * @var string
  52.      */
  53.     protected $cpath;
  54.     /**
  55.      * @param int $cid
  56.      * @param string $ctype
  57.      *
  58.      * @return bool
  59.      */
  60.     public static function isLocked($cid$ctype)
  61.     {
  62.         if ($lock self::getByElement($cid$ctype)) {
  63.             if ((time() - $lock->getDate()) > 3600 || $lock->getSessionId() === Session::getSessionId()) {
  64.                 // lock is out of date unlock it
  65.                 self::unlock($cid$ctype);
  66.                 return false;
  67.             }
  68.             return true;
  69.         }
  70.         return false;
  71.     }
  72.     /**
  73.      * @param int $cid
  74.      * @param string $ctype
  75.      *
  76.      * @return null|Editlock
  77.      */
  78.     public static function getByElement($cid$ctype)
  79.     {
  80.         try {
  81.             $lock = new self();
  82.             $lock->getDao()->getByElement($cid$ctype);
  83.             return $lock;
  84.         } catch (Model\Exception\NotFoundException $e) {
  85.             return null;
  86.         }
  87.     }
  88.     /**
  89.      * @param string $sessionId
  90.      *
  91.      * @return bool|null
  92.      */
  93.     public static function clearSession($sessionId)
  94.     {
  95.         try {
  96.             $lock = new self();
  97.             $lock->getDao()->clearSession($sessionId);
  98.             return true;
  99.         } catch (\Exception $e) {
  100.             return null;
  101.         }
  102.     }
  103.     /**
  104.      * @param int $cid
  105.      * @param string $ctype
  106.      *
  107.      * @return bool|Editlock
  108.      */
  109.     public static function lock($cid$ctype)
  110.     {
  111.         // try to get user
  112.         if (!$user \Pimcore\Tool\Admin::getCurrentUser()) {
  113.             return false;
  114.         }
  115.         $lock = new self();
  116.         $lock->setCid($cid);
  117.         $lock->setCtype($ctype);
  118.         $lock->setDate(time());
  119.         $lock->setUserId($user->getId());
  120.         $lock->setSessionId(Session::getSessionId());
  121.         $lock->save();
  122.         return $lock;
  123.     }
  124.     /**
  125.      * @param int $cid
  126.      * @param string $ctype
  127.      *
  128.      * @return bool
  129.      */
  130.     public static function unlock($cid$ctype)
  131.     {
  132.         if ($lock self::getByElement($cid$ctype)) {
  133.             $lock->delete();
  134.         }
  135.         return true;
  136.     }
  137.     /**
  138.      * @return int
  139.      */
  140.     public function getCid()
  141.     {
  142.         return $this->cid;
  143.     }
  144.     /**
  145.      * @return int
  146.      */
  147.     public function getId()
  148.     {
  149.         return $this->id;
  150.     }
  151.     /**
  152.      * @return int
  153.      */
  154.     public function getUserId()
  155.     {
  156.         return $this->userId;
  157.     }
  158.     /**
  159.      * @param int $cid
  160.      *
  161.      * @return $this
  162.      */
  163.     public function setCid($cid)
  164.     {
  165.         $this->cid = (int) $cid;
  166.         return $this;
  167.     }
  168.     /**
  169.      * @param int $id
  170.      *
  171.      * @return $this
  172.      */
  173.     public function setId($id)
  174.     {
  175.         $this->id = (int) $id;
  176.         return $this;
  177.     }
  178.     /**
  179.      * @param int $userId
  180.      *
  181.      * @return $this
  182.      */
  183.     public function setUserId($userId)
  184.     {
  185.         $this->userId = (int) $userId;
  186.         return $this;
  187.     }
  188.     /**
  189.      * @return string
  190.      */
  191.     public function getCtype()
  192.     {
  193.         return $this->ctype;
  194.     }
  195.     /**
  196.      * @param string $ctype
  197.      *
  198.      * @return $this
  199.      */
  200.     public function setCtype($ctype)
  201.     {
  202.         $this->ctype = (string) $ctype;
  203.         return $this;
  204.     }
  205.     /**
  206.      * @return string
  207.      */
  208.     public function getSessionId()
  209.     {
  210.         return $this->sessionId;
  211.     }
  212.     /**
  213.      * @param string $sessionId
  214.      *
  215.      * @return $this
  216.      */
  217.     public function setSessionId($sessionId)
  218.     {
  219.         $this->sessionId = (string) $sessionId;
  220.         return $this;
  221.     }
  222.     /**
  223.      * @return Model\User|null
  224.      */
  225.     public function getUser()
  226.     {
  227.         if ($user Model\User::getById($this->getUserId())) {
  228.             return $user;
  229.         }
  230.         return null;
  231.     }
  232.     /**
  233.      * @return int
  234.      */
  235.     public function getDate()
  236.     {
  237.         return $this->date;
  238.     }
  239.     /**
  240.      * @param int $date
  241.      *
  242.      * @return $this
  243.      */
  244.     public function setDate($date)
  245.     {
  246.         $this->date = (int) $date;
  247.         return $this;
  248.     }
  249.     /**
  250.      * @param string $cpath
  251.      *
  252.      * @return $this
  253.      */
  254.     public function setCpath($cpath)
  255.     {
  256.         $this->cpath $cpath;
  257.         return $this;
  258.     }
  259.     /**
  260.      * @return string
  261.      */
  262.     public function getCpath()
  263.     {
  264.         return $this->cpath;
  265.     }
  266. }