-
-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[5.x] Extract whereSite query method to trait (#9991)
- Loading branch information
1 parent
fce280c
commit 341f506
Showing
3 changed files
with
96 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Statamic\Stache\Query; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
trait QueriesEntryStatus | ||
{ | ||
public function whereStatus(string $status) | ||
{ | ||
if (! in_array($status, ['published', 'draft', 'scheduled', 'expired'])) { | ||
throw new \Exception("Invalid status [$status]"); | ||
} | ||
|
||
$this->ensureCollectionsAreQueriedForStatusQuery(); | ||
|
||
if ($status === 'draft') { | ||
return $this->where('published', false); | ||
} | ||
|
||
$this->where('published', true); | ||
|
||
return $this->where(fn ($query) => $this | ||
->getCollectionsForStatusQuery() | ||
->each(fn ($collection) => $query->orWhere(fn ($q) => $this->addCollectionStatusLogicToQuery($q, $status, $collection)))); | ||
} | ||
|
||
private function addCollectionStatusLogicToQuery($query, $status, $collection): void | ||
{ | ||
$this->addCollectionWhereToStatusQuery($query, $collection->handle()); | ||
|
||
if ($collection->futureDateBehavior() === 'public' && $collection->pastDateBehavior() === 'public') { | ||
if ($status === 'scheduled' || $status === 'expired') { | ||
$query->where('date', 'invalid'); // intentionally trigger no results. | ||
} | ||
} | ||
|
||
if ($collection->futureDateBehavior() === 'private') { | ||
$status === 'scheduled' | ||
? $query->where('date', '>', now()) | ||
: $query->where('date', '<', now()); | ||
|
||
if ($status === 'expired') { | ||
$query->where('date', 'invalid'); // intentionally trigger no results. | ||
} | ||
} | ||
|
||
if ($collection->pastDateBehavior() === 'private') { | ||
$status === 'expired' | ||
? $query->where('date', '<', now()) | ||
: $query->where('date', '>', now()); | ||
|
||
if ($status === 'scheduled') { | ||
$query->where('date', 'invalid'); // intentionally trigger no results. | ||
} | ||
} | ||
} | ||
|
||
protected function addCollectionWhereToStatusQuery($query, $collection): void | ||
{ | ||
$query->where('collection', $collection); | ||
} | ||
|
||
abstract protected function ensureCollectionsAreQueriedForStatusQuery(): void; | ||
|
||
abstract protected function getCollectionsForStatusQuery(): Collection; | ||
} |