ProductImageController::generateAttributeLists()   B
last analyzed

Complexity

Conditions 7
Paths 3

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 18
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 28
rs 8.8333
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
16
class ProductImageController extends Controller
17
{
18
    public function index(Request $request, $productId)
19
    {
20
        $product = ProductService::find($productId);
21
        if ($request->wantsJson()) {
22
            $query = ProductService::getImageModel()->where('product_id', '=', $productId);
23
            
24
            $datatables = \DataTables::of($query)
25
            ->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...
26
                return '<img src="/files/product/100x100/'.$query->product_id.'/'.$query->file.'"  />';
27
            })
28
29
            ->addColumn('action', function ($query) use ($productId) {
30
                $deleteLink = \Form::deleteajax(url()->route('product.images.destroy', array('productId' => $productId, 'id' => $query->id)), 'Delete', '', array('class'=>'btn btn-default btn-sm btn-danger'));
31
                $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;
32
            
33
                return $links;
34
            });
35
36
            return $datatables->rawColumns(['thumb', 'action'])->make(true);
37
        }
38
        
39
        return view('backend.product_image.index')->with(array('product' => $product));
40
    }
41
42
    public function create($productId)
43
    {
44
        $product = ProductService::find($productId);
45
        $lists = $this->generateAttributeLists($product);
46
        return view('backend.product_image.create')->with(array('attributesList' => $lists['attributesList'], 'productAttributesList' => $lists['productAttributesList'], 'product' => $product));
47
    }
48
49
    public function store(Request $request, $productId)
50
    {
51
        $result  = ProductService::createImage($request->all(), $productId);
52
        return ProductService::notificationRedirect(array('product.images.index', $productId), $result, 'The product image was inserted.');
53
    }
54
55
    public function edit($productId, $productImageId)
56
    {
57
        $product = ProductService::find($productId);
58
        $productImage = ProductService::findImage($productImageId);
59
        $lists = $this->generateAttributeLists($product);
60
        $selectedProductAttributes = array();
61
        $selectedAttributes = array();
62
63
        if ($productImage->relatedProductAttributes->count()) {
64
            foreach ($productImage->relatedProductAttributes as $row) {
65
                $selectedProductAttributes[] =  $row->pivot->product_attribute_id;
66
            }
67
        }
68
69
        if ($productImage->relatedAttributes->count()) {
70
            foreach ($productImage->relatedAttributes as $row) {
71
                $selectedAttributes[] =  $row->pivot->attribute_id;
72
            }
73
        }
74
75
        return view('backend.product_image.edit')->with(array('selectedAttributes' => $selectedAttributes, 'selectedProductAttributes' => $selectedProductAttributes, 'attributesList' => $lists['attributesList'], 'productAttributesList' => $lists['productAttributesList'], 'productImage' => $productImage, 'product' => $product));
76
    }
77
78
    public function generateAttributeLists($product)
79
    {
80
        $productAttributes =         $product->attributes;
81
        $newProductAttributes = array();
82
        $attributesList = array();
83
        $productAttributesList = array();
84
        if ($product->attributes->count()) {
85
            foreach ($productAttributes as $row) {
86
                $combinations = $row->combinations;
87
                foreach ($combinations as $combination) {
88
                    $newProductAttributes[$row->id][$combination->attribute->attributeGroup->title]['id'] = $combination->attribute->id;
89
                    $newProductAttributes[$row->id][$combination->attribute->attributeGroup->title]['value'] = $combination->attribute->value;
90
                }
91
            }
92
93
            if ($newProductAttributes) {
94
                foreach ($newProductAttributes as $key => $productAttribute) {
95
                    $newArray = array();
96
                    foreach ($productAttribute as $keyNew => $valueNew) {
97
                         $newArray[] = $keyNew.': '.$valueNew['value'];
98
                         $attributesList[$valueNew['id']] = $valueNew['value'];
99
                    }
100
                    $productAttributesList[$key] = implode(', ', $newArray);
101
                }
102
            }
103
        }
104
105
        return array('productAttributesList' => $productAttributesList, 'attributesList' => $attributesList);
106
    }
107
108
    public function update(Request $request, $productId, $productImageId)
109
    {
110
        $result  = ProductService::updateImageById($request->all(), $productId, $productImageId);
111
        return ProductService::notificationRedirect(array('product.images.index', $productId), $result, 'The product image was update.');
112
    }
113
114
    public function destroy($productId, $productImageId)
115
    {
116
        $result  = ProductService::destroyImage($productImageId);
117
118
        if ($result) {
119
            flash('The product image is deleted.');
120
            return redirect()->route('product.images.index', $productId);
121
        }
122
    }
123
}
124