Add /edit endpoint to update placeholders

This commit is contained in:
Mike Colagrosso 2024-12-17 21:06:01 -07:00 committed by Alex Cabal
parent b125758138
commit f449c024ea
7 changed files with 297 additions and 73 deletions

View file

@ -19,6 +19,12 @@ 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]
RewriteCond expr "tolower(%{REQUEST_METHOD}) =~ /^post$/"
RewriteRule ^/ebooks/([^\.]+?)$ /ebooks/post.php?url-path=$1 [L]
# List of specific URL rewrites
RewriteRule ^/contribute/accepted-ebooks/? /contribute/collections-policy [R=301,L]
RewriteRule ^/ebooks/aristotle/the-nicomachean-ethics(/?$|/.+?$) /ebooks/aristotle/nicomachean-ethics$1 [R=301,L]

View file

@ -21,6 +21,7 @@ use function Safe\shell_exec;
* @property array<Contributor> $Contributors
* @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 bool $HasDownloads
* @property string $UrlSafeIdentifier
* @property string $HeroImageUrl
@ -99,6 +100,7 @@ class Ebook{
/** @var ?array<string> $_TocEntries */
protected ?array $_TocEntries = null;
protected string $_Url;
protected string $_EditUrl;
protected bool $_HasDownloads;
protected string $_UrlSafeIdentifier;
protected string $_HeroImageUrl;
@ -365,6 +367,14 @@ class Ebook{
return $this->_Url;
}
protected function GetEditUrl(): string{
if(!isset($this->_EditUrl)){
$this->_EditUrl = $this->Url . '/edit';
}
return $this->_EditUrl;
}
protected function GetHasDownloads(): bool{
if(!isset($this->_HasDownloads)){
$this->_HasDownloads = $this->EpubUrl || $this->AdvancedEpubUrl || $this->KepubUrl || $this->Azw3Url;
@ -1059,7 +1069,7 @@ class Ebook{
*
* @throws Exceptions\InvalidEbookIdentifierException
*/
public function FillIdentifierFromTitleAndContributors(): void{
protected function FillIdentifierFromTitleAndContributors(): void{
if(!isset($this->Authors) || sizeof($this->Authors) == 0){
throw new Exceptions\InvalidEbookIdentifierException('Authors required');
}
@ -1092,6 +1102,78 @@ class Ebook{
}
}
/**
* Populates `EbookPlaceholder` and other fields from `Template::EbookPlaceholderForm()`.
*
* @throws Exceptions\InvalidEbookIdentifierException
*/
public function FillFromEbookPlaceholderForm(): void{
$title = HttpInput::Str(POST, 'ebook-title');
if(isset($title)){
$this->Title = $title;
}
$authors = [];
$authorFields = ['author-name-1', 'author-name-2', 'author-name-3'];
foreach($authorFields as $authorField){
$authorName = HttpInput::Str(POST, $authorField);
if(!isset($authorName)){
continue;
}
$author = new Contributor();
$author->Name = $authorName;
$author->UrlName = Formatter::MakeUrlSafe($author->Name);
$author->MarcRole = Enums\MarcRole::Author;
$authors[] = $author;
}
$this->Authors = $authors;
$translators = [];
$translatorFields = ['translator-name-1', 'translator-name-2'];
foreach($translatorFields as $translatorField){
$translatorName = HttpInput::Str(POST, $translatorField);
if(!isset($translatorName)){
continue;
}
$translator = new Contributor();
$translator->Name = $translatorName;
$translator->UrlName = Formatter::MakeUrlSafe($translator->Name);
$translator->MarcRole = Enums\MarcRole::Translator;
$translators[] = $translator;
}
$this->Translators = $translators;
$collectionMemberships = [];
$collectionNameFields = ['collection-name-1', 'collection-name-2', 'collection-name-3'];
foreach($collectionNameFields as $collectionNameField){
$collectionName = HttpInput::Str(POST, $collectionNameField);
if(!isset($collectionName)){
continue;
}
$collectionSequenceNumber = HttpInput::Int(POST, 'sequence-number-' . $collectionNameField);
$collection = Collection::FromName($collectionName);
$collection->Type = Enums\CollectionType::tryFrom(HttpInput::Str(POST, 'type-' . $collectionNameField) ?? '');
$cm = new CollectionMembership();
$cm->Collection = $collection;
$cm->SequenceNumber = $collectionSequenceNumber;
$collectionMemberships[] = $cm;
}
$this->CollectionMemberships = $collectionMemberships;
$ebookPlaceholder = new EbookPlaceholder();
$ebookPlaceholder->FillFromHttpPost();
$this->EbookPlaceholder = $ebookPlaceholder;
// These properties must be set before calling `Ebook::Create()` to prevent the getters from triggering DB queries or accessing `Ebook::$EbookId` before it is set.
$this->Contributors = [];
$this->Illustrators = [];
$this->LocSubjects = [];
$this->Tags = [];
$this->TocEntries = [];
$this->FillIdentifierFromTitleAndContributors();
}
// *******
// METHODS

View file

@ -0,0 +1,64 @@
<?
use function Safe\session_unset;
session_start();
$exception = HttpInput::SessionObject('exception', Exceptions\AppException::class);
/** @var string $identifier Passed from script this is included from. */
$ebook = HttpInput::SessionObject('ebook', Ebook::class);
try{
if(Session::$User === null){
throw new Exceptions\LoginRequiredException();
}
if(!Session::$User->Benefits->CanEditEbookPlaceholders){
throw new Exceptions\InvalidPermissionsException();
}
if($ebook === null){
$ebook = Ebook::GetByIdentifier($identifier);
}
if(!$ebook->IsPlaceholder() || $ebook->EbookPlaceholder === null){
throw new Exceptions\EbookNotFoundException();
}
if($exception){
http_response_code(Enums\HttpCode::UnprocessableContent->value);
session_unset();
}
}
catch(Exceptions\EbookNotFoundException){
Template::Emit404();
}
catch(Exceptions\LoginRequiredException){
Template::RedirectToLogin();
}
catch(Exceptions\InvalidPermissionsException){
Template::Emit403();
}
?>
<?= Template::Header(
[
'title' => 'Edit Ebook Placeholder for ' . $ebook->Title,
'css' => ['/css/ebook-placeholder.css'],
'highlight' => '',
'description' => 'Edit the ebook placeholder for ' . $ebook->Title
]
) ?>
<main>
<section class="narrow">
<h1>Edit Ebook Placeholder</h1>
<?= Template::Error(['exception' => $exception]) ?>
<form class="create-update-ebook-placeholder" method="<?= Enums\HttpMethod::Post->value ?>" action="<?= $ebook->Url ?>" autocomplete="off">
<input type="hidden" name="_method" value="<?= Enums\HttpMethod::Put->value ?>" />
<?= Template::EbookPlaceholderForm(['ebook' => $ebook]) ?>
</form>
</section>
</main>
<?= Template::Footer() ?>

View file

@ -1,15 +1,24 @@
<?
use function Safe\preg_replace;
use function Safe\session_unset;
session_start();
/** @var string $identifier Passed from script this is included from. */
$ebook = null;
$isSaved = HttpInput::Bool(SESSION, 'is-ebook-placeholder-saved') ?? false;
try{
$ebook = Ebook::GetByIdentifier($identifier);
if($ebook->EbookPlaceholder === null){
throw new Exceptions\EbookNotFoundException();
}
if($isSaved){
session_unset();
}
}
catch(Exceptions\EbookNotFoundException){
Template::Emit404();
@ -53,6 +62,10 @@ catch(Exceptions\EbookNotFoundException){
</hgroup>
</header>
<? if($isSaved){ ?>
<p class="message success">Ebook Placeholder saved!</p>
<? } ?>
<aside id="reading-ease">
<? if($ebook->ContributorsHtml != ''){ ?>
<p><?= $ebook->ContributorsHtml ?></p>
@ -109,6 +122,11 @@ catch(Exceptions\EbookNotFoundException){
<? if(Session::$User?->Benefits->CanEditProjects || Session::$User?->Benefits->CanManageProjects || Session::$User?->Benefits->CanReviewProjects){ ?>
<?= Template::EbookProjects(['ebook' => $ebook, 'showAddButton' => Session::$User->Benefits->CanEditProjects && $ebook->ProjectInProgress === null]) ?>
<? } ?>
<? if(Session::$User?->Benefits->CanEditEbookPlaceholders){ ?>
<h2>Edit ebook placeholder</h2>
<p><a href="<?= $ebook->EditUrl ?>">Edit this ebook placeholder.</a></p>
<? } ?>
</article>
</main>
<?= Template::Footer() ?>

View file

@ -1,82 +1,30 @@
<?
/** @var string $identifier Passed from script this is included from. */
$ebook = null;
try{
session_start();
$httpMethod = HttpInput::ValidateRequestMethod([Enums\HttpMethod::Post]);
$httpMethod = HttpInput::ValidateRequestMethod([Enums\HttpMethod::Post, Enums\HttpMethod::Put]);
$exceptionRedirectUrl = '/ebook-placeholders/new';
if(Session::$User === null){
throw new Exceptions\LoginRequiredException();
}
if(!Session::$User->Benefits->CanEditEbookPlaceholders){
throw new Exceptions\InvalidPermissionsException();
}
// POSTing a new ebook placeholder.
if($httpMethod == Enums\HttpMethod::Post){
if(!Session::$User->Benefits->CanEditEbookPlaceholders){
throw new Exceptions\InvalidPermissionsException();
}
$ebook = new Ebook();
$title = HttpInput::Str(POST, 'ebook-title');
if(isset($title)){
$ebook->Title = $title;
}
$authors = [];
$authorFields = ['author-name-1', 'author-name-2', 'author-name-3'];
foreach($authorFields as $authorField){
$authorName = HttpInput::Str(POST, $authorField);
if(!isset($authorName)){
continue;
}
$author = new Contributor();
$author->Name = $authorName;
$author->UrlName = Formatter::MakeUrlSafe($author->Name);
$author->MarcRole = Enums\MarcRole::Author;
$authors[] = $author;
}
$ebook->Authors = $authors;
$translators = [];
$translatorFields = ['translator-name-1', 'translator-name-2'];
foreach($translatorFields as $translatorField){
$translatorName = HttpInput::Str(POST, $translatorField);
if(!isset($translatorName)){
continue;
}
$translator = new Contributor();
$translator->Name = $translatorName;
$translator->UrlName = Formatter::MakeUrlSafe($translator->Name);
$translator->MarcRole = Enums\MarcRole::Translator;
$translators[] = $translator;
}
$ebook->Translators = $translators;
$collectionMemberships = [];
$collectionNameFields = ['collection-name-1', 'collection-name-2', 'collection-name-3'];
foreach($collectionNameFields as $collectionNameField){
$collectionName = HttpInput::Str(POST, $collectionNameField);
if(!isset($collectionName)){
continue;
}
$collectionSequenceNumber = HttpInput::Int(POST, 'sequence-number-' . $collectionNameField);
$collection = Collection::FromName($collectionName);
$collection->Type = Enums\CollectionType::tryFrom(HttpInput::Str(POST, 'type-' . $collectionNameField) ?? '');
$cm = new CollectionMembership();
$cm->Collection = $collection;
$cm->SequenceNumber = $collectionSequenceNumber;
$collectionMemberships[] = $cm;
}
$ebook->CollectionMemberships = $collectionMemberships;
$ebookPlaceholder = new EbookPlaceholder();
$ebookPlaceholder->FillFromHttpPost();
$ebook->EbookPlaceholder = $ebookPlaceholder;
$ebook->FillFromEbookPlaceholderForm();
// Do we have a `Project` to create at the same time?
$project = null;
if($ebookPlaceholder->IsInProgress){
if($ebook->EbookPlaceholder?->IsInProgress){
$project = new Project();
$project->FillFromHttpPost();
$project->Started = NOW;
@ -84,21 +32,13 @@ try{
$project->Validate();
}
$ebook->FillIdentifierFromTitleAndContributors();
// These properties must be set before calling `Ebook::Create()` to prevent the getters from triggering DB queries or accessing `Ebook::$EbookId` before it is set.
$ebook->Tags = [];
$ebook->LocSubjects = [];
$ebook->Illustrators = [];
$ebook->Contributors = [];
try{
$ebook->Create();
}
catch(Exceptions\DuplicateEbookException $ex){
// If the identifier already exists but a `Project` was sent with this request, create the `Project` anyway.
$existingEbook = Ebook::GetByIdentifier($ebook->Identifier);
if($ebookPlaceholder->IsInProgress && $project !== null){
if($ebook->EbookPlaceholder?->IsInProgress && $existingEbook->ProjectInProgress === null && $project !== null){
$ebook->EbookId = $existingEbook->EbookId;
$_SESSION['is-only-ebook-project-created'] = true;
}
@ -109,7 +49,7 @@ try{
}
}
if($ebookPlaceholder->IsInProgress && $project !== null){
if($ebook->EbookPlaceholder?->IsInProgress && $project !== null){
$project->EbookId = $ebook->EbookId;
$project->Ebook = $ebook;
$project->Create();
@ -121,6 +61,50 @@ try{
http_response_code(Enums\HttpCode::SeeOther->value);
header('Location: /ebook-placeholders/new');
}
// PUT a new ebook placeholder.
if($httpMethod == Enums\HttpMethod::Put){
$originalEbook = Ebook::GetByIdentifier($identifier);
$exceptionRedirectUrl = $originalEbook->EditUrl;
$ebook = new Ebook();
$ebook->FillFromEbookPlaceholderForm();
$ebook->EbookId = $originalEbook->EbookId;
$ebook->Created = $originalEbook->Created;
// Do we have a `Project` to create/save at the same time?
$project = null;
if($ebook->EbookPlaceholder?->IsInProgress){
$originalProject = $originalEbook->ProjectInProgress;
$project = new Project();
$project->FillFromHttpPost();
$project->EbookId = $ebook->EbookId;
$project->Ebook = $ebook;
if(isset($originalProject)){
$project->ProjectId = $originalProject->ProjectId;
$project->Started = $originalProject->Started;
}
else{
$project->Started = NOW;
}
$project->Validate();
}
$ebook->Save();
if($ebook->EbookPlaceholder?->IsInProgress && $project !== null){
if(isset($originalProject)){
$project->Save();
}
else{
$project->Create();
}
}
$_SESSION['is-ebook-placeholder-saved'] = true;
http_response_code(Enums\HttpCode::SeeOther->value);
header('Location: ' . $ebook->Url);
}
}
catch(Exceptions\LoginRequiredException){
Template::RedirectToLogin();

35
www/ebooks/edit.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('/standardebooks.org/web/www/ebook-placeholders/edit.php');
exit();
}
// Editing published `Ebooks` is not supported.
Template::Emit404();
}
catch(Exceptions\EbookNotFoundException){
Template::Emit404();
}
catch(Exceptions\LoginRequiredException){
Template::RedirectToLogin();
}
catch(Exceptions\InvalidPermissionsException){
Template::Emit403();
}

35
www/ebooks/post.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('/standardebooks.org/web/www/ebook-placeholders/post.php');
exit();
}
// POSTing published `Ebooks` is not supported.
Template::Emit404();
}
catch(Exceptions\EbookNotFoundException){
Template::Emit404();
}
catch(Exceptions\LoginRequiredException){
Template::RedirectToLogin();
}
catch(Exceptions\InvalidPermissionsException){
Template::Emit403();
}