Passed
Push — master ( f4b030...a2b3af )
by Matthijs
22:06 queued 15:53
created

ProductImageController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace App\Http\Controllers\Backend;
2
3
4
/**
5
 * ProductImageController
6
 *
7
 * This is the controller of the product images of the shop
8
 * @author Matthijs Neijenhuijs <[email protected]>
9
 * @version 0.1
10
 */
11
12
use App\Http\Controllers\Controller;
13
use Hideyo\Ecommerce\Framework\Services\Product\ProductFacade as ProductService;
14
use Illuminate\Http\Request;
15
use Notification;
16
17
class ProductImageController extends Controller
18
{
19
    public function index(Request $request, $productId)
20
    {
21
           $product = ProductService::find($productId);
22
        if ($request->wantsJson()) {
23
24
            $query = ProductService::getImageModel()->where('product_id', '=', $productId);
25
            
26
            $datatables = \Datatables::of($query)
0 ignored issues
show
Bug introduced by
The type Datatables was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
            ->addColumn('thumb', function ($query) use ($productId) {
0 ignored issues
show
Unused Code introduced by
The import $productId is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
28
                return '<img src="/files/product/100x100/'.$query->product_id.'/'.$query->file.'"  />';
29
            })
30
31
            ->addColumn('action', function ($query) use ($productId) {
32
                $deleteLink = \Form::deleteajax(url()->route('product.images.destroy', array('productId' => $productId, 'id' => $query->id)), 'Delete', '', array('class'=>'btn btn-default btn-sm btn-danger'));
33
                $links = '<a href="'.url()->route('product.images.edit', array('productId' => $productId, 'id' => $query->id)).'" class="btn btn-default btn-sm btn-success"><i class="entypo-pencil"></i>Edit</a>  '.$deleteLink;
34
            
35
                return $links;
36
            });
37
38
            return $datatables->make(true);
39
        }
40
        
41
        return view('backend.product_image.index')->with(array('product' => $product));
42
    }
43
44
    public function create($productId)
45
    {
46
        $product = ProductService::find($productId);
47
        $lists = $this->generateAttributeLists($product);
48
        return view('backend.product_image.create')->with(array('attributesList' => $lists['attributesList'], 'productAttributesList' => $lists['productAttributesList'], 'product' => $product));
49
    }
50
51
    public function store(Request $request, $productId)
52
    {
53
        $result  = ProductService::createImage($request->all(), $productId);
54
        return ProductService::notificationRedirect(array('product.images.index', $productId), $result, 'The product image was inserted.');
55
    }
56
57
    public function edit($productId, $productImageId)
58
    {
59
        $product = ProductService::find($productId);
60
        $productImage = ProductService::findImage($productImageId);
61
        $lists = $this->generateAttributeLists($product);
62
        $selectedProductAttributes = array();
63
        $selectedAttributes = array();
64
65
        if ($productImage->relatedProductAttributes->count()) {
66
            foreach ($productImage->relatedProductAttributes as $row) {
67
                $selectedProductAttributes[] =  $row->pivot->product_attribute_id;
68
            }
69
        }
70
71
        if ($productImage->relatedAttributes->count()) {
72
            foreach ($productImage->relatedAttributes as $row) {
73
                $selectedAttributes[] =  $row->pivot->attribute_id;
74
            }
75
        }
76
77
        return view('backend.product_image.edit')->with(array('selectedAttributes' => $selectedAttributes, 'selectedProductAttributes' => $selectedProductAttributes, 'attributesList' => $lists['attributesList'], 'productAttributesList' => $lists['productAttributesList'], 'productImage' => $productImage, 'product' => $product));
78
    }
79
80
    public function generateAttributeLists($product)
81
    {
82
        $productAttributes =         $product->attributes;
83
        $newProductAttributes = array();
84
        $attributesList = array();
85
        $productAttributesList = array();
86
        if ($product->attributes->count()) {
87
            foreach ($productAttributes as $row) {
88
                $combinations = $row->combinations;
89
                foreach ($combinations as $combination) {
90
                    $newProductAttributes[$row->id][$combination->attribute->attributeGroup->title]['id'] = $combination->attribute->id;
91
                    $newProductAttributes[$row->id][$combination->attribute->attributeGroup->title]['value'] = $combination->attribute->value;
92
                }
93
            }
94
95
            if ($newProductAttributes) {
96
                foreach ($newProductAttributes as $key => $productAttribute) {
97
                    $newArray = array();
98
                    foreach ($productAttribute as $keyNew => $valueNew) {
99
                         $newArray[] = $keyNew.': '.$valueNew['value'];
100
                         $attributesList[$valueNew['id']] = $valueNew['value'];
101
                    }
102
                    $productAttributesList[$key] = implode(', ', $newArray);
103
                }
104
            }
105
        }
106
107
        return array('productAttributesList' => $productAttributesList, 'attributesList' => $attributesList);
108
    }
109
110
    public function update(Request $request, $productId, $productImageId)
111
    {
112
        $result  = ProductService::updateImageById($request->all(), $productId, $productImageId);
113
        return ProductService::notificationRedirect(array('product.images.index', $productId), $result, 'The product image was update.');
114
    }
115
116
    public function destroy($productId, $productImageId)
117
    {
118
        $result  = ProductService::destroyImage($productImageId);
119
120
        if ($result) {
121
            Notification::success('The product image is deleted.');
122
            return redirect()->route('product.images.index', $productId);
123
        }
124
    }
125
}
126