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

Fix getMediaModel with nested flexible Layout #267

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
9 changes: 7 additions & 2 deletions src/Concerns/HasMediaLibrary.php
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
use Illuminate\Support\Str;
use Ebess\AdvancedNovaMediaLibrary\Fields\Media;
use Whitecube\NovaFlexibleContent\Http\ScopedRequest;
use Whitecube\NovaFlexibleContent\Layouts\Layout;

trait HasMediaLibrary {

@@ -26,12 +27,16 @@ trait HasMediaLibrary {
*
* @return \Spatie\MediaLibrary\HasMedia
*/
protected function getMediaModel() : HasMedia
public function getMediaModel() : HasMedia
{
$model = Flexible::getOriginModel() ?? $this->model;

while ($model instanceof Layout) {
$model = $model->getMediaModel();
if(method_exists($model,'getMediaModel')) {
$model = $model->getMediaModel();
} else {
$model = $model->model;
}
}

if(is_null($model) || !($model instanceof HasMedia)) {
123 changes: 80 additions & 43 deletions src/Http/ParsesFlexibleAttributes.php
Original file line number Diff line number Diff line change
@@ -17,61 +17,72 @@ trait ParsesFlexibleAttributes
/**
* Check if given request should be handled by the middleware
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function requestHasParsableFlexibleInputs(Request $request)
{
return (in_array($request->method(), ['POST','PUT']) &&
is_string($request->input(FlexibleAttribute::REGISTER)));
return (in_array($request->method(), ['POST', 'PUT']) &&
is_string($request->input(FlexibleAttribute::REGISTER)));
}

/**
* Transform the request's flexible values
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function getParsedFlexibleInputs(Request $request)
{
$this->registerFlexibleFields($request->input(FlexibleAttribute::REGISTER));

return array_reduce(array_keys($request->all()), function($carry, $attribute) use ($request) {
$value = $request->input($attribute);
return array_reduce(
array_keys($request->all()),
function ($carry, $attribute) use ($request) {
$value = $request->input($attribute);

if(!$this->isFlexibleAttribute($attribute, $value)) return $carry;
if (!$this->isFlexibleAttribute($attribute, $value)) {
return $carry;
}

$carry[$attribute] = $this->getParsedFlexibleValue($value);
$carry[$attribute] = $this->getParsedFlexibleValue($value);

return $carry;
}, []);
return $carry;
},
[]
);
}

/**
* Apply JSON decode and recursively check for nested values
*
* @param mixed $value
* @param mixed $value
* @return array
*/
protected function getParsedFlexibleValue($value)
{
if(is_string($value)) {
if (is_string($value)) {
$raw = json_decode($value, true);
} else {
$raw = $value;
}

if(!is_array($raw)) return $value;
if (!is_array($raw)) {
return $value;
}

return array_map(function($group) {
return $this->getParsedFlexibleGroup($group);
}, $raw);
return array_map(
function ($group) {
return $this->getParsedFlexibleGroup($group);
},
$raw
);
}

/**
* Cleans & prepares a filled group
*
* @param array $group
* @param array $group
* @return array
*/
protected function getParsedFlexibleGroup($group)
@@ -85,29 +96,35 @@ protected function getParsedFlexibleGroup($group)
foreach ($group['attributes'] ?? [] as $attribute => $value) {
$this->fillFlexibleAttributes($clean['attributes'], $clean['key'], $attribute, $value);
}

foreach ($clean['attributes'] as $attribute => $value) {
if(!$this->isFlexibleAttribute($attribute, $value)) continue;
$clean['attributes'][$attribute] = $this->getParsedFlexibleValue($value);
if ($this->isFlexibleAttribute($attribute, $value)) {
$clean['attributes'][$attribute] = $this->getParsedFlexibleValue($value);
} elseif(is_string($value)) {
$jsonDecoded = json_decode($value, true);
if (json_last_error() === JSON_ERROR_NONE &&
is_array($jsonDecoded) &&
$this->isTranslatableAttribute($jsonDecoded)
) {
$clean['attributes'][$attribute] = $jsonDecoded;
}
}
}

return $clean;
}

/**
* Fill a flexible group's attributes with cleaned attributes & values
*
* @param array $attributes
* @param string $group
* @param string $attribute
* @param string $value
* @param array $attributes
* @param string $group
* @param string $attribute
* @param string $value
* @return void
*/
protected function fillFlexibleAttributes(&$attributes, $group, $attribute, $value)
{
$attribute = $this->parseAttribute($attribute, $group);

if($attribute->isFlexibleFieldsRegister()) {
if ($attribute->isFlexibleFieldsRegister()) {
$this->registerFlexibleFields($value, $group);
return;
}
@@ -118,8 +135,8 @@ protected function fillFlexibleAttributes(&$attributes, $group, $attribute, $val
/**
* Analyse and clean up the raw attribute
*
* @param string $attribute
* @param string $group
* @param string $attribute
* @param string $group
* @return \Whitecube\NovaFlexibleContent\Http\FlexibleAttribute
*/
protected function parseAttribute($attribute, $group)
@@ -130,17 +147,17 @@ protected function parseAttribute($attribute, $group)
/**
* Add flexible attributes to the register
*
* @param null|string $value
* @param null|string $group
* @param null|string $value
* @param null|string $group
* @return void
*/
protected function registerFlexibleFields($value, $group = null)
{
if(!$value) {
if (!$value) {
return;
}

if(!is_array($value)) {
if (!is_array($value)) {
$value = json_decode($value);
}

@@ -152,8 +169,8 @@ protected function registerFlexibleFields($value, $group = null)
/**
* Add an attribute to the register
*
* @param mixed $attribute
* @param null|string $group
* @param mixed $attribute
* @param null|string $group
* @return void
*/
protected function registerFlexibleField($attribute, $group = null)
@@ -164,20 +181,20 @@ protected function registerFlexibleField($attribute, $group = null)
}

/**
* Check if given attribute is a registered and usable
* Check if given attribute is a registered and usable
* flexible attribute
*
* @param string $attribute
* @param mixed $value
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function isFlexibleAttribute($attribute, $value)
{
if(!$this->getFlexibleAttribute($attribute)) {
if (!$this->getFlexibleAttribute($attribute)) {
return false;
}
if(!$value || !is_string($value)) {

if (!$value || !is_string($value)) {
return false;
}

@@ -187,13 +204,33 @@ protected function isFlexibleAttribute($attribute, $value)
/**
* Retrieve a registered flexible attribute
*
* @param string $attribute
* @param string $attribute
* @return \Whitecube\NovaFlexibleContent\Http\FlexibleAttribute
*/
protected function getFlexibleAttribute($attribute)
{
foreach ($this->registered as $registered) {
if($registered->name === $attribute) return $registered;
if ($registered->name === $attribute) {
return $registered;
}
}
}

/**
* Check if an attribute is a translatable
*
* @param array $attribute
* @return bool
*/
protected function isTranslatableAttribute(array $attribute): bool
{
if (config()->has('nova-translatable.locales')) {
foreach (array_keys(config('nova-translatable.locales')) as $locale) {
if (isset($attribute[$locale])) {
return true;
}
}
}
return false;
}
}
42 changes: 42 additions & 0 deletions src/Layouts/Layout.php
Original file line number Diff line number Diff line change
@@ -83,6 +83,34 @@ class Layout implements LayoutInterface, JsonSerializable, ArrayAccess, Arrayabl
* @var Illuminate\Database\Eloquent\Model
*/
protected $model;

/**
* Define that Layout is a model, when in fact it is not.
*
* @var bool
*/
protected $exists = false;

/**
* Define that Layout is a model, when in fact it is not.
*
* @var bool
*/
protected $wasRecentlyCreated = false;

/**
* The relation resolver callbacks for the Layout.
*
* @var array
*/
protected $relationResolvers = [];

/**
* The loaded relationships for the Layout.
*
* @var array
*/
protected $relations = [];

/**
* Create a new base Layout instance
@@ -573,6 +601,20 @@ protected function relationLoaded()
{
return false;
}

/**
* Get the dynamic relation resolver if defined or inherited, or return null.
* Since it is not possible to define a relation on a layout, this method
* returns null
*
* @param string $class
* @param string $key
* @return mixed
*/
public function relationResolver($class, $key)
{
return null;
}

/**
* Transform layout for serialization