Skip to content

Commit cdfbed7

Browse files
authored
Merge pull request #14 from webfactor/1.3
1.3.0
2 parents 1707911 + abc8fe2 commit cdfbed7

File tree

11 files changed

+274
-228
lines changed

11 files changed

+274
-228
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
2121
### Security
2222
- Nothing
2323

24+
## 1.3.0 - 2018-07-16
25+
26+
### Added
27+
- `addInstantCreateButtonToList()`-method
28+
29+
### Changed
30+
- some refactoring
31+
2432
## 1.2.0 - 2018-05-24
2533

2634
### Changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<a
2+
href="{{ backpack_url($crud->instantCreateButton['name'] . '/ajax/create?' . $crud->instantCreateButton['entity'] . '=' . $entry->id) }}"
3+
type="button"
4+
class="{{ $crud->instantCreateButton['class'] }}"
5+
data-toggle="modal"
6+
data-target="#{{ $crud->instantCreateButton['name'] }}_modal">
7+
{!! $crud->instantCreateButton['content'] !!}
8+
</a>
9+
<div class="modal fade"
10+
id="{{ $crud->instantCreateButton['name'] }}_modal"
11+
tabindex="-1"
12+
role="dialog"
13+
aria-labelledby="{{ $crud->instantCreateButton['name'] }}_modal"
14+
aria-hidden="true">
15+
<div class="modal-dialog" role="document">
16+
<div class="modal-content"></div>
17+
</div>
18+
</div>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<button href="{{ $field['on_the_fly']['create_view'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/create' }}"
2+
type="button"
3+
class="btn btn-primary"
4+
data-toggle="modal"
5+
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal">
6+
{{ trans('backpack::crud.add') }}
7+
</button>
8+
<div class="modal fade"
9+
id="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"
10+
tabindex="-1"
11+
role="dialog"
12+
aria-labelledby="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"
13+
aria-hidden="true">
14+
<div class="modal-dialog" role="document">
15+
<div class="modal-content"></div>
16+
</div>
17+
</div>

resources/views/fields/select2_from_ajax.blade.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,7 @@
3737
</select>
3838

3939
@if (isset($field['on_the_fly']))
40-
<button href="{{ $field['on_the_fly']['create_view'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/create' }}"
41-
type="button"
42-
class="btn btn-primary"
43-
data-toggle="modal"
44-
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal">
45-
{{ trans('backpack::crud.add') }}
46-
</button>
47-
<div class="modal fade"
48-
id="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"
49-
tabindex="-1"
50-
role="dialog"
51-
aria-labelledby="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"
52-
aria-hidden="true">
53-
<div class="modal-dialog" role="document">
54-
<div class="modal-content"></div>
55-
</div>
56-
</div>
40+
@include('webfactor::fields.inc.button-add')
5741
@endif
5842

5943
{{-- HINT --}}

resources/views/fields/select2_from_ajax_multiple.blade.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,7 @@
3030
</select>
3131

3232
@if (isset($field['on_the_fly']))
33-
<button href="{{ $field['on_the_fly']['create_view'] ?? backpack_url($field['on_the_fly']['entity']).'/ajax/create' }}"
34-
type="button"
35-
class="btn btn-primary"
36-
data-toggle="modal"
37-
data-target="#{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal">
38-
{{ trans('backpack::crud.add') }}
39-
</button>
40-
<div class="modal fade"
41-
id="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"
42-
tabindex="-1"
43-
role="dialog"
44-
aria-labelledby="{{ $field['on_the_fly']['entity'] ?? 'ajax_entity' }}_modal"
45-
aria-hidden="true">
46-
<div class="modal-dialog" role="document">
47-
<div class="modal-content"></div>
48-
</div>
49-
</div>
33+
@include('webfactor::fields.inc.button-add')
5034
@endif
5135

5236
{{-- HINT --}}

resources/views/modal/modal_layout.blade.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
<form method="post" id="{{ 'create_'.$entity }}" accept-charset="UTF-8">
33
{{--'files'=>$crud->hasUploadFields('create')])--}}
44
{{ csrf_field() }}
5-
<div class="modal-header">
6-
<button type="button" class="close" data-toggle="modal" data-target="#{{ $entity }}_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>
5+
<div class="modal-header">
6+
<button type="button" class="close" data-toggle="modal" data-target="#{{ $entity }}_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>
1414

15-
<div class="modal-footer">
16-
@yield('footer')
17-
</div>
15+
<div class="modal-footer">
16+
@yield('footer')
17+
</div>
1818
</form>
19-
@stack('crud_fields_scripts')
19+
@stack('crud_fields_scripts')

src/InstantFieldsServiceProvider.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
namespace Webfactor\Laravel\Backpack\InstantFields;
44

55
use Illuminate\Support\ServiceProvider;
6-
use Webfactor\Laravel\Generators\Commands\MakeBackpackCrudController;
7-
use Webfactor\Laravel\Generators\Commands\MakeBackpackCrudModel;
8-
use Webfactor\Laravel\Generators\Commands\MakeBackpackCrudRequest;
9-
use Webfactor\Laravel\Generators\Commands\MakeEntity;
106

117
class InstantFieldsServiceProvider extends ServiceProvider
128
{

src/Traits/CreatesListButton.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Backpack\InstantFields;
4+
5+
trait CreatesListButton
6+
{
7+
public function addInstantCreateButtonToList(
8+
string $foreignAjaxEntity,
9+
string $content,
10+
string $entity = 'entity_id',
11+
string $class = 'btn btn-xs btn-default'
12+
) {
13+
$this->crud->instantCreateButton = [
14+
'name' => $foreignAjaxEntity,
15+
'entity' => $entity,
16+
'class' => $class,
17+
'content' => $content,
18+
];
19+
20+
$this->crud->addButtonFromView('line', $foreignAjaxEntity, 'webfactor::buttons.create');
21+
}
22+
}

src/Traits/HandlesAjaxRequest.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Backpack\InstantFields;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\MessageBag;
7+
8+
trait HandlesAjaxRequest
9+
{
10+
/**
11+
* Handles the incoming ajax requests by default
12+
* @param Request $request
13+
* @param null $create
14+
* @return mixed
15+
*
16+
*/
17+
public function handleAjaxRequest(Request $request, $create = null)
18+
{
19+
if ($create) {
20+
return $this->ajaxCreate();
21+
}
22+
23+
if (strtolower($request->method()) == 'post') {
24+
return $this->ajaxStore($request);
25+
}
26+
27+
return $this->ajaxIndex($request);
28+
}
29+
30+
/**
31+
* Provides the search algorithm for the select2 field. Overwrite it in
32+
* the EntityCrudController if you need some special functionalities
33+
*
34+
* @return mixed
35+
*/
36+
public function ajaxIndex(Request $request)
37+
{
38+
$searchTerm = $request->input('q');
39+
$page = $request->input('page');
40+
41+
$field = $this->crud->getFields(null)[$request->input('field')];
42+
43+
if (isset($field['search_logic']) && is_callable($field['search_logic'])) {
44+
return $field['search_logic']($field['model']::query(), $searchTerm)->paginate(10);
45+
}
46+
47+
return $field['model']::where($field['attribute'], 'LIKE', '%' . $searchTerm . '%')->paginate(10);
48+
}
49+
50+
/**
51+
* Returns the HTML that is used for displaying the on-the-fly modal of the entity
52+
* @return string
53+
*/
54+
public function ajaxCreate()
55+
{
56+
$this->crud->hasAccessOrFail('create');
57+
58+
return \View::make('webfactor::modal.create')
59+
->with('action', 'create')
60+
->with('entity', $this->getAjaxEntity())
61+
->with('crud', $this->crud)
62+
->with('saveAction', $this->getSaveAction())
63+
->with('fields', $this->crud->getCreateFields())
64+
->with('title', trans('backpack::crud.add') . ' ' . $this->crud->entity_name)
65+
->render();
66+
}
67+
68+
/**
69+
* Checks permission and tries to store on-the-fly entity. If you want to enable request validation,
70+
* please set your StoreRequest class by using setAjaxStoreRequest() in your EntityCrudController
71+
*
72+
* @param StoreRequest $request
73+
* @return \Illuminate\Http\JsonResponse
74+
*/
75+
public function ajaxStore(Request $request)
76+
{
77+
if (!$this->crud->hasAccess('create')) {
78+
return $this->ajaxRespondNoPermission();
79+
}
80+
81+
if ($storeRequest = $this->getAjaxStoreRequest()) {
82+
if ($errors = $this->ajaxValidationFails($request, $storeRequest->rules())) {
83+
return response()->json($this->ajaxFormatMessage($errors), 422);
84+
}
85+
}
86+
87+
if (parent::storeCrud($request)) {
88+
return $this->ajaxRespondCreated();
89+
}
90+
91+
return $this->ajaxRespondError();
92+
}
93+
94+
/**
95+
* Validates the request and returns an error bag if it fails
96+
*
97+
* @return mixed
98+
*/
99+
public function ajaxValidationFails(Request $request, array $rules)
100+
{
101+
$validator = \Validator::make($request->all(), $rules);
102+
103+
if ($validator->fails()) {
104+
return $validator->errors();
105+
}
106+
107+
return false;
108+
}
109+
110+
/**
111+
* Responses 403 No Permission
112+
*
113+
* @return \Illuminate\Http\JsonResponse
114+
*/
115+
private function ajaxRespondNoPermission()
116+
{
117+
return response()->json($this->ajaxFormatMessage(trans('backpack::base.unauthorized')), 403);
118+
}
119+
120+
/**
121+
* Responses 201 Created
122+
*
123+
* @return \Illuminate\Http\JsonResponse
124+
*/
125+
private function ajaxRespondCreated()
126+
{
127+
return response()->json([], 201);
128+
}
129+
130+
/**
131+
* Responses 422 Error
132+
*
133+
* @return \Illuminate\Http\JsonResponse
134+
*/
135+
private function ajaxRespondError()
136+
{
137+
return response()->json($this->ajaxFormatMessage(trans('backpack::base.error_saving')), 422);
138+
}
139+
140+
/**
141+
* Formats the message for the notification
142+
*
143+
* @return string
144+
*/
145+
private function ajaxFormatMessage($message)
146+
{
147+
if ($message instanceof MessageBag) {
148+
$validationErrors = '<ul>';
149+
150+
foreach ($message->all() as $validationError) {
151+
$validationErrors .= '<li>' . $validationError . '</li>';
152+
}
153+
154+
$validationErrors .= '</ul>';
155+
156+
return $validationErrors;
157+
}
158+
159+
return $message;
160+
}
161+
}

0 commit comments

Comments
 (0)