|
| 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