diff --git a/lib/Ebook.php b/lib/Ebook.php index 8d8a4b49..84c4fd55 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -49,6 +49,7 @@ use function Safe\shell_exec; */ class Ebook{ use Traits\Accessor; + use Traits\FromRow; public int $EbookId; public string $Identifier; diff --git a/lib/Project.php b/lib/Project.php index df4e0e7d..9e7329f7 100644 --- a/lib/Project.php +++ b/lib/Project.php @@ -24,6 +24,7 @@ use Safe\DateTimeImmutable; */ class Project{ use Traits\Accessor; + use Traits\FromRow; use Traits\PropertyFromHttp; public int $ProjectId; @@ -606,20 +607,37 @@ class Project{ * @return array */ public static function GetAllByStatus(Enums\ProjectStatusType $status): array{ - return Db::Query('SELECT * from Projects where Status = ? order by Started desc', [$status], Project::class); + return Db::Query('SELECT * from Projects inner join Ebooks using (EbookId) where Projects.Status = ? order by Title asc', [$status], Project::class); } /** * @return array */ public static function GetAllByManagerUserId(int $userId): array{ - return Db::Query('SELECT * from Projects where ManagerUserId = ? and Status in (?, ?) order by Started desc', [$userId, Enums\ProjectStatusType::InProgress, Enums\ProjectStatusType::Stalled], Project::class); + return Db::Query('SELECT * from Projects inner join Ebooks using (EbookId) where ManagerUserId = ? and Status in (?, ?) order by Title asc', [$userId, Enums\ProjectStatusType::InProgress, Enums\ProjectStatusType::Stalled], Project::class); } /** * @return array */ public static function GetAllByReviewerUserId(int $userId): array{ - return Db::Query('SELECT * from Projects where ReviewerUserId = ? and Status in (?, ?) order by Started desc', [$userId, Enums\ProjectStatusType::InProgress, Enums\ProjectStatusType::Stalled], Project::class); + return Db::Query('SELECT * from Projects inner join Ebooks using (EbookId) where ReviewerUserId = ? and Status in (?, ?) order by Title asc', [$userId, Enums\ProjectStatusType::InProgress, Enums\ProjectStatusType::Stalled], Project::class); + } + + /** + * Creates a `Project` from a multi table array containing a `Project` and an `Ebook`. + * + * @param array $row + */ + public static function FromMultiTableRow(array $row): Project{ + $object = Project::FromRow($row['Projects']); + + // The Action may be null if it's a Scribophile adjustment. In that case, don't initialize the Action object. + if($row['Ebooks']->EbookId !== null){ + $row['Ebooks']->Ebookid = $object->EbookId; + $object->Ebook = Ebook::FromRow($row['Ebooks']); + } + + return $object; } } diff --git a/lib/Traits/FromRow.php b/lib/Traits/FromRow.php new file mode 100644 index 00000000..57f71bea --- /dev/null +++ b/lib/Traits/FromRow.php @@ -0,0 +1,52 @@ + $value){ + if(is_string($value)){ + try{ + $type = (new \ReflectionProperty($object, $property))->getType(); + if($type instanceof \ReflectionNamedType){ + $typeName = $type->getName(); + if($typeName != 'string' && is_a($typeName, 'BackedEnum', true)){ + $object->$property = $typeName::from($value); + } + else{ + $object->$property = $value; + } + } + + + } + catch(\Exception){ + $object->$property = $value; + } + } + else{ + $object->$property = $value; + } + } + + return $object; + } + + /** + * Return an object based on a database row result. This is a fallback object filler for when the parent class hasn't defined their own. + */ + public static function FromRow(\stdClass $row): static{ + return self::FillObject(new static(), $row); + } +}