Skip to content

Commit

Permalink
Builder wip
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Nov 25, 2024
1 parent a10eca6 commit e242a81
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 35 deletions.
17 changes: 11 additions & 6 deletions packages/builder/DEVLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ We work on these tasks in order from top to bottom:

### Entity

- [ ] Config and translations have wrong filenames and must be wired correctly
- [ ] Do we use the entity_blocks and entity_tabs tables anymore?
- [ ] Iterate over all blocks, presets and contexts to find out if they are working as expected
- [ ] Need to generate Tabs, Taxonomy and Relations partials, may already work partially
- [ ] Add --migration option to create command
- [WIP] Iterate over all blocks, presets and contexts to find out if they are working as expected

- [ ] The Resource needs a more complex layout introducing regions
- [ ] Presets need to be able to define where blocks should be placed

- [ ] Add default actions or try to add the Publish Traits first, then use actions to test the entities

- [ ] Config and translations have wrong filenames and must be wired correctly
- [ ] Need to generate Tabs, Taxonomy and Relations partials, may already work partially

- [ ] Refactor DeleteCommand to use new services
- [ ] Polish Entity Builder
- [ ] Add --migration option to create command
- [ ] Would Builder now be able to generate itself?

### Extras
Expand Down
12 changes: 12 additions & 0 deletions packages/builder/src/Blocks/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ abstract class AbstractBlock
'resource' => [],
];

protected array $formSections = [
'resource' => [],
];

protected array $metaFields = [
'resource' => [],
];

protected array $metaSections = [
'resource' => [],
];

protected array $tableColumns = [
'resource' => [],
];
Expand Down
19 changes: 16 additions & 3 deletions packages/builder/src/Blocks/Publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ public function __construct(
],
],
'pages' => [
'list' => ['use Illuminate\Database\Eloquent\Builder;'],
'list' => [
'use Moox\Core\Traits\SinglePublishInListPage;',
'use Illuminate\Database\Eloquent\Builder;',
],
],
];

$this->traits['model'] = ['SinglePublishInModel'];
$this->traits['resource'] = ['SinglePublishInResource'];
$this->traits['pages']['list'] = ['SinglePublishInListPage'];

$this->methods['model'] = [
'scopes' => [
Expand Down Expand Up @@ -71,9 +75,18 @@ public function __construct(
];

$this->formFields['resource'] = [
];

$this->formSections['resource'] = [
];

$this->metaFields['resource'] = [
"DateTimePicker::make('publish_at')
->label(__('core::core.publish_at'))
->nullable()",
->label(__('core::core.publish_at'))
->nullable()",
];

$this->metaSections['resource'] = [
];

$this->tableColumns['resource'] = [
Expand Down
5 changes: 0 additions & 5 deletions packages/builder/src/Blocks/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public function __construct(
$this->useStatements['resource'] = [
'forms' => ['use Filament\Forms\Components\TextInput;'],
'columns' => ['use Filament\Tables\Columns\TextColumn;'],
'filters' => ['use Filament\Tables\Filters\TextFilter;'],
];

$this->formFields['resource'] = [
Expand All @@ -37,10 +36,6 @@ public function __construct(
.($this->searchable ? '->searchable()' : ''),
];

$this->filters['resource'] = [
"TextFilter::make('{$this->name}')",
];

$this->migrations['fields'] = [
"\$table->string('{$this->name}', {$this->length})"
.($this->nullable ? '->nullable()' : ''),
Expand Down
5 changes: 0 additions & 5 deletions packages/builder/src/Blocks/TextArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function __construct(
'resource' => [
'forms' => ['use Filament\Forms\Components\Textarea;'],
'columns' => ['use Filament\Tables\Columns\TextColumn;'],
'filters' => ['use Filament\Tables\Filters\TextFilter;'],
],
];

Expand All @@ -41,10 +40,6 @@ public function __construct(
.($this->sortable ? '->sortable()' : ''),
];

$this->filters['resource'] = [
"TextFilter::make('{$this->name}')",
];

$this->migrations['fields'] = [
"\$table->text('{$this->name}')"
.($this->nullable ? '->nullable()' : ''),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,46 @@ protected function getTemplateFile(): string

return $templatePath;
}

protected function formatUseStatements(): string
{
$statements = [];
foreach ($this->getBlocks() as $block) {
$pageType = strtolower($this->getPageType());

$blockStatements = $block->getUseStatements('pages');
if (isset($blockStatements[$pageType])) {
$statements = array_merge($statements, $blockStatements[$pageType]);
}

$blockTraits = $block->getTraits('pages');
if (isset($blockTraits[$pageType])) {
foreach ($blockTraits[$pageType] as $trait) {
$statements[] = 'use Moox\Core\Traits\\'.$trait.';';
}
}
}

return implode("\n", array_map(function ($statement) {
return rtrim($statement, ';').';';
}, array_unique($statements)));
}

protected function formatTraits(): string
{
$traits = [];
foreach ($this->getBlocks() as $block) {
$pageType = strtolower($this->getPageType());
$blockTraits = $block->getTraits('pages');
if (isset($blockTraits[$pageType])) {
$traits = array_merge($traits, $blockTraits[$pageType]);
}
}

if (empty($traits)) {
return '';
}

return 'use '.implode(', ', array_unique($traits)).';';
}
}
27 changes: 26 additions & 1 deletion packages/builder/src/Templates/Entity/resource.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ declare(strict_types=1);
namespace {{ namespace }};

use Filament\Resources\Resource;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Section;
{{ use_statements }}

class {{ class_name }}Resource extends Resource
Expand All @@ -19,7 +21,30 @@ class {{ class_name }}Resource extends Resource
{
{{ form_setup }}
return $form->schema([
{{ form_schema }}
Grid::make(2)
->schema([
Grid::make()
->schema([
Section::make()
->schema([
{{ form_schema }}
]),

{{ form_sections }}
])
->columnSpan(['lg' => 2]),
Grid::make()
->schema([
Section::make()
->schema([
{{ meta_fields }}
]),

{{ meta_sections }}
])
->columnSpan(['lg' => 1]),
])
->columns(['lg' => 3]),
]);
}

Expand Down
39 changes: 39 additions & 0 deletions packages/core/src/Traits/SinglePublishInListPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Moox\Core\Traits;

use Filament\Actions\Action;
use Filament\Actions\CreateAction;
use Filament\Notifications\Notification;
use Moox\Builder\Models\Item;

trait SinglePublishInListPage
{
protected function getHeaderActions(): array
{
return [
CreateAction::make()
->using(function (array $data, string $model): Item {
return $model::create($data);
})
->hidden(fn () => $this->activeTab === 'deleted'),
Action::make('emptyTrash')
->label(__('core::core.empty_trash'))
->icon('heroicon-o-trash')
->color('danger')
->action(function () {
$trashedCount = Item::onlyTrashed()->count();
Item::onlyTrashed()->forceDelete();
Notification::make()
->title(__('core::core.trash_emptied_successfully'))
->body(trans_choice('core::core.items_permanently_deleted', $trashedCount, ['count' => $trashedCount]))
->success()
->send();
})
->requiresConfirmation()
->visible(fn () => $this->activeTab === 'deleted' && Item::onlyTrashed()->exists()),
];
}
}
15 changes: 0 additions & 15 deletions packages/core/src/Traits/SinglePublishInPages.php

This file was deleted.

0 comments on commit e242a81

Please sign in to comment.