Add Photos Rating

This commit is contained in:
themohooks 2024-07-05 11:13:28 +03:00
parent aff3d2351f
commit 38dacd9235
7 changed files with 406 additions and 195 deletions

View file

@ -0,0 +1,62 @@
<?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);
}
}
}

View file

@ -6,6 +6,7 @@ use \App\Controllers\ExceptionRegister;
use \App\Core\Page;
use \App\Controllers\Api\{Login, Register};
use \App\Controllers\Api\Images\{Upload};
use \App\Controllers\Api\Images\Rate as PhotoVote;
class ApiController
{
@ -19,5 +20,9 @@ class ApiController
public static function upload() {
return new Upload();
}
public static function photovote() {
return new PhotoVote();
}
}

View file

@ -18,6 +18,7 @@ class Routes
Router::get('/photo/$id', 'PhotoController@i');
Router::get('/author/$id', 'ProfileController@i');
Router::post('/api/login', 'ApiController@login');
Router::get('/api/photo/vote', 'ApiController@photovote');
Router::post('/api/register', 'ApiController@register');

44
app/Models/Vote.php Normal file
View file

@ -0,0 +1,44 @@
<?php
namespace App\Models;
use App\Services\{DB, GenerateRandomStr};
class Vote
{
public static function photo($user_id, $pid)
{
$result = DB::query('SELECT type FROM photos_rates WHERE user_id=:uid AND photo_id=:pid', array(':uid' => $user_id, ':pid' => $pid));
if (!empty($result)) {
$type = $result[0]['type'];
if ($type < 0) {
$type = -1;
}
return $type;
} else {
return -1;
}
}
public static function count( $pid) {
$result = DB::query('SELECT * FROM photos_rates WHERE photo_id=:pid', array(':pid' => $pid));
$votes = 0;
foreach ($result as $r) {
if ($r['type'] === 1) {
$votes++;
} else {
$votes--;
}
}
return $votes;
}
public static function token()
{
return $_COOKIE['NGALLERYSESS'];
}
}
?>

261
static/js/photo.js Normal file
View file

@ -0,0 +1,261 @@
$(document).ready(function()
{
// Убираем лишние параметры
var url = document.location.toString();
url = url.replace(/\?vid=\d+$/, '');
url = url.replace(/\?gid=\d+$/, '');
url = url.replace(/\?aid=\d+$/, '');
url = url.replace(/\?upd=\d+$/, '');
url = url.replace(/\?top=\d+$/, '');
history.replaceState({}, '', url);
// Переход к следующему фото
$('#prev, #next').click(function()
{
var next = (this.id == 'prev' ? 0 : 1);
$.get('/api.php', { action: 'move', pid: pid, vid: vid, gid: gid, aid: aid, next: next }, function(pid)
{
if (pid == 0)
{
if (!vid && !gid)
{
if (next)
alert(_text['P_MOVE_FIRST'] + '.');
else alert(_text['P_MOVE_LAST'] + '.');
}
else alert(_text[vid ? 'P_MOVE_ALONE_V' : 'P_MOVE_ALONE_G'] + '.');
}
else window.location = '/photo/' + pid + '/' + (vid ? '?vid=' + vid : (gid ? '?gid=' + gid : (aid ? '?aid=' + aid : (upd ? '?upd=1' : ''))));
});
});
// Показ карты
$('#showmap a').click(function()
{
$('#map').show();
$('#showmap').hide();
initMap(lat, lng, {
showNearMarkers: true,
showCenterMarker: true,
draggable: false,
dir: dir
});
return false;
});
// Голосование за фото
$('.vote_btn').click(function()
{
var vote = $(this).attr('vote');
if (vote != 0 && vote != 1) return false;
if (vote && subscr_pro) $('.toggle').attr('class', 'toggle on');
var pid = $(this).closest('.vote').attr('pid');
var savedClass1 = $('.vote_btn[vote="1"]').attr('class');
var savedClass0 = $('.vote_btn[vote="0"]').attr('class');
$(this).toggleClass('voted');
if ($(this).is('.voted')) $('.vote_btn[vote="' + Number(!Number(vote)) + '"]').removeClass('voted');
$('.loader[pid="' + pid + '"]').css('visibility', 'visible');
$.getJSON('/api/photo/vote', { action: 'vote-photo', pid: pid, vote: vote }, function(data)
{
if (data && !data.errors)
{
var signs, html = '', i, j;
for (i = 1; i >= 0; i--)
{
if (data.votes[i] && data.votes[i].length != 0)
{
console.log(i);
html += '<table class="vblock ' + (i ? 'pro' : 'con' ) + '"><col width="100%">';
for (j = 0; j < data.votes[i].length; j++)
html += '<tr><td><a href="/author/' + data.votes[i][j][0] + '/">' + data.votes[i][j][1] + '</a></td><td>' + (data.votes[i][j][2] > 0 ? '+' : '&ndash;') + '1</td></tr>';
html += '</table>';
}
}
$('#votes').html(html)[html == '' ? 'hide' : 'show']();
$('.vote_btn[vote="1"]')[data.buttons[1] ? 'addClass' : 'removeClass']('voted');
$('.vote_btn[vote="0"]')[data.buttons[0] ? 'addClass' : 'removeClass']('voted');
var rating = parseInt(data.rating);
if (rating > 0) $('#rating').html('+' + rating); else
if (rating < 0) $('#rating').html('&ndash;' + parseInt(-rating));
else $('#rating').html('0');
}
else
{
$('.vote_btn[vote="1"]').attr('class', savedClass1);
$('.vote_btn[vote="0"]').attr('class', savedClass0);
if (data.errors) alert(data.errors);
}
$('.loader[pid="' + pid + '"]').css('visibility', 'hidden');
})
.fail(function(jx) { if (jx.responseText != '') alert(jx.responseText); });
return false;
});
// Конкурсное голосование
$('.konk_btn').click(function()
{
var vote = $(this).attr('vote');
if (vote != 0 && vote != 1 || $(this).is('.locked')) return false;
var pid = $(this).closest('.vote').attr('pid');
var savedClass1 = $('.vote[pid="' + pid + '"] .konk_btn[vote="1"]').attr('class');
var savedClass0 = $('.vote[pid="' + pid + '"] .konk_btn[vote="0"]').attr('class');
$('.loader[pid="' + pid + '"]').css('visibility', 'visible');
$(this).toggleClass('voted');
if ($(this).is('.voted')) $('.vote[pid="' + pid + '"] .konk_btn[vote="' + Number(!Number(vote)) + '"]').removeClass('voted');
if (!self_p) // Чужие фото
{
$(this).closest('.p20p').removeAttr('class').css('padding', '6px 6px 5px');
$.getJSON('/api.php', { action: 'vote-konk', pid: pid, vote: vote }, function (data)
{
if (data && !data.errors)
{
$('.star[pid="' + pid + '"]').html(data.star ? '<img src="/img/star_' + data.star + '.png" alt="" />' : '');
$('.vote[pid="' + pid + '"] .konk_btn[vote="1"]')[data.buttons[1] ? 'addClass' : 'removeClass']('voted');
$('.vote[pid="' + pid + '"] .konk_btn[vote="0"]')[data.buttons[0] ? 'addClass' : 'removeClass']('voted');
var rat = $('.s_rating[pid="' + pid + '"]');
if (rat.length)
{
var rating = parseInt(data.rating);
if (rating > 0) rat.html('+' + rating); else
if (rating < 0) rat.html('&ndash;' + parseInt(-rating));
else rat.html('0');
}
}
else
{
$('.vote[pid="' + pid + '"] .konk_btn[vote="1"]').attr('class', savedClass1);
$('.vote[pid="' + pid + '"] .konk_btn[vote="0"]').attr('class', savedClass0);
if (data.errors) alert(data.errors);
}
$('.loader[pid="' + pid + '"]').css('visibility', 'hidden');
})
.fail(function(jx) { if (jx.responseText != '') alert(jx.responseText); });
}
else // Свои фото
{
$.getJSON('/api.php', { action: 'vote-author', pid: pid, vote: vote }, function (data)
{
if (data && !data.errors)
{
$('#star[pid="' + pid + '"]').html(data.star ? '<img src="/img/star_' + data.star + '.png" alt="" />' : '');
$('.konk_btn[vote="1"]')[data.buttons[1] ? 'addClass' : 'removeClass']('voted');
$('.konk_btn[vote="0"]')[data.buttons[0] ? 'addClass' : 'removeClass']('voted');
}
else
{
$('.konk_btn[vote="0"]').attr('class', savedClass0);
$('.konk_btn[vote="1"]').attr('class', savedClass1);
if (data.errors) alert(data.errors);
}
$('.loader[pid="' + pid + '"]').css('visibility', 'hidden');
})
.fail(function(jx) { if (jx.responseText != '') alert(jx.responseText); });
}
return false;
});
// Быстрый переход по фото
$(document).keydown(function(e)
{
if ($(e.target).is('input, textarea')) return;
if (e.ctrlKey)
{
switch (e.which)
{
case 0x25: window.location = '/ph.php?pid=' + pid + '&pub=0'; break;
case 0x27: window.location = '/ph.php?pid=' + pid + '&pub=1'; break;
}
}
});
// Избранное
$('#favLink').click(function()
{
var faved = parseInt($(this).attr('faved'));
$(this).html(_text[faved ? 'P_ADDFAV' : 'P_DELFAV']).attr('faved', faved ? 0 : 1);
if (!faved && subscr_fav) $('.toggle').attr('class', 'toggle on');
$.get('/api.php', { action: 'fav-photo', pid : pid }, function (r) { if (r != 0 && r != 1) alert(r); }).fail(function(jx) { if (jx.responseText != '') alert(jx.responseText); });
return false;
});
// Показ всего EXIF
var showexif = $('#showexif');
if (showexif.length > 0)
{
showexif.on('click', function()
{
$('#exif tr').show();
$('#exif tr:even').attr('class', 's11 h21');
$('#exif tr:odd').attr('class', 's1 h21');
$(this).closest('tr').hide();
return false;
});
if ($('#exif tr:visible').length == 1) showexif.click();
}
// Свёрнутые блоки в мобильной версии
$('.pp-item-header').on('click', function()
{
var header = $(this);
$('.chevron', header).toggleClass('active');
header.siblings('.pp-item-body').slideToggle('fast');
});
if ($('#pmain').is('.hidden')) $('.footer').addClass('hidden');
$('.top-disclaimer-close').on('click', function()
{
document.cookie = 'nodisclaim=1;max-age=' + (86400 * 35) + ';path=/';
$('.top-disclaimer').slideUp();
return false;
});
});
function showGrid() { $('#grid, #sh_gr, #hd_gr').toggle(); }
function showReasons() { $('#reasons').toggle(); }

View file

@ -19,6 +19,7 @@
<script src="/static/js/imageupload.js"></script>
<script src="/static/js/progress.js"></script>
<script src="/static/js/notie.js"></script>
<script src="/static/js/photo.js"></script>
<div class="progress-container fixed-top">
<span class="progress-bard"></span>
</div>

View file

@ -1,8 +1,10 @@
<?php
use App\Services\{DB, Auth, Date, Json};
use App\Models\{User, Vote};
$photo = new \App\Models\Photo(explode('/', $_SERVER['REQUEST_URI'])[2]);
$id = explode('/', $_SERVER['REQUEST_URI'])[2];
$photo = new \App\Models\Photo($id);
$photouser = new \App\Models\User($photo->i('user_id'));
?>
@ -153,202 +155,37 @@ $photouser = new \App\Models\User($photo->i('user_id'));
<h4 class="pp-item-header">Оценка</h4>
<div class="sm">
<img class="loader" pid="1361063" src="/img/loader.png">
<div class="rtext">Рейтинг: <b id="rating">+48</b></div>
<div class="rtext">Рейтинг: <b id="rating"><?=Vote::count($id)?></b></div>
<div class="star" pid="1361063"></div>
<div class="vote" pid="<?=$id?>">
<a href="#" vote="1" class="vote_btn"><span>Интересная фотография!</span></a><a href="#" vote="0" class="vote_btn"><span>Мне не&nbsp;нравится</span></a>
</div>
<div id="votes" class="votes">
<table class="vblock pro">
<tr>
<td><a href="/author/22530/">Alexey Becker</a></td>
<?php
$votespos = DB::query('SELECT * FROM photos_rates WHERE photo_id=:pid AND type=1', array(':pid'=>$id));
foreach ($votespos as $ps) {
$uservote = new User($ps['user_id']);
echo ' <tr>
<td><a href="/author/'.$ps['user_id'].'/">'.$uservote->i('username').'</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/9169/">tatra t4su</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/32862/">kostyan_piterski</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/15634/">Victor Irkut</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/11756/">Яков Фёдоров</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/3907/">Сергей Валерьевич</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/23757/">Empty Underground</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/34845/">KILATIV</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/6206/">Никита Лапин</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/26544/">Aleksandr_Urickiy</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/27099/">Silbervogel</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/21326/">VOLGA</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/25878/">Андрей Ширинкин</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/6185/">TOXA</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/8718/">Diman</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/26699/">Qwerty_qwertovich</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/25475/">Алексей ЛВ</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/33168/">Владислав Масев</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/19680/">Сергей Александров</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/31083/">Yastrebov Nikolay</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/33941/">Trains63</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/36219/">Темерник</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/862/">AVB</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/1028/">IvanPG</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/14003/">Михаил_123</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/259/">АК (Александр)</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/1464/">Mitay</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/533/">Андрей Янковский</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/369/">Юрий А.</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/4572/">Alexandr Matr</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/7686/">Томич</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/1560/">Александр Рябов</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/5075/">Etix1979</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/17149/">Timofeiikarus</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/11563/">Lasselan</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/5899/">Э.В.Ротгрифф</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/15817/">Егор Шмаков (Василий Фрескин)</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/11424/">Натаныч</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/986/">Александров Николай</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/359/">Виктор Бергман</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/12232/">Александр Vl</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/1572/">Palmer</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/20725/">Sergei 34</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/21132/">New_Wave</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/5929/">Barbar1s</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/515/">Andrew Gri-Shen</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/1676/">Сэм</a></td>
<td class="vv">+1</td>
</tr>
<tr>
<td><a href="/author/8764/">nss991</a></td>
<td class="vv">+1</td>
</tr>
</tr>';
}
?>
</table>
<table class="vblock coN">
<?php
$votespos = DB::query('SELECT * FROM photos_rates WHERE photo_id=:pid AND type=0', array(':pid'=>$id));
foreach ($votespos as $ps) {
$uservote = new User($ps['user_id']);
echo ' <tr>
<td><a href="/author/'.$ps['user_id'].'/">'.$uservote->i('username').'</a></td>
<td class="vv">-1</td>
</tr>';
}
?>
</table>
</div>
</div>