Completed
Push — master ( 9fccf2...e212fc )
by Jan
01:53
created

AdminModuleController::afterStore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Admin module controller
4
 * 
5
 * Basic parent controller for admin functions
6
 * 
7
 * @category Controller
8
 * @subpackage Admin
9
 * @package Olapus
10
 * @author Jan Drda <[email protected]>
11
 * @copyright Jan Drda
12
 * @license https://opensource.org/licenses/MIT MIT
13
 */
14
15
namespace App\Http\Controllers\Admin;
16
17
use App\Helpers;
18
use Illuminate\Http\Request;
19
use Illuminate\Support\Facades\Auth;
20
use Illuminate\Support\Facades\View;
21
use App\Http\Controllers\Controller;
22
use Illuminate\Database\Eloquent\Builder;
23
use Illuminate\Support\Facades\DB;
24
use Carbon\Carbon;
25
use App\Transaction;
26
use App\User;
27
28
class AdminModuleController extends Controller{
29
30
    /**
31
     * Module name
32
     * 
33
     * @var string 
34
     */
35
    protected $moduleName;
36
37
    /**
38
     * Module basic path
39
     * 
40
     * @var string
41
     */
42
    protected $moduleBasicRoute;
43
44
    /**
45
     * View basic path
46
     * 
47
     * @var string
48
     */
49
    protected $moduleBasicTemplatePath;
50
51
    /**
52
     * Model Class
53
     * 
54
     * @var string
55
     */
56
    protected $modelClass;
57
58
    /**
59
     * Rows to paginate
60
     * 
61
     * @var integer 
62
     */
63
    protected $paginateRows = NULL;
64
    
65
    /**
66
     * View variables to inject
67
     * 
68
     * @var array
69
     */
70
    protected $viewVariables = array();
71
    
72
    /**
73
     * Hidden fields for action
74
     * 
75
     * @var array 
76
     */
77
    protected $hiddenFieldsOnAction = array();
78
    
79
   /**
80
    * Binary fields to exclude from update
81
    * 
82
    * @var array 
83
    */
84
    protected $binaryFields = array();
85
86
    /**
87
     * Binary fields to exclude from update
88
     *
89
     * @var array
90
     */
91
    protected $dateTimeLocalFields = array();
92
    
93
    /**
94
     * Custom view
95
     * 
96
     * @var string 
97
     */
98
    protected $customView = NULL;
99
100
    /**
101
     * Constructor
102
     */
103
    public function __construct() {
104
105
        /**
106
         * Check if user is active
107
         */
108
       /* print_r(Auth::user());
109
        die;*/
110
111
        /**
112
         * Pagination handle
113
         */
114
        if ($this->paginateRows == NULL) {
115
116
            $this->paginateRows = env('ADMIN_PAGINATE', 10);
117
        }
118
119
        /**
120
         * Get module name
121
         */
122
        $temp = explode("\\", str_replace('Controller', '', get_called_class()));
123
        $this->moduleName = trim(last($temp));
124
        
125
        /**
126
         * Get model full name
127
         */
128
        $this->modelClass = '\\App\\' . $this->moduleName;
129
130
        /**
131
         * Set basic variables
132
         */
133
        $this->moduleBasicRoute = 'admin.' . lcfirst($this->moduleName);
134
        $this->moduleBasicTemplatePath = 'admin.modules.' . strtolower($this->moduleName);
135
136
        /**
137
         * Global template variables
138
         */
139
        View::share('moduleBasicRoute', $this->moduleBasicRoute);
140
        View::share('moduleBasicTemplatePath', $this->moduleBasicTemplatePath);
141
        View::share('moduleName', $this->moduleName);
142
143
        /**
144
         * Module name for blade
145
         */
146
        $temp = explode('.', $this->moduleBasicRoute);
147
        View::share('moduleNameBlade', strtolower($temp[0] . "_module_" . $temp[1]));
148
149
    }
150
151
    /**
152
     * Runs after store
153
     *
154
     * @param $request
155
     */
156
    protected function afterStore($request){
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
157
158
    }
159
160
    /**
161
     * Runs after update
162
     *
163
     * @param $request
164
     */
165
    protected function afterUpdate($request){
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
166
167
    }
168
169
    /**
170
     * Save transaction
171
     *
172
     * @param int $typeId
0 ignored issues
show
Bug introduced by
There is no parameter named $typeId. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
173
     * @param string $text
0 ignored issues
show
Bug introduced by
There is no parameter named $text. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
174
     * @param $userId
175
     * @param $amount
176
     */
177 View Code Duplication
    protected function _saveTransation($status_id = 1, $user_id, $amount,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
                                       $campaign_id = null, $payment_id = null, $recommendation_id = null){
179
180
        /**
181
         * Save transaction
182
         */
183
        $transaction = new Transaction();
184
        $transaction->transactionstatus_id = $status_id;
185
        $transaction->user_id = $user_id;
186
        $transaction->amount = $amount;
187
        $transaction->campaign_id = $campaign_id;
188
        $transaction->payment_id = $payment_id;
189
        $transaction->recommendation_id = $recommendation_id;
190
        $transaction->save();
191
192
        /**
193
         * Update user wallet
194
         */
195
        $user = User::find($user_id);
196
        $user->wallet = $user->wallet + $amount;
197
        $user->save();
198
    }
199
    
200
    /**
201
     * Save media to storage
202
     * 
203
     * @param object $object
204
     * @param Request $request
205
     * @param boolean $update
206
     * @return boolean
207
     */
208
    public function saveMediaToStorage($object, $request, $update = FALSE){
209
        
210
        return FALSE;
211
    }
212
    
213
    /**
214
     * Get number of pagination rows
215
     * 
216
     * @return integer
217
     */
218
    public function getRowsToPaginate() {
219
220
        if ($this->paginateRows == NULL) {
221
222
            return env('ADMIN_PAGINATE', 10);
223
        }
224
        else{
225
226
            return $this->paginateRows;
227
        }
228
    }
229
    
230
    /**
231
     * Associate relationships to other table
232
     * 
233
     * @param object $object
234
     * @param Request $request
235
     */
236
    public function associateRelationships($object, Request $request){
237
        
238
    }
239
    
240
    /**
241
     * Associate relationships to other table, where ID if object must be present
242
     * 
243
     * @param object $object
244
     * @param Request $request
245
     */
246
    public function associateRelationshipsWithID($object, Request $request){
247
        
248
    }
249
250
    /**
251
     * Reset cache 
252
     * 
253
     * @param object $object
254
     */
255
    public function resetCache($object){
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
256
        
257
    }
258
259
    /**
260
     * Change ar result if necessary
261
     *
262
     * @param $arResult
263
     * @return mixed
264
     */
265
    public function changeEditResultField($arResult){
266
        return $arResult;
267
    }
268
269
    /**
270
     * Display a listing of the resource
271
     * 
272
     * @param Request $request
273
     * @return Response
274
     */
275
    public function index(Request $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
276
277
        /**
278
         * Handle saved settings
279
         */
280
        $redirectRoute = Helpers::resetSaveIndexParameters($this->moduleBasicRoute);
281
        if ($redirectRoute !== FALSE) {
282
283
            return redirect($redirectRoute);
284
        }
285
286
        /**
287
         * Get the rows
288
         */
289
        $modelClass = $this->modelClass;
290
291
292
        $arResults = $modelClass::where(function(Builder $query) {
293
                    $query->fulltextAllColumns();
294
                })->relationships()->orderByColumns()->excludeFromIndex()
295
                        ->externalTablesFilter()->paginate($this->getRowsToPaginate());
296
297
        /**
298
         * Get media
299
         */
300
        if(method_exists($arResults, 'getMedia')){
301
            $media = $arResults->getMedia();
302
        }
303
        else{
304
            $media = null;
305
        }
306
        
307
                
308
        /**
309
         * Choose the view
310
         */
311 View Code Duplication
        if(empty($this->customView['index']) == TRUE){
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
312
            $view = $this->moduleBasicTemplatePath . '.index';
313
        }
314
        else{
315
            $view = $this->customView;
316
        }
317
318
        /**
319
         * Return page
320
         */
321
        return view($view, ['results' => $arResults, 'media' => $media]);
322
    }
323
324
    /**
325
     * Show the form for creating a new resource.
326
     *
327
     * @return Response
328
     */
329
    public function create() {
330
        
331
        /**
332
         * Choose the view
333
         */
334 View Code Duplication
        if(empty($this->customView['index']) == TRUE){
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
335
            $view = $this->moduleBasicTemplatePath . '.create_edit';
336
        }
337
        else{
338
            $view = $this->customView;
339
        }
340
        
341
        /**
342
         * Return page
343
         */
344
        return view($view);
345
    }
346
347
    /**
348
     * Store a newly created resource in storage.
349
     *
350
     * @param  Request  $request
351
     * @return Response
352
     */
353
    public function store(Request $request) {
354
355
        /**
356
         * Change the validation array
357
         */
358 View Code Duplication
        foreach ($this->arValidationArray as $name => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
359
            if(strpos($this->arValidationArray[$name], 'unique') > 0){
360
                $this->arValidationArray[$name] = $value . ',NULL,id,deleted_at,NULL';
0 ignored issues
show
Bug introduced by
The property arValidationArray does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
361
            }
362
        }
363
364
        /**
365
         * Validate input
366
         */
367
        $this->validate($request, $this->arValidationArray);
368
369
        /**
370
         * Create new object
371
         */
372
        $modelClass = $this->modelClass;
373
        $object = new $modelClass();
374
375
        /**
376
         * Create row
377
         */
378
        foreach ($this->arValidationArray as $name => $value) {
379
380
            /**
381
             * Datetime
382
             */
383
            if(in_array($name, $this->dateTimeLocalFields)){
384
385
                $object->{$name} = str_replace('T', ' ', $request->input($name)). ':00';
386
            }
387
            else {
388
389
                /**
390
                 * Change to null if needed
391
                 */
392
                if(strlen($request->$name) < 1){
393
                    $object->{$name} = null;
394
                }
395
                else {
396
                    $object->{$name} = $request->input($name);
397
                }
398
            }
399
400
401
        }
402
403
        /**
404
         * Associate relationships
405
         */
406
        $this->associateRelationships($object, $request);
407
        
408
        /**
409
         * Save main object
410
         */
411
        $object->save();
412
        
413
        /**
414
         * Save media to storage
415
         */
416
        $this->saveMediaToStorage($object, $request);
417
        
418
        /**
419
         * Associate relatinships with ID
420
         */
421
        $this->associateRelationshipsWithID($object, $request);
422
423
        /**
424
         * After store
425
         */
426
        $this->afterStore($request);
427
428
        /**
429
         * Redirect to index
430
         */
431 View Code Duplication
        if(!empty($request->custom_route)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
432
            return redirect($request->custom_route);
433
        }
434
        else {
435
            return redirect(route($this->moduleBasicRoute . '.index'));
436
        }
437
    }
438
439
    /**
440
     * Show the form for editing the specified resource.
441
     *
442
     * @param  int  $id
443
     * @return Response
444
     */
445
    public function edit($id) {
446
        
447
        $modelClass = $this->modelClass;
448
449
        /**
450
         * Get the row
451
         */
452
        $arResults = $modelClass::where('id', $id)->relationships()->excludeFromFind()->first();
453
454
455
        /**
456
         * Row does not exist - redirect
457
         */
458
        if (empty($arResults)) {
459
460
            return redirect(route($this->moduleBasicRoute . '.index'))->withInput()->withErrors(['edit' => trans('validation.row_not_exist')]);
461
        }
462
463
        /**
464
         * Set the put method for update
465
         */
466
        $arResults['_method'] = 'PUT';
467
        
468
        /**
469
         * Choose the view
470
         */
471 View Code Duplication
        if(empty($this->customView['index']) == TRUE){
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
472
            $view = $this->moduleBasicTemplatePath . '.create_edit';
473
        }
474
        else{
475
            $view = $this->customView;
476
        }
477
478
        $arResults = $this->changeEditResultField($arResults);
479
    
480
        /**
481
         * Return page
482
         */
483
        return view($view, ['results' => $arResults]);
484
    }
485
486
    /**
487
     * Display the specified resource.
488
     *
489
     * @param  int  $id
490
     * @return Response
491
     */
492
    public function show($id) {
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
493
       
494
    }
495
496
    /**
497
     * Update the specified resource in storage.
498
     *
499
     * @param  Request  $request
500
     * @param  int  $id
501
     * @return Response
502
     */
503
    public function update(Request $request, $id) {
504
505
506
        /**
507
         * Change the validation array
508
         */
509 View Code Duplication
        foreach ($this->arValidationArray as $name => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
510
            
511
            if(strpos($this->arValidationArray[$name], 'unique') > 0){
512
                $this->arValidationArray[$name] = $value . ','.$id.',id,deleted_at,NULL';
513
            }
514
        }
515
516
        /**
517
         * Validate input
518
         */
519
        $this->validate($request, $this->arValidationArray);
520
        
521
        /**
522
         * Get the row
523
         */
524
        $modelClass = $this->modelClass;
525
        $arResults = $modelClass::find($id);
526
527
        /**
528
         * Row does not exist - redirect
529
         */
530 View Code Duplication
        if ($arResults == FALSE) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
531
532
            return redirect(route($this->moduleBasicRoute . '.index'))->withInput()->withErrors(['edit' => trans('validation.row_not_exist')]);
533
        }
534
        
535
        /**
536
         * Reset cache
537
         */
538
        $this->resetCache($arResults);
539
540
        /**
541
         * Set updated values
542
         */
543
        foreach ($this->arValidationArray as $name => $value) {
544
            
545
            /**
546
             * Binary fields will not be updated if empty
547
             *
548
             */
549
            if(in_array($name, $this->binaryFields)){
550
551
            }
552
            else{
553
                
554
               /**
555
                * Empty exception
556
                */
557
               if (empty($request->input($name)) == FALSE) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
558
559
                   /**
560
                    * Datetime
561
                    */
562
                   if(in_array($name, $this->dateTimeLocalFields)){
563
564
                       $arResults->$name = str_replace('T', ' ', $request->input($name)). ':00';
565
                   }
566
                   else {
567
                       $arResults->$name = $request->input($name);
568
                   }
569
               }
570
               
571
               else{
572
                   
573
                   /**
574
                    * Numeric zero ?
575
                    */
576
                   if(isset($request->$name) && is_numeric($request->input($name)) == TRUE){
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
577
                       
578
                      $arResults->$name = $request->input($name);
579
                   }
580
                   
581
                   else{
582
                    $arResults->$name = NULL;
583
                   }
584
               }
585
            }
586
587
            
588
        }
589
590
        
591
        /**
592
         * Associate relationships
593
         */
594
        $this->associateRelationships($arResults, $request);
595
        
596
        /**
597
         * Save media to storage
598
         */
599
        if($this->saveMediaToStorage($arResults, $request, TRUE) == TRUE){
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
600
            
601
            // Update binary fields
602
            foreach ($this->binaryFields as $name => $value) {
603
                
604
                if (empty($request->$value) == FALSE) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
605
                   $arResults->$value = $request->$value;
606
                }
607
                else{
608
                    $arResults->$value = NULL;
609
                }
610
            }
611
        }
612
613
        /**
614
         * Save the changes
615
         */
616
        $arResults->save();
617
        
618
        /**
619
         * Associate relatinships with ID
620
         */
621
        $this->associateRelationshipsWithID($arResults, $request);
622
623
        /**
624
         * After update
625
         */
626
        $this->afterUpdate($request);
627
628
        /**
629
         * Redirect to index
630
         */
631 View Code Duplication
        if(!empty($request->custom_route)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
632
            return redirect($request->custom_route);
633
        }
634
        else {
635
            return redirect(route($this->moduleBasicRoute . '.index'));
636
        }
637
    }
638
639
    /**
640
     * Remove the specified resource from storage.
641
     *
642
     * @param  int  $id
643
     * @return Response
644
     */
645
646
    public function destroy($id) {
647
        /**
648
         * Delete the setting
649
         */
650
        $modelClass = $this->modelClass;
651
        $modelClass::destroy($id);
652
653
        /**
654
         * Redirect to index
655
         */
656
        return redirect(route($this->moduleBasicRoute . '.index'));
657
    }
658
659
}
660