Skip to content

Commit 6c462a6

Browse files
committed
add edit function - wip
1 parent 78815e0 commit 6c462a6

11 files changed

+209
-24
lines changed

resources/views/fields/inc/button-add.blade.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
class="btn btn-primary"
66
style="border-radius: 0px"
77
data-toggle="modal"
8-
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal">
9-
{{ trans('backpack::crud.add') }}
8+
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_create_modal">
9+
<i class="fa fa-plus"></i>
1010
</button>
1111
</span>
1212
<div class="modal fade"
13-
id="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"
13+
id="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_create_modal"
1414
tabindex="-1"
1515
role="dialog"
16-
aria-labelledby="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"
16+
aria-labelledby="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_create_modal"
1717
aria-hidden="true">
1818
<div class="modal-dialog" role="document">
1919
<div class="modal-content"></div>

resources/views/fields/inc/button-edit.blade.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
<span class="input-group-btn">
22
<button
3-
href="{{ $field['on_the_fly']['edit_view'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/edit?field_name='.$field['name'].'&attribute='.($field['on_the_fly']['attribute'] ?? 'name') }}"
3+
href="{{ $field['on_the_fly']['edit_view'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/edit?field_name='.$field['name'].'&attribute='.($field['on_the_fly']['attribute'] ?? 'name') }}&id=1"
44
type="button"
55
class="btn btn-warning"
66
style="border-radius: 0px"
77
data-toggle="modal"
8-
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal">
9-
{{ trans('backpack::crud.edit') }}
8+
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_edit_modal"
9+
onclick="">
10+
<i class="fa fa-pencil"></i>
1011
</button>
1112
</span>
1213
<div class="modal fade"
13-
id="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"
14+
id="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_edit_modal"
1415
tabindex="-1"
1516
role="dialog"
16-
aria-labelledby="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"
17+
aria-labelledby="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_edit_modal"
1718
aria-hidden="true">
1819
<div class="modal-dialog" role="document">
1920
<div class="modal-content"></div>

resources/views/fields/select2_from_ajax.blade.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class="form-control"
4141
@if (isset($field['on_the_fly']))
4242
@include('webfactor::fields.inc.button-add')
4343
@endif
44+
45+
@if (isset($field['on_the_fly']))
46+
@include('webfactor::fields.inc.button-edit')
47+
@endif
4448
</div>
4549

4650
{{-- HINT --}}

resources/views/modal/create.blade.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@extends('webfactor::modal.modal_layout')
1+
@extends('webfactor::modal.create_layout')
22

33
@section('header')
44
<h3 class="box-title">{{ trans('backpack::crud.add_a_new') }} {{ $crud->entity_name }}</h3>
@@ -11,21 +11,22 @@
1111

1212
<!-- load the view from the application if it exists, otherwise load the one in the package -->
1313
@if(view()->exists('vendor.backpack.crud.form_content'))
14-
@include('vendor.backpack.crud.form_content', ['fields' => $crud->getFields('create')])
14+
@include('vendor.backpack.crud.form_content', ['fields' => $fields])
1515
@else
16-
@include('crud::form_content', ['fields' => $crud->getFields('create')])
16+
@include('crud::form_content', ['fields' => $fields])
1717
@endif
1818
</div>
1919
</div>
2020
@endsection
2121

2222
@section('footer')
23-
@include('webfactor::modal.inc.form_save_buttons')
23+
@include('webfactor::modal.inc.create_form_save_buttons')
2424
@endsection
2525

2626
@push('crud_fields_scripts')
2727
<script>
2828
$("#create_{{ $entity }}").submit(function (e) {
29+
e.preventDefault(); // avoid to execute the actual submit of the form.
2930
3031
$.ajax({
3132
type: "PUT",
@@ -38,7 +39,7 @@
3839
text: "{{ trans('backpack::crud.insert_success') }}"
3940
});
4041
41-
$("#{{ $entity }}_modal").modal('toggle');
42+
$("#{{ $entity }}_create_modal").modal('toggle');
4243
4344
// provide auto-fill
4445
@@ -73,8 +74,6 @@
7374
});
7475
}
7576
});
76-
77-
e.preventDefault(); // avoid to execute the actual submit of the form.
7877
});
7978
</script>
8079

resources/views/modal/modal_layout.blade.php renamed to resources/views/modal/create_layout.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{{--'files'=>$crud->hasUploadFields('create')])--}}
44
{{ csrf_field() }}
55
<div class="modal-header">
6-
<button type="button" class="close" data-toggle="modal" data-target="#{{ $entity }}_modal" aria-label="Close">
6+
<button type="button" class="close" data-toggle="modal" data-target="#{{ $entity }}_create_modal" aria-label="Close">
77
<span aria-hidden="true">&times;</span>
88
</button>
99
@yield('header')
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
@extends('webfactor::modal.edit_layout')
2+
3+
@section('header')
4+
<h3 class="box-title">{{ trans('backpack::crud.edit') }} {{ $crud->entity_name }}</h3>
5+
@endsection
6+
7+
@section('content')
8+
<div class="row">
9+
<div class="col-md-10 col-md-offset-1">
10+
@include('crud::inc.grouped_errors')
11+
12+
<!-- load the view from the application if it exists, otherwise load the one in the package -->
13+
@if(view()->exists('vendor.backpack.crud.form_content'))
14+
@include('vendor.backpack.crud.form_content', ['fields' => $fields, 'action' => 'edit'])
15+
@else
16+
@include('crud::form_content', ['fields' => $fields, 'action' => 'edit'])
17+
@endif
18+
</div>
19+
</div>
20+
@endsection
21+
22+
@section('footer')
23+
@include('webfactor::modal.inc.edit_form_save_buttons')
24+
@endsection
25+
26+
@push('crud_fields_scripts')
27+
<script>
28+
$("#edit_{{ $entity }}").submit(function (e) {
29+
30+
$.ajax({
31+
type: "PATCH",
32+
url: "/{{ ltrim($crud->route . '/ajax', '/') }}",
33+
data: $("#edit_{{ $entity }}").serialize(), // serializes the form's elements.
34+
success: function (data) {
35+
new PNotify({
36+
type: "success",
37+
title: "{{ trans('backpack::base.success') }}",
38+
text: "{{ trans('backpack::crud.update_success') }}"
39+
});
40+
41+
$("#{{ $entity }}_edit_modal").modal('toggle');
42+
43+
// provide auto-fill
44+
45+
if ($("#select2_ajax_{{ $field_name }}").length) {
46+
searchfield = $("#select2_ajax_{{ $field_name }}")
47+
} else {
48+
searchfield = $("#select2_ajax_multiple_{{ $field_name }}")
49+
}
50+
51+
searchfield.val(null).trigger('change');
52+
searchfield.select2('open');
53+
54+
// Get the search box within the dropdown or the selection
55+
// Dropdown = single, Selection = multiple
56+
var search = searchfield.data('select2').dropdown.$search || searchfield.data('select2').selection.$search;
57+
// This is undocumented and may change in the future
58+
var userInput = $("#edit_{{ $entity }} [name='{{ $attribute }}']").serializeArray();
59+
60+
search.val(userInput[0]['value']);
61+
search.trigger('input');
62+
setTimeout(function () {
63+
$('.select2-results__option').trigger("mouseup");
64+
}, 200);
65+
66+
console.log(data)
67+
68+
},
69+
error: function (data) {
70+
new PNotify({
71+
type: "error",
72+
title: "{{ trans('backpack::base.error') }}",
73+
text: "{{ trans('backpack::base.error') }}: " + data.responseJSON
74+
});
75+
}
76+
});
77+
78+
e.preventDefault(); // avoid to execute the actual submit of the form.
79+
});
80+
</script>
81+
82+
@endpush
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@stack('crud_fields_styles')
2+
<form method="post" id="{{ 'edit_'.$entity }}" accept-charset="UTF-8">
3+
{{--'files'=>$crud->hasUploadFields('create')])--}}
4+
{{ csrf_field() }}
5+
<div class="modal-header">
6+
<button type="button" class="close" data-toggle="modal" data-target="#{{ $entity }}_edit_modal" aria-label="Close">
7+
<span aria-hidden="true">&times;</span>
8+
</button>
9+
@yield('header')
10+
</div>
11+
<div class="modal-body" id="modal-body">
12+
@yield('content')
13+
</div>
14+
15+
<div class="modal-footer">
16+
@yield('footer')
17+
</div>
18+
</form>
19+
@stack('crud_fields_scripts')
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div id="saveActions" class="form-group">
2+
3+
<button type="submit" class="btn btn-success">
4+
<span class="fa fa-save" role="presentation" aria-hidden="true"></span> &nbsp;
5+
<span data-value="save_and_back">{{ trans('backpack::crud.save') }}</span>
6+
</button>
7+
8+
<button class="btn btn-default" type="button" data-toggle="modal" data-target="#{{ $entity }}_create_modal">
9+
<span class="fa fa-ban"></span> {{ trans('backpack::crud.cancel') }}
10+
</button>
11+
</div>

resources/views/modal/inc/form_save_buttons.blade.php renamed to resources/views/modal/inc/edit_form_save_buttons.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
<button type="submit" class="btn btn-success">
66
<span class="fa fa-save" role="presentation" aria-hidden="true"></span> &nbsp;
7-
<span data-value="{{ $saveAction['active']['value'] }}">{{ $saveAction['active']['label'] }}</span>
7+
<span data-value="save_and_back">{{ trans('backpack::crud.save') }}</span>
88
</button>
99

10-
<button class="btn btn-default" type="button" data-toggle="modal" data-target="#{{ $entity }}_modal">
10+
<button class="btn btn-default" type="button" data-toggle="modal" data-target="#{{ $entity }}_edit_modal">
1111
<span class="fa fa-ban"></span> {{ trans('backpack::crud.cancel') }}
1212
</button>
1313
</div>

src/Traits/HandlesAjaxRequest.php

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ public function handleAjaxRequest(Request $request, $mode = null)
2020
return $this->ajaxCreate();
2121
}
2222

23-
/*if ($mode == 'edit') {
23+
if ($mode == 'edit') {
2424
return $this->ajaxEdit();
25-
}*/
25+
}
2626

2727
if (strtolower($request->method()) == 'put') {
2828
return $this->ajaxStore($request);
2929
}
3030

31+
if (strtolower($request->method()) == 'patch') {
32+
return $this->ajaxUpdate($request);
33+
}
34+
3135
return $this->ajaxIndex($request);
3236
}
3337

@@ -65,7 +69,6 @@ public function ajaxCreate()
6569
->with('action', 'create')
6670
->with('entity', $this->getAjaxEntity())
6771
->with('crud', $this->crud)
68-
->with('saveAction', $this->getSaveAction())
6972
->with('fields', $this->crud->getCreateFields())
7073
->with('title', trans('backpack::crud.add') . ' ' . $this->crud->entity_name)
7174
->with('field_name', request()->input('field_name'))
@@ -79,9 +82,19 @@ public function ajaxCreate()
7982
*/
8083
public function ajaxEdit()
8184
{
82-
$this->crud->hasAccessOrFail('edit');
85+
$this->crud->hasAccessOrFail('update');
8386

84-
//
87+
return \View::make('webfactor::modal.edit')
88+
->with('action', 'edit')
89+
->with('id', request()->input('id'))
90+
->with('entity', $this->getAjaxEntity())
91+
->with('crud', $this->crud)
92+
->with('saveAction', $this->getSaveAction())
93+
->with('fields', $this->crud->getUpdateFields(request()->input('id')))
94+
->with('title', trans('backpack::crud.add') . ' ' . $this->crud->entity_name)
95+
->with('field_name', request()->input('field_name'))
96+
->with('attribute', request()->input('attribute'))
97+
->render();
8598
}
8699

87100
/**
@@ -110,6 +123,32 @@ public function ajaxStore(Request $request)
110123
return $this->ajaxRespondError();
111124
}
112125

126+
/**
127+
* Checks permission and tries to update on-the-fly entity. If you want to enable request validation,
128+
* please set your StoreRequest class by using setAjaxStoreRequest() in your EntityCrudController
129+
*
130+
* @param StoreRequest $request
131+
* @return \Illuminate\Http\JsonResponse
132+
*/
133+
public function ajaxUpdate(Request $request)
134+
{
135+
if (!$this->crud->hasAccess('update')) {
136+
return $this->ajaxRespondNoPermission();
137+
}
138+
139+
if ($updateRequest = $this->getAjaxUpdateRequest()) {
140+
if ($errors = $this->ajaxValidationFails($request, $updateRequest->rules())) {
141+
return response()->json($this->ajaxFormatMessage($errors), 422);
142+
}
143+
}
144+
145+
if (parent::updateCrud($request)) {
146+
return $this->ajaxRespondUpdated();
147+
}
148+
149+
return $this->ajaxRespondError();
150+
}
151+
113152
/**
114153
* Validates the request and returns an error bag if it fails
115154
*
@@ -146,6 +185,16 @@ private function ajaxRespondCreated()
146185
return response()->json([], 201);
147186
}
148187

188+
/**
189+
* Responses 204 No Content
190+
*
191+
* @return \Illuminate\Http\JsonResponse
192+
*/
193+
private function ajaxRespondUpdated()
194+
{
195+
return response()->json([], 204);
196+
}
197+
149198
/**
150199
* Responses 422 Error
151200
*

0 commit comments

Comments
 (0)