FileController.php 4.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<?php

namespace App\Http\Controllers\Webprofile\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Webprofile\CategoriesFile;
use App\Repositories\Webprofile\FileRepository;

class FileController extends Controller
{
    private $repo;

    public function __construct(FileRepository $repo)
    {
        $this->repo = $repo;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $data = $this->repo->get(null, null, ['created_at', 'desc']);
            return $this->repo->datatable($data);
        }

        return view('webprofile.backend.file.index')->withTitle(trans('feature.file'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categoriesFile = CategoriesFile::where('is_active', 1)->orderBy('name', 'asc')->pluck('name', 'id');

        $data = [
            'categoriesFile' => $categoriesFile,
        ];
        
        return view('webprofile.backend.file.create', $data)->withTitle(trans('feature.create_file'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
58 59 60 61 62 63 64 65 66 67 68 69 70 71
        // dd($request);
        $request -> validate([
            'categories_file' => 'required',
            'title' => 'required',
            'file' => 'required|mimes:jpg,jpeg,png,doc,docx,pdf,xlsx,mp3,mp4,mkv,mpeg|max : 5120'
        ], [
            'categories_file.required' => 'Kategori dokumen wajib diisi',
            'title.required' => 'Nama dokumen wajib diisi',
            // 'title.max' => 'Nama dokumen terlalu panjang',
            'file.required' => 'File wajib diupload',
            'file.mimes' => 'File yang diupload harus berupa jpg, jpeg, png, doc, docx, pdf, xlsx, mp3, mp4, mkv, atau mpeg',
            'file.max' => 'Ukuran gambar maksimal 5 MB'
        ]);

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        $this->repo->store($request, 'file');

        return redirect()->route('file.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = $this->repo->findId($id);

        $categoriesFile = CategoriesFile::where('is_active', 1)->orderBy('name', 'asc')->pluck('name', 'id');

        $data = [
            'data' => $data,
            'categoriesFile' => $categoriesFile,
        ];

        return view('webprofile.backend.file.edit', $data)->withTitle(trans('feature.edit_file'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
117 118 119
        $request -> validate([
            'categories_file' => 'required',
            'title' => 'required',
120
            'file' => 'mimes:jpg,jpeg,png,doc,docx,pdf,xlsx,mp3,mp4,mkv,mpeg|max : 5120'
121 122 123 124
        ], [
            'categories_file.required' => 'Kategori dokumen wajib diisi',
            'title.required' => 'Nama dokumen wajib diisi',
            // 'title.max' => 'Nama dokumen terlalu panjang',
125
            // 'file.required' => 'File wajib diupload',
126 127 128 129
            'file.mimes' => 'File yang diupload harus berupa jpg, jpeg, png, doc, docx, pdf, xlsx, mp3, mp4, mkv, atau mpeg',
            'file.max' => 'Ukuran gambar maksimal 5 MB'
        ]);

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        $gallery = $this->repo->findId($id);
        $edit = $this->repo->update($request, $gallery, 'file');

        return redirect()->route('file.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = $this->repo->findId($id);
        $this->repo->destroy($data, 'file');

        return response()->json(['done']);
    }
}