From ab1703e008b4df6cb3c4aeaacb357375244487d8 Mon Sep 17 00:00:00 2001 From: christophboecker Date: Thu, 5 Sep 2024 15:39:09 +0200 Subject: [PATCH] Neue Methode Entry::collection() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Flexible Bereitstellung von Entry-Datensätzen/Instanzen nach Kategorie, Status und Sprache. --- lib/Entry.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/lib/Entry.php b/lib/Entry.php index 647709e..6b3e757 100644 --- a/lib/Entry.php +++ b/lib/Entry.php @@ -572,4 +572,49 @@ public function getUrl(string $profile = 'neues-entry-id'): string { return rex_getUrl(null, null, [$profile => $this->getId()]); } + + /** + * Sucht alle Einträge, die allen angegebenen Kriterien erntsprechen und gibt + * eine rex_yform_manager_collection der Einträge (Entry) zurück. + * + * Select all Entry-Records matching the given criterias and returns a + * rex_yform_manager_collection + * + * Beispiel / Example: + * $deletedEntries = Entry::collection( status: 2); + * $onlineEntriesDe = Entry::collection( status: 1, language: 'de'); + * $onlineEntriesCat = Entry::collection( status: 1, category: [1,2]); + * + * @return rex_yform_manager_collection + */ + public static function collection (int|array $category = [], int $status=1, string|int $language=0): rex_yform_manager_collection + { + $query = static::query(); + $alias = $query->getTableAlias(); + + if( !is_array($category)) { + $category = [$category]; + } + if( 0 < count($category) ) { + $query->joinRelation('category_ids', 'c') + ->whereListContains('c.id', $category); + } + + if( -2 < $status && $status < 3 ) { + $query->where($alias.'.status',$status); + } + + if( is_string($language)) { + if( '' !== $language) { + $query->joinRelation('lang_id','l') + ->where('l.code',$language); + } + } elseif(0 < $language) { + $query->where('lang_id',$language); + } + + return $query->find(); + } +} + }