Move delete form and more multi select fixes

This commit is contained in:
Alex Cabal 2024-12-19 14:40:38 -06:00
parent 93b8008c7e
commit 8e8cbb0c5e
10 changed files with 136 additions and 24 deletions

View file

@ -19,8 +19,8 @@ RewriteRule ^/collections/([^\./]+?)$ /collections/get.php?collection=$1 [QSA]
RewriteRule ^/collections/([^/]+?)/downloads$ /bulk-downloads/get.php?collection=$1
RewriteRule ^/collections/([^/]+?)/feeds$ /feeds/get.php?collection=$1
# Edit rewrites
RewriteRule ^/ebooks/(.+?)/edit$ /ebooks/edit.php?url-path=$1 [L]
RewriteRule ^/ebooks/(.+?)/delete$ /ebooks/delete.php?url-path=$1 [L]
RewriteCond expr "tolower(%{REQUEST_METHOD}) =~ /^post$/"
RewriteRule ^/ebooks/([^\.]+?)$ /ebooks/post.php?url-path=$1 [L]

View file

@ -22,6 +22,7 @@ use function Safe\shell_exec;
* @property ?array<string> $TocEntries A list of non-Roman ToC entries *only if* the work has the `se:is-a-collection` metadata element; `null` otherwise.
* @property string $Url
* @property string $EditUrl
* @property string $DeleteUrl
* @property bool $HasDownloads
* @property string $UrlSafeIdentifier
* @property string $HeroImageUrl
@ -105,6 +106,7 @@ final class Ebook{
protected ?array $_TocEntries = null;
protected string $_Url;
protected string $_EditUrl;
protected string $_DeleteUrl;
protected bool $_HasDownloads;
protected string $_UrlSafeIdentifier;
protected string $_HeroImageUrl;
@ -158,13 +160,13 @@ final class Ebook{
*/
protected function GetProjects(): array{
if(!isset($this->_Projects)){
$this->_Projects = Db::Query('
$this->_Projects = Db::MultiTableSelect('
SELECT *
from Projects
inner join Ebooks
using(EbookId)
where EbookId = ?
order by Created desc
order by Projects.Created desc
', [$this->EbookId], Project::class);
}
@ -177,7 +179,7 @@ final class Ebook{
$this->_ProjectInProgress = null;
}
else{
$this->_ProjectInProgress = Db::Query('
$this->_ProjectInProgress = Db::MultiTableSelect('
SELECT *
from Projects
inner join Ebooks
@ -200,7 +202,7 @@ final class Ebook{
$this->_PastProjects = [];
}
else{
$this->_PastProjects = Db::Query('
$this->_PastProjects = Db::MultiTableSelect('
SELECT *
from Projects
inner join Ebooks
@ -425,6 +427,14 @@ final class Ebook{
return $this->_EditUrl;
}
protected function GetDeleteUrl(): string{
if(!isset($this->_DeleteUrl)){
$this->_DeleteUrl = $this->Url . '/delete';
}
return $this->_DeleteUrl;
}
protected function GetHasDownloads(): bool{
if(!isset($this->_HasDownloads)){
$this->_HasDownloads = $this->EpubUrl || $this->AdvancedEpubUrl || $this->KepubUrl || $this->Azw3Url;

View file

@ -677,7 +677,7 @@ final class Project{
public static function FromMultiTableRow(array $row): Project{
$object = Project::FromRow($row['Projects']);
if($row['Ebooks']->EbookId !== null){
if($row['Projects']->EbookId !== null){
$row['Ebooks']->EbookId = $object->EbookId;
$object->Ebook = Ebook::FromRow($row['Ebooks']);
}

View file

@ -20,17 +20,11 @@ $showPlaceholderMetadata = $showPlaceholderMetadata ?? false;
<? if($showPlaceholderMetadata && $ebook->IsPlaceholder() && $ebook->EbookPlaceholder !== null){ ?>
<section id="placeholder-metadata">
<h2>Placeholder metadata</h2>
<p><a href="<?= $ebook->EditUrl ?>">Edit placeholder</a></p>
<details>
<summary>Delete placeholder</summary>
<form method="<?= Enums\HttpMethod::Post->value ?>" action="<?= $ebook->Url ?>">
<input type="hidden" name="_method" value="<?= Enums\HttpMethod::Delete->value ?>" />
<p>Delete the <code>Ebook</code>, <code>EbookPlaceholder</code>, <code>Project</code>, and other related objects.</p>
<div class="footer">
<button class="delete">Delete</button>
</div>
</form>
</details>
<p>
<a href="<?= $ebook->EditUrl ?>">Edit placeholder</a>
<a href="<?= $ebook->DeleteUrl ?>">Delete placeholder</a>
</p>
<table class="admin-table">
<tbody>
<tr>

View file

@ -0,0 +1,69 @@
<?
use function Safe\session_unset;
session_start();
/** @var string $identifier Passed from script this is included from. */
$ebook = HttpInput::SessionObject('ebook', Ebook::class);
$exception = HttpInput::SessionObject('exception', Exceptions\AppException::class);
try{
if($ebook === null){
$ebook = Ebook::GetByIdentifier($identifier);
}
if(!$ebook->IsPlaceholder() || $ebook->EbookPlaceholder === null){
throw new Exceptions\EbookNotFoundException();
}
if(Session::$User === null){
throw new Exceptions\LoginRequiredException();
}
if(!Session::$User->Benefits->CanEditEbookPlaceholders){
throw new Exceptions\InvalidPermissionsException();
}
if($exception){
http_response_code(Enums\HttpCode::UnprocessableContent->value);
session_unset();
}
}
catch(Exceptions\EbookNotFoundException){
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
catch(Exceptions\LoginRequiredException){
Template::RedirectToLogin();
}
catch(Exceptions\InvalidPermissionsException){
Template::ExitWithCode(Enums\HttpCode::Forbidden);
}
?>
<?= Template::Header(
[
'title' => 'Delete ' . $ebook->Title,
'css' => ['/css/ebook-placeholder.css'],
'highlight' => '',
'description' => 'Delete ' . $ebook->Title
]
) ?>
<main>
<section class="narrow">
<nav class="breadcrumbs">
<a href="<?= $ebook->AuthorsUrl ?>"><?= $ebook->AuthorsString ?></a> →
<a href="<?= $ebook->Url ?>"><?= Formatter::EscapeHtml($ebook->Title) ?></a> →
</nav>
<h1>Delete</h1>
<?= Template::Error(['exception' => $exception]) ?>
<form method="<?= Enums\HttpMethod::Post->value ?>" action="<?= $ebook->Url ?>">
<input type="hidden" name="_method" value="<?= Enums\HttpMethod::Delete->value ?>" />
<p>Are you sure you want to permanently delete <i><?= Formatter::EscapeHtml($ebook->Title) ?></i>?</p>
<div class="footer">
<button class="delete">Delete</button>
</div>
</form>
</section>
</main>
<?= Template::Footer() ?>

View file

@ -41,15 +41,19 @@ catch(Exceptions\InvalidPermissionsException){
?>
<?= Template::Header(
[
'title' => 'Edit Ebook Placeholder for ' . $ebook->Title,
'title' => 'Edit ' . $ebook->Title,
'css' => ['/css/ebook-placeholder.css'],
'highlight' => '',
'description' => 'Edit the ebook placeholder for ' . $ebook->Title
'description' => 'Edit ' . $ebook->Title
]
) ?>
<main>
<section class="narrow">
<h1>Edit Ebook Placeholder</h1>
<nav class="breadcrumbs">
<a href="<?= $ebook->AuthorsUrl ?>"><?= $ebook->AuthorsString ?></a> →
<a href="<?= $ebook->Url ?>"><?= Formatter::EscapeHtml($ebook->Title) ?></a> →
</nav>
<h1>Edit</h1>
<?= Template::Error(['exception' => $exception]) ?>

35
www/ebooks/delete.php Normal file
View file

@ -0,0 +1,35 @@
<?
$ebook = null;
try{
if(Session::$User === null){
throw new Exceptions\LoginRequiredException();
}
if(!Session::$User->Benefits->CanEditEbookPlaceholders){
throw new Exceptions\InvalidPermissionsException();
}
$identifier = EBOOKS_IDENTIFIER_PREFIX . trim(str_replace('.', '', HttpInput::Str(GET, 'url-path') ?? ''), '/');
$ebook = Ebook::GetByIdentifier($identifier);
if($ebook->IsPlaceholder()){
require(WEB_ROOT . '/ebook-placeholders/delete.php');
exit();
}
// Deleting published `Ebooks` is not supported.
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
catch(Exceptions\EbookNotFoundException){
Template::ExitWithCode(Enums\HttpCode::NotFound);
}
catch(Exceptions\LoginRequiredException){
Template::RedirectToLogin();
}
catch(Exceptions\InvalidPermissionsException){
Template::ExitWithCode(Enums\HttpCode::Forbidden);
}

View file

@ -16,7 +16,7 @@ try{
$ebook = Ebook::GetByIdentifier($identifier);
if($ebook->IsPlaceholder()){
require('/standardebooks.org/web/www/ebook-placeholders/edit.php');
require(WEB_ROOT . '/ebook-placeholders/edit.php');
exit();
}

View file

@ -20,7 +20,7 @@ try{
$ebook = Ebook::GetByIdentifier($identifier);
if($ebook->IsPlaceholder()){
require('/standardebooks.org/web/www/ebook-placeholders/get.php');
require(WEB_ROOT . '/ebook-placeholders/get.php');
exit();
}
@ -71,7 +71,7 @@ catch(Exceptions\EbookNotFoundException){
// Are we accessing a placeholder for a Public Domain Day book that is not yet released?
if(array_key_exists($identifier, PD_DAY_EBOOKS)){
require('/standardebooks.org/web/www/ebooks/public-domain-day-placeholder.php');
require(WEB_ROOT . '/ebooks/public-domain-day-placeholder.php');
exit();
}

View file

@ -16,7 +16,7 @@ try{
$ebook = Ebook::GetByIdentifier($identifier);
if($ebook->IsPlaceholder()){
require('/standardebooks.org/web/www/ebook-placeholders/post.php');
require(WEB_ROOT . '/ebook-placeholders/post.php');
exit();
}