Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.64 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: ~~~
  5.  * Date: 19/07/2018
  6.  * Time: 9:01 AM
  7.  */
  8.  
  9. class ContentLock_Model extends CI_Model
  10. {
  11.     /**
  12.      * @var integer
  13.      */
  14.     private $content_id;
  15.     /**
  16.      * @var string
  17.      */
  18.     private $content_type;
  19.     /**
  20.      * @var integer
  21.      */
  22.     private $user_id;
  23.     /**
  24.      * @var string
  25.      */
  26.     private $time_limit;
  27.     /**
  28.      * content locks table
  29.      */
  30.     const CONTENT_LOCK_TABLE = 'content_locks';
  31.  
  32.     /**
  33.      * ContentLock_Model constructor.
  34.      */
  35.     public function __construct()
  36.     {
  37.         parent::__construct();
  38.         $this->content_type = 'job';
  39.         $this->time_limit = '-5 minutes';
  40.     }
  41.  
  42.     /**
  43.      * @param $id
  44.      */
  45.     public function setContentId($id)
  46.     {
  47.         $this->content_id = $id;
  48.     }
  49.  
  50.     /**
  51.      * @param $user_id
  52.      */
  53.     public function setUserId($user_id)
  54.     {
  55.         $this->user_id = $user_id;
  56.     }
  57.  
  58.     /**
  59.      * Checks if the specified content has a lock on it
  60.      * @return bool
  61.      */
  62.     public function isContentLocked()
  63.     {
  64.         $contentLock = $this->getContentLock();
  65.  
  66.         $num_rows = $contentLock['num_rows'];
  67.         $result = $contentLock['result'];
  68.  
  69.         // If empty, add a new content lock
  70.         if($num_rows < 1) {
  71.             $this->addContentLock();
  72.             return false;
  73.         }
  74.  
  75.         // If the lock belongs to the requesting user, return false
  76.         if($this->isOwnLock($result->user_id)) {
  77.             return false;
  78.         }
  79.  
  80.         // If sufficient time has elapsed, set the user to the content lock with current time
  81.         if($this->hasTimeElapsed($result->timestamp)) {
  82.             $this->updateLockTime();
  83.             $this->updateLockUser();
  84.             return false;
  85.         }
  86.  
  87.         return true;
  88.     }
  89.  
  90.     /**
  91.      * Gets the content lock record
  92.      * @return array
  93.      */
  94.     private function getContentLock()
  95.     {
  96.         $criteria = array(
  97.             'content_id' => $this->content_id,
  98.             'content_type' => $this->content_type
  99.         );
  100.  
  101.         $query = $this->db
  102.             ->select(array("timestamp", "user_id"))
  103.             ->from(self::CONTENT_LOCK_TABLE)
  104.             ->where($criteria)
  105.             ->get();
  106.  
  107.         return array(
  108.             'result' => $query->row(),
  109.             'num_rows' => $query->num_rows()
  110.         );
  111.     }
  112.  
  113.     /**
  114.      * Deletes the content lock record
  115.      */
  116.     public function clearContentLock()
  117.     {
  118.         $criteria = array(
  119.             'content_id' => $this->content_id,
  120.             'content_type' => $this->content_type,
  121.             'user_id' => $this->user_id
  122.         );
  123.  
  124.         $this->db
  125.             ->where($criteria)
  126.             ->delete(self::CONTENT_LOCK_TABLE);
  127.     }
  128.  
  129.     /**
  130.      * Inserts a new content lock record
  131.      */
  132.     public function addContentLock()
  133.     {
  134.  
  135.         $data = array(
  136.             'content_type' => $this->content_type,
  137.             'user_id' => $this->user_id,
  138.             'timestamp' => date('Y-m-d H:i:s'),
  139.             'content_id' => $this->content_id
  140.         );
  141.  
  142.         $this->db->insert(self::CONTENT_LOCK_TABLE, $data);
  143.     }
  144.  
  145.     /**
  146.      * Updates the content lock timestamp record
  147.      */
  148.     private function updateLockTime()
  149.     {
  150.         $criteria = array(
  151.             'content_id' => $this->content_id,
  152.             'content_type' => $this->content_type
  153.         );
  154.  
  155.         $this->db
  156.             ->set('timestamp', date('Y-m-d H:i:s'))
  157.             ->where($criteria)
  158.             ->update(self::CONTENT_LOCK_TABLE);
  159.     }
  160.  
  161.     /**
  162.      * Updates the content lock user
  163.      */
  164.     private function updateLockUser()
  165.     {
  166.         $criteria = array(
  167.             'content_id' => $this->content_id,
  168.             'content_type' => $this->content_type
  169.         );
  170.  
  171.         $this->db
  172.             ->set('user_id', date($this->user_id))
  173.             ->where($criteria)
  174.             ->update(self::CONTENT_LOCK_TABLE);
  175.     }
  176.  
  177.     /**
  178.      * Checks if the time has elapsed on the given record
  179.      * @param $content_timestamp
  180.      * @return bool
  181.      */
  182.     private function hasTimeElapsed($content_timestamp)
  183.     {
  184.         if(strtotime($content_timestamp) < strtotime($this->time_limit)) {
  185.             return true;
  186.         } else {
  187.             return false;
  188.         }
  189.     }
  190.  
  191.     /**
  192.      * Checks if lock belongs to the given owner
  193.      * @param $content_user_id
  194.      * @return bool
  195.      */
  196.     private function isOwnLock($content_user_id)
  197.     {
  198.         if($content_user_id === $this->user_id) {
  199.             return true;
  200.         } else {
  201.             return false;
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement