Completed
Push — development ( 61259f...16e7ec )
by Ashutosh
11:24
created

PageController::getServiceTemplate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 18
rs 9.8666
c 0
b 0
f 0
cc 3
nc 2
nop 3
1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Front\FrontendPage;
7
use Illuminate\Http\Request;
8
9
class PageController extends Controller
10
{
11
    public $page;
12
13
    public function __construct()
14
    {
15
        $this->middleware('auth');
16
        $this->middleware('admin');
17
18
        $page = new FrontendPage();
19
        $this->page = $page;
20
    }
21
22
    public function index()
23
    {
24
        try {
25
            return view('themes.default1.front.page.index');
26
        } catch (\Exception $ex) {
27
            return redirect()->back()->with('fails', $ex->getMessage());
28
        }
29
    }
30
31
    public function getPages()
32
    {
33
        return \DataTables::of($this->page->get())
34
                        ->addColumn('checkbox', function ($model) {
35
                            return "<input type='checkbox' class='page_checkbox' value=".$model->id.' name=select[] id=check>';
36
                        })
37
                        ->addColumn('name', function ($model) {
38
                            return ucfirst($model->name);
39
                        })
40
                        ->addColumn('url', function ($model) {
41
                            return $model->url;
42
                        })
43
                        ->addColumn('created_at', function ($model) {
44
                            return $model->created_at;
45
                        })
46
47
                        ->addColumn('content', function ($model) {
48
                            return str_limit($model->content, 10, '...');
49
                        })
50
                        ->addColumn('action', function ($model) {
51
                            return '<a href='.url('pages/'.$model->id.'/edit')." class='btn btn-sm btn-primary'>Edit</a>";
52
                        })
53
54
                          ->rawColumns(['checkbox', 'name', 'url',  'created_at', 'content', 'action'])
55
                        ->make(true);
56
        // ->searchColumns('name', 'content')
57
                        // ->orderColumns('name')
58
                        // ->make();
59
    }
60
61
    public function create()
62
    {
63
        try {
64
            $parents = $this->page->pluck('name', 'id')->toArray();
65
66
            return view('themes.default1.front.page.create', compact('parents'));
67
        } catch (\Exception $ex) {
68
            return redirect()->back()->with('fails', $ex->getMessage());
69
        }
70
    }
71
72
    public function edit($id)
73
    {
74
        try {
75
            $page = $this->page->where('id', $id)->first();
76
            $parents = $this->page->where('id', '!=', $id)->pluck('name', 'id')->toArray();
77
78
            return view('themes.default1.front.page.edit', compact('parents', 'page'));
79
        } catch (\Exception $ex) {
80
            return redirect()->back()->with('fails', $ex->getMessage());
81
        }
82
    }
83
84
    public function store(Request $request)
85
    {
86
        $this->validate($request, [
87
            'name'    => 'required',
88
            'publish' => 'required',
89
            'slug'    => 'required',
90
            'url'     => 'required',
91
            'content' => 'required',
92
        ]);
93
94
        try {
95
            $this->page->fill($request->input())->save();
96
97
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
98
        } catch (\Exception $ex) {
99
            return redirect()->back()->with('fails', $ex->getMessage());
100
        }
101
    }
102
103
    public function update($id, Request $request)
104
    {
105
        $this->validate($request, [
106
            'name'    => 'required',
107
            'publish' => 'required',
108
            'slug'    => 'required',
109
            'url'     => 'required',
110
            'content' => 'required',
111
        ]);
112
113
        try {
114
            $page = $this->page->where('id', $id)->first();
115
            $page->fill($request->input())->save();
116
117
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
118
        } catch (\Exception $ex) {
119
            return redirect()->back()->with('fails', $ex->getMessage());
120
        }
121
    }
122
123
    public function getPageUrl($slug)
124
    {
125
        $productController = new \App\Http\Controllers\Product\ProductController();
126
        $url = $productController->GetMyUrl();
127
        $segment = $this->addSegment(['public/pages']);
128
        $url = $url.$segment;
129
130
        $slug = str_slug($slug, '-');
131
        echo $url.'/'.$slug;
132
    }
133
134
    public function getSlug($slug)
135
    {
136
        $slug = str_slug($slug, '-');
137
        echo $slug;
138
    }
139
140
    public function addSegment($segments = [])
141
    {
142
        $segment = '';
143
        foreach ($segments as $seg) {
144
            $segment .= '/'.$seg;
145
        }
146
147
        return $segment;
148
    }
149
150
    public function generate(Request $request)
151
    {
152
        // dd($request->all());
153
        if ($request->has('slug')) {
154
            $slug = $request->input('slug');
155
156
            return $this->getSlug($slug);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getSlug($slug) targeting App\Http\Controllers\Fro...geController::getSlug() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
157
        }
158
        if ($request->has('url')) {
159
            $slug = $request->input('url');
160
161
            return $this->getPageUrl($slug);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getPageUrl($slug) targeting App\Http\Controllers\Fro...ontroller::getPageUrl() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
162
        }
163
    }
164
165
    public function show($slug)
166
    {
167
        try {
168
            $page = $this->page->where('slug', $slug)->where('publish', 1)->first();
169
            if ($page->type == 'cart') {
170
                return $this->cart();
171
            }
172
173
            return view('themes.default1.front.page.show', compact('page'));
174
        } catch (\Exception $ex) {
175
            return redirect()->back()->with('fails', $ex->getMessage());
176
        }
177
    }
178
179
    /**
180
     * Remove the specified resource from storage.
181
     *
182
     * @param int $id
183
     *
184
     * @return \Response
185
     */
186
    public function destroy(Request $request)
187
    {
188
        try {
189
            $ids = $request->input('select');
190
            if (!empty($ids)) {
191
                foreach ($ids as $id) {
192
                    $page = $this->page->where('id', $id)->first();
193
                    if ($page) {
194
                        // dd($page);
195
                        $page->delete();
196
                    } else {
197
                        echo "<div class='alert alert-danger alert-dismissable'>
198
                    <i class='fa fa-ban'></i>
199
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */
200
                    \Lang::get('message.failed').'
201
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
202
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
203
                </div>';
204
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
205
                    }
206
                }
207
                echo "<div class='alert alert-success alert-dismissable'>
208
                    <i class='fa fa-ban'></i>
209
210
                    <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */
211
                    \Lang::get('message.success').'
212
213
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
214
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
215
                </div>';
216
            } else {
217
                echo "<div class='alert alert-danger alert-dismissable'>
218
                    <i class='fa fa-ban'></i>
219
                    <b>".\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */\Lang::get('message.failed').'
0 ignored issues
show
Bug introduced by
Are you sure Lang::get('message.alert') of type null|string|array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

219
                    <b>"./** @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */\Lang::get('message.failed').'
Loading history...
220
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
221
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
222
                </div>';
223
                //echo \Lang::get('message.select-a-row');
224
            }
225
        } catch (\Exception $e) {
226
            echo "<div class='alert alert-danger alert-dismissable'>
227
                    <i class='fa fa-ban'></i>
228
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
229
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
230
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
231
                        '.$e->getMessage().'
232
                </div>';
233
        }
234
    }
235
236
    public function search(Request $request)
237
    {
238
        try {
239
            $search = $request->input('q');
240
            $model = $this->result($search, $this->page);
241
242
            return view('themes.default1.front.page.search', compact('model'));
243
        } catch (\Exception $ex) {
244
            return redirect()->back()->with('fails', $ex->getMessage());
245
        }
246
    }
247
248
    public function result($search, $model)
249
    {
250
        try {
251
            $model = $model->where('name', 'like', '%'.$search.'%')->orWhere('content', 'like', '%'.$search.'%')->paginate(10);
252
253
            return $model->setPath('search');
254
        } catch (\Exception $ex) {
255
            //dd($ex);
256
            throw new \Exception('Can not get the search result');
257
        }
258
    }
259
260
    public function transform($type, $data, $trasform = [])
261
    {
262
        $config = \Config::get("transform.$type");
263
        $result = '';
264
        $array = [];
265
        foreach ($trasform as $trans) {
266
            $array[] = $this->checkConfigKey($config, $trans);
267
        }
268
        $c = count($array);
269
        for ($i = 0; $i < $c; $i++) {
270
            $array1 = $this->keyArray($array[$i]);
271
            $array2 = $this->valueArray($array[$i]);
272
            $result .= str_replace($array1, $array2, $data);
273
        }
274
275
        return $result;
276
    }
277
278
    public function checkString($data, $string)
279
    {
280
        if (strpos($data, $string) !== false) {
281
            return true;
282
        }
283
    }
284
285
286
    public function getLocation()
287
    {
288
         if (!empty($_SERVER['HTTP_CLIENT_IP'])) {   //check ip from share internet
289
          $ip = $_SERVER['HTTP_CLIENT_IP'];
290
          } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {   //to check ip is pass from proxy
291
              $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
292
          } else {
293
              $ip = $_SERVER['REMOTE_ADDR'];
294
          }
295
296
         if ($ip != '::1') {
297
            $location = json_decode(file_get_contents('http://ip-api.com/json/'.$ip), true);
298
        } else {
299
            $location = json_decode(file_get_contents('http://ip-api.com/json'), true);
300
        }
301
        return $location;
302
    }
303
304
305
    public function getCurrency($location)
306
    {
307
        if ($location['country'] == 'India') {
308
            $currency = 'INR';
309
        } else {
310
            $currency = 'USD';
311
        }
312
         if (\Auth::user()) {
313
            $currency = 'INR';
314
            $user_currency = \Auth::user()->currency;
315
            if ($user_currency == 1 || $user_currency == 'USD') {
316
                $currency = 'USD';
317
            }
318
        }
319
        return $currency;
320
    }
321
322
    public function cart()
323
    {
324
        $location = $this->getLocation();
325
        $country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($location['countryCode']);
326
        $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($location['countryCode']);
327
        $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray();
328
        $state_code = $location['countryCode'].'-'.$location['region'];
329
        $state = \App\Http\Controllers\Front\CartController::getStateByCode($state_code);
330
        $mobile_code = \App\Http\Controllers\Front\CartController::getMobileCodeByIso($location['countryCode']);
331
        $currency=$this->getCurrency($location);
332
        
333
        \Session::put('currency', $currency);
334
        if (!\Session::has('currency')) {
335
            \Session::put('currency', 'INR');
336
        }
337
        $pages = $this->page->find(1);
338
        $data = $pages->content;
339
340
        $product = new \App\Model\Product\Product();
341
        $helpdesk_products = $product->where('id', '!=', 1)->where('category', '=', 'helpdesk')->get()->toArray();
342
        $temp_controller = new \App\Http\Controllers\Common\TemplateController();
343
        $trasform = [];
344
        $template = $this->getHelpdeskTemplate($helpdesk_products,$data,$trasform);
345
        $sevice_desk_products = $product->where('id', '!=', 1)->where('category', '=', 'servicedesk')->get()->toArray();
346
        $trasform1 = [];
347
        $servicedesk_template =$this->getServiceDeskdeskTemplate($sevice_desk_products,$data,$trasform1);
348
       
349
        $service = $product->where('id', '!=', 1)->where('category', '=', 'service')->get()->toArray();
350
        $trasform2 = [];
351
        $service_template = $this->getServiceTemplate($service,$data,$trasform2);
352
       
353
       
354
355
        return view('themes.default1.common.template.shoppingcart', compact('template', 'trasform', 'servicedesk_template', 'trasform1', 'service_template', 'trasform2'));
356
    }
357
358
    
359
    /**
360
    * Get  Template For Helpdsk Products
361
    */
362
    public function getHelpdeskTemplate($helpdesk_products,$data,$trasform)
363
    {
364
        $temp_controller = new \App\Http\Controllers\Common\TemplateController();
365
         if (count($helpdesk_products) > 0) {
366
            foreach ($helpdesk_products as $key => $value) {
367
            $trasform[$value['id']]['price'] = $temp_controller->leastAmount($value['id']);
368
            $trasform[$value['id']]['name'] = $value['name'];
369
            $trasform[$value['id']]['feature'] = $value['description'];
370
            $trasform[$value['id']]['subscription'] = $temp_controller->plans($value['shoping_cart_link'], $value['id']);
371
            $trasform[$value['id']]['url'] = "<input type='submit' value='Buy' class='btn btn-primary'></form>";
372
            }
373
            $template = $this->transform('cart', $data, $trasform);
374
         }else{
375
        $template = '';
376
    }
377
         return $template;
378
    }
379
380
    /**
381
    * Get  Template For Service Desk Products
382
    */
383
    public function getServiceDeskdeskTemplate($sevice_desk_products,$data,$trasform1)
384
    {
385
        $temp_controller = new \App\Http\Controllers\Common\TemplateController();
386
          if (count($sevice_desk_products) > 0) {
387
            foreach ($sevice_desk_products as $key => $value) {
388
            $trasform1[$value['id']]['price'] = $temp_controller->leastAmount($value['id']);
389
            $trasform1[$value['id']]['name'] = $value['name'];
390
            $trasform1[$value['id']]['feature'] = $value['description'];
391
            $trasform1[$value['id']]['subscription'] = $temp_controller->plans($value['shoping_cart_link'], $value['id']);
392
393
            $trasform1[$value['id']]['url'] = "<input type='submit' value='Buy' class='btn btn-primary'></form>";
394
            }
395
            $servicedesk_template = $this->transform('cart', $data, $trasform1);
396
        } else{
397
        $servicedesk_template = '';
398
    }
399
         return $servicedesk_template;
400
    }
401
402
403
     /**
404
    * Get  Template For Services
405
    */
406
    public function getServiceTemplate($service,$data,$trasform2)
407
    {
408
        $temp_controller = new \App\Http\Controllers\Common\TemplateController();
409
          if (count($service) > 0) {
410
            foreach ($service as $key => $value) {
411
        $trasform2[$value['id']]['price'] = $temp_controller->leastAmountService($value['id']);
412
        $trasform2[$value['id']]['name'] = $value['name'];
413
        $trasform2[$value['id']]['feature'] = $value['description'];
414
         $trasform2[$value['id']]['subscription'] = $temp_controller->plans($value['shoping_cart_link'], $value['id']);
415
416
        $trasform2[$value['id']]['url'] = "<input type='submit' value='Buy' class='btn btn-primary'></form>";
417
            }
418
            $service_template = $this->transform('cart', $data, $trasform2);
419
        }
420
        else{
421
        $service_template = '';
422
    }
423
         return $service_template;
424
    }
425
426
    public function checkConfigKey($config, $transform)
427
    {
428
        $result = [];
429
        //        dd($config);
430
        if (count($config) > 0) {
431
            foreach ($config as $key => $value) {
432
                if (array_key_exists($key, $transform)) {
433
                    $result[$value] = $transform[$key];
434
                }
435
            }
436
        }
437
438
        return $result;
439
    }
440
441
    public function keyArray($array)
442
    {
443
        $result = [];
444
        foreach ($array as $key => $value) {
445
            $result[] = $key;
446
        }
447
448
        return $result;
449
    }
450
451
    public function valueArray($array)
452
    {
453
        $result = [];
454
        foreach ($array as $key => $value) {
455
            $result[] = $value;
456
        }
457
458
        return $result;
459
    }
460
}
461