class Image extends Entity
{
protected $attributes = [
'viewkey' => NULL,
'id' => NULL,
'uploader' => NULL,
'filename' => NULL,
'title' => NULL,
'tags' => NULL,
'createdAt' => NULL,
'modifiedAt' => NULL,
'likeCount' => NULL,
'dislikeCount' => NULL,
'viewCount' => NULL,
'favoriteCount' => NULL,
'commentCount' => NULL,
'rating' => NULL,
'userLiked' => NULL,
'userDisliked' => NULL,
'userViewed' => NULL,
'userFavorited' => NULL,
'userCommented' => NULL,
'userSubscribed' => NULL,
'action' => NULL,
];
protected $datamap = [
'createdAt' => 'created_at',
'modifiedAt' => 'modified_at',
];
protected $dates = ['createdAt', 'updatedAt',];
protected $casts = [
'likeCount' => 'int',
'dislikeCount' => 'int',
'viewCount' => 'int',
'favoriteCount' => 'int',
'commentCount' => 'int',
'rating' => 'float',
'userDisliked' => 'bool',
'userLiked' => 'bool',
'userViewed' => 'bool',
'userFavorited' => 'bool',
];
protected $builder;
public function __construct (array $data = NULL)
{
parent::__construct($data);
$db = \Config\Database::connect();
$this->builder = $db->table('actions');
}
/**
* Custom __set Methods
*/
public function setDislikeCount(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'action' => 0,
];
$this->attributes['dislikeCount'] = $this->builder
->where($where)
->countAllResults();
}
public function setLikeCount(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'action' => 1,
];
$this->attributes['likeCount'] = $this->builder
->where($where)
->countAllResults();
}
public function setViewCount(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'action' => 2,
];
$this->attributes['viewCount'] = $this->builder
->where($where)
->countAllResults();
}
public function setFavoriteCount(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'action' => 3,
];
$this->attributes['favoriteCount'] = $this->builder
->where($where)
->countAllResults();
}
public function setCommentCount(string $viewkey)
{
$this->attributes['commentCount'] = $this->builder
->where('viewkey', $viewkey)
->countAllResults();
}
public function setRating(string $viewkey)
{
helper('arithmetic');
$whereDislike = $whereLike = [];
$whereDislike = [
'viewkey' => $viewkey,
'action' => 0,
];
$dislikes = $this->builder
->where($whereDislike)
->countAllResults();
$whereLike = [
'viewkey' => $viewkey,
'action' => 1,
];
$likes = $this->builder
->where($whereLike)
->countAllResults();
$this->attributes['rating'] = get_percentage($likes + $dislikes, $likes, 0);
}
public function setUserDisliked(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'username' => session()->get('username'),
'action' => 0,
];
$userDisliked = $this->builder
->where($where)
->countAllResults();
if ($userDisliked === 1) {
$this->attributes['userDisliked'] = TRUE;
} else {
$this->attributes['userDisliked'] = FALSE;
}
}
public function setUserLiked(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'username' => session()->get('username'),
'action' => 1,
];
$userLiked = $this->builder
->where($where)
->countAllResults();
if ($userLiked === 1) {
$this->attributes['userLiked'] = TRUE;
} else {
$this->attributes['userLiked'] = FALSE;
}
}
public function setUserViewed(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'username' => session()->get('username'),
'action' => 2,
];
$userViewed = $this->builder
->where($where)
->countAllResults();
if ($userViewed === 1) {
$this->attributes['userViewed'] = TRUE;
} else {
$this->attributes['userViewed'] = FALSE;
}
}
public function setUserFavorited(string $viewkey)
{
$where = [];
$where = [
'viewkey' => $viewkey,
'username' => session()->get('username'),
'action' => 3,
];
$userFavorited = $this->builder
->where($where)
->countAllResults();
if ($userFavorited === 1) {
$this->attributes['userFavorited'] = TRUE;
} else {
$this->attributes['userFavorited'] = FALSE;
}
}
public function setUserCommented(string $subscriber)
{
$db = \Config\Database::connect();
$this->builder = $db->table('comments');
$userCommented = $this->builder
->where('commenter')
->countAllResults();
if ($userCommented === 1) {
$this->attributes['userCommented'] = TRUE;
} else {
$this->attributes['userCommented'] = FALSE;
}
}
public function setUserSubscribed(string $uploader)
{
$db = \Config\Database::connect();
$this->builder = $db->table('subscribers');
$where = [];
$where = [
'profile' => $uploader,
'subscriber' => session()->get('username'),
];
$userSubscribed = $this->builder
->where($where)
->countAllResults();
if ($userSubscribed === 1) {
$this->attributes['userSubscribed'] = TRUE;
} else {
$this->attributes['userSubscribed'] = FALSE;
}
}
/**
* Custom __get Methods
*/
}