%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/silvzytp/dsr_code/app/Http/Controllers/Backend/
Upload File :
Create Path :
Current File : //home/silvzytp/dsr_code/app/Http/Controllers/Backend/AdminController.php

<?php

namespace App\Http\Controllers\Backend;

use DataTables;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Requests\AdminRequest;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Hash;

class AdminController extends Controller
{

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

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

        return view('backend.pages.admin.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',2)->latest('id');

            return DataTables::eloquent($users)
                ->addIndexColumn()
                ->filter(function ($query) use ($request){
                    if (!empty($request->search_text)) {
                        $query->where('role_id',2)
                            ->where('name', 'LIKE',"%{$request->search_text}%")
                            ->orWhere(function($whereQuery) use ($request){
                                $whereQuery->where('role_id',2)
                                    ->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 '
                        <label class="ui-checkbox">
                            <input type="checkbox" name="checked" value="'.$user->id.'">
                            <span class="input-span"></span>
                        </label>
                    ';
                })
                ->addColumn('action', function($user){
                    $action = '';
                    if (Gate::allows('app.agents.show')) {
                        $action .= '<a href="'.route(routeName().'.admins.show', $user->id).'" class="btn-style btn-style-view btn-view ml-1" data-id="'.$user->id.'"><i class="fa fa-eye"></i>
                        </a>';
                    }

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

                    if (Gate::allows('app.agents.destroy')) {
                        $action .= '<button class="btn-style btn-style-danger ml-1 deletable-btn" 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.admins.create');

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

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\AdminRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AdminRequest $request)
    {
        // file upload
        $file = file_store($request->file('profile'),'profile/');

        // admin create
        User::create([
            'role_id'       => 2,
            '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().'.admins.index')->with('success',ALERT_SUCCESS);
    }


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

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

        return view('backend.pages.admin.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.admins.edit');

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

        // page title
        page_title('Edit Admin');
        // breadcrumb
        $breadcrumb = ['Dashboard'=>route('super.dashboard'),'Admins'=>route(routeName().'.admins.index'),'Edit'=>''];

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\AdminRequest  $request
     * @param  \App\Models\User  $id
     * @return \Illuminate\Http\Response
     */
    public function update(AdminRequest $request, $id)
    {
        $admin = User::findOrFail($id);
        // file upload
        $file = file_update($request->file('profile'),'profile/',$admin->avatar);

        // admin create
        $admin->update([
            'role_id'       => 2,
            'name'          => $request->full_name,
            'email'         => $request->email,
            'password'      => Hash::make($request->password),
            'password_text' => $request->password,
            'status'        => $request->status,
            'phone_no'      => $request->phone,
            '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()) {
            $admin = User::find($request->data_id);
            if ($admin == true) {
                file_remove($admin->avatar); // profile image remove from storage
                $admin->delete(); // user remove from database
                $output = response_alert('success',ALERT_DELETE);
            }else{
                $output = response_alert('error',SERVER_ERROR);
            }

            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 (!empty($request->bulk_id)) {
                $userId = explode(',', $request->bulk_id);
                $users = User::whereIn('id', $userId);
                if ($users) {
                    if ($request->operation_type == 'delete') {
                        $users->delete();
                        $output = response_alert('success',ALERT_DELETE);
                    }elseif(in_array($request->operation_type, ['enabled','disabled']) == true){
                        $user_status = $request->operation_type == 'enabled' ? 1 : 2;
                        $users->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');
            }

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

}

Zerion Mini Shell 1.0