mirror of
https://github.com/claradex/nativegallery.git
synced 2024-11-15 11:39:07 +03:00
62 lines
2.5 KiB
PHP
62 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Api\Images;
|
|
|
|
|
|
|
|
use App\Services\{Auth, Router, GenerateRandomStr, DB, Json, EXIF};
|
|
use App\Models\{User, Vote};
|
|
|
|
|
|
class Rate
|
|
{
|
|
public function __construct()
|
|
{
|
|
|
|
if (isset($_GET['vote']) && isset($_GET['pid'])) {
|
|
if (Vote::photo(Auth::userid(), $_GET['pid']) === -1) {
|
|
DB::query('INSERT INTO photos_rates VALUES (\'0\', :id, :pid, :type)', array(':id'=>Auth::userid(), ':pid' => $_GET['pid'], ':type'=>$_GET['vote']));
|
|
if (Vote::photo(Auth::userid(), $_GET['pid']) != $_GET['vote']) {
|
|
DB::query('DELETE FROM photos_rates WHERE user_id=:id AND photo_id=:pid AND type=:type', array(':id'=>Auth::userid(), ':pid' => $_GET['pid'], ':type'=>Vote::photo(Auth::userid(), $_GET['pid'])));
|
|
}
|
|
} else if (Vote::photo(Auth::userid(), $_GET['pid']) === (int)$_GET['vote']) {
|
|
DB::query('DELETE FROM photos_rates WHERE user_id=:id AND photo_id=:pid', array(':id'=>Auth::userid(), ':pid' => $_GET['pid']));
|
|
} else {
|
|
DB::query('UPDATE photos_rates SET type=:type WHERE user_id=:id AND photo_id=:pid', array(':id'=>Auth::userid(), ':pid' => $_GET['pid'], ':type'=>$_GET['vote']));
|
|
|
|
}
|
|
$votes = DB::query('SELECT * FROM photos_rates WHERE photo_id=:id ORDER BY id DESC', array(':id' => $_GET['pid']));
|
|
|
|
$formattedVotesPos = [];
|
|
$formattedVotesNeg = [];
|
|
foreach ($votes as $vote) {
|
|
$user = new User($vote['user_id']);
|
|
if ($vote['type'] === 0) {
|
|
$type = -1;
|
|
$formattedVotesNeg[] = [$vote['user_id'], $user->i('username'), $type];
|
|
} else {
|
|
$type = 1;
|
|
$formattedVotesPos[] = [$vote['user_id'], $user->i('username'), $type];
|
|
}
|
|
|
|
}
|
|
|
|
if (Vote::photo(Auth::userid(), $_GET['pid']) === 0) {
|
|
$negbtn = true;
|
|
$posbtn = false;
|
|
} else {
|
|
$negbtn = false;
|
|
$posbtn = true;
|
|
}
|
|
$result = [
|
|
'votes' => [$formattedVotesNeg, $formattedVotesPos],
|
|
'buttons' => [$negbtn, $posbtn],
|
|
'errors' => '',
|
|
'rating' => Vote::count($_GET['pid'])
|
|
];
|
|
|
|
header('Content-Type: application/json');
|
|
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
|
}
|
|
}
|
|
}
|