Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
matfish3 committed Nov 23, 2021
1 parent 340b03a commit ca9e5c5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/Handles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
1 change: 1 addition & 0 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function safeDown()
BlogListingMigrator::remove();
BlogCategoriesMigrator::remove();
BlogTagsMigrator::remove();
BlogFieldGroupMigrator::remove();
BlogAssetsVolumeMigrator::remove();
BlogThumbnailTransform::remove();
TagPageMigrator::remove();
Expand Down
20 changes: 17 additions & 3 deletions src/migrations/Migrators/BlogFieldGroupMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use Craft;
use craft\models\FieldGroup;
use craft\records\FieldGroup as FieldGroupRecord;
use matfish\Blogify\Handles;

class BlogFieldGroupMigrator extends Migrator
{
Expand All @@ -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);
Expand All @@ -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;
}
}
20 changes: 14 additions & 6 deletions src/services/FieldsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@


use Craft;
use craft\records\FieldGroup;
use matfish\Blogify\Handles;

class FieldsService
{
Expand Down Expand Up @@ -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;
}
}
8 changes: 7 additions & 1 deletion src/services/SectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ca9e5c5

Please sign in to comment.