Skip to content

Commit

Permalink
Support custom repository in dashboard and buckets configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ifox committed Feb 28, 2020
1 parent ea14f33 commit 9d3578e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/Http/Controllers/Admin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function search(Request $request)
return $modules->filter(function ($module) {
return ($module['search'] ?? false);
})->map(function ($module) use ($request) {
$repository = $this->getRepository($module['name']);
$repository = $this->getRepository($module['name'], $module['repository'] ?? null);

$found = $repository->cmsSearch($request->get('search'), $module['search_fields'] ?? ['title'])->take(10);

Expand Down Expand Up @@ -349,7 +349,7 @@ private function getShortcuts($modules)
return $modules->filter(function ($module) {
return ($module['count'] ?? false) || ($module['create'] ?? false);
})->map(function ($module) {
$repository = $this->getRepository($module['name']);
$repository = $this->getRepository($module['name'], $module['repository'] ?? null);

$moduleOptions = [
'count' => $module['count'] ?? false,
Expand Down Expand Up @@ -388,7 +388,7 @@ private function getDrafts($modules)
return $modules->filter(function ($module) {
return ($module['draft'] ?? false);
})->map(function ($module) {
$repository = $this->getRepository($module['name']);
$repository = $this->getRepository($module['name'], $module['repository'] ?? null);

$drafts = $repository->draft()->mine()->limit(3)->latest()->get();

Expand All @@ -406,8 +406,8 @@ private function getDrafts($modules)
* @param string $module
* @return \A17\Twill\Repositories\ModuleRepository
*/
private function getRepository($module)
private function getRepository($module, $forModule = null)
{
return $this->app->make($this->config->get('twill.namespace') . "\Repositories\\" . ucfirst(Str::singular($module)) . "Repository");
return $this->app->make($forModule ?: $this->config->get('twill.namespace') . "\Repositories\\" . ucfirst(Str::singular($module)) . "Repository");
}
}
11 changes: 6 additions & 5 deletions src/Http/Controllers/Admin/FeaturedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ private function getFeaturedItemsByBucket($featuredSection, $featuredSectionKey)
'acceptedSources' => Collection::make($bucket['bucketables'])->pluck('module'),
'withToggleFeatured' => $bucket['with_starred_items'] ?? false,
'toggleFeaturedLabels' => $bucket['starred_items_labels'] ?? [],
'children' => Feature::where('bucket_key', $bucketKey)->with('featured')->get()->map(function ($feature) {
'children' => Feature::where('bucket_key', $bucketKey)->with('featured')->get()->map(function ($feature) use ($bucket) {
if (($item = $feature->featured) != null) {
$repository = $this->getRepository($feature->featured_type);
$forModuleRepository = collect($bucket['bucketables'])->where('module', $feature->featured_type)->first()['repository'] ?? null;
$repository = $this->getRepository($feature->featured_type, $forModuleRepository);
$withImage = classHasTrait($repository, HandleMedias::class);

return [
Expand Down Expand Up @@ -149,7 +150,7 @@ private function getFeaturedSources(Request $request, $featuredSection, $search
return Collection::make($bucket['bucketables'])->mapWithKeys(function ($bucketable) use (&$fetchedModules, $bucketKey, $search, $request) {

$module = $bucketable['module'];
$repository = $this->getRepository($module);
$repository = $this->getRepository($module, $bucketable['repository'] ?? null);
$translated = classHasTrait($repository, HandleTranslations::class);
$withImage = classHasTrait($repository, HandleMedias::class);

Expand Down Expand Up @@ -231,8 +232,8 @@ public function save(Request $request, DB $db)
* @param string $bucketable
* @return \A17\Twill\Repositories\ModuleRepository
*/
private function getRepository($bucketable)
private function getRepository($bucketable, $forModule = null)
{
return $this->app->make($this->config->get('twill.namespace') . "\Repositories\\" . ucfirst(Str::singular($bucketable)) . "Repository");
return $this->app->make($forModule ?: $this->config->get('twill.namespace') . "\Repositories\\" . ucfirst(Str::singular($bucketable)) . "Repository");
}
}

0 comments on commit 9d3578e

Please sign in to comment.