@@ -24,6 +24,10 @@ public function handleAjaxRequest(Request $request, $mode = null)
2424 return $ this ->ajaxEdit ();
2525 }
2626
27+ if ($ mode == 'delete ' ) {
28+ return $ this ->ajaxDelete ();
29+ }
30+
2731 if (strtolower ($ request ->method ()) == 'put ' ) {
2832 return $ this ->ajaxStore ($ request );
2933 }
@@ -32,6 +36,10 @@ public function handleAjaxRequest(Request $request, $mode = null)
3236 return $ this ->ajaxUpdate ($ request );
3337 }
3438
39+ if (strtolower ($ request ->method ()) == 'delete ' ) {
40+ return $ this ->ajaxDestroy ($ request ->input ('id ' ));
41+ }
42+
3543 return $ this ->ajaxIndex ($ request );
3644 }
3745
@@ -89,14 +97,33 @@ public function ajaxEdit()
8997 ->with ('id ' , request ()->input ('id ' ))
9098 ->with ('entity ' , $ this ->getAjaxEntity ())
9199 ->with ('crud ' , $ this ->crud )
92- ->with ('saveAction ' , $ this ->getSaveAction ())
93100 ->with ('fields ' , $ this ->crud ->getUpdateFields (request ()->input ('id ' )))
94101 ->with ('title ' , trans ('backpack::crud.add ' ) . ' ' . $ this ->crud ->entity_name )
95102 ->with ('field_name ' , request ()->input ('field_name ' ))
96103 ->with ('attribute ' , request ()->input ('attribute ' ))
97104 ->render ();
98105 }
99106
107+ /**
108+ * Returns the HTML that is used for displaying the on-the-fly modal of the deleting an entity
109+ * @return string
110+ */
111+ public function ajaxDelete ()
112+ {
113+ $ this ->crud ->hasAccessOrFail ('delete ' );
114+
115+ return \View::make ('webfactor::modal.delete ' )
116+ ->with ('action ' , 'delete ' )
117+ ->with ('id ' , request ()->input ('id ' ))
118+ ->with ('entry ' , $ this ->crud ->model ::find (request ()->input ('id ' )))
119+ ->with ('entity ' , $ this ->getAjaxEntity ())
120+ ->with ('crud ' , $ this ->crud )
121+ ->with ('title ' , trans ('backpack::crud.add ' ) . ' ' . $ this ->crud ->entity_name )
122+ ->with ('field_name ' , request ()->input ('field_name ' ))
123+ ->with ('attribute ' , request ()->input ('attribute ' ))
124+ ->render ();
125+ }
126+
100127 /**
101128 * Checks permission and tries to store on-the-fly entity. If you want to enable request validation,
102129 * please set your StoreRequest class by using setAjaxStoreRequest() in your EntityCrudController
@@ -149,6 +176,27 @@ public function ajaxUpdate(Request $request)
149176 return $ this ->ajaxRespondError ();
150177 }
151178
179+ /**
180+ * Checks permission and tries to delete on-the-fly entity.
181+ *
182+ * @param int $id
183+ * @return \Illuminate\Http\JsonResponse
184+ */
185+ public function ajaxDestroy (int $ id )
186+ {
187+ if (!$ this ->crud ->hasAccess ('delete ' )) {
188+ return $ this ->ajaxRespondNoPermission ();
189+ }
190+
191+ try {
192+ $ this ->crud ->delete ($ id );
193+ } catch (\Exception $ exception ) {
194+ return response ()->json ($ this ->ajaxFormatMessage ($ exception ), 422 );
195+ }
196+
197+ return $ this ->ajaxRespondDeleted ();
198+ }
199+
152200 /**
153201 * Validates the request and returns an error bag if it fails
154202 *
@@ -195,6 +243,16 @@ private function ajaxRespondUpdated()
195243 return response ()->json ([], 204 );
196244 }
197245
246+ /**
247+ * Responses 204 No Content
248+ *
249+ * @return \Illuminate\Http\JsonResponse
250+ */
251+ private function ajaxRespondDeleted ()
252+ {
253+ return response ()->json ([], 204 );
254+ }
255+
198256 /**
199257 * Responses 422 Error
200258 *
@@ -224,6 +282,10 @@ private function ajaxFormatMessage($message)
224282 return $ validationErrors ;
225283 }
226284
285+ if ($ message instanceof \Exception) {
286+ return $ message ->getMessage ();
287+ }
288+
227289 return $ message ;
228290 }
229291}
0 commit comments