Skip to content

Commit

Permalink
fix Input old
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanMeurisse committed Jul 25, 2024
1 parent c7ffb4a commit f90ea01
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 36 deletions.
65 changes: 36 additions & 29 deletions src/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ class CrudController extends Controller

public function __construct($entity, $config = [])
{

$this->entity = $entity;

$config = count($config) ? $config : config('crud.'.$this->route);
$config = count($config) ? $config : config('crud.' . $this->route);


if (is_array($config)) {
foreach ($config as $key => $value) {
Expand All @@ -71,19 +73,19 @@ public function __construct($entity, $config = [])

$this->actions = [
'details' => function ($row, $route) {
return '<a href="'.route($route.'.show', $row->{$row->getKeyName()}).'" class="btn-default btn btn-xs">
return '<a href="' . route($route . '.show', $row->{$row->getKeyName()}) . '" class="btn-default btn btn-xs">
<i class="fas fa-eye"></i>
</a>';
},
'edit' => function ($row, $route) {
return '<a href="'.route($route.'.edit', $row->{$row->getKeyName()}).'" class="btn-primary btn btn-xs edit_element">
return '<a href="' . route($route . '.edit', $row->{$row->getKeyName()}) . '" class="btn-primary btn btn-xs edit_element">
<i class="far fa-edit"></i>
</a>';
},
'delete' => function ($row, $route) {
return '<a href="'.route($route.'.destroy', $row->{$row->getKeyName()}).'"
return '<a href="' . route($route . '.destroy', $row->{$row->getKeyName()}) . '"
class="btn-danger btn btn-xs delete_element"
onclick="return confirm(\''.trans('eliurkis::crud.confirmation_delete').'\');">
onclick="return confirm(\'' . trans('eliurkis::crud.confirmation_delete') . '\');">
<i class="far fa-trash-alt"></i>
</a>';
},
Expand Down Expand Up @@ -227,17 +229,18 @@ public function store(Request $request)

DB::commit();

event($this->route.'.store', [$row, $mediaFiles]);
event($this->route . '.store', [$row, $mediaFiles]);

return redirect()
->route($this->route.'.index')
->route($this->route . '.index')
->with('success', isset($this->textsGeneral['save_action'])
? $this->textsGeneral['save_action']
: trans('eliurkis::crud.save_action'));
}

public function edit($id)
{

if (!$this->entityInstance) {
$this->entityInstance = $this->entity->findOrFail($id);
}
Expand Down Expand Up @@ -285,10 +288,10 @@ public function update(Request $request, $id)

DB::commit();

event($this->route.'.update', [$row, $mediaFiles]);
event($this->route . '.update', [$row, $mediaFiles]);

return redirect()
->route($this->route.'.index', $this->getParamsFilters($row))
->route($this->route . '.index', $this->getParamsFilters($row))
->with('success', isset($this->textsGeneral['save_action'])
? $this->textsGeneral['save_action']
: trans('eliurkis::crud.save_action'));
Expand All @@ -300,10 +303,10 @@ public function destroy($id)
$row = $this->entity->findOrFail($id);
$row->delete();

event($this->route.'.destroy', [$row]);
event($this->route . '.destroy', [$row]);
} catch (ModelNotFoundException $e) {
return redirect()
->route($this->route.'.index')
->route($this->route . '.index')
->with('error', __('The element that you are trying to delete does not exist'));
} catch (\Exception $e) {
\Log::error($e);
Expand All @@ -312,12 +315,12 @@ public function destroy($id)
}

return redirect()
->route($this->route.'.index')
->route($this->route . '.index')
->with('error', __('An error occurred, try again'));
}

return redirect()
->route($this->route.'.index')
->route($this->route . '.index')
->with('success', isset($this->textsGeneral['delete_action'])
? $this->textsGeneral['delete_action']
: trans('eliurkis::crud.delete_action'));
Expand Down Expand Up @@ -388,8 +391,8 @@ protected function htmlFilters()
if (isset($urlParams['filter'][$filter])) {
unset($urlParams['filter'][$filter]);
}
$this->fields[$filter]['attributes']['data-filter-url'] = route($this->route.'.index', $urlParams)
.(count($urlParams) ? '&' : '?');
$this->fields[$filter]['attributes']['data-filter-url'] = route($this->route . '.index', $urlParams)
. (count($urlParams) ? '&' : '?');

// Create array
$this->action = 'list';
Expand All @@ -414,7 +417,7 @@ protected function paginate($entity, $request)

if ($request->get('filter')) {
foreach ($request->get('filter') as $field => $value) {
$rows->appends(['filter['.$field.']' => $value]);
$rows->appends(['filter[' . $field . ']' => $value]);
}
}

Expand All @@ -434,7 +437,7 @@ protected function search($entity, $request)

$entity = $entity->where(function (Builder $query) use ($request, $searchableCols) {
foreach ($searchableCols as $field) {
$query->orWhere($field, 'like', '%'.$request->get('q').'%');
$query->orWhere($field, 'like', '%' . $request->get('q') . '%');
}
});

Expand Down Expand Up @@ -506,8 +509,8 @@ protected function prepareLinks()
$links = ['index', 'create', 'store'];

foreach ($links as $link) {
if (!isset($this->links[$link]) && Route::has($this->route.'.'.$link)) {
$this->links[$link] = route($this->route.'.'.$link);
if (!isset($this->links[$link]) && Route::has($this->route . '.' . $link)) {
$this->links[$link] = route($this->route . '.' . $link);
}
}

Expand Down Expand Up @@ -582,15 +585,17 @@ protected function prepareRelationalFields($name)

protected function prepareFields()
{


foreach ($this->fields as $name => $properties) {
$this->fields[$name]['html'] = $this->prepareField($name, $properties);
$this->fields[$name]['value'] = $this->entityInstance->$name ?? null;
$this->fields[$name]['value_text'] = $this->prepareFieldShow($name, $properties);
}

return $this->fields;
}

protected function prepareFieldShow($name, $properties = [])
{
// Init
Expand All @@ -608,7 +613,8 @@ protected function prepareFieldShow($name, $properties = [])

if ($this->entityInstance) {
if (($properties['type'] === 'date' || $properties['type'] === 'datetime') &&
$this->entityInstance->$name != '') {
$this->entityInstance->$name != ''
) {
$fieldValue = $this->entityInstance->$name;

if (!is_object($fieldValue)) {
Expand All @@ -621,12 +627,12 @@ protected function prepareFieldShow($name, $properties = [])
}

if (substr($properties['type'], 0, 4) === 'file' && $this->entityInstance->getMedia($name)->last()) {
$value = '<a href="'.route($this->route.'.download', [$this->entityInstance->id, $name]).
'" target="_blank">'.(
isset($this->fields[$name]['link_name'])
$value = '<a href="' . route($this->route . '.download', [$this->entityInstance->id, $name]) .
'" target="_blank">' . (
isset($this->fields[$name]['link_name'])
? $this->fields[$name]['link_name']
: 'download'
).'</a>';
) . '</a>';
}

if (isset($config['entity'])) {
Expand Down Expand Up @@ -657,12 +663,13 @@ protected function prepareField($name, $properties = [])

$value = $this->entityInstance
? isset($properties['value_alias'])
? $this->entityInstance->{$properties['value_alias']}
: $this->entityInstance->$name
? $this->entityInstance->{$properties['value_alias']}
: $this->entityInstance->$name
: ($this->fields[$name]['default_value'] ?? null);

// Define field type class namespace
$className = Str::studly($properties['type']);
$className = '\Eliurkis\Crud\FieldTypes\\'.Str::studly($properties['type']);

if (!class_exists($className)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/FieldTypes/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static function prepare($name, $value = null, $properties = [])
return '<div class="input-group date">
<span class="input-group-addon"><i class="far fa-calendar-alt"></i></span>
' . Html::text($name)
->value(Input::old($name, $value))
->value(old($name, $value))
->attributes($properties['attributes'] ?? [])
->toHtml() . '
</div>';
Expand Down
2 changes: 1 addition & 1 deletion src/FieldTypes/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Email
public static function prepare($name, $value = null, $properties = [])
{
return Html::email($name)
->value(Input::old($name, $value))
->value(old($name, $value))
->attributes($properties['attributes'] ?? []);
}
}
2 changes: 1 addition & 1 deletion src/FieldTypes/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Number
public static function prepare($name, $value = null, $properties = [])
{
return Html::input('number', $name)
->value(Input::old($name, $value))
->value(old($name, $value))
->attributes($properties['attributes'] ?? []);
}
}
2 changes: 1 addition & 1 deletion src/FieldTypes/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Select
public static function prepare($name, $options = [], $value = null, $properties = [])
{
return Html::select($name, $options)
->value(Input::old($name, $value))
->value(old($name, $value))
->attributes($properties['attributes'] ?? []);
}
}
3 changes: 1 addition & 2 deletions src/FieldTypes/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

namespace Eliurkis\Crud\FieldTypes;

use Illuminate\Support\Facades\Input;
use Spatie\Html\Facades\Html;

class Text
{
public static function prepare($name, $value = null, $properties = [])
{
return Html::text($name)
->value(Input::old($name, $value))
->value(old($name, $value))
->attributes($properties['attributes'] ?? []);
}
}
2 changes: 1 addition & 1 deletion src/FieldTypes/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Textarea
public static function prepare($name, $value = null, $properties = [])
{
return Html::textarea($name)
->value(Input::old($name, $value))
->value(old($name, $value))
->attributes($properties['attributes']);
}
}

0 comments on commit f90ea01

Please sign in to comment.