%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/ManagerController.php

<?php

namespace App\Http\Controllers\Backend;

use DataTables;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Hash;
use App\Http\Requests\ManagerRequest;
use App\Http\Requests\ManagerFormRequest;

class ManagerController extends Controller
{
/**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // authorized
        Gate::authorize('app.managers.index');

        // page title
        page_title('Managers');
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Managers'=>''];

        return view('backend.pages.manager.index',['breadcrumb'=>$breadcrumb]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function getData(Request $request){
        if ($request->ajax()) {
            $users = User::with('role')->where('role_id',3)->latest('id');

            return DataTables::eloquent($users)
                ->addIndexColumn()
                ->filter(function ($query) use ($request){
                    $query->where('role_id',3)
                        ->where('name', 'LIKE',"%{$request->search_text}%")
                        ->orWhere(function($whereQuery) use ($request){
                            $whereQuery->where('role_id',3)
                            ->where('phone_no','LIKE',"%{$request->search_text}%");
                        })->orWhere(function($whereQuery) use ($request){
                            $whereQuery->where('role_id',3)
                            ->where('email','LIKE',"%{$request->search_text}%");
                        });
                })
                ->addColumn('profile', function($user){
                    if (file_exists($user->avatar)) {
                        $img = '<img class="avatar-img" src="'.asset($user->avatar).'" >';
                    }else{
                        $img = '<img class="avatar-img" src="'.asset('uploads/images/user.png').'" >';
                    }

                    return $img;
                })
                ->addColumn('role', function($user){
                    $roleName = $user->role ? $user->role->name : '';
                    return '<span class="badge badge-warning">'.$roleName.'</span>';
                })
                ->addColumn('status', function($user){
                    if ($user->role_id == 4) {
                        return '--';
                    }else{
                        $checked = $user->status == 1 ? 'checked' : '';
                        return '<div class="onoffswitch">
                            <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox user-status" data-id="'.$user->id.'" id="status-'.$user->id.'" '.$checked.'>
                            <label class="onoffswitch-label" for="status-'.$user->id.'">
                                <span class="onoffswitch-inner"></span>
                                <span class="onoffswitch-switch"></span>
                            </label>
                        </div>';
                    }
                })
                ->addColumn('bulk_check', function($user){
                    return '
                        <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input" name="checked" value="'.$user->id.'"" id="checked-'.$user->id.'">
                            <label class="custom-control-label" for="checked-'.$user->id.'"></label>
                        </div>
                    ';
                })
                ->addColumn('action', function($user){
                    $action = '';
                    if (Gate::allows('app.managers.show')) {
                        $action .= '<a href="'.route(routeName().'.managers.show', $user->id).'" class="btn-style ml-1 btn-style-view btn-view" data-id="'.$user->id.'"><i class="fa fa-eye"></i>
                        </a>';
                    }

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

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

                    return '
                        <div class="dropdown show text-right">
                            '.$action.'
                        </div>';
                })
                ->rawColumns(['bulk_check','profile','role','status','action'])
                ->make(true);
        }
    }

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

        // page title
        page_title('New Manager');
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Managers'=>route(routeName().'.managers.index'),'Create'=>''];

        return view('backend.pages.manager.form',['breadcrumb'=>$breadcrumb]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\ManagerRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ManagerRequest $request)
    {
        // authorized
        Gate::authorize('app.managers.create');

        // file upload
        $file = file_store($request->file('profile'),'profile/');

        // admin create
        User::create([
            'role_id'       => 3,
            'name'          => $request->full_name,
            'email'         => $request->email,
            'password'      => Hash::make($request->password),
            'password_text' => $request->password,
            'phone_no'      => $request->phone,
            'status'        => $request->status,
            'avatar'        => $file
        ]);

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


    public function show($id){
        // authorized
        Gate::authorize('app.managers.show');

        $admin = User::findOrFail($id);
        // page title
        page_title('Manager Details');
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Managers'=>route(routeName().'.managers.index'),'Details'=>''];

        return view('backend.pages.manager.view',['breadcrumb'=>$breadcrumb,'admin'=>$admin]);

    }

    /**
     * User status
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function status(Request $request){
        if ($request->ajax()) {
            User::findOrFail($request->data_id)->update(['status'=>$request->status]);

            $output = ['status'=>'success','message'=>'Status updated.'];
            return response()->json($output);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\User  $user
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // authorized
        Gate::authorize('app.managers.edit');

        $manager = User::findOrFail($id);

        // page title
        page_title('Edit Manager');
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Managers'=>route(routeName().'.managers.index'),'Edit'=>''];

        return view('backend.pages.manager.form',['breadcrumb'=>$breadcrumb,'manager'=>$manager]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\ManagerRequest  $request
     * @param  \App\Models\User  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ManagerRequest $request, $id)
    {
        // authorized
        Gate::authorize('app.managers.edit');

        $manager = User::findOrFail($id);
        // file upload
        $file = file_update($request->file('profile'),'profile/',$manager->avatar);

        // manager create
        $manager->update([
            'role_id'       => 3,
            'name'          => $request->full_name,
            'email'         => $request->email,
            'password'      => Hash::make($request->password),
            'password_text' => $request->password,
            'phone_no'      => $request->phone,
            'status'        => $request->status,
            'avatar'        => $file
        ]);

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\User  $user
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        if ($request->ajax()) {
            if (Gate::allows('app.managers.destroy')) {
                $manager = User::find($request->data_id);
                if ($manager == true) {
                    file_remove($manager->avatar); // profile image remove from storage
                    $manager->delete(); // user remove from database
                    $output = response_alert('success',ALERT_DELETE);
                }else{
                    $output = response_alert('error',SERVER_ERROR);
                }
            }else{
                $output = UNAUTHORIZED;
            }

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

    /**
     * Remove the users resource from storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function bulkDestroy(Request $request){
        if ($request->ajax()) {
            if (Gate::allows('app.managers.bulk-destroy')) {
                if (!empty($request->bulk_id)) {
                    $userId = explode(',', $request->bulk_id);
                    $manager = User::whereIn('id', $userId);
                    if ($manager) {
                        if ($request->operation_type == 'delete') {
                            $manager->delete();
                            $output = response_alert('success',ALERT_DELETE);
                        }elseif(in_array($request->operation_type, ['enabled','disabled']) == true){
                            $user_status = $request->operation_type == 'enabled' ? 1 : 2;
                            $manager->update(['status'=>$user_status]);
                            $output = response_alert('success',ALERT_SUCCESS);
                        }else{
                            $output = response_alert('error','Operation somthing wrong!');
                        }
                    }else{
                        $output = response_alert('error',SERVER_ERROR);
                    }
                }else{
                    $output = reponse_alert('error','Please user selected try');
                }
            }else{
                $output = UNAUTHORIZED;
            }

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

}

Zerion Mini Shell 1.0