Backend\Core\Engine\Navigation::cleanup PHP Method

cleanup() private method

Clean the navigation
private cleanup ( array $navigation ) : array
$navigation array The navigation array.
return array
    private function cleanup(array $navigation)
    {
        foreach ($navigation as $key => $value) {
            $allowedChildren = array();
            $allowed = true;
            if (!isset($value['url']) || !isset($value['label'])) {
                $allowed = false;
            }
            list($module, $action) = explode('/', $value['url']);
            $module = \SpoonFilter::toCamelCase($module);
            $action = \SpoonFilter::toCamelCase($action);
            if (!Authentication::isAllowedModule($module)) {
                $allowed = false;
            }
            if (!Authentication::isAllowedAction($action, $module)) {
                $allowed = false;
            }
            if (isset($value['children']) && is_array($value['children']) && !empty($value['children'])) {
                foreach ($value['children'] as $keyB => $valueB) {
                    $allowed = true;
                    $allowedChildrenB = array();
                    if (!isset($valueB['url']) || !isset($valueB['label'])) {
                        $allowed = false;
                    }
                    list($module, $action) = explode('/', $valueB['url']);
                    $module = \SpoonFilter::toCamelCase($module);
                    $action = \SpoonFilter::toCamelCase($action);
                    if (!Authentication::isAllowedModule($module)) {
                        $allowed = false;
                    }
                    if (!Authentication::isAllowedAction($action, $module)) {
                        $allowed = false;
                    }
                    // has children
                    if (isset($valueB['children']) && is_array($valueB['children']) && !empty($valueB['children'])) {
                        // loop children
                        foreach ($valueB['children'] as $keyC => $valueC) {
                            $allowed = true;
                            if (!isset($valueC['url']) || !isset($valueC['label'])) {
                                $allowed = false;
                            }
                            list($module, $action) = explode('/', $valueC['url']);
                            $module = \SpoonFilter::toCamelCase($module);
                            $action = \SpoonFilter::toCamelCase($action);
                            if (!Authentication::isAllowedModule($module)) {
                                $allowed = false;
                            }
                            if (!Authentication::isAllowedAction($action, $module)) {
                                $allowed = false;
                            }
                            if (!$allowed) {
                                unset($navigation[$key]['children'][$keyB]['children'][$keyC]);
                                continue;
                            } elseif (!in_array($navigation[$key]['children'][$keyB]['children'][$keyC], $allowedChildrenB)) {
                                // store allowed children
                                $allowedChildrenB[] = $navigation[$key]['children'][$keyB]['children'][$keyC];
                            }
                        }
                    }
                    if (!$allowed && empty($allowedChildrenB)) {
                        // error occurred and no allowed children on level B
                        unset($navigation[$key]['children'][$keyB]);
                        continue;
                    } elseif (!in_array($navigation[$key]['children'][$keyB], $allowedChildren)) {
                        // store allowed children on level B
                        $allowedChildren[] = $navigation[$key]['children'][$keyB];
                    }
                    // assign new base url for level B
                    if (!empty($allowedChildrenB)) {
                        $navigation[$key]['children'][$keyB]['url'] = $allowedChildrenB[0]['url'];
                    }
                }
            }
            // error occurred and no allowed children
            if (!$allowed && empty($allowedChildren)) {
                unset($navigation[$key]);
                continue;
            } elseif (!empty($allowedChildren)) {
                $allowed = true;
                list($module, $action) = explode('/', $allowedChildren[0]['url']);
                if (!Authentication::isAllowedModule($module)) {
                    $allowed = false;
                }
                if (!Authentication::isAllowedAction($action, $module)) {
                    $allowed = false;
                }
                if ($allowed) {
                    $navigation[$key]['url'] = $allowedChildren[0]['url'];
                } else {
                    $child = reset($navigation[$key]['children']);
                    $navigation[$key]['url'] = $child['url'];
                }
            }
        }
        return $navigation;
    }