Viewing File: /home/ubuntu/shop-website-base/app/Http/Controllers/Admin/AdminController.php

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use App\Helpers\Helper, App\Helpers\EnvEditorHelper;

use Carbon\Carbon;

use DB, Hash, Setting, Auth, Validator, Exception, Enveditor;

use App\Models\User,App\Models\Order,App\Models\OrderPayment;

class AdminController extends Controller {
    
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    protected $paginate_count;

    public function __construct() {

        $this->middleware('auth:admin');
        
        $this->paginate_count = Setting::get('admin_take_count', 10);
    }

    /**
     * @method profile()
     *
     * @uses  Used to display the logged in admin details
     *
     * @created Akshata
     *
     * @updated
     *
     * @param 
     *
     * @return view page 
     */

    public function profile() {

        $admin = Auth::guard('admin')->user();

        return view('admin.account.profile')
                ->with('page', 'profile')
                ->with('admin', $admin);
    
    }

    /**
     * @method change_password()
     *
     * @uses  change password in admin details
     *
     * @created Sakthi
     *
     * @updated
     *
     * @param 
     *
     * @return view page 
     */

    public function change_password() {

        $admin = Auth::guard('admin')->user();

        return view('admin.account.password')
                ->with('page', 'profile')
                ->with('admin', $admin);
    
    }

    /**
     * @method profile_save()
     *
     * @uses To update the admin details
     *
     * @created Akshata
     *
     * @updated
     *
     * @param -
     *
     * @return view page 
     */

    public function profile_save(Request $request) {

        try {

            DB::beginTransaction();

            $rules = 
                [
                    'name' => 'max:191',
                    'email' => $request->admin_id ? 'email|max:191|unique:admins,email,'.$request->admin_id : 'email|max:191|unique:admins,email,NULL',
                    'admin_id' => 'required|exists:admins,id',
                    'mobile' => 'required',
                    'picture' => 'mimes:jpeg,jpg,png'
                ];
            
            Helper::custom_validator($request->all(),$rules);
            
            $admin = \App\Models\Admin::find($request->admin_id);

            if(!$admin) {

                Auth::guard('admin')->logout();

                throw new Exception(tr('admin_not_found'), 101);
            }
        
            $admin->name = $request->name ?: $admin->name;

            $admin->email = $request->email ?: $admin->email;

            $admin->mobile = $request->mobile ?: $admin->mobile;

            $admin->about = $request->about ?: $admin->about;

  
            if($request->hasFile('picture') ) {
                
                Helper::storage_delete_file($admin->picture, PROFILE_PATH_ADMIN); 
                
                $admin->picture = Helper::storage_upload_file($request->file('picture'), PROFILE_PATH_ADMIN);
            }
            
            $admin->remember_token = Helper::generate_token();

            $admin->save();

            DB::commit();

            return redirect()->route('admin.profile')->with('flash_success', tr('admin_profile_success'));


        } catch(Exception $e) {

            DB::rollback();

            return redirect()->back()->withInput()->with('flash_error' , $e->getMessage());

        }    
    
    }

    /**
     * @method change_password()
     *
     * @uses To change the admin password
     *
     * @created Akshata
     *
     * @updated
     *
     * @param 
     *
     * @return view page 
     */

    public function save_password(Request $request) {

        try {

            DB::begintransaction();

            $rules = 
            [              
                'password' => 'required|confirmed|min:6',
                'old_password' => 'required',
            ];
            
            Helper::custom_validator($request->all(),$rules);

            $admin = \App\Models\Admin::find(Auth::guard('admin')->user()->id);

            if(!$admin) {

                Auth::guard('admin')->logout();
                              
                throw new Exception(tr('admin_not_found'), 101);

            }

            if(Hash::check($request->old_password,$admin->password)) {

                $admin->password = Hash::make($request->password);

                $admin->save();

                DB::commit();

                Auth::guard('admin')->logout();

                return redirect()->route('admin.login')->with('flash_success', tr('admin_password_change_success'));
                
            } else {

                throw new Exception(tr('admin_password_mismatch'));
            }

        } catch(Exception $e) {

            DB::rollback();

            return redirect()->back()->withInput()->with('flash_error' , $e->getMessage());

        }    
    
    }

    /**
     * @method users_index()
     *
     * @uses Show the application dashboard.
     *
     * @created Arun
     *
     * @updated Arun
     *
     * @param 
     * 
     * @return return view page
     *
     */
    public function index() {

        $dashboard_data = [];

        $dashboard_data['total_users'] = User::count();

        $orders = Order::orderBy('updated_at','desc')->take(10)->get();

        $orders->order_delivered = Order::where('status',ORDER_DELIVERED)->count();

        $orders->order_cancelled = Order::where('status',ORDER_CANCELLED)->count();

        $orders->order_placed = Order::where('status',ORDER_PLACED)->count();

        $data = new \stdClass();

        $data->total_users = User::where('status',APPROVED)->count();

        $data->today_users = User::whereDate('created_at', Carbon::today())->count();

        $data->today_income = OrderPayment::whereDate('created_at', Carbon::today())->sum('total');

        $data->total_revenue = OrderPayment::sum('total');

        $recent_users= User::orderBy('updated_at' , 'desc')->skip(0)->take(6)->get();

        $analytics = last_x_days_revenue(10);
        
        return view('admin.dashboard')
                ->with('page', 'dashboard')
                ->with('orders',$orders)
                ->with('data',$data)
                ->with('analytics',$analytics);


    }


}
Back to Directory File Manager