Support: Fix answers evaluating

Now it works
This commit is contained in:
Maxim Leshchenko 2021-12-16 20:25:42 +02:00
parent 26ddfada05
commit e00e3b9097
No known key found for this signature in database
GPG key ID: BB9C44A8733FBEEE
3 changed files with 35 additions and 31 deletions

View file

@ -2,7 +2,7 @@
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Util\DateTime; use openvk\Web\Util\DateTime;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
use openvk\Web\Models\Repositories\{Users, SupportAliases}; use openvk\Web\Models\Repositories\{Users, SupportAliases, Tickets};
class TicketComment extends RowModel class TicketComment extends RowModel
{ {
@ -29,6 +29,11 @@ class TicketComment extends RowModel
{ {
return (new Users)->get($this->getRecord()->user_id); return (new Users)->get($this->getRecord()->user_id);
} }
function getTicket(): Ticket
{
return (new Tickets)->get($this->getRecord()->ticket_id);
}
function getAuthorName(): string function getAuthorName(): string
{ {
@ -116,17 +121,10 @@ class TicketComment extends RowModel
function isLikedByUser(): ?bool function isLikedByUser(): ?bool
{ {
$mark = $this->getMark(); $mark = $this->getMark();
switch ($mark) { if(is_null($mark))
case 0: return NULL;
return false; else
break; return $mark === 1;
case 1:
return true;
break;
default:
return NULL;
break;
}
} }
use Traits\TRichText; use Traits\TRichText;

View file

@ -31,12 +31,11 @@ final class SupportPresenter extends OpenVKPresenter
$tickets = $this->tickets->getTicketsByuId($this->user->id); $tickets = $this->tickets->getTicketsByuId($this->user->id);
if($tickets) if($tickets)
$this->template->tickets = $tickets; $this->template->tickets = $tickets;
if($_SERVER["REQUEST_METHOD"] === "POST") { if($_SERVER["REQUEST_METHOD"] === "POST") {
if(!empty($this->postParam("name")) && !empty($this->postParam("text"))) { if(!empty($this->postParam("name")) && !empty($this->postParam("text"))) {
$this->assertNoCSRF(); $this->assertNoCSRF();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$ticket = new Ticket; $ticket = new Ticket;
$ticket->setType(0); $ticket->setType(0);
$ticket->setUser_id($this->user->id); $ticket->setUser_id($this->user->id);
@ -230,18 +229,19 @@ final class SupportPresenter extends OpenVKPresenter
{ {
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$this->assertUserLoggedIn(); $this->assertUserLoggedIn();
$this->assertNoCSRF();
$comment = $this->comments->get($id); $comment = $this->comments->get($id);
if($this->user->id !== $this->tickets->get($comment->getTicketId())->getUser()->getId()) if($this->user->id !== $comment->getTicket()->getUser()->getId())
exit(header("HTTP/1.1 403 Forbidden")); exit(header("HTTP/1.1 403 Forbidden"));
if($mark === 1 || $mark === 2) if($mark !== 1 && $mark !== 2)
header("HTTP/1.1 200 OK");
else
exit(header("HTTP/1.1 400 Bad Request")); exit(header("HTTP/1.1 400 Bad Request"));
$comment->setMark($mark); $comment->setMark($mark);
$comment->save(); $comment->save();
exit(header("HTTP/1.1 200 OK"));
} }
} }

View file

@ -8,7 +8,7 @@
{block content} {block content}
<script> <script>
function markAnswer(id, mark) { function markAnswer(id, mark) {
let url = "/support/comment/" + id + "/rate/" + mark; let url = "/support/comment/" + id + "/rate/" + mark + "?hash=" + {urlencode($csrfToken)};
$.ajax(url, { $.ajax(url, {
error: errorHandler, error: errorHandler,
success: success(id, mark) success: success(id, mark)
@ -16,11 +16,12 @@
} }
function success(id, mark) { function success(id, mark) {
if(mark == 1) { if(mark == 1)
document.getElementById("markText-" + id).innerHTML = {_support_good_answer_user}; document.getElementById("markText-" + id).innerHTML = {_support_good_answer_user};
} else { else
document.getElementById("markText-" + id).innerHTML = {_support_bad_answer_user}; document.getElementById("markText-" + id).innerHTML = {_support_bad_answer_user};
}
document.getElementById("markLinks-" + id).remove();
} }
function errorHandler(id, mark) { function errorHandler(id, mark) {
@ -120,18 +121,23 @@
{if $comment->getUType() === 1} {if $comment->getUType() === 1}
<div class="post-menu"> <div class="post-menu">
{var isLikedByUser = $comment->isLikedByUser()}
<strong id="markText-{$comment->getId()}"> <strong id="markText-{$comment->getId()}">
{if $comment->isLikedByUser()} {if !is_null($isLikedByUser)}
{_support_good_answer_user} {if $comment->isLikedByUser()}
{else} {_support_good_answer_user}
{_support_bad_answer_user} {else}
{_support_bad_answer_user}
{/if}
{/if} {/if}
</strong> </strong>
{if $comment->isLikedByUser() === null} <div id="markLinks-{$comment->getId()}">
<a onClick="markAnswer({$comment->getId()}, 1)">{_support_rate_good_answer}</a> {if is_null($isLikedByUser)}
| <a onClick="markAnswer({$comment->getId()}, 1)">{_support_rate_good_answer}</a>
<a onClick="markAnswer({$comment->getId()}, 2)">{_support_rate_bad_answer}</a> |
{/if} <a onClick="markAnswer({$comment->getId()}, 2)">{_support_rate_bad_answer}</a>
{/if}
</div>
{/if} {/if}
</div> </div>
</div> </div>