From 15888c0c9338eb07fdec85907fdafb3fc2fb4fc5 Mon Sep 17 00:00:00 2001 From: Gareth Somers Date: Thu, 26 Sep 2019 15:35:24 +0100 Subject: [PATCH 1/5] Added novaPageIndexQuery method to StaticResource which is called by the QueriesResource Concern to mimic the core indexQuery method. Added a orderBy method to the Query to allow control ordering. --- src/Pages/Concerns/QueriesResources.php | 14 ++++++++++- src/Pages/Query.php | 31 ++++++++++++++++++++++++- src/Pages/StaticResource.php | 14 ++++++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/Pages/Concerns/QueriesResources.php b/src/Pages/Concerns/QueriesResources.php index ed1dfc0..6c5769b 100644 --- a/src/Pages/Concerns/QueriesResources.php +++ b/src/Pages/Concerns/QueriesResources.php @@ -3,6 +3,7 @@ namespace Whitecube\NovaPage\Pages\Concerns; use Route; +use Whitecube\NovaPage\Pages\Query; use Whitecube\NovaPage\Pages\Template; use Whitecube\NovaPage\Pages\PageResource; use Whitecube\NovaPage\Pages\OptionResource; @@ -19,7 +20,9 @@ trait QueriesResources */ public function queryIndexResources(ResourceIndexRequest $request, $type) { $query = $this->newQueryWithoutScopes(); - return $query->whereType($type)->get(false)->map(function($template) use ($type) { + $query->whereType($type); + $this->applyIndexQueryForType($type, $query); + return $query->get(false)->map(function($template) use ($type) { return $this->getResourceForType($type, $template); }); } @@ -49,4 +52,13 @@ protected function getResourceForType($type, Template $resource) { case 'option': return new OptionResource($resource); } } + + protected function applyIndexQueryForType($type, Query $query) { + $page_resource_class = config('novapage.default_page_resource'); + $option_resource_class = config('novapage.default_option_resource'); + switch ($type) { + case 'route': return $page_resource_class::novaPageIndexQuery($query); + case 'option': return $option_resource_class::novaPageIndexQuery($query); + } + } } diff --git a/src/Pages/Query.php b/src/Pages/Query.php index 5e42388..78f3287 100644 --- a/src/Pages/Query.php +++ b/src/Pages/Query.php @@ -29,6 +29,16 @@ class Query */ protected $type; + /** + * The column to order by + */ + protected $orderColumn; + + /** + * The direction to order by + */ + protected $orderDirection; + /** * The locale filter used to retrieve the resource * @@ -70,6 +80,19 @@ public function whereType($type) return $this; } + /** + * Mimic eloquent's Builder and register a orderBy statment + * + * @param mixed $column + * @param string $direction + */ + public function orderBy($column, string $direction = 'ASC') + { + $this->orderColumn = $column; + $this->orderDirection = $direction; + return $this; + } + /** * Mimic eloquent's Builder and return corresponding Resource * @@ -97,7 +120,7 @@ public function get($throwOnMissing = false) { $resources = $this->repository->getFiltered(trim($this->type . '.*', '.')); - return Collection::make($resources) + $result = Collection::make($resources) ->map(function($template, $key) { return $this->repository->getResourceTemplate($key); }) @@ -107,6 +130,12 @@ public function get($throwOnMissing = false) list($type, $name) = explode('.', $key, 2); return $this->repository->load($type, $name, $this->locale, $throwOnMissing); }); + + if ($this->orderColumn && $this->orderDirection) { + $result = $result->sortBy($this->orderColumn, SORT_REGULAR, ($this->orderDirection === 'DESC') ? true : false); + } + + return $result; } /** diff --git a/src/Pages/StaticResource.php b/src/Pages/StaticResource.php index fc97d10..5d27a7d 100644 --- a/src/Pages/StaticResource.php +++ b/src/Pages/StaticResource.php @@ -2,6 +2,7 @@ namespace Whitecube\NovaPage\Pages; +use App\Scopes\People\PrimaryClubPersonScope; use Laravel\Nova\Resource; use Laravel\Nova\Fields\ID; use Illuminate\Http\Request; @@ -104,6 +105,17 @@ public function fields(Request $request) ); } + /** + * A blank method which allows index queries to be manipulated by the Resource + * + * @param Query $query + * @return Query + */ + public static function novaPageIndexQuery(Query $query) + { + return $query; + } + /** * Get the base fields displayed at the top of the resource's form. * @@ -221,4 +233,4 @@ protected function serializeWithId(Collection $fields) ]; } -} \ No newline at end of file +} From 5076404d0252b1b116626c59f4bccaf3fd41681e Mon Sep 17 00:00:00 2001 From: Gareth Somers Date: Thu, 26 Sep 2019 15:52:04 +0100 Subject: [PATCH 2/5] Whoops --- src/Pages/StaticResource.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Pages/StaticResource.php b/src/Pages/StaticResource.php index 5d27a7d..2eba9f7 100644 --- a/src/Pages/StaticResource.php +++ b/src/Pages/StaticResource.php @@ -2,7 +2,6 @@ namespace Whitecube\NovaPage\Pages; -use App\Scopes\People\PrimaryClubPersonScope; use Laravel\Nova\Resource; use Laravel\Nova\Fields\ID; use Illuminate\Http\Request; From 9415d266713760c15cec34c6d1ad9b34eee43591 Mon Sep 17 00:00:00 2001 From: Gareth Somers Date: Fri, 11 Oct 2019 09:33:01 +0100 Subject: [PATCH 3/5] Documenting functionality --- docs/README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 0c2546c..0d3f38d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -71,6 +71,61 @@ php artisan migrate You can customize the table name in the migration file and then update the `table_name` parameter in the `novapage.php` config file, if you wish. +### Extending the Page Resource +Nova Page uses `Whitecube\NovaPage\Pages\PageResource` as the Nova Resource for Page's. + +You can customize it's behaviour by extending the class. For example in `app\Nova\PageResource.php`: +``` +namespace App\Nova; + +use Whitecube\NovaPage\Pages\PageResource as BasePageResource; + +class PageResource extends BasePageResource +{ + /** + * Overrwrites the Page Resource's fields + * + * @param \Illuminate\Http\Request $request + * @return array + */ + public function fields(Request $request) + { + return parent::fields($request); + } +} +``` +And then in the config file, change the 'default_page_resource' option to your own class name. For example: +``` +'default_page_resource' => \App\Nova\PageResource::class, +``` +### Extending the Option Resource +Nova Page uses `Whitecube\NovaPage\Pages\OptionResource` as the Nova Resource for Option's. + +You can customize it's behaviour by extending the class. For example in `app\Nova\OptionResource.php`: +``` +namespace App\Nova; + +use Whitecube\NovaPage\Pages\OptionResource as BaseOptionResource; + +class OptionResource extends BaseOptionResource +{ + /** + * Overrwrites the Option Resource's fields + * + * @param \Illuminate\Http\Request $request + * @return array + */ + public function fields(Request $request) + { + return parent::fields($request); + } +} +``` +And then in the config file, change the 'default_option_resource' option to your own class name. For example: +``` +'default_option_resource' => \App\Nova\OptionResource::class, +``` + ## Templates In order to assign fields (and even cards!) to a page's edition form, we'll have to create a `Template` class and register this class on one or more routes. You'll see, it's quite easy. @@ -371,4 +426,4 @@ class Home extends Template */ protected $jsonAttributes = ['my_json_attribute', 'another_json_attribute']; } -``` \ No newline at end of file +``` From 01c3f63cebaa5d754b28fe280c87159391ccf171 Mon Sep 17 00:00:00 2001 From: Gareth Somers Date: Fri, 11 Oct 2019 09:34:57 +0100 Subject: [PATCH 4/5] Further documentation + moved it down --- docs/README.md | 128 ++++++++++++++++++++++++++++--------------------- 1 file changed, 73 insertions(+), 55 deletions(-) diff --git a/docs/README.md b/docs/README.md index 0d3f38d..e73bfd4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -71,61 +71,6 @@ php artisan migrate You can customize the table name in the migration file and then update the `table_name` parameter in the `novapage.php` config file, if you wish. -### Extending the Page Resource -Nova Page uses `Whitecube\NovaPage\Pages\PageResource` as the Nova Resource for Page's. - -You can customize it's behaviour by extending the class. For example in `app\Nova\PageResource.php`: -``` -namespace App\Nova; - -use Whitecube\NovaPage\Pages\PageResource as BasePageResource; - -class PageResource extends BasePageResource -{ - /** - * Overrwrites the Page Resource's fields - * - * @param \Illuminate\Http\Request $request - * @return array - */ - public function fields(Request $request) - { - return parent::fields($request); - } -} -``` -And then in the config file, change the 'default_page_resource' option to your own class name. For example: -``` -'default_page_resource' => \App\Nova\PageResource::class, -``` -### Extending the Option Resource -Nova Page uses `Whitecube\NovaPage\Pages\OptionResource` as the Nova Resource for Option's. - -You can customize it's behaviour by extending the class. For example in `app\Nova\OptionResource.php`: -``` -namespace App\Nova; - -use Whitecube\NovaPage\Pages\OptionResource as BaseOptionResource; - -class OptionResource extends BaseOptionResource -{ - /** - * Overrwrites the Option Resource's fields - * - * @param \Illuminate\Http\Request $request - * @return array - */ - public function fields(Request $request) - { - return parent::fields($request); - } -} -``` -And then in the config file, change the 'default_option_resource' option to your own class name. For example: -``` -'default_option_resource' => \App\Nova\OptionResource::class, -``` - ## Templates In order to assign fields (and even cards!) to a page's edition form, we'll have to create a `Template` class and register this class on one or more routes. You'll see, it's quite easy. @@ -404,6 +349,79 @@ And use it as a regular object in the `pages.home` template: As you can see, for convenience regular attributes (= defined fields) can be directly retrieved as properties of the `Whitecube\NovaPage\Pages\Template` instances. +## Extending the Resources +Nova Page uses two classes for the Page and Options. They can be extended to customize their functionality. + +### Page Resource +Nova Page uses `Whitecube\NovaPage\Pages\PageResource` as the Nova Resource for Page's. + +You can customize it's behaviour by extending the class. For example in `app\Nova\PageResource.php`: +``` +namespace App\Nova; + +use Whitecube\NovaPage\Pages\PageResource as BasePageResource; + +class PageResource extends BasePageResource +{ + /** + * Overrwrites the Page Resource's fields + * + * @param \Illuminate\Http\Request $request + * @return array + */ + public function fields(Request $request) + { + return parent::fields($request); + } +} +``` +And then in the config file, change the 'default_page_resource' option to your own class name. For example: +``` +'default_page_resource' => \App\Nova\PageResource::class, +``` +### Option Resource +Nova Page uses `Whitecube\NovaPage\Pages\OptionResource` as the Nova Resource for Option's. + +You can customize it's behaviour by extending the class. For example in `app\Nova\OptionResource.php`: +``` +namespace App\Nova; + +use Whitecube\NovaPage\Pages\OptionResource as BaseOptionResource; + +class OptionResource extends BaseOptionResource +{ + /** + * Overrwrites the Option Resource's fields + * + * @param \Illuminate\Http\Request $request + * @return array + */ + public function fields(Request $request) + { + return parent::fields($request); + } +} +``` +And then in the config file, change the 'default_option_resource' option to your own class name. For example: +``` +'default_option_resource' => \App\Nova\OptionResource::class, +``` + +### Resource Index Queries +Each Resource is provided a method `novaPageIndexQuery` to mimic the core `indexQuery` on Nova Resource's. + +After extending either the Page or Option Resource classes you can override the `novaPageIndexQuery` method. For example: +``` +public static function novaPageIndexQuery(\Whitecube\NovaPage\Pages\Query $query) +{ + return $query->orderBy(function ($item) { + return $item->getTitle(); + }, 'ASC'); +} +``` + +Take note: The first parameter given to `novaPageIndexQuery` is an instance of `Whitecube\NovaPage\Pages\Query` and has own set of methods. + ## Advanced features ### Working with JSON fields From 42a772eb2cb83d0e2e6a16bb5d0c89cce9197da8 Mon Sep 17 00:00:00 2001 From: Gareth Somers Date: Fri, 11 Oct 2019 09:35:04 +0100 Subject: [PATCH 5/5] Added defaults to config classes --- src/Pages/Concerns/QueriesResources.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Pages/Concerns/QueriesResources.php b/src/Pages/Concerns/QueriesResources.php index a18e0ed..3e62b32 100644 --- a/src/Pages/Concerns/QueriesResources.php +++ b/src/Pages/Concerns/QueriesResources.php @@ -46,8 +46,8 @@ public function queryResourcesCount(ResourceIndexRequest $request, $type) * @return \Laravel\Nova\Resource */ protected function getResourceForType($type, Template $resource) { - $page_resource_class = config('novapage.default_page_resource'); - $option_resource_class = config('novapage.default_option_resource'); + $page_resource_class = config('novapage.default_page_resource', \Whitecube\NovaPage\Pages\PageResource::class); + $option_resource_class = config('novapage.default_option_resource', \Whitecube\NovaPage\Pages\OptionResource::class); switch ($type) { case 'route': return new $page_resource_class($resource); case 'option': return new $option_resource_class($resource); @@ -55,8 +55,8 @@ protected function getResourceForType($type, Template $resource) { } protected function applyIndexQueryForType($type, Query $query) { - $page_resource_class = config('novapage.default_page_resource'); - $option_resource_class = config('novapage.default_option_resource'); + $page_resource_class = config('novapage.default_page_resource', \Whitecube\NovaPage\Pages\PageResource::class); + $option_resource_class = config('novapage.default_option_resource', \Whitecube\NovaPage\Pages\OptionResource::class); switch ($type) { case 'route': return $page_resource_class::novaPageIndexQuery($query); case 'option': return $option_resource_class::novaPageIndexQuery($query);