Viewing File: /home/ubuntu/vedadeals-backend-base/app/Http/Controllers/Admin/AccountController.php

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use App\Helpers\Helper;

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

use App\Models\{ Admin, User, Product, Order, OrderPayment };

use App\Http\Requests\Admin\Account\{ UpdateProfileRequest, ChangePasswordRequest };

use App\Jobs\SendEmailJob;

use Illuminate\Support\Str;

class AccountController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(Request $request)
    {
        $this->middleware('auth:admin');

        $this->skip = $request->skip ?: 0;
       
        $this->take = $request->take ?: (Setting::get('admin_take_count') ?: TAKE_COUNT);

    }

    public function dashboard() {

        $data = new stdClass();

        $data->total_users = User::count();

        $data->total_products = Product::count();

        $data->total_orders = Order::count();

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

        $recent_users = User::orderBy('id' , 'desc')->take($this->take)->get(['id', 'name', 'name', 'email', 'picture', 'created_at']);

        $recent_products = Product::orderBy('id' , 'desc')->take($this->take)->get(['id', 'name', 'file', 'created_at']);

        $recent_orders = Order::with(['user'])->withSum('orderProducts', 'quantity')->orderBy('id' , 'desc')->take($this->take)->get();

        $data->orders_data = Helper::orders_day_graph(10);
        
        return view('admin.dashboard')
              ->with(compact(['data', 'recent_users', 'recent_products', 'recent_orders']))
              ->with('page', 'dashboard');
    }

    /**
     * @method profile()
     *
     * @uses to show the profile page.
     *
     * @created Karthick
     *
     * @updated 
     *
     * @param 
     * 
     * @return to profile page
     *
     */
    public function profile() {

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

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

    /**
     * @method update_profile()
     *
     * @uses to update admin profile details.
     *
     * @created Karthick
     *
     * @updated 
     *
     * @param 
     * 
     * @return to profile page
     *
     */
    public function update_profile(UpdateProfileRequest $request) {

        try {

            DB::beginTransaction();
            
            $admin = Admin::find($request->admin_id);

            $admin->update($request->validated());
  
            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('profile_update_success'));


        } catch(Exception $e) {

            DB::rollback();

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

        }    
    
    }

    /**
     * @method show_change_password()
     *
     * @uses to show the Change Password page.
     *
     * @created Karthick
     *
     * @updated 
     *
     * @param 
     * 
     * @return to Change Password page.
     *
     */
    public function show_change_password() {

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

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

    /**
     * @method change_password()
     *
     * @uses to change the admin password
     *
     * @created Karthick
     *
     * @updated
     *
     * @param 
     *
     * @return to login page, if success
     */

    public function change_password(ChangePasswordRequest $request) {

        try {

            if(Str::contains($request->old_password, ' ') || Str::contains($request->password, ' ') || Str::contains($request->password_confirmation, ' ')) {

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

            DB::begintransaction();

            $admin = Admin::find($request->admin_id);

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

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

                $admin->save();

                DB::commit();

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

                $email_data['subject'] = tr('change_password_success' , Setting::get('site_name'));
    
                $email_data['email']  = $admin->email;
        
                $email_data['name']  = $admin->name;
        
                $email_data['page'] = "emails.admin.reset_password"; // To send change password success email.
                
                SendEmailJob::dispatch($email_data);

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

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

        } catch(Exception $e) {

            DB::rollback();

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

        }    
    
    }
}
Back to Directory File Manager