repository = $material; $this->repository ->pushCriteria(\Litepie\Repository\Criteria\RequestCriteria::class) ->pushCriteria(\Shopping\Material\Repositories\Criteria\MaterialResourceCriteria::class); } /** * Display a list of material. * * @return Response */ public function index(MaterialRequest $request) { $view = $this->response->theme->listView(); if ($this->response->typeIs('json')) { $function = camel_case('get-' . $view); return $this->repository ->setPresenter(\Shopping\Material\Repositories\Presenter\MaterialPresenter::class) ->$function(); } $materials = $this->repository->paginate(); return $this->response->title(trans('material::material.names')) ->view('material::material.index', true) ->data(compact('materials')) ->output(); } /** * Display material. * * @param Request $request * @param Model $material * * @return Response */ public function show(MaterialRequest $request, Material $material) { if ($material->exists) { $view = 'material::material.show'; } else { $view = 'material::material.new'; } return $this->response->title(trans('app.view') . ' ' . trans('material::material.name')) ->data(compact('material')) ->view($view, true) ->output(); } /** * Show the form for creating a new material. * * @param Request $request * * @return Response */ public function create(MaterialRequest $request) { $material = $this->repository->newInstance([]); return $this->response->title(trans('app.new') . ' ' . trans('material::material.name')) ->view('material::material.create', true) ->data(compact('material')) ->output(); } /** * Create new material. * * @param Request $request * * @return Response */ public function store(MaterialRequest $request) { try { $attributes = $request->all(); $attributes['user_id'] = user_id(); $attributes['user_type'] = user_type(); $material = $this->repository->create($attributes); return $this->response->message(trans('messages.success.created', ['Module' => trans('material::material.name')])) ->code(204) ->status('success') ->url(guard_url('material/material/' . $material->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('/material/material')) ->redirect(); } } /** * Show material for editing. * * @param Request $request * @param Model $material * * @return Response */ public function edit(MaterialRequest $request, Material $material) { return $this->response->title(trans('app.edit') . ' ' . trans('material::material.name')) ->view('material::material.edit', true) ->data(compact('material')) ->output(); } /** * Update the material. * * @param Request $request * @param Model $material * * @return Response */ public function update(MaterialRequest $request, Material $material) { try { $attributes = $request->all(); $material->update($attributes); return $this->response->message(trans('messages.success.updated', ['Module' => trans('material::material.name')])) ->code(204) ->status('success') ->url(guard_url('material/material/' . $material->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('material/material/' . $material->getRouteKey())) ->redirect(); } } /** * Remove the material. * * @param Model $material * * @return Response */ public function destroy(MaterialRequest $request, Material $material) { try { $material->delete(); return $this->response->message(trans('messages.success.deleted', ['Module' => trans('material::material.name')])) ->code(202) ->status('success') ->url(guard_url('material/material/0')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('material/material/' . $material->getRouteKey())) ->redirect(); } } /** * Remove multiple material. * * @param Model $material * * @return Response */ public function delete(MaterialRequest $request, $type) { try { $ids = hashids_decode($request->input('ids')); if ($type == 'purge') { $this->repository->purge($ids); } else { $this->repository->delete($ids); } return $this->response->message(trans('messages.success.deleted', ['Module' => trans('material::material.name')])) ->status("success") ->code(202) ->url(guard_url('material/material')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->status("error") ->code(400) ->url(guard_url('/material/material')) ->redirect(); } } /** * Restore deleted materials. * * @param Model $material * * @return Response */ public function restore(MaterialRequest $request) { try { $ids = hashids_decode($request->input('ids')); $this->repository->restore($ids); return $this->response->message(trans('messages.success.restore', ['Module' => trans('material::material.name')])) ->status("success") ->code(202) ->url(guard_url('/material/material')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->status("error") ->code(400) ->url(guard_url('/material/material/')) ->redirect(); } } }