mBannerController.php 6.71 KB
<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;
use App\Models\Banner;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;
use App\Helpers\Gambar;
use Illuminate\Support\Carbon;
use OjiSatriani\Fungsi;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\File\File;

class mBannerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('backend.banner.index');
    }

    public function data(Request $request)
    {
    if ($request->ajax()) {

        $banner  = Banner::orderBy('nama','desc');

        return Datatables::of($banner)

        ->addIndexColumn()
        ->addColumn('lampiran', function($banner){
            return '<a href="'. $banner->file_url . '">Download</a>';
        })

        ->addColumn(
            'action',
                        '<center>
                          <a class="edit ubah" data-toggle="tooltip" data-placement="top" title="Edit" banner-id="{{ $id }}">
                              <i class="fa fa-pencil text-warning"></i>
                          </a>&nbsp &nbsp
                          <a class="delete hidden-xs hidden-sm hapus" data-toggle="tooltip" data-placement="top" title="Delete" banner-id="{{ $id }}">
                              <i class="fa fa-trash text-danger"></i>
                          </a>
                        </center>'
        )

        ->rawColumns(['action', 'lampiran'])->make(true);

    } else {
        exit("Not an AJAX request -_-");
    }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view ('backend.banner.tambah');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'nama'		    => 'required',
            'tipe'      => 'required',
            // 'file'		=> 'required',
            'keterangan'    => 'required'
          ]);

        if ($validator->fails()) {
            $respon = array('status'=>false, 'pesan' => $validator->messages());
        } else {
            $path       = NULL;
            $namafile   = NULL;

            if($request->hasFile('lampiran')){
                // $path	= '/app/Models/Banner/'.date('Y').'/'.date('m').'/'.date('d').'/';
                // $destinationPath	= storage_path($path);
                 $file = Storage::putFile('banner/'.date('Y').'/'.date('m').'/'.date('d'),$request->file('lampiran'));
                // $namafile	        = with(new Fungsi)->namaBaru($request->file('lampiran')->getClientOriginalName(), Str::slug($request->nama.'-'.Carbon::now()));
                // $request->file('lampiran')->move($destinationPath, $namafile)
                // $request->file('lampiran');
            }
            $request->request->add([
                'file'  =>  [
                                'disk'      => config('filesystems.default'),
                                'target'    => $file,
                            ],
            ]);
            //  akhir cek berkas
            if (Banner::create($request->all())) {
                $respon = array('status'=>true, 'pesan' => ['msg' => 'Data berhasil disimpan']);
            } else {
                $respon = array('status'=>false, 'pesan' => ['msg' => 'Data gagal disimpan']);
            }
        }
        return response()->json($respon);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $banner = Banner::findOrFail($id);
        return view ('backend.banner.index', compact('banner'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $banner = Banner::find($id);
        return view('backend.banner.ubah', compact('banner'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'nama'         => 'required',
            'file'         => 'required',
            'keterangan'   => 'required',
        ]);
        if ($validator->fails()) {
            $respon = array('status'=>false, 'pesan' => $validator->messages());
        } else {
            $banner = Banner::find($id);
            // if($request->hasFile('file_foto')){
                // try {
                    // if {
                    Gambar::hapus($banner);
                    $file = Storage::putFile('banner/'.date('Y').'/'.date('m').'/'.date('d'),$request->file('file_foto'));
                    $request->request->add([
                        'file'  =>  [
                                        'disk'      => config('filesystems.default'),
                                        'target'    => $file,
                                    ],
                    ]);
                // } catch (\Throwable $th) {
                    //throw $th;
                // }
            }
            if (Banner::find($id)->update($request->all())) {
                $respon = array('status'=>true, 'pesan' => ['msg' => 'Data berhasil diubah']);
            } else {
                $respon = array('status'=>false, 'pesan' => ['msg' => 'Data gagal diubah']);
            }

        return response()->json($respon);
    }

    public function hapus($id)
    {
        $banner = Banner::find($id);
//        return view('backend.banner.hapus', ['data' => $banner]);
        return view('backend.banner.hapus', compact('banner'));

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $banner = Banner::find($id);
        if ($banner->delete()) {
            $respon = array('status'=>true, 'pesan' => ['msg' => 'Data berhasil dihapus']);
        } else {
            $respon = array('status'=>false, 'pesan' => ['msg' => 'Data gagal dihapus']);
        }
        return response()->json($respon);
    }

    public function download($id)
    {
        $banner = Banner::find($id);
         return response()->download($banner->file_download);
    }

}