Benefits->CanEditEbookPlaceholders){ throw new Exceptions\InvalidPermissionsException(); } // POSTing an `EbookPlaceholder`. if($httpMethod == Enums\HttpMethod::Post){ $ebook = new Ebook(); $ebook->FillFromEbookPlaceholderForm(); // Do we have a `Project` to create at the same time? $project = null; if($ebook->EbookPlaceholder?->IsInProgress){ $project = new Project(); $project->FillFromHttpPost(); $project->Started = NOW; $project->Validate(true, true); } try{ $ebook->Create(); } catch(Exceptions\EbookExistsException $ex){ $ebook = Ebook::GetByIdentifier($ebook->Identifier); // An existing `EbookPlaceholder` already exists. $ex = new Exceptions\EbookPlaceholderExistsException('An ebook placeholder already exists for this book: ' . Formatter::EscapeHtml($ebook->Title) . '.'); $ex->MessageType = Enums\ExceptionMessageType::Html; throw $ex; } if($ebook->EbookPlaceholder?->IsInProgress && $project !== null){ $project->EbookId = $ebook->EbookId; $project->Ebook = $ebook; $project->Create(); } $_SESSION['ebook'] = $ebook; $_SESSION['is-ebook-placeholder-created'] = true; http_response_code(Enums\HttpCode::SeeOther->value); header('Location: /ebook-placeholders/new'); } // PUT an `EbookPlaceholder`. 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; if(isset($ebook->EbookPlaceholder) && isset($originalEbook->EbookPlaceholder)){ $ebook->EbookPlaceholder->IsInProgress = $originalEbook->EbookPlaceholder->IsInProgress; } try{ $ebook->Save(); } catch(Exceptions\EbookExistsException){ throw new Exceptions\EbookPlaceholderExistsException(); } $_SESSION['is-ebook-placeholder-saved'] = true; http_response_code(Enums\HttpCode::SeeOther->value); header('Location: ' . $ebook->Url); } // DELETE an `EbookPlaceholder`. if($httpMethod == Enums\HttpMethod::Delete){ $ebook = Ebook::GetByIdentifier($identifier); $ebook->Delete(); $_SESSION['ebook'] = $ebook; $_SESSION['is-ebook-placeholder-deleted'] = true; http_response_code(Enums\HttpCode::SeeOther->value); header('Location: /ebook-placeholders/new'); } } catch(Exceptions\LoginRequiredException){ Template::RedirectToLogin(); } catch(Exceptions\InvalidPermissionsException | Exceptions\HttpMethodNotAllowedException){ Template::ExitWithCode(Enums\HttpCode::Forbidden); } catch(Exceptions\InvalidEbookException | Exceptions\EbookPlaceholderExistsException | Exceptions\InvalidProjectException $ex){ $_SESSION['ebook'] = $ebook; $_SESSION['exception'] = $ex; http_response_code(Enums\HttpCode::SeeOther->value); header('Location: ' . $exceptionRedirectUrl); }