Delete notes func

This commit is contained in:
veselcraft 2021-01-16 18:19:54 -05:00
parent b08c33e092
commit b494368585
5 changed files with 53 additions and 2 deletions

View file

@ -6,7 +6,7 @@ trait TOwnable
{
function canBeModifiedBy(User $user): bool
{
if(is_callable([$this, "isCreatedBySystem"]))
if(method_exists($this, "isCreatedBySystem"))
if($this->isCreatedBySystem())
return false;

View file

@ -68,4 +68,22 @@ final class NotesPresenter extends OpenVKPresenter
$this->redirect("/note" . $this->user->id . "_" . $note->getId());
}
}
function renderDelete(int $owner, int $id): void
{
$this->assertUserLoggedIn();
$this->willExecuteWriteAction();
$this->assertNoCSRF();
$note = $this->notes->get($id);
if(!$note) $this->notFound();
if($note->getOwner()->getId() . "_" . $note->getId() !== $owner . "_" . $id || $note->isDeleted()) $this->notFound();
if(is_null($this->user) || !$note->canBeModifiedBy($this->user->identity))
$this->flashFail("err", "Ошибка доступа", "Недостаточно прав для модификации данного ресурса.");
$name = $note->getName();
$note->delete();
$this->flash("succ", "Заметка удалена", "Заметка \"$name\" была успешно удалена.");
$this->redirect("/notes" . $this->user->id);
}
}

View file

@ -39,11 +39,19 @@
</article>
<div style="width: 100%; min-height: 100px;">
<div style="float: left; min-height: 100px; width: 70%;">
{include "../components/comments.xml",
comments => $comments,
count => $cCount,
page => $cPage,
model => "notes",
parent => $note}
</div>
<div style="float: right; min-height: 100px; width: 30%;">
<h4>{_actions}</h4>
<div n:if="isset($thisUser) && $thisUser->getId() === $note->getOwner()->getId()">
<a id="_noteDelete" href="/note{$note->getOwner()->getId()}_{$note->getId()}/delete" class="profile_link" style="display:block;width:96%;">{_delete}</a>
</div>
</div>
</div>
{/block}

View file

@ -173,6 +173,8 @@ routes:
handler: "Notes->view"
- url: "/notes/create"
handler: "Notes->create"
- url: "/note{num}_{num}/delete"
handler: "Notes->delete"
- url: "/invite"
handler: "About->invite"
- url: "/away.php"

View file

@ -59,4 +59,27 @@ u("#_photoDelete").on("click", function(e) {
return e.preventDefault();
});
/* @rem-pai why this func wasn't named as "#_deleteDialog"? It looks universal IMO */
u("#_noteDelete").on("click", function(e) {
var formHtml = "<form id='tmpPhDelF' action='" + u(this).attr("href") + "' >";
formHtml += "<input type='hidden' name='hash' value='" + u("meta[name=csrf]").attr("value") + "' />";
formHtml += "</form>";
u("body").append(formHtml);
MessageBox("Внимание", "Удаление нельзя отменить. Вы действительно уверены в том что хотите сделать?", [
"Да",
"Нет"
], [
(function() {
u("#tmpPhDelF").nodes[0].submit();
}),
(function() {
u("#tmpPhDelF").remove();
}),
]);
return e.preventDefault();
});
}); //END ONREADY DECLS