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 novaPageIndexQuery to StaticResource to mimic indexQuery #46

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Pages/Concerns/QueriesResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\OptionResource;
use Laravel\Nova\Http\Requests\ResourceIndexRequest;
Expand All @@ -18,7 +19,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);
});
}
Expand Down Expand Up @@ -50,4 +53,13 @@ protected function getResourceForType($type, Template $resource) {
case 'option': return new $option_resource_class($resource);
}
}

protected function applyIndexQueryForType($type, Query $query) {
$page_resource_class = config('novapage.default_page_resource');
$option_resource_class = config('novapage.default_option_resource');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both config() calls should default to the provided Resource classes in case the config file does not (yet) contain the default_page_resource or default_option_resource keys in order to ensure update compatibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Nyratas I've added documentation docs/README.md and I've set the default values in src/Pages/Concerns/QueriesResources.php .

Let me know if there's anything I've missed. Thanks!

switch ($type) {
case 'route': return $page_resource_class::novaPageIndexQuery($query);
case 'option': return $option_resource_class::novaPageIndexQuery($query);
}
}
}
31 changes: 30 additions & 1 deletion src/Pages/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
})
Expand All @@ -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;
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/Pages/StaticResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,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.
*
Expand Down Expand Up @@ -221,4 +232,4 @@ protected function serializeWithId(Collection $fields)
];
}

}
}