Skip to content

Commit

Permalink
Update form interpreter for multiple level forms.
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemjohn committed Jul 7, 2023
1 parent 5fe1b98 commit 8e5571c
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 48 deletions.
199 changes: 170 additions & 29 deletions src/Litepie/Form/FormInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Litepie\Form;

use Closure;
use Illuminate\Support\Arr;

/**
* Base class to store all form elements.
*/
Expand All @@ -19,7 +22,7 @@ abstract class FormInterpreter
*
* @var array
*/
protected static $grouped = true;
protected static $only = null;

/**
* Variable to store groups.
Expand Down Expand Up @@ -70,46 +73,124 @@ abstract class FormInterpreter
*/
public static $filters;

public static function grouped($grouped = true)
/**
* Variable to store lists.
*
* @var array
*/
public static $lists;

/**
* Variable to store actions.
*
* @var array
*/
public static $actions;

/**
* Set the only property to filter the form elements.
*
* @param string|null $only
* @return static
*/
public static function only($only = null)
{
self::$grouped = $grouped;
self::$only = $only;
return new static();
}

/**
* Get the translated lists.
*
* @return array
*/
public static function lists()
{
$lists = collect(self::$lists)->map(function ($val) {
$val['label'] = trans($val['label']);
return $val;
})->groupBy('group');

if (empty(Self::$only)) {
return $lists->toArray();
}


if (isset($lists[Self::$only])) {
return collect($lists[Self::$only])->toArray();
}
return [];
}

/**
* Get the translated fields.
*
* @return array
*/
public static function fields()
{
$item = collect(self::$fields)->map(function ($val) {
$fields = collect(self::$fields)->map(function ($val) {
$val['label'] = trans($val['label']);
$val['placeholder'] = trans($val['placeholder']);
if (isset($val['options']) && is_callable($val['options'])) {
if (isset($val['options']) && is_callable($val['options']) && $val['options'] instanceof Closure) {
$val['options'] = call_user_func($val['options']);
}
return $val;
});
})
->groupBy(['group']);

if (!self::$grouped) {
return $item;
if (empty(Self::$only)) {
return $fields->toArray();
}

if (self::$grouped === true || self::$grouped == 1) {
return $item->groupBy(['group'], true);
}

if (self::$grouped == 2) {
return $item->groupBy(['group', 'section'], true);
$fields = Arr::undot($fields);
if (isset($fields[Self::$only])) {
return collect(Arr::dot($fields[Self::$only]))->toArray();
}

return [];
}

/**
* Get the translated groups.
*
* @return array
*/
public static function groups()
{
foreach (self::$groups as $key => $val) {
self::$groups[$key]['name'] = trans($val['name']);
self::$groups[$key]['title'] = trans($val['title']);
$groups = collect(self::$groups)->map(function ($val) {
$val['name'] = trans($val['name']);
$val['title'] = trans($val['title']);
return $val;
})->keyBy(['group']);

foreach ($groups as $key => $group) {
unset($groups[$key]);
$key = str_replace('.', '.groups.', $key) . '';
$groups[$key] = $group;
}
return self::$groups;
}
$groups = Arr::undot($groups);

if (empty(Self::$only)) {
return $groups;
}

if (isset($groups[Self::$only])) {
if (isset($groups[Self::$only]['groups'])) {
return $groups[Self::$only]['groups'];
} else {
$groups[Self::$only];
}

}

return [];

}
/**
* Translates the values in the $orderBy array using the trans() function.
*
* @return array
*/
public static function orderBy()
{
foreach (self::$orderBy as $key => $val) {
Expand All @@ -118,23 +199,43 @@ public static function orderBy()
return self::$orderBy;
}

/**
* Translates the values in the $search array using the trans() function.
* If the 'options' key is set and is callable, it calls the function and assigns the result to the 'options' key.
*
* @return array
*/
public static function search()
{
return collect(self::$search)->map(function ($val) {
$val['label'] = trans($val['label']);
$val['placeholder'] = trans($val['placeholder']);
if (isset($val['options']) && is_callable($val['options']) && $val['options'] instanceof Closure) {
$val['options'] = call_user_func($val['options']);
}
return $val;
})->toArray();
}

public static function list() {

/**
* Translates the values in the $list array using the trans() function.
*
* @return array
*/
public static function ilist()
{
return collect(self::$list)->map(function ($val) {
$val['label'] = trans($val['label']);
return $val;
})->toArray();
}

/**
* Translates the values in the $urls array using the trans() function.
* If the 'url' key does not contain '://', it prepends the guard_url() function to the 'url' value.
*
* @return array
*/
public static function urls()
{
return collect(self::$urls)->map(function ($val) {
Expand All @@ -146,6 +247,12 @@ public static function urls()
})->toArray();
}

/**
* Translates the values in the $filters array using the trans() function.
* If the 'sub_menus' key is set, it translates the 'name' values in the sub_menus array.
*
* @return array
*/
public static function filters()
{
return collect(self::$filters)->map(function ($val) {
Expand All @@ -159,18 +266,52 @@ public static function filters()
})->toArray();
}

/**
* Filters the $actions array to only include items with 'Group' type.
* Translates the 'label' values in the filtered array.
* Groups the filtered array by 'group' key.
*
* @return array
*/
public static function actions()
{
$actions = collect(self::$actions)->filter(function ($item) {
return in_array('Group', $item['type']);
});
$actions = $actions->map(function ($action) {
$action['label'] = trans($action['label']);
return $action;
})->groupBy('group');

if (empty(Self::$only)) {
return $actions->toArray();
}
if (isset($actions[Self::$only])) {
return collect($actions[Self::$only])->toArray();
}

return [];

}

/**
* Returns an array containing all the translated values from the different arrays in the class.
*
* @return array
*/
public static function toArray()
{
return [
'urls' => static::urls(),
'list' => static::list(),
'search' => static::search(),
'orderBy' => static::orderBy(),

'groups' => static::groups(),
'fields' => static::fields()->toArray(),
'urls' => self::urls(),
'list' => self::ilist(),
'search' => self::search(),
'orderBy' => self::orderBy(),
'filters' => self::filters(),

'groups' => self::groups(),
'fields' => self::fields(),
'actions' => self::actions(),
'lists' => self::lists(),
];
}
}
33 changes: 14 additions & 19 deletions src/Litepie/Form/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,24 @@
*
* @return array
*/
function form_merge_form($form, $value, $grouped = true)
function form_merge_form($forms, $value, $grouped = true)
{
$data = json_decode(json_encode($value), true);

array_walk($form, function (&$val, $key) use ($data) {
$key = $val['key'];
if (isset($data['data'][$key])) {
$val['value'] = $data['data'][$key];
}
if ($val['element'] == 'file') {
$val['url'] = str_replace('//file', '/' . $key . '/file', $data['meta']['upload_url']);
}
});

if (!$grouped) {
return $form;
foreach ($forms as $fkey => $form) {
foreach($form as $key => $val) {
$k = $val['key'];
if (isset($data['data'][$k])) {
$forms[$fkey][$key]['value'] = $data['data'][$k];
}
if ($val['element'] == 'file') {
$forms[$fkey][$key]['url'] = str_replace('//file', '/' . $k . '/file', $data['meta']['upload_url']);
}
};
}

return collect($form)->groupBy('group', true)->toArray();
return $forms;
}
}


if (!function_exists('form_merge_list')) {
/**
* Merge form array with values.
Expand All @@ -47,7 +42,7 @@ function form_merge_list($form, $value)
{
$data = json_decode(json_encode($value), true);

foreach($form as $key => $val){
foreach ($form as $key => $val) {
$k = $val['key'];
if (isset($data[$k])) {
$val['value'] = $data[$k];
Expand All @@ -56,7 +51,7 @@ function form_merge_list($form, $value)
if ($val['type'] == 'file') {
$val['url'] = str_replace('//file', '/' . $k . '/file', $data['meta']['upload_url']);
}
$form[$k] = $val;
$form[$k] = $val;
unset($form[$key]);
}
return $form;
Expand Down

0 comments on commit 8e5571c

Please sign in to comment.