1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Page module controller |
4
|
|
|
* |
5
|
|
|
* Controller for module Page |
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 Illuminate\Http\Request; |
18
|
|
|
use Illuminate\Support\Facades\Validator; |
19
|
|
|
use Illuminate\Support\Facades\Auth; |
20
|
|
|
|
21
|
|
View Code Duplication |
class PageController extends AdminModuleController { |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor |
25
|
|
|
* |
26
|
|
|
* @var Request $request |
27
|
|
|
*/ |
28
|
|
|
public function __construct(Request $request) { |
|
|
|
|
29
|
|
|
parent::__construct(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Add lookup tables |
33
|
|
|
*/ |
34
|
|
|
$this->middleware('add.lookup.tables:User', ['only' => ['create', 'edit']]); |
35
|
|
|
$this->middleware('add.lookup.tables:PageCategory', ['only' => ['create', 'edit']]); |
36
|
|
|
$this->middleware('add.published.at', ['only' => ['store', 'update']]); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Other tables |
40
|
|
|
*/ |
41
|
|
|
$this->middleware('media.image.select:Image', ['only' => ['create', 'edit']]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Validation rules |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $arValidationArray = [ |
50
|
|
|
'name' => 'required|max:255|unique:page,name', |
51
|
|
|
'meta_title' => 'max:255', |
52
|
|
|
'meta_description' => 'max:255', |
53
|
|
|
'meta_keywords' => 'max:255', |
54
|
|
|
'text' => 'max:1000000', |
55
|
|
|
'url' => 'required|max:255|unique:page,url', |
56
|
|
|
'author_name' => 'max:255', |
57
|
|
|
'published_at' => 'date', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Associate relationships to other table |
62
|
|
|
* |
63
|
|
|
* @param object $object |
64
|
|
|
* @param Request $request |
65
|
|
|
*/ |
66
|
|
|
public function associateRelationships($object, Request $request) { |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Validate user ID, if failed set to actual authorized user |
70
|
|
|
*/ |
71
|
|
|
$validator = Validator::make($request->all(), [ |
72
|
|
|
'user_id' => 'required|integer|min:1|exists:users,id', |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Validator fails - add actual authorized user |
77
|
|
|
*/ |
78
|
|
|
if ($validator->fails()) { |
79
|
|
|
|
80
|
|
|
$object->users()->associate(Auth::user()->id); |
|
|
|
|
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Validator OK - save it |
86
|
|
|
*/ |
87
|
|
|
else { |
88
|
|
|
|
89
|
|
|
$object->users()->associate($request->input('user_id')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Validate image ID, if failed set to default |
94
|
|
|
*/ |
95
|
|
|
$validator = Validator::make($request->all(), [ |
96
|
|
|
'image_id' => 'integer|min:1|exists:image,id', |
97
|
|
|
]); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Validator fails - nothing to do |
101
|
|
|
*/ |
102
|
|
|
if ($validator->fails()) { |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Validator OK - save it |
109
|
|
|
*/ |
110
|
|
|
else { |
111
|
|
|
|
112
|
|
|
$object->images()->associate($request->input('image_id')); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Associate relationships to other table, where ID if object must be present |
118
|
|
|
* |
119
|
|
|
* @param object $object |
120
|
|
|
* @param Request $request |
121
|
|
|
*/ |
122
|
|
|
public function associateRelationshipsWithID($object, Request $request) { |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Validate page category ID, if failed set to default |
126
|
|
|
*/ |
127
|
|
|
if($request->has('pagecategory_id')){ |
128
|
|
|
$validIDs = []; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* For each category |
132
|
|
|
*/ |
133
|
|
|
foreach ($request->input('pagecategory_id') as $pagecategory_id) { |
134
|
|
|
|
135
|
|
|
$arrayForValidator = ['pagecategory_id' => $pagecategory_id]; |
136
|
|
|
|
137
|
|
|
$validator = Validator::make($arrayForValidator, [ |
138
|
|
|
'pagecategory_id' => 'required|integer|min:1|exists:pagecategory,id', |
139
|
|
|
]); |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Validator fails - nothing to do |
143
|
|
|
*/ |
144
|
|
|
if ($validator->fails()) { |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Validator OK - save it |
150
|
|
|
*/ |
151
|
|
|
else { |
152
|
|
|
|
153
|
|
|
$validIDs[] = $pagecategory_id; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Sync to pivot |
159
|
|
|
*/ |
160
|
|
|
$object->pagecategories()->sync($validIDs); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
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.