123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- <?php
-
- namespace App\Http\Controllers\Admin;
-
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Http\Request;
- use App\Helpers\Helper;
- use Validator;
- use Hashids;
- use Yajra\Datatables\Datatables;
- use Auth;
- use Illuminate\Support\Facades\Route;
- use Illuminate\Support\Facades\File;
- use Illuminate\Support\Facades\Storage;
-
-
- use App\Model\Transportation;
-
- class TransportationController extends Controller
- {
-
-
- private $title = 'Transportation';
- private $route = 'admin.transportation.';
- private $header = 'Transportation';
- private $sub_header = 'Transportation';
-
- private $namespace_controller = 'Admin';
- private $url_prefix = 'admin/transportation/';
-
- public function __construct()
- {
-
-
-
-
-
-
- }
-
-
-
-
-
- public function index()
- {
- Helper::swal();
-
- $data = [
-
- 'title' => $this->title,
- 'route' => $this->route,
- 'header' => $this->header,
- 'sub_header' => $this->sub_header,
- ];
-
- return view($this->route . 'index', $data);
- }
-
-
- public function getData()
- {
- $query = Transportation::query();
- $data_table = Datatables::of($query)
- ->addIndexColumn()
- ->addColumn('aksi', function ($query) {
-
-
- $aksi = '';
- $aksi .= "<a href='" . route($this->route . 'update', ['id_di_routenya' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-primary btn-edit'>Edit</a>";
-
-
-
-
- return $aksi;
- })
- ->addIndexColumn()
- ->rawColumns(['aksi'])
- ->escapeColumns([])
- ->toJson();
- return $data_table;
- }
-
-
-
- public function create()
- {
- $data = [
-
- 'title' => $this->title,
- 'route' => $this->route,
- 'header' => $this->header,
- 'sub_header' => $this->sub_header,
-
- ];
-
- Helper::swal();
-
- return view($this->route . 'create', $data);
- }
-
-
-
- public function create_action(Request $request)
- {
-
- $post = $request->all();
- $file = $request->file('gambar');
-
-
- $rules = [
- 'gambar' => 'required|file|max:1000',
- ];
-
- $alert = [
- 'file' => 'File maksimal 1MB'
- ];
- $validator = Validator::make($post, $rules, $alert);
-
- if ($validator->passes()) {
-
- DB::beginTransaction();
-
- $nama_file = time()."_".$file->getClientOriginalName();
-
- $tujuan_upload = 'gambar';
-
-
- Storage::putFileAs('public/images/transportation/', $file, $nama_file );
-
- $query = new Transportation();
- $query->name = $request->name;
- $query->transportation_identity = $request->transportation_identity;
- $query->transportation_type = $request->transportation_type;
- $query->transportation_photo = $nama_file;
- $query->user_id = Auth::user()->id;
- $query->save();
-
- if ($query) {
- DB::commit();
- $message = 'Berhasil';
- return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
- } else {
- DB::rollback();
- $message = 'Gagal';
- return redirect()->back()->with('error', Helper::parsing_alert($message));
- }
- }
-
- $message = Helper::parsing_alert($validator->errors()->all());
- return redirect()->back()->with('error', $message)->withInput();
- }
-
-
- public function update($id_modelmu)
- {
-
- Helper::swal();
-
-
- $id_modelmu = Hashids::decode($id_modelmu);
- if (!empty($id_modelmu)) {
-
- $cek_data = MasterAlat::where('id', $id_modelmu[0])->first();
-
- if ($cek_data) {
- $data = [
-
- 'title' => $this->title,
- 'route' => $this->route,
- 'header' => $this->header,
- 'sub_header' => $this->sub_header,
- 'data' => $cek_data
- ];
- return view($this->route . 'update', $data);
- }
- $message = 'Id tidak ditemukan atau sudah dihapus';
- return redirect()->back()->with('error', $message);
- }
- $message = 'Id tdak ditemukan';
- return redirect()->back()->with('error', $message);
- }
-
-
-
- public function update_action(Request $request)
- {
- $post = $request->all();
-
- $rules = [];
-
- $alert = [];
- $validator = Validator::make($post, $rules, $alert);
-
- $id_modelmu = Hashids::decode($request['id']);
-
- if (!empty($id_modelmu)) {
-
- $query = MasterAlat::find($id_modelmu[0]);
-
- if ($query) {
-
- if ($validator->passes()) {
-
- DB::beginTransaction();
- $file = $request->file('gambar');
- if($file){
- $nama_file = time()."_".$file->getClientOriginalName();
- $tujuan_upload = 'gambar';
-
- Storage::putFileAs('public/images/', $file, $nama_file );
-
- Storage::delete('public/images/'.$query->gambar);
- $query->gambar = $nama_file;
- }
-
- $query->nama = $request->nama;
- $query->type_alat_id = $request->type_alat_id;
- $query->deskripsi = $request->deskripsi;
- $query->unit_donor_darah_id = Auth::user()->petugas->unit_donor_darah_id;
- $query->save();
-
- if ($query) {
- DB::commit();
- $message = 'Berhasil';
- return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
- } else {
- DB::rollback();
- $message = 'Gagal';
- return redirect()->back()->with('error', Helper::parsing_alert($message));
- }
- }
-
- $message = Helper::parsing_alert($validator->errors()->all());
-
- return redirect()->back()->with('error', $message);
- } else {
- $message = 'Id tdak ditemukan';
- return redirect()->back()->with('error', $message);
- }
- } else {
- $message = 'Id tidak boleh kosong';
- return redirect()->back()->with('error', $message);
- }
- }
-
-
-
- public function delete_action($id_modelmu)
- {
- $id = Hashids::decode($id_modelmu);
-
-
- if (!empty($id)) {
-
- $cek_data = MasterAlat::find($id[0]);
-
- if ($cek_data) {
- DB::beginTransaction();
- try {
-
- Storage::delete('public/images/'.$cek_data->gambar);
- $delete = $cek_data->delete();
- DB::commit();
- $message = 'Sukses';
- $response = [
- 'message' => $message,
- 'status' => true,
- ];
- return response()->json($response);
- } catch (Exception $e) {
- DB::rollback();
- $message = 'Gagal';
- $response = [
- 'message' => $message,
- 'status' => false,
- ];
- return response()->json($response);
- }
- }
- $message = 'Id tidak ditemukan atau sudah dihapus';
- $response = [
- 'message' => $message,
- 'status' => false,
- ];
- return response()->json($response);
- }
- $message = 'Id tdak ditemukan';
- $response = [
- 'message' => $message,
- 'status' => false,
- ];
- return response()->json($response);
- }
-
-
- public function route(){
- $route = Route::namespace($this->namespace_controller)
- ->prefix($this->url_prefix)
- ->name($this->route)
- ->group(function () {
- Route::get('/', ['as' => 'index', 'uses' => 'TransportationController@index']);
- Route::get('/get-data/{udd_id?}', ['as' => 'get_data', 'uses' => 'TransportationController@getData']);
- Route::get('/datatables', ['as' => 'datatables', 'uses' => 'TransportationController@datatables']);
- Route::get('/create', ['as' => 'create', 'uses' => 'TransportationController@create']);
- Route::get('/update/{id_di_routenya}', ['as' => 'update', 'uses' => 'TransportationController@update']);
- Route::get('/delete-action/{id_di_routenya}', ['as' => 'delete_action', 'uses' => 'TransportationController@delete_action']);
-
- Route::post('/create-action', ['as' => 'create_action', 'uses' => 'TransportationController@create_action']);
- Route::post('/update-action', ['as' => 'update_action', 'uses' => 'TransportationController@update_action']);
- });
- return $route;
- }
-
- }
|