Current File : /home/tradevaly/demo.tradevaly.com.bd/app/Http/Controllers/CategoryController.php
<?php

namespace App\Http\Controllers;

use App\Models\ProductCategory;
use Illuminate\Http\Request;
use DB;
use Hash;
use Session;
use Auth;


class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */


    function __construct()
    {
         $this->middleware('permission:product-category-list|product-category-create|product-category-edit|product-category-delete', ['only' => ['index','store']]);
         $this->middleware('permission:product-category-list', ['only' => ['index']]);
         $this->middleware('permission:product-category-create', ['only' => ['create','store']]);
         $this->middleware('permission:product-category-edit', ['only' => ['edit','update']]);
         $this->middleware('permission:product-category-delete', ['only' => ['destroy']]);
    }



    public function index(Request $request)
    {
        

        $data = ProductCategory::select('id', 'category_name', 'first_parent_id', 'second_parent_id', 'third_parent_id', 'status')->get()->toArray();

        //dd($data);
       

        $reArrange = [];

        if ( isset($data) && !empty($data) ) {
            
            foreach ($data as $key => $value) {
                
                $id =  $value['id'];

                $catName =  $value['category_name'];  

                $firstParent =  $value['first_parent_id'];   
                $secondParent =  $value['second_parent_id'];   
                $thirdParent  =  $value['third_parent_id'];

                if( $firstParent == 0){

                    $reArrange['first_step_cat'][$id] = $catName;

                }
                if( $firstParent > 0 && $secondParent == 0 && $thirdParent == 0 ){
                    
                    $reArrange['second_step_cat'][$firstParent][$id]['id'] = $id;
                    
                    $reArrange['second_step_cat'][$firstParent][$id]['category_name'] = $catName;
                    
                    $reArrange['second_step_cat'][$firstParent][$id]['first_parent_id'] = $firstParent;
                    
                    $reArrange['second_step_cat'][$firstParent][$id]['second_parent_id'] = $secondParent;
                    
                    $reArrange['second_step_cat'][$firstParent][$id]['third_parent_id'] = $thirdParent;
                    
                }
                if( $secondParent > 0  && $thirdParent == 0 ){
                    
                    $reArrange['third_step_cat'][$secondParent][$id]['id'] = $id;
                    
                    $reArrange['third_step_cat'][$secondParent][$id]['category_name'] = $catName;
                    
                    $reArrange['third_step_cat'][$secondParent][$id]['first_parent_id']  = $firstParent;
                    
                    $reArrange['third_step_cat'][$secondParent][$id]['second_parent_id'] = $secondParent;
                    
                    $reArrange['third_step_cat'][$secondParent][$id]['third_parent_id'] = $thirdParent;
                }

                if( $thirdParent > 0  ){
                    
                    $reArrange['fourth_step_cat'][$thirdParent][$id]['id'] = $id;
                    
                    $reArrange['fourth_step_cat'][$thirdParent][$id]['category_name'] = $catName;
                    
                    $reArrange['fourth_step_cat'][$thirdParent][$id]['first_parent_id'] = $firstParent;
                    
                    $reArrange['fourth_step_cat'][$thirdParent][$id]['second_parent_id'] = $secondParent;
                    
                    $reArrange['fourth_step_cat'][$thirdParent][$id]['third_parent_id'] = $thirdParent;
                }
            }
    
        
        }



        //$categoryList = ProductCategory::getCategoryList();
        
        $categoryList = ProductCategory::getCategoryWithImageList([]);
        //dd($categoryList);

        $categoryListStatus = ProductCategory::getCategoryStatusList();

        //dd($categoryListStatus);
        
        return view('backend.categories.index',compact('reArrange', 'categoryList', 'categoryListStatus'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = ProductCategory::get();
        return view('backend.categories.create',compact('data'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

            

        $this->validate($request, [
            'category_name' => 'required|unique:product_categories,category_name',
            'icon' => 'image|mimes:jpeg,png,jpg',
        ]);
        

        $input = $request->all();
        $input['created_user_id'] = !empty(Auth::user()->id) ? Auth::user()->id : 0;
        $input['updated_at'] = Null;


        if ( !empty($input['icon']) ) {
            
           $imagePath = $input['icon'];
           $imageName = date('Ymdhis').$imagePath->getClientOriginalName();
          
           $path = $input['icon']->storeAs('product-top-category-icon', $imageName, 'public');
        
           $input['icon'] = $imageName;

           
        
        }
        

        session()->put('first_parent_id', $input['first_parent_id']);
        session()->put('second_parent_id', $input['second_parent_id']);
        session()->put('third_parent_id', $input['third_parent_id']);
        

        $responseStatus = '';
        $responseMessage = '';

        //dd($input);
        if( ProductCategory::create($input) ) {
            $responseStatus = 'success';
            $responseMessage = 'Category added successfully.';

            //dd(Session::get('first_parent_id')) ;

        }else{
            $responseStatus = 'error';
            $responseMessage = 'Something went wrong, please try again.';
       
        }

        

        //$add = ProductCategory::create(['name' => $request->input('name')]);
    
        return redirect()->route('categories.index')
                        ->with($responseStatus, $responseMessage);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function show(ProductCategory $category)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function edit(ProductCategory $category)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ProductCategory $category)
    {
        //dd($request->toArray());
        session()->put('first_parent_id', $category->first_parent_id);
        session()->put('second_parent_id', $category->second_parent_id);
        session()->put('third_parent_id', $category->third_parent_id);

        $this->validate($request, [
             'category_name' => 'required|unique:product_categories,category_name,'.$category->id,

            'icon' => 'mimes:jpeg,png,jpg',
        ]);

        $input = $request->all();
        $input['modified_user_id'] = !empty(Auth::user()->id) ? Auth::user()->id : 0;
        $input['updated_at'] = date("Y-m-d h:i:sa");;

       if ( !empty($input['icon']) ) {
            
           $imagePath = $input['icon'];
           $imageName = date('Ymdhis').$imagePath->getClientOriginalName();
          
           $path = $input['icon']->storeAs('product-top-category-icon', $imageName, 'public');
        
           $input['icon'] = $imageName;

           if( !empty($category->icon) ){


                if( file_exists('storage/app/public/product-top-category-icon/'.$category->icon) ){
                    unlink('storage/app/public/product-top-category-icon/'.$category->icon);
                
                }
           }
        
        }

        $category = ProductCategory::find($category->id);
       
        if( $category->update($input) ) {
            $responseStatus = 'success';
            $responseMessage = 'Category updated successfully.';

        }else{
            $responseStatus = 'error';
            $responseMessage = 'Something went wrong, please try again.';
       
        }

        //$add = ProductCategory::create(['name' => $request->input('name')]);
    
        return redirect()->route('categories.index')
                        ->with($responseStatus, $responseMessage);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function destroy(ProductCategory $category)
    {
        //$dataDetails = $category->toArray();
        session()->put('first_parent_id', $category->first_parent_id);
        session()->put('second_parent_id', $category->second_parent_id);
        session()->put('third_parent_id', $category->third_parent_id);



        // chek if it is parent category
        if( empty($category->first_parent_id) &&  empty($category->second_parent_id)  &&  empty($category->third_parent_id) ){

            ProductCategory::where('first_parent_id', $category->id)->delete();
        }


         // chek if it is second child category
        if( !empty($category->first_parent_id) &&  empty($category->second_parent_id)  &&  empty($category->third_parent_id) ){

            ProductCategory::where('second_parent_id', $category->id)->delete();
        }


         // chek if it is third child category
        if( !empty($category->first_parent_id) &&  !empty($category->second_parent_id)  &&  empty($category->third_parent_id) ){

            ProductCategory::where('third_parent_id', $category->id)->delete();
        }

        
        if( ProductCategory::find($category->id)->delete() ) {

            $responseStatus = 'success';
            $responseMessage = 'Category deleted successfully.';

        }else{
            $responseStatus = 'error';
            $responseMessage = 'Something went wrong, please try again.';
       
        }

        //$add = ProductCategory::create(['name' => $request->input('name')]);
    
        return redirect()->route('categories.index')
                        ->with($responseStatus, $responseMessage);
    }




    public function activeInactive($id)
    {
      
       

        $category = ProductCategory::findOrFail($id);

        session()->put('first_parent_id', $category->first_parent_id);
        session()->put('second_parent_id', $category->second_parent_id);
        session()->put('third_parent_id', $category->third_parent_id);


        $responseMessage = '';

        if($category->status == 0){

            $input['status'] = 1;

            $responseMessage = 'Category active successfully';
       

        }else{

            $input['status'] = 0;

            $responseMessage = 'Category inactive successfully';
        }




        // chek if it is parent category
        if( empty($category->first_parent_id) &&  empty($category->second_parent_id)  &&  empty($category->third_parent_id) ){

            ProductCategory::where('first_parent_id', $id)->update($input);
        }


         // chek if it is second child category
        if( !empty($category->first_parent_id) &&  empty($category->second_parent_id)  &&  empty($category->third_parent_id) ){

            ProductCategory::where('second_parent_id', $id)->update($input);
        }


         // chek if it is third child category
        if( !empty($category->first_parent_id) &&  !empty($category->second_parent_id)  &&  empty($category->third_parent_id) ){

            ProductCategory::where('third_parent_id', $id)->update($input);
        }

       
        if( $category->update($input) ) {

            $responseStatus = 'success';

        }else{

            $responseStatus = 'error';
            $responseMessage = 'Something went wrong, please try again.';
       
        }

        //$add = ProductCategory::create(['name' => $request->input('name')]);
    
        return redirect()->route('categories.index')
                        ->with($responseStatus, $responseMessage);
    }


}