From ca9e5c538233ea817b23cb0bc7984335d0522d0b Mon Sep 17 00:00:00 2001 From: Mat Fish Date: Tue, 23 Nov 2021 21:07:45 +0200 Subject: [PATCH] refactor --- CHANGELOG.md | 6 ++++++ src/Handles.php | 3 +++ src/migrations/Install.php | 1 + .../Migrators/BlogFieldGroupMigrator.php | 20 ++++++++++++++++--- src/services/FieldsService.php | 20 +++++++++++++------ src/services/SectionService.php | 8 +++++++- 6 files changed, 48 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bee801d..5bba24d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Release Notes for Blogify +## 1.0.6 - 2021-11-23 +### Improved +- Abort installation if section creation fails +- Refactor field group query +- Remove field group on uninstall + ## 1.0.5 - 2021-11-23 ### Improved - Added subheadings to fake post content diff --git a/src/Handles.php b/src/Handles.php index 195cb70..7f150ac 100644 --- a/src/Handles.php +++ b/src/Handles.php @@ -21,4 +21,7 @@ interface Handles const POST_IMAGE = 'blogifyPostImage'; const POST_TAGS = 'blogifyPostTags'; const POST_CATEGORIES = 'blogifyPostCategories'; + + // Field group does not have a handle + const BLOG_FIELDS_GROUP_NAME= 'Blog Fields'; } \ No newline at end of file diff --git a/src/migrations/Install.php b/src/migrations/Install.php index c813629..4d889e4 100644 --- a/src/migrations/Install.php +++ b/src/migrations/Install.php @@ -49,6 +49,7 @@ public function safeDown() BlogListingMigrator::remove(); BlogCategoriesMigrator::remove(); BlogTagsMigrator::remove(); + BlogFieldGroupMigrator::remove(); BlogAssetsVolumeMigrator::remove(); BlogThumbnailTransform::remove(); TagPageMigrator::remove(); diff --git a/src/migrations/Migrators/BlogFieldGroupMigrator.php b/src/migrations/Migrators/BlogFieldGroupMigrator.php index 7e2f70c..18243d9 100644 --- a/src/migrations/Migrators/BlogFieldGroupMigrator.php +++ b/src/migrations/Migrators/BlogFieldGroupMigrator.php @@ -5,6 +5,8 @@ use Craft; use craft\models\FieldGroup; +use craft\records\FieldGroup as FieldGroupRecord; +use matfish\Blogify\Handles; class BlogFieldGroupMigrator extends Migrator { @@ -13,7 +15,7 @@ public static function add(): bool blogify_log("Adding Blog Fields group"); $group = new FieldGroup([ - 'name' => 'Blog Fields' + 'name' => Handles::BLOG_FIELDS_GROUP_NAME ]); return Craft::$app->fields->saveGroup($group); @@ -22,7 +24,19 @@ public static function add(): bool public static function remove(): bool { - // removed by BlogChannelMigrator? - return true; + $name = Handles::BLOG_FIELDS_GROUP_NAME; + + $group = FieldGroupRecord::findOne([ + 'name' => $name + ]); + + if ($group) { + blogify_log("Removing {$name} group"); + $group->delete(); + return true; + } + + blogify_log("Field group {$name} not found. Skipping."); + return false; } } \ No newline at end of file diff --git a/src/services/FieldsService.php b/src/services/FieldsService.php index 6ce8b27..bb52141 100644 --- a/src/services/FieldsService.php +++ b/src/services/FieldsService.php @@ -5,6 +5,8 @@ use Craft; +use craft\records\FieldGroup; +use matfish\Blogify\Handles; class FieldsService { @@ -61,12 +63,18 @@ public function remove($handle): bool private function getFieldGroupId() { - $res = (new Craft\db\Query)->select(['id']) - ->from(getenv('DB_TABLE_PREFIX') . 'fieldgroups') - ->where("name='Blog Fields'") - ->andWhere('dateDeleted is null') - ->all(); + $group = FieldGroup::findOne([ + 'name' => Handles::BLOG_FIELDS_GROUP_NAME + ]); - return $res[0]['id']; + if ($group) { + return $group->id; + } + + $group = FieldGroup::findOne('1=1'); + + blogify_log("Failed to find blog fields group. Using {$group->name} group instead"); + + return $group->id; } } \ No newline at end of file diff --git a/src/services/SectionService.php b/src/services/SectionService.php index 43beaea..167edb6 100644 --- a/src/services/SectionService.php +++ b/src/services/SectionService.php @@ -35,7 +35,13 @@ public function add($name, $handle, $type, $url, $template): bool ] ]); - return Craft::$app->sections->saveSection($section); + if (!Craft::$app->sections->saveSection($section)) { + blogify_log("Failed to create section {$name}"); + blogify_log(json_encode($section->getErrors())); + throw new \Exception("Failed to create section {$name}"); + } + + return true; } public function remove($handle): bool