Файл: system/classes/Like.php
Строк: 82
<?php
/**
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) 2013, Taras Chornyi, Sergiy Mazurenko, Ivan Kotliar
* @link http://perf-engine.net
* @package PerfEngine
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
class Like
{
/*
* @var $type string: type of like
*/
public $type;
/*
* @var $id int: id of object
*/
public $id;
/*
* @var $data array: params for query to database;
*/
public $data;
/*
* @param $type string: type of like
* @param $id int: id of object
* @param $data array: config for query to db ('table', 'row')
*/
public function __construct($type, $id, $data = array())
{
/*
* assign object vars
*/
$this->data = $data;
$this->type = input($type);
$this->id = (int)$id;
/*
* getting database instance
*/
global $db;
/*
* assign table and row for query
*/
$table = isset($this->data['table']) ? $this->data['table'] : $this->type;
$row = isset($this->data['row']) ? $this->data['row'] : 'id';
/*
* Checking, notice if errror
*/
if($db->query("SELECT * FROM `{$table}` WHERE `{$row}` = '{$id}'")->rowCount() == 0)
{
echo 'Error!';
}
}
/*
* @return like or unlike link
*/
public function show()
{
global $db;
/*
* count likes for object
*/
$count = $db->query("SELECT * FROM `likes` WHERE `id` = '{$this->id}' AND `type` = '{$this->type}'")->rowCount();
/*
* check if user
*/
if(User::logged())
{
/*
* check if user not liked object and showing like or unlike link
*/
if($db->query("SELECT * FROM `likes` WHERE `id` = '{$this->id}' AND `type` = '{$this->type}' AND `user_id` = '". User::Id() ."'")->rowCount() == 0)
{
return img('like.png').' <a href="'.pagelink().'?_like_=like&_like_type_='.$this->type.'&_like_id_='.$this->id.'">Like '.($count > 0 ? '['. $count .']': null).'</a>';
}
else
{
return img('like.png').' <a href="'.pagelink().'?_like_=unlike&_like_type_='.$this->type.'&_like_id_='.$this->id.'"><b>Like</b> ['. $count .']</a> ';
}
}
}
/*
* @return void
* Function add or deleting like
*/
public function change()
{
global $db;
if(User::logged())
{
/*
* check if user not liked object and changing number of likes
*/
if($db->query("SELECT * FROM `likes` WHERE `id` = '{$this->id}' AND `type` = '{$this->type}' AND `user_id` = '". User::Id() ."'")->rowCount() == 0)
{
$db->query("INSERT INTO `likes` SET `id` = '{$this->id}', `type` = '{$this->type}', `user_id` = '". User::Id() ."'");
}
else
{
$db->query("DELETE FROM `likes` WHERE `id` = '{$this->id}' AND `type` = '{$this->type}' AND `user_id` = '". User::Id() ."' LIMIT 1");
}
}
}
}