1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
use Illuminate\Http\Request; |
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Config; |
6
|
|
|
use Jenssegers\Mongodb\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
if (!function_exists('getTranslatedContent')) { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @param array $mlCollection |
12
|
|
|
* |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
|
|
function getTranslatedContent($mlCollection) |
16
|
|
|
{ |
17
|
|
|
//Get current Lang |
18
|
|
|
$cl = Config::get('app.locale'); |
19
|
|
|
|
20
|
|
|
if (is_array($mlCollection) && (array_key_exists('en_EN', $mlCollection) || array_key_exists('it_IT', $mlCollection) || !is_null($mlCollection || !isset($destination)))) { |
|
|
|
|
21
|
|
|
return $mlCollection[$cl]; |
22
|
|
|
} else { |
23
|
|
|
return ""; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if (!function_exists('cl')) { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @return string current Lang |
33
|
|
|
*/ |
34
|
|
|
function cl() |
35
|
|
|
{ |
36
|
|
|
//Get current Lang |
37
|
|
|
return Config::get('app.locale'); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (!function_exists('ml')) { |
42
|
|
|
//save a localized field |
43
|
|
|
/** |
44
|
|
|
* @param array $destination |
45
|
|
|
* @param string $input |
46
|
|
|
* |
47
|
|
|
* @return array ready to be saved |
48
|
|
|
*/ |
49
|
|
|
function ml($destination, $input) |
50
|
|
|
{ |
51
|
|
|
if (is_null($destination)) { |
|
|
|
|
52
|
|
|
$destination = array(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return array_merge($destination, array(cl() => $input)); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!function_exists('isML')) { |
60
|
|
|
function isML($value) |
61
|
|
|
{ |
62
|
|
|
if (array_key_exists('is-ml', $value)) { |
63
|
|
|
return $value['is-ml']; |
64
|
|
|
} else { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
if (!function_exists('isMD')) { |
72
|
|
|
function isMD($value) |
73
|
|
|
{ |
74
|
|
|
if (array_key_exists('is-md', $value)) { |
75
|
|
|
return $value['is-md']; |
76
|
|
|
} else { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
if (!function_exists('is_EM')) { |
82
|
|
|
function is_EM($value) |
83
|
|
|
{ |
84
|
|
|
if ($value === 'EmbedsMany') { |
85
|
|
|
return true; |
86
|
|
|
} else { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (!function_exists('is_EO')) { |
93
|
|
|
function is_EO($value) |
94
|
|
|
{ |
95
|
|
|
if ($value === 'EmbedsOne') { |
96
|
|
|
return true; |
97
|
|
|
} else { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!function_exists('is_HM')) { |
104
|
|
|
function is_HM($value) |
105
|
|
|
{ |
106
|
|
|
if ($value === 'HasMany') { |
107
|
|
|
return true; |
108
|
|
|
} else { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!function_exists('is_HO')) { |
115
|
|
|
function is_HO($value) |
116
|
|
|
{ |
117
|
|
|
if ($value === 'HasOne') { |
118
|
|
|
return true; |
119
|
|
|
} else { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (!function_exists('isEditable')) { |
126
|
|
|
function isEditable($value) |
127
|
|
|
{ |
128
|
|
|
if (array_key_exists('is-editable', $value)) { |
129
|
|
|
return $value['is-editable']; |
130
|
|
|
} else { |
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (!function_exists('hasTarget')) { |
137
|
|
|
function hasTarget($value) |
138
|
|
|
{ |
139
|
|
|
if (array_key_exists('has-target', $value)) { |
140
|
|
|
return $value['has-target']; |
141
|
|
|
} else { |
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
if (!function_exists('isFillable')) { |
147
|
|
|
function isFillable($value, $event) |
148
|
|
|
{ |
149
|
|
|
if ($event === 'add') { |
150
|
|
|
return true; |
151
|
|
|
} else { |
152
|
|
|
return isEditable($value); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (!function_exists('getRequestToBeSync')) { |
158
|
|
|
/** |
159
|
|
|
* @param $ref_id |
160
|
|
|
* @param $modelOnTarget |
161
|
|
|
* @param Request $request |
162
|
|
|
* |
163
|
|
|
* @return Request |
164
|
|
|
*/ |
165
|
|
|
function getRequestToBeSync($ref_id, $modelOnTarget, Request $request, $methodOnTarget) |
166
|
|
|
{ |
167
|
|
|
$new_req_embeded = new stdClass(); |
168
|
|
|
$model = new $modelOnTarget; |
169
|
|
|
$items = $model->getItems(); |
170
|
|
|
|
171
|
|
|
foreach ($items as $key => $item) { |
172
|
|
|
if ($key == "ref_id") { |
173
|
|
|
$new_req_embeded->$key = $ref_id; |
174
|
|
|
} else { |
175
|
|
|
$new_req_embeded->$key = $request->input($key); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$new_req = array(); |
180
|
|
|
$new_req[$methodOnTarget] = json_encode($new_req_embeded); |
181
|
|
|
$request = new Request; |
182
|
|
|
$request->merge($new_req); |
183
|
|
|
return $request; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (!function_exists('getPrimaryRequest')) { |
188
|
|
|
/** |
189
|
|
|
* @param String $request |
190
|
|
|
* |
191
|
|
|
* @return String |
192
|
|
|
*/ |
193
|
|
|
function getPrimaryRequest($request) |
194
|
|
|
{ |
195
|
|
|
if (!is_null($request)) { |
|
|
|
|
196
|
|
|
$arr = []; |
197
|
|
|
$categorylistdataJson = $request; |
198
|
|
|
$categorylistdataArr = json_decode($categorylistdataJson); |
199
|
|
|
if (empty($categorylistdataArr)) { |
200
|
|
|
return null; |
201
|
|
|
} else { |
202
|
|
|
//extract first array and create the primary category |
203
|
|
|
$primarycategory = ($categorylistdataArr[0]); |
204
|
|
|
$arr[] = $primarycategory; |
205
|
|
|
$out = json_encode($arr); |
206
|
|
|
return $out; |
207
|
|
|
} |
208
|
|
|
} else { |
209
|
|
|
return ""; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if (!function_exists('getAID')) { |
215
|
|
|
/** |
216
|
|
|
* @param String $request |
217
|
|
|
* |
218
|
|
|
* @return String |
219
|
|
|
*/ |
220
|
|
|
function getAID(Model $model) |
221
|
|
|
{ |
222
|
|
|
//Get Last Obj |
223
|
|
|
$q = $model->orderBy('created_at', 'desc')->first(); |
224
|
|
|
//check if Obj exist and increments numeric id |
225
|
|
|
if ($q == null) { |
226
|
|
|
$arr['autoincrement_id'] = 1; |
|
|
|
|
227
|
|
|
} else { |
228
|
|
|
$arr['autoincrement_id'] = $q->autoincrement_id + 1; |
229
|
|
|
} |
230
|
|
|
return $arr['autoincrement_id']; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if (!function_exists('processList')) { |
235
|
|
|
/** |
236
|
|
|
* @param array $array |
237
|
|
|
* |
238
|
|
|
* @return String |
239
|
|
|
*/ |
240
|
|
|
function processList($array) |
241
|
|
|
{ |
242
|
|
|
$final = []; |
243
|
|
|
for ($i = 0; $i < count($array); $i++) { |
|
|
|
|
244
|
|
|
$obj = []; |
245
|
|
|
if ($array[$i] != null) { |
246
|
|
|
$obj = ['label' => $array[$i], 'key' => $i]; |
247
|
|
|
$final[] = $obj; |
248
|
|
|
} else { |
249
|
|
|
$final[] = $obj; |
250
|
|
|
array_pop($final); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
return json_encode($final); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if (!function_exists('isRequestReadyToBeProcessed')) { |
258
|
|
|
function isRequestReadyToBeProcessed(Request $request) |
259
|
|
|
{ |
260
|
|
|
$requests = $request->all(); |
261
|
|
|
unset($requests['_token']); |
262
|
|
|
|
263
|
|
|
foreach ($requests as $key => $val) { |
264
|
|
|
$pos = strpos($key, "-"); |
265
|
|
|
if ($pos === false) { |
266
|
|
|
return false; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
return true; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
if (!function_exists('removeSubCollectionInput')) { |
274
|
|
|
function removeSubCollectionInput(Request $request) |
275
|
|
|
{ |
276
|
|
|
return $request; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
if (!function_exists('prepareRequest')) { |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param Request $request |
284
|
|
|
* @param array $additionalData |
285
|
|
|
* |
286
|
|
|
* @return Request |
287
|
|
|
*/ |
288
|
|
|
function prepareRequest(Request $request, array $additionalData) |
289
|
|
|
{ |
290
|
|
|
$request = $request->merge($additionalData); |
291
|
|
|
$additionalData = removeSubCollectionInput($request); |
292
|
|
|
$request = new Request; |
293
|
|
|
|
294
|
|
|
if (isRequestReadyToBeProcessed($request)) { |
295
|
|
|
return $request->merge($additionalData); |
|
|
|
|
296
|
|
|
} else { |
297
|
|
|
return prepareRequest($request, $additionalData); |
|
|
|
|
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
|
303
|
|
|
if (!function_exists('getArrayWithEmptyObj')) { |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param $model |
307
|
|
|
* |
308
|
|
|
* @return array |
309
|
|
|
*/ |
310
|
|
|
function getArrayWithEmptyObj($model, $is_EO, $is_EM) |
|
|
|
|
311
|
|
|
{ |
312
|
|
|
$arr = []; |
313
|
|
|
if ($is_EO) { |
314
|
|
|
$obj = new stdClass(); |
315
|
|
|
$embedObj = new $model; |
316
|
|
|
$EOitems = $embedObj->getItems(); |
317
|
|
|
|
318
|
|
|
//Current Obj Create |
319
|
|
|
foreach ($EOitems as $EOkey => $item) { |
320
|
|
|
$obj->$EOkey = null; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
$arr[] = $obj; |
324
|
|
|
}// $is_EM == empty array |
325
|
|
|
|
326
|
|
|
return $arr; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
if (!function_exists('getCounterForRelationships')) { |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @param $method |
334
|
|
|
* @param $is_EO |
335
|
|
|
* @param $is_EM |
336
|
|
|
* @param $i |
337
|
|
|
* |
338
|
|
|
* @return string |
339
|
|
|
*/ |
340
|
|
|
function getCounterForRelationships($method, $is_EO, $is_EM, $i) |
341
|
|
|
{ |
342
|
|
|
if ($method == "" || ($method !== "" && $is_EO) || ($method == "" && $is_EM)) { |
343
|
|
|
return ""; |
344
|
|
|
} else { |
345
|
|
|
return "-" . $i; |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: