Add CanEditEbooks benefit and clean up some PHPStan errors

This commit is contained in:
Alex Cabal 2024-12-14 11:50:16 -06:00
parent 23b5c8ef31
commit 1a742baa51
10 changed files with 75 additions and 14 deletions

View file

@ -8,6 +8,7 @@ CREATE TABLE IF NOT EXISTS `Benefits` (
`CanReviewOwnArtwork` tinyint(1) unsigned NOT NULL DEFAULT 0, `CanReviewOwnArtwork` tinyint(1) unsigned NOT NULL DEFAULT 0,
`CanEditUsers` tinyint(1) unsigned NOT NULL DEFAULT 0, `CanEditUsers` tinyint(1) unsigned NOT NULL DEFAULT 0,
`CanEditCollections` tinyint(1) unsigned NOT NULL DEFAULT 0, `CanEditCollections` tinyint(1) unsigned NOT NULL DEFAULT 0,
`CanEditEbooks` tinyint(1) unsigned NOT NULL DEFAULT 0,
`CanCreateEbookPlaceholders` tinyint(1) unsigned NOT NULL DEFAULT 0, `CanCreateEbookPlaceholders` tinyint(1) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`UserId`), PRIMARY KEY (`UserId`),
KEY `idxBenefits` (`CanAccessFeeds`,`CanVote`,`CanBulkDownload`) KEY `idxBenefits` (`CanAccessFeeds`,`CanVote`,`CanBulkDownload`)

View file

@ -55,15 +55,22 @@ class AtomFeed extends Feed{
foreach($this->Entries as $entry){ foreach($this->Entries as $entry){
$obj = new StdClass(); $obj = new StdClass();
if($entry instanceof Ebook){ if($entry instanceof Ebook){
if($entry->EbookUpdated !== null){
$obj->Updated = $entry->EbookUpdated->format(Enums\DateTimeFormat::Iso->value); $obj->Updated = $entry->EbookUpdated->format(Enums\DateTimeFormat::Iso->value);
$obj->Id = SITE_URL . $entry->Url; $obj->Id = SITE_URL . $entry->Url;
} }
}
else{ else{
if($entry->Updated !== null){
$obj->Updated = $entry->Updated->format(Enums\DateTimeFormat::Iso->value); $obj->Updated = $entry->Updated->format(Enums\DateTimeFormat::Iso->value);
$obj->Id = $entry->Id; $obj->Id = $entry->Id;
} }
}
if(isset($obj->Id)){
$currentEntries[] = $obj; $currentEntries[] = $obj;
} }
}
$oldEntries = []; $oldEntries = [];
try{ try{

View file

@ -16,6 +16,7 @@ class Benefits{
public bool $CanReviewOwnArtwork = false; public bool $CanReviewOwnArtwork = false;
public bool $CanEditUsers = false; public bool $CanEditUsers = false;
public bool $CanEditCollections = false; public bool $CanEditCollections = false;
public bool $CanEditEbooks = false;
public bool $CanCreateEbookPlaceholders = false; public bool $CanCreateEbookPlaceholders = false;
protected bool $_HasBenefits; protected bool $_HasBenefits;
@ -30,6 +31,10 @@ class Benefits{
|| ||
$this->CanEditUsers $this->CanEditUsers
|| ||
$this->CanEditCollections
||
$this->CanEditEbooks
||
$this->CanCreateEbookPlaceholders $this->CanCreateEbookPlaceholders
){ ){
return true; return true;

View file

@ -444,7 +444,7 @@ class Ebook{
protected function GetReadingTime(): string{ protected function GetReadingTime(): string{
if(!isset($this->_ReadingTime)){ if(!isset($this->_ReadingTime)){
$readingTime = ceil($this->WordCount / AVERAGE_READING_WORDS_PER_MINUTE); $readingTime = ceil(($this->WordCount ?? 0) / AVERAGE_READING_WORDS_PER_MINUTE);
$this->_ReadingTime = (string)$readingTime; $this->_ReadingTime = (string)$readingTime;
if($readingTime < 60){ if($readingTime < 60){

View file

@ -110,6 +110,10 @@ foreach(Ebook::GetAll() as $ebook){
continue; continue;
} }
if($ebook->EbookCreated === null || $ebook->EbookUpdated === null){
continue;
}
$timestamp = $ebook->EbookCreated->format('Y-m'); $timestamp = $ebook->EbookCreated->format('Y-m');
$updatedTimestamp = $ebook->EbookUpdated->getTimestamp(); $updatedTimestamp = $ebook->EbookUpdated->getTimestamp();

View file

@ -47,7 +47,9 @@ $collection = $collection ?? null;
</div> </div>
<? } ?> <? } ?>
<? if(!$ebook->IsPlaceholder()){ ?> <? if(!$ebook->IsPlaceholder()){ ?>
<? if($ebook->WordCount !== null){ ?>
<p><?= number_format($ebook->WordCount) ?> words • <?= $ebook->ReadingEase ?> reading ease</p> <p><?= number_format($ebook->WordCount) ?> words • <?= $ebook->ReadingEase ?> reading ease</p>
<? } ?>
<ul class="tags"> <ul class="tags">
<? foreach($ebook->Tags as $tag){ ?> <? foreach($ebook->Tags as $tag){ ?>
<li> <li>

View file

@ -24,9 +24,9 @@ use function Safe\filesize;
<? } ?> <? } ?>
</author> </author>
<? } ?> <? } ?>
<published><?= $entry->EbookCreated->format(Enums\DateTimeFormat::Iso->value) ?></published> <published><?= $entry->EbookCreated?->format(Enums\DateTimeFormat::Iso->value) ?></published>
<dc:issued><?= $entry->EbookCreated->format(Enums\DateTimeFormat::Iso->value) ?></dc:issued> <dc:issued><?= $entry->EbookCreated?->format(Enums\DateTimeFormat::Iso->value) ?></dc:issued>
<updated><?= $entry->EbookUpdated->format(Enums\DateTimeFormat::Iso->value) ?></updated> <updated><?= $entry->EbookUpdated?->format(Enums\DateTimeFormat::Iso->value) ?></updated>
<dc:language><?= Formatter::EscapeXml($entry->Language) ?></dc:language> <dc:language><?= Formatter::EscapeXml($entry->Language) ?></dc:language>
<dc:publisher>Standard Ebooks</dc:publisher> <dc:publisher>Standard Ebooks</dc:publisher>
<rights>Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.</rights> <rights>Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.</rights>

View file

@ -10,7 +10,7 @@ use function Safe\preg_replace;
<title><?= Formatter::EscapeXml($entry->Title) ?>, by <?= Formatter::EscapeXml(strip_tags($entry->AuthorsHtml)) ?></title> <title><?= Formatter::EscapeXml($entry->Title) ?>, by <?= Formatter::EscapeXml(strip_tags($entry->AuthorsHtml)) ?></title>
<link><?= SITE_URL . Formatter::EscapeXml($entry->Url) ?></link> <link><?= SITE_URL . Formatter::EscapeXml($entry->Url) ?></link>
<description><?= Formatter::EscapeXml($entry->Description) ?></description> <description><?= Formatter::EscapeXml($entry->Description) ?></description>
<pubDate><?= $entry->EbookCreated->format(Enums\DateTimeFormat::Rss->value) ?></pubDate> <pubDate><?= $entry->EbookCreated?->format(Enums\DateTimeFormat::Rss->value) ?></pubDate>
<guid><?= Formatter::EscapeXml(preg_replace('/^url:/ius', '', $entry->Identifier)) ?></guid> <guid><?= Formatter::EscapeXml(preg_replace('/^url:/ius', '', $entry->Identifier)) ?></guid>
<? foreach($entry->Tags as $tag){ ?> <? foreach($entry->Tags as $tag){ ?>
<category domain="https://standardebooks.org/vocab/subjects"><?= Formatter::EscapeXml($tag->Name) ?></category> <category domain="https://standardebooks.org/vocab/subjects"><?= Formatter::EscapeXml($tag->Name) ?></category>

View file

@ -102,6 +102,24 @@ catch(Exceptions\EbookNotFoundException){
<p>This book is not yet in the U.S. public domain. We cant offer it until it is.</p> <p>This book is not yet in the U.S. public domain. We cant offer it until it is.</p>
<? } ?> <? } ?>
</section> </section>
<? if(Session::$User?->Benefits->CanEditEbooks){ ?>
<section id="metadata">
<h2>Metadata</h2>
<table class="admin-table">
<tbody>
<tr>
<td>Ebook ID:</td>
<td><?= $ebook->EbookId ?></td>
</tr>
<tr>
<td>Identifier:</td>
<td><?= Formatter::EscapeHtml($ebook->Identifier) ?></td>
</tr>
</tbody>
</table>
</section>
<? } ?>
</article> </article>
</main> </main>
<?= Template::Footer() ?> <?= Template::Footer() ?>

View file

@ -123,7 +123,9 @@ catch(Exceptions\EbookNotFoundException){
<aside id="reading-ease"> <aside id="reading-ease">
<? if($ebook->WordCount !== null){ ?>
<p><?= number_format($ebook->WordCount) ?> words (<?= $ebook->ReadingTime ?>) with a reading ease of <?= $ebook->ReadingEase ?> (<?= $ebook->ReadingEaseDescription ?>)</p> <p><?= number_format($ebook->WordCount) ?> words (<?= $ebook->ReadingTime ?>) with a reading ease of <?= $ebook->ReadingEase ?> (<?= $ebook->ReadingEaseDescription ?>)</p>
<? } ?>
<? if($ebook->ContributorsHtml != ''){ ?> <? if($ebook->ContributorsHtml != ''){ ?>
<p><?= $ebook->ContributorsHtml ?></p> <p><?= $ebook->ContributorsHtml ?></p>
<? } ?> <? } ?>
@ -175,8 +177,12 @@ catch(Exceptions\EbookNotFoundException){
<meta property="schema:image" content="<?= Formatter::EscapeHtml(SITE_URL . $ebook->DistCoverUrl) ?>"/> <meta property="schema:image" content="<?= Formatter::EscapeHtml(SITE_URL . $ebook->DistCoverUrl) ?>"/>
<meta property="schema:thumbnailUrl" content="<?= Formatter::EscapeHtml(SITE_URL . $ebook->Url . '/downloads/cover-thumbnail.jpg') ?>"/> <meta property="schema:thumbnailUrl" content="<?= Formatter::EscapeHtml(SITE_URL . $ebook->Url . '/downloads/cover-thumbnail.jpg') ?>"/>
<meta property="schema:inLanguage" content="<?= Formatter::EscapeHtml($ebook->Language) ?>"/> <meta property="schema:inLanguage" content="<?= Formatter::EscapeHtml($ebook->Language) ?>"/>
<? if($ebook->EbookCreated !== null){ ?>
<meta property="schema:datePublished" content="<?= Formatter::EscapeHtml($ebook->EbookCreated->format('Y-m-d')) ?>"/> <meta property="schema:datePublished" content="<?= Formatter::EscapeHtml($ebook->EbookCreated->format('Y-m-d')) ?>"/>
<? } ?>
<? if($ebook->EbookUpdated !== null){ ?>
<meta property="schema:dateModified" content="<?= Formatter::EscapeHtml($ebook->EbookUpdated->format('Y-m-d')) ?>"/> <meta property="schema:dateModified" content="<?= Formatter::EscapeHtml($ebook->EbookUpdated->format('Y-m-d')) ?>"/>
<? } ?>
<div property="schema:potentialAction" typeof="http://schema.org/ReadAction"> <div property="schema:potentialAction" typeof="http://schema.org/ReadAction">
<meta property="schema:actionStatus" content="http://schema.org/PotentialActionStatus"/> <meta property="schema:actionStatus" content="http://schema.org/PotentialActionStatus"/>
<div property="schema:target" typeof="schema:EntryPoint"> <div property="schema:target" typeof="schema:EntryPoint">
@ -391,6 +397,24 @@ catch(Exceptions\EbookNotFoundException){
<p>You can also <a href="/donate">donate to Standard Ebooks</a> to help fund continuing improvement of this and other ebooks.</p> <p>You can also <a href="/donate">donate to Standard Ebooks</a> to help fund continuing improvement of this and other ebooks.</p>
</section> </section>
<? if(Session::$User?->Benefits->CanEditEbooks){ ?>
<section id="metadata">
<h2>Metadata</h2>
<table class="admin-table">
<tbody>
<tr>
<td>Ebook ID:</td>
<td><?= $ebook->EbookId ?></td>
</tr>
<tr>
<td>Identifier:</td>
<td><?= Formatter::EscapeHtml($ebook->Identifier) ?></td>
</tr>
</tbody>
</table>
</section>
<? } ?>
<? if(sizeof($carousel) > 0){ ?> <? if(sizeof($carousel) > 0){ ?>
<aside id="more-ebooks"> <aside id="more-ebooks">
<h2>More free<? if($carouselTag !== null){ ?> <?= strtolower($carouselTag->Name) ?><? } ?> ebooks</h2> <h2>More free<? if($carouselTag !== null){ ?> <?= strtolower($carouselTag->Name) ?><? } ?> ebooks</h2>