mirror of
https://github.com/standardebooks/web.git
synced 2025-07-07 15:20:32 -04:00
On `Ebook::Save()` and `Ebook::Delete()`, remove any unreferenced `Tag`, `LocSubject`, and `Collection` records. These are analogous to these lines in `Ebook::Create()`: ``` $this->CreateTags(); $this->CreateLocSubjects(); $this->CreateCollections(); ``` `EbookPlaceholder`s can't have `Tags` or `LocSubjects` at the moment, but other mistakes in production that are later corrected could leave unused `Tags` and `LocSubjects`. Context: https://github.com/standardebooks/web/pull/447#issuecomment-2555734692
79 lines
1.5 KiB
PHP
79 lines
1.5 KiB
PHP
<?
|
|
class LocSubject{
|
|
public int $LocSubjectId;
|
|
public string $Name;
|
|
|
|
// *******
|
|
// METHODS
|
|
// *******
|
|
|
|
/**
|
|
* @throws Exceptions\InvalidLocSubjectException
|
|
*/
|
|
public function Validate(): void{
|
|
$error = new Exceptions\InvalidLocSubjectException();
|
|
|
|
if(isset($this->Name)){
|
|
$this->Name = trim($this->Name);
|
|
|
|
if($this->Name == ''){
|
|
$error->Add(new Exceptions\LocSubjectNameRequiredException());
|
|
}
|
|
|
|
if(strlen($this->Name) > EBOOKS_MAX_STRING_LENGTH){
|
|
$error->Add(new Exceptions\StringTooLongException('LoC subject: '. $this->Name));
|
|
}
|
|
}
|
|
else{
|
|
$error->Add(new Exceptions\LocSubjectNameRequiredException());
|
|
}
|
|
|
|
if($error->HasExceptions){
|
|
throw $error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @throws Exceptions\InvalidLocSubjectException
|
|
*/
|
|
public function Create(): void{
|
|
$this->Validate();
|
|
|
|
Db::Query('
|
|
INSERT into LocSubjects (Name)
|
|
values (?)
|
|
', [$this->Name]);
|
|
$this->LocSubjectId = Db::GetLastInsertedId();
|
|
}
|
|
|
|
/**
|
|
* @throws Exceptions\InvalidLocSubjectException
|
|
*/
|
|
public function GetByNameOrCreate(string $name): LocSubject{
|
|
$result = Db::Query('
|
|
SELECT *
|
|
from LocSubjects
|
|
where Name = ?
|
|
', [$name], LocSubject::class);
|
|
|
|
if(isset($result[0])){
|
|
return $result[0];
|
|
}
|
|
else{
|
|
$this->Create();
|
|
return $this;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Deletes `LocSubject`s that are not associated with any `Ebook`s.
|
|
*/
|
|
public static function DeleteUnused(): void{
|
|
Db::Query('
|
|
DELETE
|
|
from LocSubjects
|
|
where LocSubjectId not in
|
|
(select distinct LocSubjectId from EbookLocSubjects)
|
|
');
|
|
}
|
|
}
|