AdminProductController.php 7.16 KB
Newer Older
Angel MAS committed
1 2 3 4 5 6 7 8 9 10 11
<?php

namespace Onestartup\Product\Controller;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Yajra\Datatables\Datatables;
use Onestartup\Product\Model\ProductCategory as Category;
use Onestartup\Product\Model\ProductImage as Gallery;
use Onestartup\Product\Model\Product;
Angel MAS committed
12
use Onestartup\Product\Model\Variable;
Angel MAS committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

class AdminProductController extends Controller
{
    public function index()
    {
        return view('product::product.index');
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        $categories = Category::pluck('name', 'id');
Angel MAS committed
28
        $variable = Variable::first();
Angel MAS committed
29 30

        return view('product::product.create')
Angel MAS committed
31 32
            ->with('categories', $categories)
            ->with('variable', $variable);
Angel MAS committed
33 34 35 36 37 38 39 40 41 42 43
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        
        $product = new Product($request->all());
Angel MAS committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57

        if (isset($request->cover)) {
            
            $file = $request->file('cover');
            $nombre = $file->getClientOriginalName();
            $nombre_file = str_replace(' ', '_', $nombre);
            $ubicacion_donde_guarda ='products/cover/'.$nombre_file;
            \Storage::disk('local')->put($ubicacion_donde_guarda,  \File::get($file));
            $product->cover = $ubicacion_donde_guarda;
        } 


        \Auth::user()->products()->save($product);
        
Angel MAS committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71

        return redirect()
                ->route('admin.product.edit', $product->id)
                ->with('message_success', "Producto guardado correctamente, continua agregando las imagenes correspondientes");
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit($id)
    {
        $product = Product::find($id);
        $categories = Category::pluck('name', 'id');
Angel MAS committed
72
        $variable = Variable::first();
Angel MAS committed
73 74 75

        return view('product::product.edit')
            ->with('categories', $categories)
Angel MAS committed
76 77
            ->with('product', $product)
            ->with('variable', $variable);
Angel MAS committed
78 79 80 81 82 83 84 85 86 87 88
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request, $id)
    {
        $product = Product::find($id);
        $product->fill($request->all());
Angel MAS committed
89 90 91 92 93 94 95 96 97 98 99 100

        if (isset($request->cover)) {
            
            $file = $request->file('cover');
            $nombre = $file->getClientOriginalName();
            $nombre_file = str_replace(' ', '_', $nombre);
            $ubicacion_donde_guarda ='products/cover/'.$nombre_file;
            \Storage::disk('local')->put($ubicacion_donde_guarda,  \File::get($file));
            $product->cover = $ubicacion_donde_guarda;
        } 


Angel MAS committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        $product->save();

        return redirect()
            ->back()
            ->with('message_info', 'Información actualizada correctamente');
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
        $product = Product::find($id);
        $product->delete();

        return redirect()
            ->back()
            ->with('message_danger', 'Producto eliminada correctamente');
    }

    public function getProducts()
    {

        $products = Product::select([
            'id',
            'name',
            'slug', 
            'category_id', 
            'created_at', 
            'description', 
            'features', 
            'specifications'
        ]);

        return Datatables::of($products)
            ->addColumn('main', function ($product) {
                return 
                        "<p class='mb0'>Nombre: <b>$product->name</b></p>".
                        "<p class='mb0'>Slug: <b>$product->slug</b></p>".
                        "<p class='mb0'>Categoria: <b>".$product->category->name."</b></p>".
                        "<p class='mb0'>Fecha creación: <b>".$product->created_at->format('d/m/Y')."</b></p>";
            })
            ->addColumn('form', function ($product) {
                return "<form method='POST' action='".route('admin.product.destroy',$product->id)."'>".
                "<input name='_method' type='hidden' value='DELETE' class='has-value'>".
                csrf_field() .
                "<button class='btn btn-danger btn-xs button-mb' onclick='return confirm();' type='submit'>".
                "<i class='fas fa-trash-alt icon-special-size'></i>Eliminar</button>".
                "</form>".
                "<a href='".route('show.product', $product->slug)."' class='btn btn-xs info button-mb' target='new'>".
                "<i class='fas fa-eye icon-special-size'></i>Ver</a>".
                "<br><a href='".route('admin.product.edit', $product->id)."' class='btn btn-xs accent mb0'>".
                "<i class='fas fa-edit icon-special-size'></i>Editar</a>";
            })
            ->rawColumns(['main', 'description', 'features', 'specifications', 'form'])
            ->make();
        
    }

    public function storeGallery(Request $request, $product_id)
    {
        $product = Product::find($product_id);

        $asset = '/storage/product/'.$product->id.'/gallery/';
        $path = public_path().$asset;
        $files = $request->file('file');

        foreach($files as $file){
            $fileName = $file->getClientOriginalName();
            $nombre_file = str_replace(' ', '_', $fileName);
            $file->move($path, $nombre_file);
            $gallery = new Gallery([
174
                'path'=>$asset.$nombre_file
Angel MAS committed
175 176 177 178 179 180 181 182 183 184 185 186
            ]);

            $product->images()->save($gallery);
        }


        return $product;
    }

    public function deleteImage($id)
    {
        $gallery = Gallery::find($id);
Angel MAS committed
187 188 189 190 191

        if (file_exists(public_path().$gallery->path)) {
            unlink(public_path().$gallery->path);
        }
        
Angel MAS committed
192 193 194 195 196 197 198 199
        $gallery->delete();

        return redirect()
            ->back()
            ->with('message_danger', 'Imagen eliminada correctamente');

    }

Angel MAS committed
200 201 202
    public function deleteCover($id)
    {
        $product = Product::find($id);
Angel MAS committed
203 204 205 206 207
        
        if (file_exists(public_path().'/storage/'.$product->cover)) {
            unlink(public_path().'/storage/'.$product->cover);
        }
        
Angel MAS committed
208 209 210 211 212 213 214 215 216 217 218 219
        $product->cover = null;
        $product->save();

        
        //$gallery->delete();

        return redirect()
            ->back()
            ->with('message_danger', 'Imagen eliminada correctamente');

    }

Angel MAS committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    public function showVars()
    {

        $variable = Variable::first();

        if ($variable == null) {
            $variable = new Variable();
        }

        return view('product::variable.edit')
            ->with('variable', $variable);
    }

    public function postVars(Request $request)
    {

        $variable = Variable::first();

        if ($variable == null) {
            $variable = new Variable();
        }

        $variable->fill($request->all());

        $variable->save();

        return redirect()
            ->back()
            ->with('message_success', 'Información actualizada');
    }

Angel MAS committed
251
}