Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
9 changes: 7 additions & 2 deletions src/Concerns/HasMediaLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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)) {
Expand Down
118 changes: 75 additions & 43 deletions src/Http/ParsesFlexibleAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -85,29 +96,36 @@ 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);
} else {
if(is_string($value)) {
$jsonDecoded = json_decode($value, true);
if (json_last_error() == JSON_ERROR_NONE) {
if ($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;
}
Expand All @@ -118,8 +136,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)
Expand All @@ -130,17 +148,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);
}

Expand All @@ -152,8 +170,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)
Expand All @@ -164,20 +182,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;
}

Expand All @@ -187,13 +205,27 @@ 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;
}
}
}

protected function isTranslatableAttribute($attribute): bool
{
if (config()->has('nova-translatable.locales')) {
foreach (array_keys(config('nova-translatable.locales')) as $locale) {
if (is_array($attribute) && isset($attribute[$locale])) {
return true;
}
}
}
return false;
}
}