Skip to content

Commit

Permalink
Use uri keys and send current resource in request so callback can use…
Browse files Browse the repository at this point in the history
… it (#117)
  • Loading branch information
ricklambrechts authored Jun 21, 2022
1 parent c100c44 commit abe7396
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 28 deletions.
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default {
viaResource: {},
viaResourceId: {},
viaRelationship: {},
relationshipType: {},
},
data() {
Expand Down Expand Up @@ -150,7 +151,13 @@ export default {
this.options = (
await Nova.request().post("/nova-vendor/nova-belongsto-depend", {
resourceClass: this.field.resourceParentClass,
resource: this.resourceName,
resourceId: this.resourceId,
viaResource: this.viaResource,
viaResourceId: this.viaResourceId,
viaRelationship: this.viaRelationship,
relationshipType: this.relationshipType,
relatedResource: this.viaResource,
modelClass: dependsOnValue.field.modelClass,
attribute: this.field.attribute,
dependsMap: this.field.dependsMap
Expand Down
68 changes: 44 additions & 24 deletions src/Http/Controllers/FieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Orlyapps\NovaBelongsToDepend\Http\Controllers;

use Illuminate\Database\Eloquent\Model;
use \Illuminate\Http\Resources\MergeValue;
use Illuminate\Routing\Controller;
use Illuminate\Support\Collection;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Nova;
use Laravel\Nova\Panel;
use Laravel\Nova\Resource;
use Orlyapps\NovaBelongsToDepend\NovaBelongsToDepend;

class FieldController extends Controller
Expand All @@ -17,10 +20,14 @@ public function index(NovaRequest $request)
abort(500, 'Depend On Relationship not found on the Resource specified for the Field "' . $request->attribute . '" Please check you have set correct /App/Nova/Resource');
}

if (method_exists($request->resourceClass, 'newModel')) {
$resource = new $request->resourceClass($request->resourceClass::newModel());
} else {
$resource = new $request->resourceClass;
$resource = Nova::resourceInstanceForKey($request->resource);
if (is_null($resource)) {
abort(500, 'Could not find resource "' . $request->resource);
}

$resourceModel = $resource->model()->find($request->resourceId);
if (is_null($resource)) {
abort(500, 'Could not find resource "' . $request->resource . '" with ' . $request->resourceId);
}

$attributedField = $this->getAttributedField($request, $resource);
Expand All @@ -31,56 +38,71 @@ public function index(NovaRequest $request)
return [];
}

$options = $this->getOptions($attributedField, $modelMap);
$options = $this->getOptions($attributedField, $modelMap, $resourceModel);

return $options instanceof Collection ? $options : [$options];
}

public function returnFields($fields)
/**
* @param array $fields
* @return Collection
*/
public function returnFields(array $fields)
{
return collect($fields)->map(function ($field) {
if (isset($field->data)) {
return $this->returnFields($field->data);
} elseif (isset($field->meta['fields'])) {
}

if (isset($field->meta['fields'])) {
return $this->returnFields($field->meta['fields']);
} elseif (isset($field->fields)) {
}

if (isset($field->fields)) {
return $this->returnFields($field->fields);
}

return $field;
})->flatten();
}

private function getAttributedField($request, $resource)
/**
* @param NovaRequest $request
* @param Resource $resource
* @return NovaBelongsToDepend
*/
private function getAttributedField(NovaRequest $request, Resource $resource)
{
// Create Nested Array Fields from Panels, filter out irrelevant fields
$fields = $this->returnFields($resource->fields($request));

$fields = $fields->filter(
function ($value) use ($request) {
return ($value instanceof NovaBelongsToDepend);
}
);
$fields = $fields->filter(function ($value) {
return ($value instanceof NovaBelongsToDepend);
});

//get the attributed field
// get the attributed field
$attributedField = $fields->first(function ($value, $key) use ($request) {
return ($value instanceof NovaBelongsToDepend && $value->attribute == $request->attribute);
});

if (is_null($attributedField)) {
abort(500, 'Can not find the Field "' . $request->attribute . '" in the Model "' . $request->resourceClass . '"');
abort(500, 'Can not find the Field "' . $request->attribute . '" in the Model "' . $resource::class . '"');
}

return $attributedField;
}

private function getModelMap($request, $attributedField)
/**
* @param NovaRequest $request
* @param NovaBelongsToDepend $attributedField
* @return array
*/
private function getModelMap(NovaRequest $request, NovaBelongsToDepend $attributedField)
{
$models = [];

foreach ($request->dependsMap as $value) {

$modelClass = $value['key'] ?? $request->modalClass;
$modelClass = Nova::modelInstanceForKey($value['key'] ?? $request->modalClass);
if (is_null($modelClass::find($value['value']))) {
abort(500, 'Can not find the Model "' . $modelClass . '::find(' . $value['value'] . ')');
}
Expand All @@ -99,17 +121,15 @@ private function getModelMap($request, $attributedField)
return $modelMap;
}

private function getOptions($attributedField, $modelMap)
private function getOptions(NovaBelongsToDepend $attributedField, array $modelMap, Model $resourceModel)
{
$options = null;

if (count($modelMap) == 1 && count($attributedField->dependsOn) == 1) {

$model = $modelMap[$attributedField->dependsOn[0]];
$options = ($attributedField->optionResolveCallback)($model);

$options = ($attributedField->optionResolveCallback)($model, $resourceModel);
} else {
$options = ($attributedField->optionResolveCallback)((object)$modelMap);
$options = ($attributedField->optionResolveCallback)((object)$modelMap, $resourceModel);
}

if (is_null($options)) {
Expand Down
4 changes: 2 additions & 2 deletions src/NovaBelongsToDepend.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct($name, $attribute = null, $resource = null)
$resource = $resource ?? ResourceRelationshipGuesser::guessResource($name);
parent::__construct($name, $attribute, $resource);

$this->modelClass = get_class($resource::newModel());
$this->modelClass = $resource::uriKey();
$this->modelPrimaryKey = $resource::newModel()->getKeyName();
$this->titleKey = $resource::$title;
$this->optionResolveCallback = function () {
Expand Down Expand Up @@ -147,7 +147,7 @@ public function resolve($resource, $attribute = null)
} else {
$value = $resource->{$this->attribute}()->withoutGlobalScopes()->first();
}

if ($value) {
$this->valueKey = $value->getKey();
$this->value = $this->formatDisplayValue($value);
Expand Down

0 comments on commit abe7396

Please sign in to comment.