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