Skip to content

Commit

Permalink
Builder wip
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Dec 7, 2024
1 parent d624438 commit 9bd70c1
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 32 deletions.
22 changes: 20 additions & 2 deletions packages/builder/src/Blocks/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,15 @@ public function getDefaultSortDirection(): string

public function getTableBulkActions(): array
{
return $this->actions['bulk'] ?? [];
if (! isset($this->actions['bulk'])) {
return [];
}

if (is_string($this->actions['bulk'])) {
return [$this->actions['bulk']];
}

return $this->actions['bulk'];
}

public function getTitle(): string
Expand Down Expand Up @@ -537,7 +545,17 @@ protected function flattenArray(array $array): array

public function getTableActions(): array
{
return $this->getActions('resource');
if (! isset($this->actions['resource'])) {
return [];
}

// If it's a string (method call), wrap it in an array
if (is_string($this->actions['resource'])) {
return [$this->actions['resource']];
}

// If it's already an array, return as is
return $this->actions['resource'];
}

public function getPageActions(string $page): array
Expand Down
2 changes: 1 addition & 1 deletion packages/builder/src/Blocks/Features/SimpleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function __construct(
[
'field' => 'type',
'operator' => '=',
'value' => 'Article',
'value' => $type,
],
],
], $this->enum),
Expand Down
9 changes: 2 additions & 7 deletions packages/builder/src/Blocks/Singles/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ public function __construct(
'static::getSimpleFormActions()',
]);

$this->actions['resource'] = [
'static::getSimpleResourceActions()',
];

$this->actions['bulk'] = [
'static::getSimpleBulkActions()',
];
$this->actions['resource'] = 'static::getSimpleResourceActions()';
$this->actions['bulk'] = 'static::getSimpleBulkActions()';
}
}
14 changes: 10 additions & 4 deletions packages/builder/src/Generators/Entity/ResourceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,14 @@ protected function getTableActions(): string
foreach ($this->getBlocks() as $block) {
$blockActions = $block->getTableActions();
if (! empty($blockActions)) {
$actions = array_merge($actions, $blockActions);
if (is_string($blockActions) && str_contains($blockActions, 'static::')) {
return $blockActions;
}
$actions = array_merge($actions, (array) $blockActions);
}
}

return implode(",\n ", $actions);
return '['.implode(",\n ", $actions).']';
}

protected function getTableFilters(): string
Expand All @@ -283,12 +286,15 @@ protected function getTableBulkActions(): string
if (method_exists($block, 'getTableBulkActions')) {
$blockActions = $block->getTableBulkActions();
if (! empty($blockActions)) {
$actions = array_merge($actions, $blockActions);
if (is_string($blockActions) && str_contains($blockActions, 'static::')) {
return $blockActions;
}
$actions = array_merge($actions, (array) $blockActions);
}
}
}

return implode(",\n ", $actions);
return '['.implode(",\n ", $actions).']';
}

protected function getNavigationGroup(): string
Expand Down
8 changes: 2 additions & 6 deletions packages/builder/src/Templates/Entity/resource.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,8 @@ class {{ class_name }}Resource extends Resource
{{ table_columns }}
])
->defaultSort('{{ default_sort_column }}', '{{ default_sort_direction }}')
->actions([
{{ table_actions }}
])
->bulkActions([
{{ table_bulk_actions }}
])
->actions({{ table_actions }})
->bulkActions({{ table_bulk_actions }})
->filters([
{{ table_filters }}
]);
Expand Down
23 changes: 11 additions & 12 deletions packages/core/src/Traits/Tabs/TabsInResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,21 @@ public static function setCurrentTab(?string $tab): void

protected static function applyTabQuery(Builder $query, string $currentTab): Builder
{
// Skip if this is a soft-delete tab as it's already handled
if (in_array($currentTab, ['trash', 'deleted'])) {
return $query;
}

// Get tab configuration
$tabsConfig = config(static::getResourceKey().'.tabs', []);

if (isset($tabsConfig[$currentTab]['query'])) {
foreach ($tabsConfig[$currentTab]['query'] as $condition) {
$value = $condition['value'];

// Handle closure values
if (is_string($value) && str_contains($value, 'function')) {
$value = eval("return {$value};");
if (! isset($condition['field']) || ! isset($condition['operator'])) {
continue;
}

// Apply configured query conditions
$query->where(
$value = $condition['value'] ?? null;

$query = $query->where(
$condition['field'],
$condition['operator'],
is_callable($value) ? $value() : $value
Expand All @@ -58,10 +54,13 @@ protected static function applyTabQuery(Builder $query, string $currentTab): Bui

protected static function getResourceKey(): string
{
// Convert class name to config key
// e.g., App\Resources\UserResource -> 'user'
$className = class_basename(static::class);
$key = strtolower(str_replace('Resource', '', $className));

if (str_contains(static::class, 'Builder\\Resources')) {
return 'previews.'.str_replace('_', '-', $key);
}

return strtolower(str_replace('Resource', '', $className));
return $key;
}
}

0 comments on commit 9bd70c1

Please sign in to comment.