Anons79 Mini Shell

Directory : /home/aplikasiposinfo/.trash/app.3/Http/Controllers/
Upload File :
Current File : /home/aplikasiposinfo/.trash/app.3/Http/Controllers/KegiatanController.php

<?php

namespace App\Http\Controllers;

use App\Models\Program;
use App\Models\IndukOpd;
use App\Models\Kegiatan;
use Illuminate\Http\Request;
use App\Models\detailKegiatan;
use App\Models\IndikatorKegiatan;
use App\Models\SasaranKegiatan;
use Illuminate\Support\Facades\Auth;
use Yajra\DataTables\Facades\DataTables;

class KegiatanController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    protected $routeName = 'kegiatan';
    protected $viewName = 'kegiatan';
    protected $title = 'Kegiatan';

    public function index()
    {
        //
        $route = $this->routeName;
        $title = $this->title;
        $induk_opd_arr = IndukOpd::pluck('nama', 'id');
        if(Auth::user()->roles->first()->name !== "Admin"){
            $induk_opd_arr = IndukOpd::where('id', Auth::user()->induk_opd_id)->pluck('nama', 'id');
        }
        $sasaranKegiatan = SasaranKegiatan::all();
        $indikatorKegiatan = IndikatorKegiatan::all();
        return view('kegiatan.index',compact('route','title', 'induk_opd_arr', 'sasaranKegiatan', 'indikatorKegiatan'));
    }

    public function datatable(Request $request){
        $datas = Kegiatan::where('program_id', $request->program_id)->whereYear('created_at', $request->tahun)->get();

        $datatables = DataTables::of($datas)->
        addIndexColumn()->
        addColumn('kode', function($data){
            return $data->full_kode;
        })->addColumn('nama', function($data){
            $route = 'kegiatan';
            return view('layouts.includes.program-nama',compact('data','route'));
        })->addColumn('sasaran', function($data){
            $route = 'sasaranKegiatan';
            return view('layouts.includes.program-sasaran',compact('data','route'));
        })->addColumn('indikator', function($data){
            $route = 'indikatorKegiatan';
            return view('layouts.includes.program-indikator',compact('data','route'));
        });
        return $datatables->make(true);
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        try {
            $Kegiatan = Kegiatan::create($request->all());

            return redirect(route($this->routeName.'.index'))->with(['success'=>'Berhasil Menambah Data Kegiatan : '.$Kegiatan->id]);
        } catch (\Throwable $th) {
            //throw $th;
            dd($th->getMessage());
        }
    }

    /**
     * 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)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $detailKegiatan = detailKegiatan::where("kegiatan_id", $id)->first();
        $Kegiatan = Kegiatan::find($id);
        try {
            $Kegiatan->update([
                'uraian' => $request->uraian,
                'kode' => $request->kode
            ]);
            if($detailKegiatan){
                $detailKegiatan->update([
                    'nama_kegiatan' => $request->uraian,
                ]);
            }
            return redirect(route($this->routeName.'.index'))->with(['success'=>'Berhasil mengubah Data Kegiatan : '.$Kegiatan->id]);
            //code...
        } catch (\Throwable $th) {
            //throw $th;
            dd($th->getMessage());
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $kegiatan = Kegiatan::find($id);
        $detailKegiatan = detailKegiatan::where('kegiatan_id', $id)->first();

        try {
            if($detailKegiatan){
                return redirect(route($this->routeName.'.index'))->with(['success'=>'Kegiatan Masih Memiliki data rincian']);
            }
            $kegiatan->delete();

            return redirect(route($this->routeName.'.index'))->with(['success'=>'Berhasil menghapus Data Kegiatan : '.$kegiatan->id]);
        } catch (\Throwable $th) {
            //throw $th;
            dd($th->getMessage());
        }
    }
    public function apiProgram(Request $request, $id){
        $program = Program::where('induk_opd_id', $id)->whereYear('created_at', $request->tahun)->get();

        return response()->json([
            'status' => 'success',
            'data' => $program,
        ]);
    }
    public function apiEdit(Request $request, $id){
        $program = Kegiatan::find($id);

        return response()->json([
            'status' => 'success',
            'data' => $program,
        ]);
    }
}

Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]