HEX
Server: Apache
System: Linux srv13.cpanelhost.cl 3.10.0-962.3.2.lve1.5.38.el7.x86_64 #1 SMP Thu Jun 18 05:28:41 EDT 2020 x86_64
User: cca63905 (4205)
PHP: 7.3.20
Disabled: NONE
Upload Files
File: /home4/cca63905/public_html/nueva/modules/wnetsecurity/src/Installer/TabInstaller.php
<?php
/**
 * Copyright since 2014 Waynet Sp. z o.o.
 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.md.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/OSL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to kontakt@waynet.pl so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to https://devdocs.prestashop-project.org/ for more information.
 *
 * @author    Waynet Sp. z o.o. <kontakt@waynet.pl>
 * @copyright since 2014 Waynet Sp. z o.o.
 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
 */

declare(strict_types=1);

namespace Waynet\Security\Installer;

class TabInstaller implements InstallerInterface
{
    private const SUFFIX = '_MTR';

    private $languages;

    public function __construct(array $languages = null)
    {
        $this->languages = $languages ?? \Language::getLanguages(false);
    }

    public function install(\Module $module): bool
    {
        if (!is_callable([$module, 'getTabs']) || \Tools::version_compare(_PS_VERSION_, '1.7.1', '>=')) {
            return true;
        }

        foreach ($module->getTabs() as $tabDetails) {
            if (!$this->installTab($module, $tabDetails)) {
                return false;
            }
        }

        return true;
    }

    public function uninstall(\Module $module): bool
    {
        if (\Tools::version_compare(_PS_VERSION_, '1.7.1', '>=')) {
            return true;
        }

        $tabs = \Tab::getCollectionFromModule($module->name);

        return array_reduce(iterator_to_array($tabs), static function (bool $result, \Tab $tab): bool {
            return $tab->delete() && $result;
        }, true);
    }

    private function installTab(\Module $module, array $tabDetails): bool
    {
        if (!isset($tabDetails['class_name'])) {
            throw new \InvalidArgumentException('Expected "class_name" in tab data');
        }

        if (\Tab::getIdFromClassName($tabDetails['class_name'])) {
            return true;
        }

        $tab = new \Tab();
        $tab->active = $tabDetails['visible'] ?? true;
        $tab->class_name = $tabDetails['class_name'];
        $tab->module = $module->name;
        $tab->name = $this->getTabNames($tabDetails['name'] ?? $module->name);
        $tab->icon = $tabDetails['icon'] ?? null;
        $tab->id_parent = $this->findParentId($tabDetails);

        return $tab->add();
    }

    /** @param string[]|string $names */
    private function getTabNames($names): array
    {
        $translatedNames = [];

        foreach ($this->languages as $language) {
            if (!is_array($names)) {
                $translatedNames[$language['id_lang']] = $names;
            } elseif (array_key_exists($language['locale'], $names)) {
                $translatedNames[$language['id_lang']] = $names[$language['locale']];
            } elseif (array_key_exists($language['language_code'], $names)) {
                $translatedNames[$language['id_lang']] = $names[$language['language_code']];
            } elseif (array_key_exists($language['iso_code'], $names)) {
                $translatedNames[$language['id_lang']] = $names[$language['iso_code']];
            } else {
                $translatedNames[$language['id_lang']] = reset($names);
            }
        }

        return $translatedNames;
    }

    private function findParentId(array $tabDetails): int
    {
        $parentId = 0;

        if (!empty($tabDetails['parent_class_name'])) {
            $parentId = (int) \Tab::getIdFromClassName($tabDetails['parent_class_name'] . self::SUFFIX);
            if (!$parentId) {
                $parentId = (int) \Tab::getIdFromClassName($tabDetails['parent_class_name']);
            }
        } elseif ($tabDetails['visible'] ?? true) {
            $parentId = (int) \Tab::getIdFromClassName('DEFAULT');
        }

        return 0 === $parentId ? $parentId : $this->duplicateParentIfAlone($parentId);
    }

    private function duplicateParentIfAlone(int $parentId): int
    {
        if ($this->hasChildren($parentId)) {
            return $parentId;
        }

        $currentTab = new \Tab($parentId);
        $newTab = clone $currentTab;
        $newTab->id = null;
        $newTab->id_parent = $currentTab->id_parent;
        $newTab->class_name = $currentTab->class_name . self::SUFFIX;
        $newTab->add();

        $newTab->position = $currentTab->position;
        $newTab->update();

        $currentTab->id_parent = $newTab->id;
        $currentTab->update();

        return (int) $newTab->id;
    }

    private function hasChildren(int $parentId): bool
    {
        $collection = (new \PrestaShopCollection(\Tab::class))
            ->where('id_parent', '=', $parentId);

        return 0 !== count($collection);
    }
}