From 731a17c98ed432918fdfa505e37102a509fe0741 Mon Sep 17 00:00:00 2001 From: christophboecker Date: Sat, 7 Sep 2024 14:13:44 +0200 Subject: [PATCH] Status-Werte als Konstanten --- docs/03_neues_entry.md | 4 ++-- install/cronjob_publish.php | 2 +- install/tableset.json | 4 ++-- lib/Api/Restful.php | 4 ++-- lib/Category.php | 18 ++++++++++++++++++ lib/Cronjob/Publish.php | 4 ++-- lib/Entry.php | 28 +++++++++++++++++++++++++--- lib/neues.php | 2 +- 8 files changed, 53 insertions(+), 13 deletions(-) diff --git a/docs/03_neues_entry.md b/docs/03_neues_entry.md index 01a662e..0f3c5ab 100644 --- a/docs/03_neues_entry.md +++ b/docs/03_neues_entry.md @@ -244,12 +244,12 @@ Findet Online-Einträge. Wenn eine Kategorie-ID angegeben ist, werden nur Eintr $entries = FriendsOfRedaxo\Neues\Entry::findOnline(1); ``` -### findByCategory(?int $category_id = null, int $status = 1) +### findByCategory(?int $category_id = null, int $status = Entry::ONLINE) Findet Einträge nach Kategorie. ```php -$entries = FriendsOfRedaxo\Neues\Entry::findByCategory(1, 1); +$entries = FriendsOfRedaxo\Neues\Entry::findByCategory(1, Entry::ONLINE); ``` ### getUrl(string $profile = 'neues-entry-id') diff --git a/install/cronjob_publish.php b/install/cronjob_publish.php index c83045d..2854281 100644 --- a/install/cronjob_publish.php +++ b/install/cronjob_publish.php @@ -21,7 +21,7 @@ $sql->setTable(rex::getTable('cronjob')); $sql->setValue('name', '[neues] Geplante Beiträge veröffentlichen'); -$sql->setValue('description', 'Veröffentlicht alle Beiträge (status = 1), deren Status geplant (status = 0) ist und deren Veröffentlichungszeitpunkt erreicht wurde (publishdate < now()).'); +$sql->setValue('description', 'Veröffentlicht alle Beiträge (status = Entry::ONLINE), deren Status geplant (status = Entry::PLANNED) ist und deren Veröffentlichungszeitpunkt erreicht wurde (publishdate < now()).'); $sql->setValue('type', Publish::class); $sql->setValue('parameters', '[]'); $sql->setValue('interval', json_encode($job_intervall)); diff --git a/install/tableset.json b/install/tableset.json index b21bb4c..b8d8521 100644 --- a/install/tableset.json +++ b/install/tableset.json @@ -223,7 +223,7 @@ "not_required": "", "multiple": "0", "expanded": "0", - "choices": "translate:neues_status_draft=-1,translate:neues_status_online=1", + "choices": "FriendsOfRedaxo\\Neues\\Category::statusChoice", "choice_attributes": "", "attributes": "", "default": "0", @@ -360,7 +360,7 @@ "not_required": "", "multiple": "0", "expanded": "0", - "choices": "translate:neues_status_deleted=2,translate:neues_status_draft=-1,translate:neues_status_planned=0,translate:neues_status_online=1", + "choices": "FriendsOfRedaxo\\Neues\\Entry::statusChoice", "choice_attributes": "", "attributes": "", "default": "-1", diff --git a/lib/Api/Restful.php b/lib/Api/Restful.php index 25c2535..3344fbe 100644 --- a/lib/Api/Restful.php +++ b/lib/Api/Restful.php @@ -17,7 +17,7 @@ public static function init(): void 'path' => '/neues/entry/5.0.0/', 'auth' => '\rex_yform_rest_auth_token::checkToken', 'type' => Entry::class, - 'query' => Entry::query()->where('status', 1, '>='), + 'query' => Entry::query()->where('status', Entry::ONLINE, '>='), 'get' => [ 'fields' => [ Entry::class => [ @@ -105,7 +105,7 @@ public static function init(): void 'path' => '/neues/category/5.0.0/', 'auth' => '\rex_yform_rest_auth_token::checkToken', 'type' => Category::class, - 'query' => Category::query()->where('status', 1, '>='), + 'query' => Category::query()->where('status', Category::ONLINE, '>='), 'get' => [ 'fields' => [ Category::class => [ diff --git a/lib/Category.php b/lib/Category.php index 380a5ad..7444d21 100644 --- a/lib/Category.php +++ b/lib/Category.php @@ -2,6 +2,7 @@ namespace FriendsOfRedaxo\Neues; +use rex_i18n; use rex_yform_manager_collection; use rex_yform_manager_dataset; @@ -22,6 +23,9 @@ */ class Category extends rex_yform_manager_dataset { + public const DRAFT = -1; + public const ONLINE = 1; + /** * Gibt den Namen der Kategorie zurück. * Returns the name of the Category. @@ -90,4 +94,18 @@ public function getUrl(string $profile = 'neues-category-id'): ?string } return null; } + + /** + * Callback für das Entry-Formular: Auswahlmöglichkeiten des Status-Feldes + * FriendsOfRedaxo\Neues\Category::statusChoice. + * @api + * @return array + */ + public static function statusChoice(): array + { + return [ + self::DRAFT => rex_i18n::msg('neues_status_draft'), + self::ONLINE => rex_i18n::msg('neues_status_online'), + ]; + } } diff --git a/lib/Cronjob/Publish.php b/lib/Cronjob/Publish.php index 9946845..40faf42 100644 --- a/lib/Cronjob/Publish.php +++ b/lib/Cronjob/Publish.php @@ -16,8 +16,8 @@ class Publish extends rex_cronjob public function execute() { /* Collection von Neues-Einträgen, die noch nicht veröffentlicht sind, aber es sein sollten. (geplant) */ - $neues_entry_to_publish = Entry::query()->where('status', 0)->where('publishdate', date('Y-m-d'), '<')->find(); - $neues_entry_to_publish->setValue('status', 1); + $neues_entry_to_publish = Entry::query()->where('status', Entry::PLANNED)->where('publishdate', date('Y-m-d'), '<')->find(); + $neues_entry_to_publish->setValue('status', Entry::ONLINE); if (!$neues_entry_to_publish->save()) { $this->setMessage(sprintf(rex_i18n::msg('neues_entry_publish_error'), count($neues_entry_to_publish))); return false; diff --git a/lib/Entry.php b/lib/Entry.php index 647709e..bed1386 100644 --- a/lib/Entry.php +++ b/lib/Entry.php @@ -8,6 +8,7 @@ use rex_csrf_token; use rex_extension_point; use rex_formatter; +use rex_i18n; use rex_media; use rex_media_plus; use rex_url; @@ -32,6 +33,11 @@ */ class Entry extends rex_yform_manager_dataset { + public const DELETED = 2; // FIXME: muss auf -2 geändert werden. + public const DRAFT = -1; + public const PLANNED = 0; + public const ONLINE = 1; + /** * Standards für das Formular anpassen * - Editor-Konfiguration einfügen. @@ -501,7 +507,7 @@ public static function findOnline(?int $category_id = null): rex_yform_manager_c if (null !== $category_id) { return self::findByCategory($category_id); } - return self::query()->where('status', 1, '>=')->find(); + return self::query()->where('status', self::ONLINE, '>=')->find(); } /** @@ -517,7 +523,7 @@ public static function findOnline(?int $category_id = null): rex_yform_manager_c * * @api */ - public static function findByCategory(?int $category_id = null, int $status = 1): rex_yform_manager_collection + public static function findByCategory(?int $category_id = null, int $status = self::ONLINE): rex_yform_manager_collection { $query = self::query(); $alias = $query->getTableAlias(); @@ -538,7 +544,7 @@ public static function findByCategory(?int $category_id = null, int $status = 1) * * @api */ - public static function findByCategoryIds(string|array|null $category_ids = null, int $status = 1): rex_yform_manager_collection + public static function findByCategoryIds(string|array|null $category_ids = null, int $status = self::ONLINE): rex_yform_manager_collection { $query = self::query()->where('status', $status, '>='); @@ -572,4 +578,20 @@ public function getUrl(string $profile = 'neues-entry-id'): string { return rex_getUrl(null, null, [$profile => $this->getId()]); } + + /** + * Callback für das Entry-Formular: Auswahlmöglichkeiten des Status-Feldes + * FriendsOfRedaxo\Neues\Entry::statusChoice. + * @api + * @return array + */ + public static function statusChoice(): array + { + return [ + self::DELETED => rex_i18n::msg('neues_status_deleted'), + self::DRAFT => rex_i18n::msg('neues_status_draft'), + self::PLANNED => rex_i18n::msg('neues_status_planned'), + self::ONLINE => rex_i18n::msg('neues_status_online'), + ]; + } } diff --git a/lib/neues.php b/lib/neues.php index 3935d71..ea6f6fe 100644 --- a/lib/neues.php +++ b/lib/neues.php @@ -27,7 +27,7 @@ class Neues public static function getList(int $rowsPerPage = 10, string $pageCursor = 'page'): string { $query = Entry::query() - ->where('status', 1, '>=') + ->where('status', Entry::ONLINE, '>=') ->where('publishdate', rex_sql::datetime(), '<=') ->orderBy('publishdate', 'desc'); $pager = new rex_pager($rowsPerPage, $pageCursor);