Completed
Push — development ( c87275...b28f77 )
by Ashutosh
11:48
created

ExtendedBaseProductController::adminDownload()   A

Complexity

Conditions 5
Paths 22

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 24
rs 9.3554
c 0
b 0
f 0
cc 5
nc 22
nop 3
1
<?php
2
3
namespace App\Http\Controllers\Product;
4
5
use Bugsnag;
6
use App\Http\Controllers\Controller;
7
use App\Model\Payment\TaxProductRelation;
8
use App\Model\Product\Product;
9
use App\Model\Product\ProductUpload;
10
use Illuminate\Http\Request;
11
12
class ExtendedBaseProductController extends Controller
13
{
14
    public function getUpload($id)
15
    {
16
        $new_upload = ProductUpload::where('product_id', '=', $id)
17
        ->select('id', 'product_id', 'title', 'description', 'version', 'file')
18
        ->get();
19
20
        return \DataTables::of($new_upload)
21
    ->addColumn('checkbox', function ($model) {
22
        return "<input type='checkbox' class='upload_checkbox' value=".$model->id.' name=select[] id=checks>';
23
    })
24
25
    ->addColumn('product_id', function ($model) {
26
        return ucfirst($this->product->where('id', $model->product_id)->first()->name);
27
    })
28
29
    ->addColumn('title', function ($model) {
30
        return ucfirst($model->title);
31
    })
32
    ->addColumn('description', function ($model) {
33
        return ucfirst($model->description);
34
    })
35
    ->addColumn('version', function ($model) {
36
        return $model->version;
37
    })
38
39
    ->addColumn('file', function ($model) {
40
        return $model->file;
41
    })
42
    ->addColumn('action', function ($model) {
43
        return '<a href='.('#edit-upload-option/'.$model->id).' 
44
         class=" btn btn-sm btn-primary " data-title="'.$model->title.'"
45
          data-description="'.$model->description.'" data-version="'
46
          .$model->version.'" data-id="'.$model->id.'" onclick="openEditPopup(this)" >Edit</a>';
47
    })
48
    ->rawcolumns(['checkbox', 'product_id', 'title', 'description', 'version', 'file', 'action'])
49
    ->make(true);
50
    }
51
52
    //Update the File Info
53
    public function uploadUpdate($id, Request $request)
54
    {
55
        $file_upload = ProductUpload::find($id);
56
57
        $file_upload->title = $request->input('title');
58
        $file_upload->description = $request->input('description');
59
        $file_upload->version = $request->input('version');
60
        if ($request->file) {
61
            $file = $request->file('file')->getClientOriginalName();
62
63
            $destination = storage_path().'/products';
64
            $request->file('file')->move($destination, $file);
65
            $file_upload->file = $file;
66
        }
67
        $file_upload->save();
68
69
        return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
70
    }
71
72
    public function saveTax($taxes, $product_id)
73
    {
74
        if ($taxes) {
75
            TaxProductRelation::where('product_id', $product_id)->delete();
76
            foreach ($taxes as $tax) {
77
                $newTax = new TaxProductRelation();
78
                $newTax->product_id = $product_id;
79
                $newTax->tax_class_id = $tax;
80
                $newTax->save();
81
            }
82
        }
83
84
        return $newTax;
85
    }
86
87
    public function getProductField($productid)
88
    {
89
        try {
90
            $field = '';
91
            $product = Product::find($productid);
92
            if ($product) {
93
                if ($product->require_domain == 1) {
94
                    $field .= "<div class='col-md-4 form-group'>
95
                        <label class='required'>"./* @scrutinizer ignore-type */
96
                         \Lang::get('message.domain')."</label>
97
                        <input type='text' name='domain' class='form-control' 
98
                        id='domain' placeholder='http://example.com'>
99
                </div>";
100
                }
101
            }
102
103
            return $field;
104
        } catch (\Exception $ex) {
105
            Bugsnag::notifyException($ex);
106
107
            return $ex->getMessage();
108
        }
109
    }
110
111
112
    public function adminDownload($id, $invoice = '', $api = false)
113
    {
114
        try {
115
            $role = \Auth::user()->role;
116
            $release = $this->getLinkToDownload($role, $invoice, $id);
117
118
            if (is_array($release) && array_key_exists('type', $release)) {
119
                header('Location: '.$release['release']);
120
                exit;
121
            } else {
122
                header('Content-type: Zip');
123
                header('Content-Description: File Transfer');
124
                header('Content-Disposition: attachment; filename=Faveo.zip');
125
                header('Content-Length: '.filesize($release));
126
                flush();
127
                readfile("$release");
128
            }
129
        } catch (\Exception $e) {
130
            if ($api) {
131
                return response()->json(['error'=>$e->getMessage()]);
132
            }
133
            Bugsnag::notifyException($e);
134
135
            return redirect()->back()->with('fails', $e->getMessage());
136
        }
137
    }
138
}
139