repository = $piece; parent::__construct(); } /** * Display a list of piece. * * @return Response */ public function index(PieceRequest $request) { if ($request->wantsJson()) { return $this->getJson($request); } $this ->theme->prependTitle(trans('piece::piece.names').' :: '); return $this->theme->of('piece::admin.piece.index')->render(); } /** * Display a list of piece. * * @return Response */ public function getJson(PieceRequest $request) { $pageLimit = $request->input('pageLimit'); $pieces = $this->repository ->pushCriteria(app('Litepie\Repository\Criteria\RequestCriteria')) ->setPresenter('\\Payroll\\Piece\\Repositories\\Presenter\\PieceListPresenter') ->scopeQuery(function($query){ return $query->orderBy('id','DESC'); })->paginate($pageLimit); $pieces['recordsTotal'] = $pieces['meta']['pagination']['total']; $pieces['recordsFiltered'] = $pieces['meta']['pagination']['total']; $pieces['request'] = $request->all(); return response()->json($pieces, 200); } /** * Display piece. * * @param Request $request * @param Model $piece * * @return Response */ public function show(PieceRequest $request, Piece $piece) { if (!$piece->exists) { return response()->view('piece::admin.piece.new', compact('piece')); } Form::populate($piece); return response()->view('piece::admin.piece.show', compact('piece')); } /** * Show the form for creating a new piece. * * @param Request $request * * @return Response */ public function create(PieceRequest $request) { $piece = $this->repository->newInstance([]); Form::populate($piece); return response()->view('piece::admin.piece.create', compact('piece')); } /** * Create new piece. * * @param Request $request * * @return Response */ public function store(PieceRequest $request) { try { $attributes = $request->all(); $attributes['user_id'] = user_id('admin.web'); $piece = $this->repository->create($attributes); return response()->json([ 'message' => trans('messages.success.updated', ['Module' => trans('piece::piece.name')]), 'code' => 204, 'redirect' => trans_url('/admin/piece/piece/'.$piece->getRouteKey()) ], 201); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 400, ], 400); } } /** * Show piece for editing. * * @param Request $request * @param Model $piece * * @return Response */ public function edit(PieceRequest $request, Piece $piece) { Form::populate($piece); return response()->view('piece::admin.piece.edit', compact('piece')); } /** * Update the piece. * * @param Request $request * @param Model $piece * * @return Response */ public function update(PieceRequest $request, Piece $piece) { try { $attributes = $request->all(); $piece->update($attributes); return response()->json([ 'message' => trans('messages.success.updated', ['Module' => trans('piece::piece.name')]), 'code' => 204, 'redirect' => trans_url('/admin/piece/piece/'.$piece->getRouteKey()) ], 201); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 400, 'redirect' => trans_url('/admin/piece/piece/'.$piece->getRouteKey()), ], 400); } } /** * Remove the piece. * * @param Model $piece * * @return Response */ public function destroy(PieceRequest $request, Piece $piece) { try { $t = $piece->delete(); return response()->json([ 'message' => trans('messages.success.deleted', ['Module' => trans('piece::piece.name')]), 'code' => 202, 'redirect' => trans_url('/admin/piece/piece/0'), ], 202); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 400, 'redirect' => trans_url('/admin/piece/piece/'.$piece->getRouteKey()), ], 400); } } }