Jump to content

Back Office Custom Menu Sections


Recommended Posts

Hello Everyone!

I'm working on a module where I created a custom top level menu (called Administration). Under it, are up to 2 level sub menu items, like this:
 

Administration

-- Son

---- GrandSon

 

This works and is shown in the back office. The problem is that the permissions don't work. I mean, when I create/modify a profile, every checkbox I click related to the custom tabs get reset, hence I cannot restrict employees/profiles from accessing these menus.

If anyone knows a better way of creating these menus (or if you have and can share some code), please let me know :).

I'm using PrestaShop 1.7.7.0 and using this code (based on ps_linklist):


$sections = [
    'TOP' => [
        'name' => 'Admin',
        'parent' => 0,
        'position' => 1,
        'children' => [
            'SubParent1' => [
                'name' => 'SubParent1_Child',
                'children' => [
                    [
                        'name' => 'Sub', // One name for all langs
                        'className' => 'SubParent1_GrandSon',
                        'route' => 'the_route_name',
                    ]
                ],
            ],
            'SubParent2' => [
                'name' => 'SubParent2_Child',
                'children' => [
                    [
                        'name' => 'Sub Child',
                        'className' => 'SubParent2_GrandSon',
                        'route' => 'the_route_name'
                    ]
                ]
            ]
        ],
    ],
];

foreach ($this->sections as $tabName => $tab) {
    $parent = $this->installTab($tabName, $tab['name'], $tab['parent'], null, null, $tab['position']);

    if (!$parent) {
        return false;
    }

    /*
    * HACK: force the position to 1 to keep it below the home item.
    */
    Db::getInstance()->update('tab', ['position' => 1], "id_tab = $parent");
    $position = 0;

    foreach ($tab['children'] as $key => $child) {
        if (isset($child['children'])) {
            // This is a sub parent
            $icon = $this->getValueOrNull($child, 'icon');
            $subparent = $this->installTab($key, $child['name'], $parent, null, null, 0, $icon);

            if (!$subparent) {
                return false;
            }

            $subpos = 0;

            foreach ($child['children'] as $subchild) {
                $module = $this->getValueOrNull($subchild, 'module');
                $route = $this->getValueOrNull($subchild, 'route');
                $icon = $this->getValueOrNull($subchild, 'icon');
                $install = $this->installTab($subchild['className'], $subchild['name'], $subparent, $route, $module, $subpos, $icon);

                if (!$install) {
                    return false;
                }

                ++$subpos;
            }
        } else {
            $module = $this->getValueOrNull($child, 'module');
            $route = $this->getValueOrNull($child, 'route');
            $install = $this->installTab($child['className'], $child['name'], $parent, $route, $module, $position);

            if (!$install) {
                return false;
            }

            ++$position;
        }
    }
}
    
function installTab(string $className, string $displayName, int $parent = 0, string $route = null,
                                string $module = null, int $position = 0, string $icon = null)
{
    $query = ['className' => $className, 'idParent' => $parent];

    if (!is_null($route)) {
        $query['routeName'] = $route;
    }

    $found = $this->tabRepository->findOneBy($query);

    if (!is_null($found)) {
        return $found->id;
    }

    $tab = new Tab();
    $tab->class_name = $className;
    $tab->name = [];

    foreach (Language::getLanguages() as $lang) {
        $tab->name[$lang['id_lang']] = $displayName;
    }

    $tab->id_parent = $parent;
    $tab->position = $position;
    $tab->module = PharmaFacil_BackOffice::MODULE_NAME;

    if (!is_null('route')) {
        $tab->route_name = $route;
    }

    if (!is_null($icon)) {
        $tab->icon = $icon;
    }

    if ($tab->add()) {
        return (int) $tab->id;
    }

    return null;
}

Thanks for reading!

Regards!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...