Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
theofanisv committed Oct 6, 2022
1 parent b9bc7d8 commit baf7300
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 172 deletions.
189 changes: 96 additions & 93 deletions src/Http/Controllers/ResolvesFields.php
Original file line number Diff line number Diff line change
@@ -1,200 +1,203 @@
<?php

namespace Armincms\Fields\Http\Controllers;

use Illuminate\Database\Eloquent\Relations\Pivot;

use Illuminate\Database\Eloquent\Relations\Pivot;
use Laravel\Nova\Fields\Field;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Fields\FieldCollection;
use Laravel\Nova\Contracts\ListableField;
use Laravel\Nova\Contracts\Resolvable;
use Laravel\Nova\Resource;
use Laravel\Nova\ResourceToolElement;
use Armincms\Fields\ManyToMany;
use Armincms\Fields\ManyToMany;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Nova;

trait ResolvesFields
{
trait ResolvesFields
{
public function fields(NovaRequest $request, $resource)
{
{
$fields = $this->resolvePivotFields(
$request, $resource, $request->relatedResource
$request, $resource, $request->field
);

if($request->isUpdateOrUpdateAttachedRequest()) {
if ($request->isUpdateOrUpdateAttachedRequest()) {
return $this->removeNonUpdateFields($request, $fields, $resource);
}

if($request->isCreateOrAttachRequest()) {
if ($request->isCreateOrAttachRequest()) {
return $this->removeNonCreationFields($request, $fields);
}

return $this->removeNonDetailFields($request, $fields, $resource)
->each(function($field) use ($request, $resource) {
$relatedClass = Nova::resourceForKey($request->relatedResource);

$relatedResource = new $relatedClass(
$this->pivotModel($request, $resource, $request->relatedResource)
);

$field->resolveForDisplay($relatedResource);
});
}

->each(function ($field) use ($request, $resource) {
$relatedClass = Nova::resourceForKey($request->relatedResource);
$relatedResource = new $relatedClass(
$this->pivotModel($request, $resource, $request->field)
);
$field->resolveForDisplay($relatedResource);
});
}
/**
* Resolve the pivot fields for the requested resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Resource $resource
* @param string $relatedResource
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Resource $resource
* @param string $relatedResource
* @return \Laravel\Nova\Fields\FieldCollection
*/
public function resolvePivotFields(NovaRequest $request, $resource, $relatedResource)
{
{
$pivotModel = $this->pivotModel($request, $resource, $relatedResource);

$fields = $this->pivotFieldsFor($request, $resource, $relatedResource);

$resolveCallback = function ($field) use ($pivotModel) {
if ($field instanceof Resolvable) {
if ($field instanceof Resolvable) {
$field->resolve($pivotModel);
}
};

return FieldCollection::make($this->filter($fields->each($resolveCallback)->filter->authorize($request)->values()->all()))->values();
}

/**
* Resolve the pivot model for the requested resource and requested related id.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Resource $resource
* @param string $relatedResource
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Resource $resource
* @param string $relatedResource
* @return \Illuminate\Database\Eloquent\Model
*/
public function pivotModel(NovaRequest $request, $resource, $relatedResource)
{
{
$relatedField = $this->relatedFieldFor($request, $resource, $relatedResource);

throw_unless($relatedField, \Exception::class, "Related field not found for resource " . get_class($resource) . " for field $relatedResource");
$accessor = $this->pivotAccessorFor($request, $resource, $relatedField);

$query = $resource->{$relatedField->manyToManyRelationship}();

if($related = $query->wherePivot('id', $request->pivotId)->first()) {
$query = $resource->{$relatedField->manyToManyRelationship}();
if ($related = $query->wherePivot('id', $request->pivotId)->first()) {
return $related->{$accessor};
}

}
return new Pivot;
}

/**
* Get the pivot fields for the resource and relation.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param string $resource
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param Resource $resource
* @return \Laravel\Nova\Fields\FieldCollection
*/
public function pivotFieldsFor($request, $resource)
{
$field = $this->relatedFieldFor($request, $resource, $request->relatedResource);

if($field && isset($field->fieldsCallback)) {
$field = $this->relatedFieldFor($request, $resource, $request->field);
if ($field && isset($field->fieldsCallback)) {
return FieldCollection::make(array_values(
$this->filter(call_user_func($field->fieldsCallback, $request, $resource))
))->each(function ($field) {
$field->pivot = true;
});
});
}

return new FieldCollection;
}

}
/**
* Get the pivot fields for the resource and relation.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param string $attribute
* @return \Laravel\Nova\Fields\FieldCollection
* @param NovaRequest $request
* @param Resource $resource
* @param string $attribute
* @return Field|null
*/
public function relatedFieldFor(NovaRequest $request, $resource, $attribute)
{
return $resource->availableFields($request)
->whereInstanceOf(ManyToMany::class)
->where('attribute', $attribute)
->first();
}

->whereInstanceOf(ManyToMany::class)
->where('attribute', $attribute)
->first();
}
/**
* Get the name of the pivot accessor for the requested relationship.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Resource $resource
* @param \Laravel\Nova\Field $field
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Resource $resource
* @param Field $field
* @return string
*/
public function pivotAccessorFor(NovaRequest $request, $resource, $field)
{
{
return $resource->{$field->manyToManyRelationship}()->getPivotAccessor();
}

/**
* Remove non-update fields from the given collection.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Fields\FieldCollection $fields
* @param \Laravel\Nova\Resource $resource
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Fields\FieldCollection $fields
* @param \Laravel\Nova\Resource $resource
* @return \Laravel\Nova\Fields\FieldCollection
*/
protected function removeNonUpdateFields(NovaRequest $request, FieldCollection $fields, $resource)
{
return $fields->reject(function($field) use ($request, $resource) {
return $this->isNonEditableField($request, $field) ||
! $field->isShownOnUpdate($request, $resource);
return $fields->reject(function ($field) use ($request, $resource) {
return $this->isNonEditableField($request, $field) ||
!$field->isShownOnUpdate($request, $resource);
});
}

}
/**
* Remove non-creation fields from the given collection.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Fields\FieldCollection $fields
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Fields\FieldCollection $fields
* @return \Laravel\Nova\Fields\FieldCollection
*/
protected function removeNonCreationFields(NovaRequest $request, FieldCollection $fields)
{
return $fields->reject(function($field) use ($request) {
return $this->isNonEditableField($request, $field) ||
! $field->isShownOnCreation($request);
return $fields->reject(function ($field) use ($request) {
return $this->isNonEditableField($request, $field) ||
!$field->isShownOnCreation($request);
});
}

}
/**
* Remove non-creation fields from the given collection.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Fields\FieldCollection $fields
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Fields\FieldCollection $fields
* @return \Laravel\Nova\Fields\FieldCollection
*/
protected function removeNonDetailFields(NovaRequest $request, FieldCollection $fields, $resource)
{
return $fields->reject(function($field) use ($request, $resource) {
return ! $field->isShownOnDetail($request, $resource);
return $fields->reject(function ($field) use ($request, $resource) {
return !$field->isShownOnDetail($request, $resource);
});
}
}

/**
* Detect if the field cannot be edit.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Fields\Field $field
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Laravel\Nova\Fields\Field $field
* @return boolean
*/
protected function isNonEditableField(NovaRequest $request, $field)
{
$resource = $request->newResource();

return $field instanceof ListableField ||
$field instanceof ResourceToolElement ||
$field->attribute === 'ComputedField' ||
($field instanceof ID && $field->attribute === $resource->getKeyName());
return $field instanceof ListableField ||
$field instanceof ResourceToolElement ||
$field->attribute === 'ComputedField' ||
($field instanceof ID && $field->attribute === $resource->getKeyName());
}
}
Loading

0 comments on commit baf7300

Please sign in to comment.