|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
|
6
|
|
|
use Illuminate\Routing\Controller as BaseController; |
|
7
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
|
8
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
|
9
|
|
|
|
|
10
|
|
|
class Controller extends BaseController |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
use AuthorizesRequests, DispatchesJobs, ValidatesRequests; |
|
13
|
|
|
|
|
14
|
|
|
public function getRelativeIds($input, $relativeType) { |
|
|
|
|
|
|
15
|
|
|
$relativeIds = []; |
|
16
|
|
|
if (isset($input['relatives'])) { |
|
17
|
|
|
$relatives = $input['relatives']; |
|
18
|
|
|
if (isset($relatives[$relativeType])) { |
|
19
|
|
|
if (isset($relatives[$relativeType]['new'])) { |
|
20
|
|
|
foreach($relatives[$relativeType]['new'] as $relativeInput) { |
|
|
|
|
|
|
21
|
|
|
$relativeIds[] = $relativeInput['id']; |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
if (isset($relatives[$relativeType]['old'])) { |
|
25
|
|
|
foreach($relatives[$relativeType]['old'] as $relativeInput) { |
|
|
|
|
|
|
26
|
|
|
$relativeIds[] = $relativeInput['id']; |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
return $relativeIds; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function shiftFirstLevelArrayKeysToBottom(array $nestedArray) { |
|
|
|
|
|
|
35
|
|
|
$expandedArray = $this->expandNestedArraysIntoKeyListAndValue($nestedArray); |
|
36
|
|
|
$rotatedArray = $this->rotateKeys($expandedArray, 1); |
|
37
|
|
|
$mergedArray = $this->mergeExpandedTrees($rotatedArray); |
|
38
|
|
|
return $mergedArray; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
protected function addKeyAsFinalIndex($finalKey, $array) { |
|
|
|
|
|
|
42
|
|
|
if (!is_array($array)) { |
|
43
|
|
|
return [$finalKey => $array]; |
|
44
|
|
|
} else { |
|
45
|
|
|
$newArray = []; |
|
46
|
|
|
foreach($array as $key => $value) { |
|
|
|
|
|
|
47
|
|
|
$newArray[$key] = $this->addKeyAsFinalIndex($finalKey, $value); |
|
48
|
|
|
} |
|
49
|
|
|
return $newArray; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function expandNestedArraysIntoKeyListAndValue($nestedArray) { |
|
|
|
|
|
|
54
|
|
|
if (!is_array($nestedArray)) { |
|
55
|
|
|
$expandedArray = [ |
|
56
|
|
|
[ |
|
57
|
|
|
'keys' => [], |
|
58
|
|
|
'value' => $nestedArray, |
|
59
|
|
|
] |
|
60
|
|
|
]; |
|
61
|
|
|
return $expandedArray; |
|
62
|
|
|
} else { |
|
63
|
|
|
$expandedArray = []; |
|
64
|
|
|
foreach($nestedArray as $key => $value) { |
|
|
|
|
|
|
65
|
|
|
$subArray = $this->expandNestedArraysIntoKeyListAndValue($value); |
|
66
|
|
|
foreach($subArray as $item) { |
|
|
|
|
|
|
67
|
|
|
array_unshift($item['keys'], $key); |
|
68
|
|
|
$expandedArray[] = $item; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
return $expandedArray; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function mergeExpandedTrees($expandedArray) { |
|
|
|
|
|
|
76
|
|
|
$mergedArray = []; |
|
77
|
|
|
foreach($expandedArray as $item) { |
|
|
|
|
|
|
78
|
|
|
$tail = &$mergedArray; |
|
79
|
|
|
$size = count($item['keys']); |
|
80
|
|
|
$i = 0; |
|
81
|
|
|
foreach($item['keys'] as $key) { |
|
|
|
|
|
|
82
|
|
|
$i = ($i + 1); |
|
83
|
|
|
//Check if this is the last key |
|
84
|
|
|
if ($i == ($size)) { |
|
85
|
|
|
if (!isset($tail[$key])) { |
|
86
|
|
|
$tail[$key] = $item['value']; |
|
87
|
|
|
} else if (!is_array($tail[$key])) { |
|
88
|
|
|
$value = $tail[$key]; |
|
89
|
|
|
$tail[$key] = [$value, $item['value']]; |
|
90
|
|
|
} else { |
|
91
|
|
|
array_push($tail[$key], $item['value']); |
|
92
|
|
|
} |
|
93
|
|
|
} else { |
|
94
|
|
|
//If this is not the last key, it needs to contain an array |
|
95
|
|
|
if (!isset($tail[$key])) { |
|
96
|
|
|
$tail[$key] = []; |
|
97
|
|
|
} else if (!is_array($tail[$key])) { |
|
98
|
|
|
$value = $tail[$key]; |
|
99
|
|
|
$tail[$key] = [$value]; |
|
100
|
|
|
} |
|
101
|
|
|
$tail = &$tail[$key]; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
return $mergedArray; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected function rotateKeys($expandedArray, $steps) { |
|
|
|
|
|
|
109
|
|
|
$rotatedArray = []; |
|
110
|
|
|
foreach($expandedArray as $item) { |
|
|
|
|
|
|
111
|
|
|
for ($i=0; $i<$steps; $i++) { |
|
112
|
|
|
array_push($item['keys'], array_shift($item['keys'])); |
|
113
|
|
|
} |
|
114
|
|
|
$rotatedArray[] = $item; |
|
115
|
|
|
} |
|
116
|
|
|
return $rotatedArray; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Formats the $content array into JSON. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string[] $content The array being returned in response. |
|
|
|
|
|
|
123
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
|
|
124
|
|
|
*/ |
|
125
|
|
|
protected function formatAjaxResponse(array $content): \Illuminate\Http\Response |
|
126
|
|
|
{ |
|
127
|
|
|
return response()->json($content); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|