Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: ~~~
- * Date: 19/07/2018
- * Time: 9:01 AM
- */
- class ContentLock_Model extends CI_Model
- {
- /**
- * @var integer
- */
- private $content_id;
- /**
- * @var string
- */
- private $content_type;
- /**
- * @var integer
- */
- private $user_id;
- /**
- * @var string
- */
- private $time_limit;
- /**
- * content locks table
- */
- const CONTENT_LOCK_TABLE = 'content_locks';
- /**
- * ContentLock_Model constructor.
- */
- public function __construct()
- {
- parent::__construct();
- $this->content_type = 'job';
- $this->time_limit = '-5 minutes';
- }
- /**
- * @param $id
- */
- public function setContentId($id)
- {
- $this->content_id = $id;
- }
- /**
- * @param $user_id
- */
- public function setUserId($user_id)
- {
- $this->user_id = $user_id;
- }
- /**
- * Checks if the specified content has a lock on it
- * @return bool
- */
- public function isContentLocked()
- {
- $contentLock = $this->getContentLock();
- $num_rows = $contentLock['num_rows'];
- $result = $contentLock['result'];
- // If empty, add a new content lock
- if($num_rows < 1) {
- $this->addContentLock();
- return false;
- }
- // If the lock belongs to the requesting user, return false
- if($this->isOwnLock($result->user_id)) {
- return false;
- }
- // If sufficient time has elapsed, set the user to the content lock with current time
- if($this->hasTimeElapsed($result->timestamp)) {
- $this->updateLockTime();
- $this->updateLockUser();
- return false;
- }
- return true;
- }
- /**
- * Gets the content lock record
- * @return array
- */
- private function getContentLock()
- {
- $criteria = array(
- 'content_id' => $this->content_id,
- 'content_type' => $this->content_type
- );
- $query = $this->db
- ->select(array("timestamp", "user_id"))
- ->from(self::CONTENT_LOCK_TABLE)
- ->where($criteria)
- ->get();
- return array(
- 'result' => $query->row(),
- 'num_rows' => $query->num_rows()
- );
- }
- /**
- * Deletes the content lock record
- */
- public function clearContentLock()
- {
- $criteria = array(
- 'content_id' => $this->content_id,
- 'content_type' => $this->content_type,
- 'user_id' => $this->user_id
- );
- $this->db
- ->where($criteria)
- ->delete(self::CONTENT_LOCK_TABLE);
- }
- /**
- * Inserts a new content lock record
- */
- public function addContentLock()
- {
- $data = array(
- 'content_type' => $this->content_type,
- 'user_id' => $this->user_id,
- 'timestamp' => date('Y-m-d H:i:s'),
- 'content_id' => $this->content_id
- );
- $this->db->insert(self::CONTENT_LOCK_TABLE, $data);
- }
- /**
- * Updates the content lock timestamp record
- */
- private function updateLockTime()
- {
- $criteria = array(
- 'content_id' => $this->content_id,
- 'content_type' => $this->content_type
- );
- $this->db
- ->set('timestamp', date('Y-m-d H:i:s'))
- ->where($criteria)
- ->update(self::CONTENT_LOCK_TABLE);
- }
- /**
- * Updates the content lock user
- */
- private function updateLockUser()
- {
- $criteria = array(
- 'content_id' => $this->content_id,
- 'content_type' => $this->content_type
- );
- $this->db
- ->set('user_id', date($this->user_id))
- ->where($criteria)
- ->update(self::CONTENT_LOCK_TABLE);
- }
- /**
- * Checks if the time has elapsed on the given record
- * @param $content_timestamp
- * @return bool
- */
- private function hasTimeElapsed($content_timestamp)
- {
- if(strtotime($content_timestamp) < strtotime($this->time_limit)) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * Checks if lock belongs to the given owner
- * @param $content_user_id
- * @return bool
- */
- private function isOwnLock($content_user_id)
- {
- if($content_user_id === $this->user_id) {
- return true;
- } else {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement