<?php
/**
  * Copyright ETS Software Technology Co., Ltd
 *
 * NOTICE OF LICENSE
 *
 * This file is not open source! Each license that you purchased is only available for 1 website only.
 * If you want to use this file on more websites (or projects), you need to purchase additional licenses.
 * You are not allowed to redistribute, resell, lease, license, sub-license or offer our resources to any third party.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future.
 *
 * @author ETS Software Technology Co., Ltd
 * @copyright  ETS Software Technology Co., Ltd
 * @license    Valid for 1 website (or project) for each purchase of license
 */

if (!defined('_PS_VERSION_')) { exit; }

function add_attribute_position()
{
    $groups = Db::getInstance()->executeS('SELECT DISTINCT `id_attribute_group` FROM `' . _DB_PREFIX_ . 'attribute`');
    if (is_array($groups) && count($groups)) {
        $query = '';
        foreach ($groups as $group) {
            $attributes = Db::getInstance()->executeS('SELECT * FROM `' . _DB_PREFIX_ . 'attribute` WHERE `id_attribute_group` = ' . (int)($group['id_attribute_group']));
            $pos = 0;
            if (is_array($attributes) && count($attributes)) {
                foreach ($attributes as $attribute) {
                    $query .= '
					UPDATE `' . _DB_PREFIX_ . 'attribute`
					SET `position` = ' . $pos++ . '
					WHERE `id_attribute` = ' . (int)$attribute['id_attribute'] . '
					AND `id_attribute_group` = ' . (int)$attribute['id_attribute_group'] . ';';

                }
            }
        }
        if ($query)
            Db::getInstance()->execute($query);
    }
}
