Skip to content

Commit

Permalink
added support for mested module on the dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
zeezo887 authored and ifox committed May 24, 2024
1 parent 11a68ae commit 486e7e6
Showing 1 changed file with 38 additions and 7 deletions.
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 @@ public function search(Request $request): Collection
$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 @@ private function getShortcuts(Collection $modules): Collection
'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 @@ private function getDrafts(Collection $modules): Collection
if ($repository->hasBehavior('revisions')) {
$query->mine();
}
$parentRelationship = $module['parentRelationship'] ?? null;

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

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])
)
];
});
})->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));
}
} else {
$moduleName = ucfirst(Str::singular($module));
}
}

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

0 comments on commit 486e7e6

Please sign in to comment.