| 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 |  |  |                 foreach ($relatives[$relativeType] as $relativeInput) { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |                     $relativeIds[] = $relativeInput['id']; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         return $relativeIds; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function shiftFirstLevelArrayKeysToBottom(array $nestedArray) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $expandedArray = $this->expandNestedArraysIntoKeyListAndValue($nestedArray); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $rotatedArray = $this->rotateKeys($expandedArray, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $mergedArray = $this->mergeExpandedTrees($rotatedArray); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return $mergedArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected function addKeyAsFinalIndex($finalKey, $array) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         if (!is_array($array)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             return [$finalKey => $array]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $newArray = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             foreach($array as $key => $value) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 $newArray[$key] = $this->addKeyAsFinalIndex($finalKey, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             return $newArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     protected function expandNestedArraysIntoKeyListAndValue($nestedArray) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (!is_array($nestedArray)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $expandedArray = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     'keys' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                     'value' => $nestedArray, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             return $expandedArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $expandedArray = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             foreach($nestedArray as $key => $value) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 $subArray = $this->expandNestedArraysIntoKeyListAndValue($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 foreach($subArray as $item) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     array_unshift($item['keys'], $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                     $expandedArray[] = $item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             return $expandedArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     protected function mergeExpandedTrees($expandedArray) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $mergedArray = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         foreach($expandedArray as $item) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $tail = &$mergedArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $size = count($item['keys']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $i = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             foreach($item['keys'] as $key) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $i = ($i + 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 //Check if this is the last key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 if ($i == ($size)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     if (!isset($tail[$key])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                         $tail[$key] = $item['value']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     } else if (!is_array($tail[$key])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                         $value = $tail[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                         $tail[$key] = [$value, $item['value']]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                         array_push($tail[$key], $item['value']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                     //If this is not the last key, it needs to contain an array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                     if (!isset($tail[$key])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                         $tail[$key] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     } else if (!is_array($tail[$key])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                         $value = $tail[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                         $tail[$key] = [$value]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     $tail = &$tail[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return $mergedArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     protected function rotateKeys($expandedArray, $steps) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $rotatedArray = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         foreach($expandedArray as $item) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             for ($i=0; $i<$steps; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 array_push($item['keys'], array_shift($item['keys'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $rotatedArray[] = $item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         return $rotatedArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * Formats the $content array into JSON. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param string[] $content The array being returned in response. | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @return \Illuminate\Http\Response | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     protected function formatAjaxResponse(array $content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return response()->json($content); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |  |