192 lines
No EOL
5.4 KiB
PHP
Executable file
192 lines
No EOL
5.4 KiB
PHP
Executable file
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Product;
|
|
use App\Models\ProductImage;
|
|
use App\Repositories\ProductRepository;
|
|
use Input;
|
|
use Validator;
|
|
|
|
|
|
|
|
class ProductController extends Controller
|
|
{
|
|
protected $productRepo;
|
|
|
|
public function __construct(ProductRepository $productRepo)
|
|
{
|
|
$this->middleware('admin');
|
|
$this->productRepo = $productRepo;
|
|
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$data = [
|
|
'values' => Product::orderBy('pos', 'DESC')->orderBy('id', 'DESC')->get(),
|
|
];
|
|
return view('admin.product.index', $data);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
if($id == "new"){
|
|
$model = new Product();
|
|
$model->active = true;
|
|
}else{
|
|
$model = Product::findOrFail($id);
|
|
}
|
|
$data = [
|
|
'product' => $model,
|
|
];
|
|
return view('admin.product.edit', $data);
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
|
|
$data = Input::all();
|
|
|
|
$rules = array(
|
|
'name' => 'required',
|
|
);
|
|
$validator = Validator::make(Input::all(), $rules);
|
|
|
|
if($data['id'] == "new"){
|
|
$model = new Product();
|
|
}else{
|
|
$model = Product::findOrFail($data['id']);
|
|
}
|
|
$data = [
|
|
'product' => $model,
|
|
];
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return view('admin.product.edit', $data)->withErrors($validator);
|
|
|
|
} else {
|
|
$product = $this->productRepo->update(Input::all());
|
|
\Session()->flash('alert-save', true);
|
|
return redirect(route('admin_product_edit', [$product->id]));
|
|
}
|
|
|
|
\Session()->flash('alert-save', '1');
|
|
return redirect(route('admin_product_show'));
|
|
|
|
|
|
}
|
|
|
|
public function copy($id){
|
|
$model = Product::findOrFail($id);
|
|
|
|
$product = $this->productRepo->copy($model);
|
|
|
|
|
|
|
|
\Session()->flash('alert-success', 'Eintrag kopiert');
|
|
return redirect(route('admin_product_show'));
|
|
}
|
|
|
|
public function delete($id){
|
|
$model = Product::findOrFail($id);
|
|
$model->delete();
|
|
\Session()->flash('alert-success', 'Eintrag gelöscht');
|
|
return redirect(route('admin_product_show'));
|
|
}
|
|
|
|
|
|
// Upload FILE -----------------------------------------------------------------------------------------------------------------------
|
|
|
|
public function imageUpload(){
|
|
|
|
$product_id = Input::get('product_id');
|
|
$product = Product::findOrFail($product_id);
|
|
|
|
try {
|
|
$image = \App\Services\Slim::getImages('images')[0];
|
|
|
|
if ( isset($image['output']['data']) )
|
|
{
|
|
|
|
// Base64 of the image
|
|
$data = $image['output']['data'];
|
|
$file_ex = array( 'image/jpeg' => 'jpg', 'image/png' => 'png');
|
|
|
|
if (!isset($file_ex[$image['output']['type']])) {
|
|
\Session()->flash('alert-danger', 'File is not jpg or png!');
|
|
return redirect(route('admin_product_edit', [$product->id]));
|
|
}
|
|
|
|
$ext = $file_ex[$image['output']['type']];
|
|
// Original file name
|
|
$name = $image['output']['name'];
|
|
$name = \App\Services\Slim::sanitizeFileName($name);
|
|
$name = uniqid() . '_' . $name;
|
|
|
|
$data = \Storage::disk('public')->put(
|
|
'images/product/'.$product->id.'/'.$name,
|
|
$data
|
|
);
|
|
|
|
ProductImage::create([
|
|
'product_id' => $product->id,
|
|
'filename' => $name,
|
|
'original_name' => $image['output']['name'],
|
|
'ext' => $ext,
|
|
'mine' => $image['output']['type'],
|
|
'size' => $image['input']['size']
|
|
]);
|
|
|
|
|
|
\Session()->flash('alert-success', "Datei hochgeladen");
|
|
return redirect(route('admin_product_edit', [$product->id]));
|
|
}
|
|
\Session()->flash('alert-danger', "Datei leer");
|
|
return redirect(route('admin_product_edit', [$product->id]));
|
|
|
|
}
|
|
catch (Exception $e) {
|
|
\Session()->flash('alert-danger', "Fehler".$e);
|
|
return redirect(route('admin_product_edit', [$product->id]));
|
|
}
|
|
}
|
|
|
|
public function imageDelete($image_id, $product_id){
|
|
|
|
$product = Product::findOrFail($product_id);
|
|
$product_image = ProductImage::findOrFail($image_id);
|
|
|
|
if($product_image->product_id == $product->id){
|
|
$file = 'images/product/'.$product->id.'/'.$product_image->filename;
|
|
\Storage::disk('public')->delete($file);
|
|
|
|
$product_image->delete();
|
|
|
|
\Session()->flash('alert-success', "Datei gelöscht");
|
|
return redirect(route('admin_product_edit', [$product->id]));
|
|
|
|
}
|
|
\Session()->flash('alert-danger', "Datei nicht gefunden");
|
|
return redirect(route('admin_product_edit', [$product->id]));
|
|
|
|
}
|
|
|
|
public function imageAttribute($product_id, $attr, $val = false){
|
|
|
|
if(is_numeric($val) && $val < 0){
|
|
$val = 0;
|
|
}
|
|
|
|
$product_image = ProductImage::findOrFail($product_id);
|
|
|
|
$product_image->{$attr} = $val;
|
|
$product_image->save();
|
|
|
|
\Session()->flash('alert-success', "Wert gespeichert");
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
} |