Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for nested module on the dashboard #2547

Merged
merged 1 commit into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 38 additions & 7 deletions src/Http/Controllers/Admin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,17 @@
$date = $item->created_at->toIso8601String();
}

$parentRelationship = $module['parentRelationship'] ?? null;
$parent = $item->$parentRelationship;

return [
'id' => $item->id,
'href' => moduleRoute($module['name'], $module['routePrefix'] ?? null, 'edit', $item->id),
'href' => moduleRoute(
$module['name'],
$module['routePrefix'] ?? null,
'edit',
array_merge($parentRelationship ? [$parent->id] : [], [$item->id])
),
'thumbnail' => method_exists($item, 'defaultCmsImage') ? $item->defaultCmsImage(['w' => 100, 'h' => 100]) : null,
'published' => $item->published,
'activity' => twillTrans('twill::lang.dashboard.search.last-edit'),
Expand Down Expand Up @@ -510,19 +518,21 @@
'singular' => $module['label_singular'] ?? Str::singular($module['name']),
];

$isNestedModule = Str::contains($module['name'], '.');

return [
'label' => ucfirst($moduleOptions['label']),
'singular' => ucfirst($moduleOptions['singular']),
'number' => $moduleOptions['count'] ? $repository->getCountByStatusSlug(
'all',
$module['countScope'] ?? []
) : null,
'url' => moduleRoute(
'url' => !$isNestedModule ? moduleRoute(
$module['name'],
$module['routePrefix'] ?? null,
'index'
),
'createUrl' => $moduleOptions['create'] ? moduleRoute(
) : null,
'createUrl' => ($moduleOptions['create'] && !$isNestedModule) ? moduleRoute(
$module['name'],
$module['routePrefix'] ?? null,
'index',
Expand All @@ -544,19 +554,40 @@
if ($repository->hasBehavior('revisions')) {
$query->mine();
}
$parentRelationship = $module['parentRelationship'] ?? null;

return $query->get()->map(function ($draft) use ($module, $parentRelationship) {
$parent = $draft->$parentRelationship;

Check warning on line 560 in src/Http/Controllers/Admin/DashboardController.php

View check run for this annotation

Codecov / codecov/patch

src/Http/Controllers/Admin/DashboardController.php#L560

Added line #L560 was not covered by tests

return $query->get()->map(function ($draft) use ($module) {
return [
'type' => ucfirst($module['label_singular'] ?? Str::singular($module['name'])),
'name' => $draft->titleInDashboard ?? $draft->title,
'url' => moduleRoute($module['name'], $module['routePrefix'] ?? null, 'edit', [$draft->id]),
'url' => moduleRoute(
$module['name'],
$module['routePrefix'] ?? null,
'edit',
array_merge($parentRelationship ? [$parent->id] : [], [$draft->id])
)

Check warning on line 570 in src/Http/Controllers/Admin/DashboardController.php

View check run for this annotation

Codecov / codecov/patch

src/Http/Controllers/Admin/DashboardController.php#L565-L570

Added lines #L565 - L570 were not covered by tests
];
});
})->collapse()->values();
}

private function getRepository(string $module, string $forModule = null): ModuleRepository
{
return $this->app->make($forModule ?? $this->config->get('twill.namespace') . "\Repositories\\" . ucfirst(Str::singular($module)) . 'Repository');
$moduleName = '';

if (!$forModule) {
if (Str::contains($module, '.')) {
$parts = explode('.', $module);
foreach ($parts as $part) {
$moduleName .= ucfirst(Str::singular($part));

Check warning on line 584 in src/Http/Controllers/Admin/DashboardController.php

View check run for this annotation

Codecov / codecov/patch

src/Http/Controllers/Admin/DashboardController.php#L582-L584

Added lines #L582 - L584 were not covered by tests
}
} else {
$moduleName = ucfirst(Str::singular($module));
}
}

return $this->app->make($forModule ?? $this->config->get('twill.namespace') . "\Repositories\\" . $moduleName . 'Repository');
}
}