%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/silvzytp/ccd-ind-code/app/Http/Controllers/Backend/
Upload File :
Create Path :
Current File : //home/silvzytp/ccd-ind-code/app/Http/Controllers/Backend/CallingSubStatusController.php

<?php

namespace App\Http\Controllers\Backend;

use Illuminate\Http\Request;
use App\Models\CallingStatus;
use App\Models\CallingSubStatus;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Yajra\DataTables\Facades\DataTables;

class CallingSubStatusController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        Gate::authorize('app.sub-status.index');
        
        if($request->ajax()){
            $getData = CallingSubStatus::with('callings','callingStatus');
            return DataTables::eloquent($getData)
                ->addIndexColumn()
                ->filter(function ($query) use ($request){
                    if (!empty($request->search_text)) {
                        $query->where('name', 'LIKE',"%{$request->search_text}%");
                    }
                })
                ->addColumn('sub_status', function($row){
                    return $row->callingStatus ? $row->callingStatus->name : 'N/A';
                })
                ->addColumn('status', function($row){
                    return status($row->status);
                })
                ->addColumn('action', function($row){
                    $action = '<div class="d-flex align-items-center justify-content-end">';

                    if (Gate::allows('app.sub-status.edit')) {
                        $action .= '<a class="btn-style btn-style-edit ml-1" href="'.route(routeName().'.sub-status.edit',$row->id).'"><i class="fa fa-edit"></i></a>';
                    }

                    if (Gate::allows('app.sub-status.delete')) {
                        if ($row->callings->isEmpty()) {
                            $action .= '<button class="btn-style btn-style-danger deletable-btn ml-1" data-title="Do you really want to delete this record?" data-id="'.$row->id.'"><i class="fa fa-trash"></i></button>';
                        }else{
                            $action .= '';
                        }
                    }

                    $action .= '</div>';

                    return $action;
                })
                ->rawColumns(['status','action'])
                ->make(true);
        }
        
        // page title
        page_title('Sub-Status');

        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Sub-Status'=>''];
        return view('backend.pages.sub-status.index',['breadcrumb'=>$breadcrumb]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        Gate::authorize('app.sub-status.create');

        // page title
        page_title('New Sub-Status');
        $status = CallingStatus::where('status',1)->get();
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Sub-Status'=>route(routeName().'.sub-status.index'),'Create'=>''];
        return view('backend.pages.sub-status.form',['breadcrumb'=>$breadcrumb,'status'=>$status]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'status'       => 'required|integer',
            'is_published' => 'required|in:1,2'
        ]);

        if ($request->has('sub_status')) {
            foreach ($request->sub_status as $key => $value) {
                CallingSubStatus::create([
                    'user_id'   => Auth::id(),
                    'status_id' => $request->status,
                    'name'      => $value['name'],
                    'status'    => $request->is_published
                ]);
            }
        }

        return redirect()->route(routeName().'.sub-status.index')->with('success',ALERT_SUCCESS);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        Gate::authorize('app.sub-status.edit');

        // page title
        page_title('Edit Sub-Status');
        $callingStatus = CallingSubStatus::findOrFail($id);
        $status = CallingStatus::where('status',1)->get();
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Sub-Status'=>route(routeName().'.sub-status.index'),'Edit'=>''];
        return view('backend.pages.sub-status.form',['breadcrumb'=>$breadcrumb,'callingStatus'=>$callingStatus,'status'=>$status]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'name'         => 'required|string',
            'is_published' => 'required|in:1,2',
            'status'       => 'required|integer'
        ]);

        CallingSubStatus::find($id)->update([
            'user_id'   => Auth::id(),
            'status_id' => $request->status,
            'name'      => $request->name,
            'status'    => $request->is_published
        ]);

        return back()->with('success',ALERT_UPDATE);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        if ($request->ajax()) {
            if (Gate::allows('app.sub-status.delete')) {
                CallingSubStatus::findOrFail($request->data_id)->delete();
                $output = ['status'=>'success','message'=>ALERT_DELETE];
            }else{
                $output = UNAUTHORIZED;
            }

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

Zerion Mini Shell 1.0