1<?php
2/**
3 * Copyright since 2007 PrestaShop SA and Contributors
4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Open Software License (OSL 3.0)
9 * that is bundled with this package in the file LICENSE.md.
10 * It is also available through the world-wide-web at this URL:
11 * https://opensource.org/licenses/OSL-3.0
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@prestashop.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to https://devdocs.prestashop.com/ for more information.
21 *
22 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
23 * @copyright Since 2007 PrestaShop SA and Contributors
24 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25 */
26
27function generate_ntree()
28{
29    $categories = Db::getInstance()->executeS('SELECT id_category, id_parent FROM '._DB_PREFIX_.'category ORDER BY id_parent ASC, position ASC');
30    $categoriesArray = array();
31    if (is_array($categories)) {
32        foreach ($categories as $category) {
33            $categoriesArray[(int)$category['id_parent']]['subcategories'][(int)$category['id_category']] = 1;
34        }
35    }
36    $n = 1;
37    generate_ntree_subTree($categoriesArray, 1, $n);
38}
39
40function generate_ntree_subTree(&$categories, $id_category, &$n)
41{
42    $left = (int)$n++;
43    if (isset($categories[(int)$id_category]['subcategories'])) {
44        foreach (array_keys($categories[(int)$id_category]['subcategories']) as $id_subcategory) {
45            generate_ntree_subTree($categories, (int)$id_subcategory, $n);
46        }
47    }
48    $right = (int)$n++;
49
50    Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'category
51		SET nleft = '.(int)$left.', nright = '.(int)$right.'
52		WHERE id_category = '.(int)$id_category.' LIMIT 1');
53}
54