marlonpd
12/4/2018 - 10:38 AM

products controller

<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\ProductCategory;
use App\Product;
use Auth;
use Image;

class ProductController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }
    
    public function index()
    {
        if($this->check_permission(Auth::user()->permissions ,'products'))
        {
            return view('restricted');
        }

        return view('admin/products');
    }

    public function product_category()
    {
        if($this->check_permission(Auth::user()->permissions ,'product_category'))
        {
            return view('restricted');
        }

        return view('admin/product_category');
    }

    public function product_sub_category()
    {
        if($this->check_permission(Auth::user()->permissions ,'products'))
        {
            return view('restricted');
        }

        return view('admin/product_sub_category');
    }

    public function api_parent_category_get()
    {
        $categories = ProductCategory::where('parent_id' ,'=', 0)->get();

        return json_pretty(['status'    => 'success', 'categories' => $categories]);
    }

    /*public function api_sub_category_get()
    {
        $sub_categories = ProductCategory::where('parent_id' ,'>', 0)->with('parent')->get();

        $query = ProductCategory::query();
        return json_pretty(['status'    => 'success', 'sub_categories' => $sub_categories]);
    }*/

    public function api_sub_category_get(Request $request)
    {   
        $query = ProductCategory::query();
        $lastdate= $request->input('lastdate');
        $remaining = 0;

        if($lastdate == '')
        {
            $query = $query->with('parent')->take(10)
                           ->orderBy('created_at', 'ASC');  
        }
        else
        {
            $query = $query->with('parent')->where('created_at','>' , $lastdate)
                           ->take(10)
                           ->orderBy('created_at', 'ASC');  
        }

        $product_category = $query->where('parent_id' , '>' , 0)->get();
        $lastitem = $product_category->last();

        if($lastitem)
        {
            $remaining = ProductCategory::where('created_at', '>' , $lastitem->created_at)
                                        ->where('parent_id' , '>' , 0)
                                        ->count();
        }


        return json_pretty(['status'         => 'success',
                            'sub_categories' => $product_category ,
                            'remaining'      => $remaining,
       ]);
    }

    public function api_product_category_post(Request $request)
    {
        $id = $request->input('id');
        $name = $request->input('name');
        $parent_id = ($request->input('parent_id') > 0 ) ? ($request->input('parent_id')) : 0 ;
        $description = $request->input('description');
        $publish = $request->input('publish');
        $images_dir =  '/images/product/';

        if($id)
        {
            $product_category = ProductCategory::find($id);
        }
        else
        {
            $product_category = new ProductCategory();

        }

        $product_category->name = $name;
        $product_category->slug = sluggify($name);
        $product_category->parent_id = $parent_id;
        $product_category->description =  str_replace("../images/tinymce","/images/tinymce",$description);
        $product_category->publish = $publish;

        if ($request->hasFile('photo')) 
        {
            $file_name = str_random(10);
            $extension = $request->file('photo')->getClientOriginalExtension();
            $safe_name = $file_name.'.'.$extension;
            $request->file('photo')->move(public_path().$images_dir, $safe_name);
            $product_category->photo = $images_dir.$safe_name;
            $thumb = $images_dir.'thumb/'.$safe_name;
            Image::make( public_path().$images_dir.$safe_name)->resize(262, 175)->save( public_path().$thumb);
            $product_category->thumb = $thumb;
        }

        if($product_category->save())
        {
            $new_product_category = ProductCategory::with('parent')
                                                    ->where('id', '=', $product_category->id) 
                                                    ->first(); 
            return json_pretty(['status' => 'success', 'category' => $new_product_category]);
        }
        else
        {
            return json_pretty(['status' => 'error']);
        }
    }


    public function api_product_category_get(Request $request)
    {   
        $query = ProductCategory::query();
        $lastdate= $request->input('lastdate');
        $remaining = 0;

        if($lastdate == '')
        {
            $query = $query->with('parent')->take(10)
                           ->orderBy('created_at', 'ASC');  
        }
        else
        {
            $query = $query->with('parent')->where('created_at','>' , $lastdate)
                           ->take(10)
                           ->orderBy('created_at', 'ASC');  
        }

        $product_category = $query->where('parent_id' , '=' , 0)->get();
        $lastitem = $product_category->last();

        if($lastitem)
        {
            $remaining = ProductCategory::where('created_at', '>' , $lastitem->created_at)
                                        ->where('parent_id' , '=' , 0)
                                        ->count();
        }


        return json_pretty(['status'    => 'success',
                            'categories' => $product_category ,
                            'remaining' => $remaining,
       ]);
    }


    public function api_product_category_delete_post(Request $request)
    {
        if(is_array($request->input('items')))
        {
            $items = $request->input('items');

            $product_category =  ProductCategory::whereIn('id', $items)->delete(); 

            if($product_category)
            {
                Product::whereIn('category_id', $items)->delete(); 
            }
        
        }
        else
        {

            $id = $request->input('id');

            $product_category = ProductCategory::where('id' , '=', $id)->delete();
            if($product_category)
            {
                Product::where('category_id' , '=' , $id)->delete();
            }

        }
        
        return json_pretty(['status' => 'success']);

    }


    /** Products** */

    public function api_product_post(Request $request)
    {

        $photos = $request->input('photo');
        $counter = $request->input('counter');
        $category_id = $request->input('category_id');
        $product_dir = '/images/product/';


        for($key=0 ; $key < $counter ; $key++)
        {
            $product = new Product();
            $product->category_id = $category_id;
            $title = trim($request->input('title'.$key));
            $product->title =  $title;
            $product->slug =   sluggify($title);
            $product->description =  $request->input('description'.$key);

            $photo = "photo".$key;

            $file_name = str_random(10);
            $extension = $request->file($photo)->getClientOriginalExtension();
            $safe_name = $file_name.'.'.$extension;
            $request->file($photo)->move(public_path().$product_dir, $safe_name);    

            $product->photo = $product_dir.$safe_name;
            $thumb = $product_dir.'thumb/'.$safe_name;
            Image::make( public_path().$product_dir.$safe_name)->fit(250,  250, function ($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize();
            })->save( public_path().$thumb);
            $product->thumb = $thumb;
            $product->publish = 1;

            $product->save();
        }

        $products = Product::where('publish', '=' , 1)
                           ->where('category_id' , '=' , $category_id) 
                           ->get();

        return json_pretty(['status'    => 'success' , 'products' => $products]);


    }

    public function api_product_update_post(Request $request)
    {
        $id = $request->input('id');
        $title = $request->input('title');
        $description = $request->input('description');

        $product = Product::find($id);
        $product->title = $title;
        $product->slug = sluggify($title);
        $product->description = $description;

        if($product->save())
        {

            return json_pretty(['status'    => 'success']);
        }
        else
        {
            return json_pretty(['status'    => 'error']);
        }
        
    }


    public function api_product_delete_post(Request $request)
    {
        if(is_array($request->input('items')))
        {
            $items = $request->input('items');
            foreach ($items as $item) 
            {
              $product =  Product::destroy( $item);
            }
        }
        else
        {
            $product = Product::destroy($request->input());
        }
        

        return json_pretty(['status' => 'success']);

    }

    public function api_products_get($category_id)
    {
        $products = Product::where('publish', '=' , 1)
                           ->where('category_id' , '=' , $category_id) 
                           ->get();

        return json_pretty(['status'    => 'success' , 'products' => $products]);
    }


    public function api_product_get(Request $request)
    {   
        $query = Product::query();
        $lastdate= $request->input('lastdate');
        $remaining = 0;

        if($lastdate == '')
        {
            $query = $query->with('category')->take(10)
                           ->orderBy('id', 'DESC');  
        }
        else
        {
            $query = $query->with('category')->where('id','<' , $lastdate)
                           ->take(10)
                           ->orderBy('id', 'DESC');  
        }

        $products = $query->get();
        $lastitem = $products->last();

        if($lastitem)
        {
            $remaining = Product::where('id', '<' , $lastitem->id)
                                ->count();
        }


        return json_pretty(['status'    => 'success',
                            'products'  => $products ,
                            'remaining' => $remaining,
       ]);
    }

}