ProposalController.php 7.86 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace App\Http\Controllers\Mahasiswa;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Controllers\GetDataApiController;
8
use App\Models\JadwalKegiatan;
9 10 11 12 13 14 15
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

use App\Models\Proposal;
use App\Models\Kelompok;
use App\Models\Periode;
use App\Models\Jenis;
16
use Carbon\Carbon;
17 18 19 20 21 22 23 24 25 26 27 28

use Session;
use Alert;
use Auth;

class ProposalController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
29
    public function index(Request $request)
30 31 32 33 34
    {
        //
        $title    = 'Proposal Mahasiswa';
        $bio      = auth()->user()->rBiodata;
        $nim      = $bio->noidentitas;
35 36
        $periode  = Periode::query()->get();
        $year     = !is_null($request->year) ? $request->year : now()->year;
37 38 39 40 41 42 43

        $proposal = Proposal::with(['rKelompok', 'rJenis'])
                    ->whereHas('rKelompok', function ($query) use($nim){
                        $query->whereHas('rAnggota', function ($query) use($nim){
                            $query->where('nim', $nim);
                        });
                    })
44
                    ->whereYear('created_at', $year)
45 46 47 48 49 50
                    ->orderBy('kelompok_id')
                    ->get();

        $data = [
            'proposal'  => $proposal,
            'title'     => $title,
51 52
            'periode'   => $periode,
            'year'      => $year,
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        ];

        return view('backend.mahasiswa.proposal.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $title      = 'Upload Proposal Mahasiswa';
        $periode    = Periode::where('status', 1)->first();
        $getDosen   = GetDataApiController::getDosen();
        $jenis      = Jenis::pluck('nama','jenis_id');
        $bio        = auth()->user()->rBiodata;
Farendi Giotivano R.P committed
71
        $proposal   = null;
72 73 74

        $kelompok   = Kelompok::where('created_user', auth()->user()->id)
                              ->where('periode_id', $periode->periode_id)
75
                              ->where('status', '1')
76 77
                              ->first();

78 79 80
        $cekproposal  = Proposal::where('created_user', auth()->user()->id)
                                ->where('periode_id', $periode->periode_id)
                                ->first();
81 82 83 84

        $jadwal   = JadwalKegiatan::where('periode_id', $periode->where('status', 1)->first()->periode_id)
                                  ->where('nama','Upload Proposal Seleksi Internal')->first();

85
        if(($jadwal->tanggal_mulai <= Carbon::now()) && ($jadwal->tanggal_selesai <= Carbon::now())){
86 87 88
            return redirect()->route('mahasiswa.proposal.index')->with('warning', 'Batas Pengajuan telah berakhir!');
        }

89 90 91 92
        if($cekproposal) {
            return redirect()->route('mahasiswa.proposal.index')->with('warning', 'Sudah ada Proposal yang di ajukan!');
        }

93 94 95 96 97 98 99 100 101 102
        if(is_null($kelompok)){

            return redirect()->route('mahasiswa.proposal.index')->with('warning', 'Belum ada kelompok!');
        }

        $data = [
            'title'     => $title,
            'dosen'     => $getDosen['data'],
            'periode'   => $periode,
            'jenis'     => $jenis,
Farendi Giotivano R.P committed
103
            'kelompok'  => $kelompok,
104
            'proposal'  => $proposal,
105
            'jadwal'    => $jadwal
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        ];

        return view('backend.mahasiswa.proposal.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $proposal   = $request->except('_token');

Farendi Giotivano R.P committed
122 123 124 125 126 127 128 129 130
        $this->validate($request,
            [
                'file' => 'required|mimes:pdf|max:5000'
            ], [
                'file.required' => 'Tidak ada file yang di upload',
                'file.mimes' => 'File harus pdf',
                'file.max' => 'File tidak boleh lebih dari 5 mb',
            ]);

131 132 133 134 135 136 137 138 139 140 141 142 143
        $periode    = Periode::where('status', 1)->first();
        $uuid       = Str::uuid();

        $file_nama  = $periode->nama.'_'.$uuid.'.'.$proposal['file']->getClientOriginalExtension();

        Storage::disk('static')->put('simpkm/proposal/'.$periode->nama.'/'.$file_nama, file_get_contents($proposal['file']->getRealPath()));

        $kel = Proposal::create([
            'proposal_id'    => $uuid,
            'kelompok_id'    => $proposal['kode_kelompok'],
            'jenis_id'       => $proposal['jenis'],
            'periode_id'     => $proposal['periode_id'],
            'judul'          => $proposal['judul'],
144
            'status'         => '0',
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
            'upload_dokumen' => $file_nama,
            'date_upload'    => now(),
            'created_user'   => Auth::user()->id
        ]);

        return redirect()->route('mahasiswa.proposal.index')->with('success', 'Proposal Berhasil ditambahkan');
    }

    /**
     * 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)
    {
        //
        $title      = 'Edit Upload Proposal Mahasiswa';
        $periode    = Periode::where('status', 1)->first();
        $getDosen   = GetDataApiController::getDosen();
        $jenis      = Jenis::pluck('nama','jenis_id');

        $proposal = Proposal::with(['rKelompok', 'rJenis'])->find(decrypt($id));

        $data = [
            'title'     => $title,
            'dosen'     => $getDosen['data'],
            'periode'   => $periode,
            'jenis'     => $jenis,
            'proposal'  => $proposal
        ];

        return view('backend.mahasiswa.proposal.create', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $proposal   = $request->except('_token');
Farendi Giotivano R.P committed
202 203 204 205 206 207 208 209
        // dd($proposal);
        $this->validate($request,
        [
            'file' => 'mimes:pdf|max:5000'
        ], [
            'file.mimes' => 'File harus pdf',
            'file.max' => 'File tidak boleh lebih dari 5 mb',
        ]);
210 211 212

        $pro = Proposal::with(['rPeriode'])->find($id);

Farendi Giotivano R.P committed
213
        if(!isset($proposal['file'])){
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
            $file_nama = $pro->upload_dokumen;
        } else {
            // Storage::disk('static')->delete('simpkm/proposal/'.$pro->rPeriode->nama.'/'.$pro->upload_dokumen);

            $file_nama = $pro->rPeriode->nama.'_'.$pro->proposal_id.'.'.$proposal['file']->getClientOriginalExtension();
            Storage::disk('static')->put('simpkm/proposal/'.$pro->rPeriode->nama.'/'.$file_nama, file_get_contents($proposal['file']->getRealPath()));
        }

        $pro->jenis_id          = $proposal['jenis'];
        $pro->judul             = $proposal['judul'];
        $pro->upload_dokumen    = $file_nama;
        $pro->date_upload       = now();
        $pro->updated_at        = now();
        $pro->updated_user       = Auth::user()->id;

        $pro->save();

        return redirect()->route('mahasiswa.proposal.index')->with('success', 'Proposal Berhasil di revisi');
    }

    public function hapus(Request $request)
    {
        $pro = $request->except('_token');

        $proposal = Proposal::query()->find(decrypt($pro['id']));

        Storage::disk('static')->delete('simpkm/proposal/'.$proposal->rPeriode->nama.'/'.$proposal->upload_dokumen);

        $proposal->delete();

        Alert::success('Berhasil dihapus');

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