mirror of
https://github.com/claradex/nativegallery.git
synced 2025-02-23 12:12:11 +03:00
update rate contest photos
This commit is contained in:
parent
0581edcc3d
commit
4ee7f4b79b
2 changed files with 58 additions and 14 deletions
|
@ -3,7 +3,7 @@
|
||||||
namespace App\Controllers\Api\Images;
|
namespace App\Controllers\Api\Images;
|
||||||
|
|
||||||
use App\Services\{Auth, Router, GenerateRandomStr, DB, Json, EXIF};
|
use App\Services\{Auth, Router, GenerateRandomStr, DB, Json, EXIF};
|
||||||
use App\Models\{User, Vote};
|
use App\Models\{User, Vote, VoteContest};
|
||||||
|
|
||||||
class Rate
|
class Rate
|
||||||
{
|
{
|
||||||
|
@ -14,28 +14,29 @@ class Rate
|
||||||
$photoId = $_GET['pid'];
|
$photoId = $_GET['pid'];
|
||||||
$voteType = (int) $_GET['vote'];
|
$voteType = (int) $_GET['vote'];
|
||||||
$contest = (isset($_GET['action']) && $_GET['action'] === 'vote-konk') ? 1 : 0;
|
$contest = (isset($_GET['action']) && $_GET['action'] === 'vote-konk') ? 1 : 0;
|
||||||
|
$contestId = $_GET['cid'];
|
||||||
|
|
||||||
if ($contest === 1) {
|
if ($contest === 1) {
|
||||||
if (Vote::photoContest($userId, $photoId) === -1) {
|
if (VoteContest::photo($userId, $photoId, $contestId) === -1) {
|
||||||
DB::query(
|
DB::query(
|
||||||
'INSERT INTO photos_rates (id, user_id, photo_id, type, contest) VALUES (NULL, :id, :pid, :type, 1)',
|
'INSERT INTO photos_rates_contest (id, user_id, photo_id, type, contest_id) VALUES (NULL, :id, :pid, :type, :cid)',
|
||||||
[':id' => $userId, ':pid' => $photoId, ':type' => $voteType]
|
[':id' => $userId, ':pid' => $photoId, ':type' => $voteType, ':cid'=>$contestId]
|
||||||
);
|
);
|
||||||
if (Vote::photoContest($userId, $photoId) != $voteType) {
|
if (VoteContest::photo($userId, $photoId, $contestId) != $voteType) {
|
||||||
DB::query(
|
DB::query(
|
||||||
'DELETE FROM photos_rates WHERE user_id=:id AND photo_id=:pid AND type=:type AND contest=1',
|
'DELETE FROM photos_rates_contest WHERE user_id=:id AND photo_id=:pid AND type=:type AND contest_id=:cid',
|
||||||
[':id' => $userId, ':pid' => $photoId, ':type' => Vote::photo($userId, $photoId)]
|
[':id' => $userId, ':pid' => $photoId, ':type' => VoteContest::photo($userId, $photoId, $contestId), ':cid'=>$contestId]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} elseif (Vote::photoContest($userId, $photoId) === $voteType) {
|
} elseif (VoteContest::photo($userId, $photoId, $contestId) === $voteType) {
|
||||||
DB::query(
|
DB::query(
|
||||||
'DELETE FROM photos_rates WHERE user_id=:id AND photo_id=:pid AND contest=1',
|
'DELETE FROM photos_rates_contest WHERE user_id=:id AND photo_id=:pid AND contest_id=:cid',
|
||||||
[':id' => $userId, ':pid' => $photoId]
|
[':id' => $userId, ':pid' => $photoId, ':cid'=>$contestId]
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
DB::query(
|
DB::query(
|
||||||
'UPDATE photos_rates SET type=:type WHERE user_id=:id AND photo_id=:pid AND contest=1',
|
'UPDATE photos_rates_contest SET type=:type WHERE user_id=:id AND photo_id=:pid AND contest_id=:cid',
|
||||||
[':id' => $userId, ':pid' => $photoId, ':type' => $voteType]
|
[':id' => $userId, ':pid' => $photoId, ':type' => $voteType, ':cid'=>$contestId]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -77,7 +78,13 @@ class Rate
|
||||||
}
|
}
|
||||||
|
|
||||||
$currentVote = Vote::photo($userId, $photoId);
|
$currentVote = Vote::photo($userId, $photoId);
|
||||||
$contCurrentVote = Vote::photoContest($userId, $photoId);
|
$contCurrentVote = VoteContest::photo($userId, $photoId, $contestId);
|
||||||
|
|
||||||
|
if ($contest === 0) {
|
||||||
|
$count = Vote::count($photoId);
|
||||||
|
} else {
|
||||||
|
$count = VoteContest::count($photoId, $contestId);
|
||||||
|
}
|
||||||
$result = [
|
$result = [
|
||||||
'buttons' => [
|
'buttons' => [
|
||||||
'negbtn' => $currentVote === 0,
|
'negbtn' => $currentVote === 0,
|
||||||
|
@ -86,7 +93,7 @@ class Rate
|
||||||
'posbtn_contest' => $contCurrentVote === 1,
|
'posbtn_contest' => $contCurrentVote === 1,
|
||||||
],
|
],
|
||||||
'errors' => '',
|
'errors' => '',
|
||||||
'rating' => Vote::count($photoId),
|
'rating' => $count,
|
||||||
'votes' => [
|
'votes' => [
|
||||||
1 => $formattedVotesPos,
|
1 => $formattedVotesPos,
|
||||||
0 => $formattedVotesNeg
|
0 => $formattedVotesNeg
|
||||||
|
|
37
app/Models/VoteContest.php
Normal file
37
app/Models/VoteContest.php
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
<?php
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Services\{DB, GenerateRandomStr};
|
||||||
|
|
||||||
|
class VoteContest
|
||||||
|
{
|
||||||
|
public static function photo($user_id, $pid, $cid)
|
||||||
|
{
|
||||||
|
$result = DB::query('SELECT type FROM photos_rates_contest WHERE user_id=:uid AND photo_id=:pid AND contest_id=:id', array(':uid' => $user_id, ':pid' => $pid, ':id'=>$cid));
|
||||||
|
if (!empty($result)) {
|
||||||
|
$type = $result[0]['type'];
|
||||||
|
if ($type < 0) {
|
||||||
|
$type = -1;
|
||||||
|
}
|
||||||
|
return $type;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function count($pid, $cid) {
|
||||||
|
$result = DB::query('SELECT * FROM photos_rates_contest WHERE photo_id=:pid AND contest_id=:id', array(':pid' => $pid, ':id'=>$cid));
|
||||||
|
$votes = 0;
|
||||||
|
foreach ($result as $r) {
|
||||||
|
if ($r['type'] === 1) {
|
||||||
|
$votes++;
|
||||||
|
} else {
|
||||||
|
$votes--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $votes;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
?>
|
Loading…
Reference in a new issue