Completed
Push — development ( db1c54...00218f )
by Ashutosh
10:15
created

BaseProductController::getLinkToDownload()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 3
1
<?php
2
3
namespace App\Http\Controllers\Product;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Payment\Plan;
7
use App\Model\Product\Product;
8
use App\Model\Product\ProductUpload;
9
use Bugsnag;
10
use Illuminate\Http\Request;
11
12
class BaseProductController extends Controller
13
{
14
    public function getMyUrl()
15
    {
16
        $server = new Request();
17
        $url = $_SERVER['REQUEST_URI'];
18
        $server = parse_url($url);
19
        $server['path'] = dirname($server['path']);
20
        $server = parse_url($server['path']);
21
        $server['path'] = dirname($server['path']);
22
23
        $server = 'http://'.$_SERVER['HTTP_HOST'].$server['path'];
24
25
        return $server;
26
    }
27
28
29
        /**
30
         * Remove the specified resource from storage.
31
         *
32
         * @param int $id
33
         *
34
         * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Product\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
35
         */
36
   public function fileDestroy(Request $request)
37
    {
38
        $ids = $request->input('select');
39
        if (!empty($ids)) {
40
            foreach ($ids as $id) {
41
                  $product = ProductUpload::where('id', $id)->first();
42
                if ($product) {
43
                    $product->delete();
44
                } else {
45
                    echo "<div class='alert alert-success alert-dismissable'>
46
                    <i class='fa fa-ban'></i>
47
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
48
                    /* @scrutinizer ignore-type */\Lang::get('message.success').'
49
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
50
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
51
                </div>';
52
                    //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
53
                }
54
            }
55
            echo "<div class='alert alert-success alert-dismissable'>
56
                    <i class='fa fa-ban'></i>
57
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
58
                    /* @scrutinizer ignore-type */\Lang::get('message.success').'
59
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
60
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
61
                </div>';
62
        } else {
63
            echo "<div class='alert alert-success alert-dismissable'>
64
                    <i class='fa fa-ban'></i>
65
                    <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> '.
66
                    /* @scrutinizer ignore-type */ \Lang::get('message.success').'
67
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
68
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
69
                </div>';
70
            //echo \Lang::get('message.select-a-row');
71
        }
72
    }
73
74
    public function getProductQtyCheck($productid)
75
    {
76
        try {
77
            $check = self::checkMultiProduct($productid);
78
            if ($check == true) {
79
                return "<div class='col-md-4 form-group'>
80
	                        <label class='required'>"./* @scrutinizer ignore-type */\Lang::get('message.quantity')."</label>
81
	                        <input type='text' name='quantity' class='form-control' id='quantity' value='1'>
82
	                </div>";
83
            }
84
        } catch (\Exception $ex) {
85
            Bugsnag::notifyException($ex);
86
87
            return $ex->getMessage();
88
        }
89
    }
90
91
    public function getSubscriptionCheck($productid, Request $request)
92
    {
93
        try {
94
            $controller = new \App\Http\Controllers\Front\CartController();
95
            $check = $controller->allowSubscription($productid);
96
            $field = '';
97
            $price = '';
98
            if ($check === true) {
99
                $plan = new Plan();
100
                $plans = $plan->pluck('name', 'id')->toArray();
101
                $script = ''; //$this->getSubscriptionCheckScript();
102
                $field = "<div class='col-md-4 form-group'>
103
                        <label class='required'>"./* @scrutinizer ignore-type */ \Lang::get('message.subscription').'</label>
104
                       '.\Form::select('plan', ['' => 'Select', 'Plans' => $plans], null, ['class' => 'form-control', 'id' => 'plan', 'onchange' => 'getPrice(this.value)']).'
105
                </div>'.$script;
106
            } else {
107
                $userid = $request->input('user');
108
                $price = $controller->productCost($productid, $userid);
109
            }
110
            $field .= $this->getDescriptionField($productid);
111
            $result = ['price' => $price, 'field' => $field];
112
113
            return response()->json($result);
114
        } catch (\Exception $ex) {
115
            Bugsnag::notifyException($ex);
116
117
            return $ex->getMessage();
118
        }
119
    }
120
121
    public function userDownload($uploadid, $userid, $invoice_number, $version_id = '')
122
    {
123
        try {
124
            if (\Auth::user()->role != 'admin') {
125
                if (\Auth::user()->id != $userid) {
126
                    throw new \Exception('This user has no permission for this action');
127
                }
128
            }
129
            $user = new \App\User();
130
            $user = $user->findOrFail($userid);
131
132
            $invoice = new \App\Model\Order\Invoice();
133
            $invoice = $invoice->where('number', $invoice_number)->first();
134
135
            if ($user && $invoice) {
136
                if ($user->active == 1) {
137
                    $order = $invoice->order()->orderBy('id', 'desc')->select('product')->first();
138
                    $product_id = $order->product;
139
                    $invoice_id = $invoice->id;
140
                    $release = $this->downloadProduct($uploadid, $userid, $invoice_id, $version_id);
141
                    if (is_array($release) && array_key_exists('type', $release)) {
142
                        $release = $release['release'];
143
144
                        return view('themes.default1.front.download', compact('release', 'form'));
145
                    } else {
146
                        header('Content-type: Zip');
147
                        header('Content-Description: File Transfer');
148
                        header('Content-Disposition: attachment; filename=Faveo.zip');
149
                        //header("Content-type: application/zip");
150
                        header('Content-Length: '.filesize($release));
151
                        //ob_clean();
152
                        flush();
153
                        readfile("$release");
154
                    }
155
                } else {
156
                    return redirect('auth/login')->with('fails', \Lang::get('activate-your-account'));
157
                }
158
            } else {
159
                return redirect('auth/login')->with('fails', \Lang::get('please-purcahse-a-product'));
160
            }
161
        } catch (\Exception $ex) {
162
            Bugsnag::notifyException($ex);
163
164
            return redirect('auth/login')->with('fails', $ex->getMessage());
165
        }
166
    }
167
168
    public function getRelease($owner, $repository, $order_id, $file)
169
    {
170
        if ($owner && $repository) {//If the Product is downloaded from Github
171
            $github_controller = new \App\Http\Controllers\Github\GithubController();
172
            $relese = $github_controller->listRepositories($owner, $repository, $order_id);
173
174
            return ['release'=>$relese, 'type'=>'github'];
175
        } elseif ($file) {
176
            //If the Product is Downloaded from FileSystem
177
            $fileName = $file->file;
178
            $relese = storage_path().'/products'.'//'.$fileName; //For Local Server
179
            // $relese = '/home/faveo/products/'.$file->file;
180
181
            return $relese;
182
        }
183
    }
184
185
    public function getReleaseAdmin($owner, $repository, $file)
186
    {
187
        if ($owner && $repository) {
188
            $github_controller = new \App\Http\Controllers\Github\GithubController();
189
            $relese = $github_controller->listRepositoriesAdmin($owner, $repository);
190
191
            return ['release'=>$relese, 'type'=>'github'];
192
        } elseif ($file->file) {
193
            // $relese = storage_path().'\products'.'\\'.$file->file;
194
            $relese = '/home/faveo/products/'.$file->file;
195
196
            return $relese;
197
        }
198
    }
199
200
    public function getLinkToDownload($role, $invoice, $id)
201
    {
202
        if ($role == 'user') {
203
            if ($invoice && $invoice != '') {
204
                return $this->downloadProductAdmin($id);
205
            } else {
206
                throw new \Exception('This user has no permission for this action');
207
            }
208
        }
209
210
        return $this->downloadProductAdmin($id);
211
    }
212
213
    public function downloadProductAdmin($id)
214
    {
215
        try {
216
            $product = Product::findOrFail($id);
217
            $type = $product->type;
218
            $owner = $product->github_owner;
219
            $repository = $product->github_repository;
220
            $file = ProductUpload::where('product_id', '=', $id)->select('file')
221
            ->orderBy('created_at', 'desc')
222
            ->first();
223
224
            if ($type == 2) {
225
                $relese = $this->getReleaseAdmin($owner, $repository, $file);
226
227
                return $relese;
228
            }
229
        } catch (\Exception $e) {
230
            Bugsnag::notifyException($e);
231
232
            return redirect()->back()->with('fails', $e->getMessage());
233
        }
234
    }
235
236
    public function getPrice(Request $request)
237
    {
238
        try {
239
            $id = $request->input('product');
240
            // dd($id);
241
            $userid = $request->input('user');
242
            $plan = $request->input('plan');
243
            $controller = new \App\Http\Controllers\Front\CartController();
244
            $price = $controller->cost($id, $userid, $plan);
245
            $field = $this->getProductField($id).$this->getProductQtyCheck($id);
246
247
            $result = ['price' => $price, 'field' => $field];
248
249
            return response()->json($result);
250
        } catch (\Exception $ex) {
251
            Bugsnag::notifyException($ex);
252
            $result = ['price' => $ex->getMessage(), 'field' => ''];
253
254
            return response()->json($result);
255
        }
256
    }
257
258
    public function updateVersionFromGithub($productid)
259
    {
260
        try {
261
            if (\Input::has('github_owner') && \Input::has('github_repository')) {
262
                $owner = \Input::get('github_owner');
263
                $repo = \Input::get('github_repository');
264
                $product = Product::find($productid);
265
                $github_controller = new \App\Http\Controllers\Github\GithubController();
266
                $version = $github_controller->findVersion($owner, $repo);
267
                $product->version = $version;
268
                $product->save();
269
            }
270
        } catch (\Exception $ex) {
271
            Bugsnag::notifyException($ex);
272
273
            throw new \Exception($ex->getMessage());
274
        }
275
    }
276
277
    public static function checkMultiProduct($productid)
278
    {
279
        try {
280
            $product = new Product();
281
            $product = $product->find($productid);
282
            if ($product) {
283
                // dd($product->multiple_qty == 1);
284
                if ($product->multiple_qty == 1) {
285
                    return true;
286
                }
287
            }
288
289
            return false;
290
        } catch (\Exception $ex) {
291
            Bugsnag::notifyException($ex);
292
        }
293
    }
294
295
    public function getDescriptionField($productid)
296
    {
297
        try {
298
            $product = Product::find($productid);
299
            $field = '';
300
301
            if ($product->retired == 1) {
302
                $field .= "<div class='col-md-4 form-group'>
303
	                        <label class='required'>"./* @scrutinizer ignore-type */ \Lang::get('message.description')."</label>
304
	                        <textarea name='description' class='form-control' id='description' placeholder='Description'></textarea>
305
	                </div>";
306
            }
307
308
            return $field;
309
        } catch (\Exception $ex) {
310
            Bugsnag::notifyException($ex);
311
312
            return $ex->getMessage();
313
        }
314
    }
315
}
316