<?php

class Upgrade28 extends UpgradeNew
{
    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_1()
    {
        return findModule(\Modules::MISSING_PRODUCTS) && $this->checkColumnExists('products_variations', 'in_store_min');
    }

    // provest samotny upgrade
    public function makeChanges_1()
    {
        try {
            sqlQuery('ALTER TABLE products ADD `in_store_min` INT(11) DEFAULT NULL');
        } catch (Exception $e) {
        }

        sqlQuery('ALTER TABLE products_variations ADD `in_store_min` INT(11) DEFAULT NULL');

        $this->upgradeOK();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_2()
    {
        return $this->checkColumnExists('sections_relation', 'position');
    }

    // provest samotny upgrade
    public function makeChanges_2()
    {
        sqlQuery('ALTER TABLE '.getTableName('sections_relation').' ADD `position` INT(11) NOT NULL DEFAULT 0');

        sqlQuery('UPDATE '.getTableName('sections_relation').' sr
				LEFT JOIN '.getTableName('sections').' s ON s.id=sr.id_section
				SET sr.`position`=COALESCE(s.menu_order, 0)');

        global $cfg;
        require_once $cfg['Path']['shared_version'].'admin/lists/SectionsList.php';
        foreach (sqlQuery('SELECT id FROM '.getTableName('sections').'') as $row) {
            SectionsList::orderTreeLevel($row['id']);
        }

        $this->upgradeOK();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_3()
    {
        return $this->checkColumnIsNull('sections_relation', 'id_topsection', true);
    }

    // provest samotny upgrade
    public function makeChanges_3()
    {
        sqlQuery('ALTER TABLE '.getTableName('sections_relation').' ADD INDEX(`id_section`)');

        sqlQuery('ALTER TABLE '.getTableName('sections_relation').' DROP PRIMARY KEY');

        sqlQuery('ALTER TABLE '.getTableName('sections_relation').' CHANGE `id_topsection` `id_topsection` INT(11) NULL DEFAULT NULL');

        sqlQuery('ALTER TABLE '.getTableName('sections_relation').' ADD UNIQUE( `id_section`, `id_topsection`)');

        sqlQuery('ALTER TABLE '.getTableName('sections_relation').' DROP INDEX `id_section`');

        sqlQuery('INSERT INTO '.getTableName('sections_relation').'
	           SELECT s.id, NULL
	           FROM '.getTableName('sections').' s
	           LEFT JOIN '.getTableName('sections_relation').' sr ON s.id=sr.id_section
	           WHERE sr.id_section IS NULL');

        sqlQuery('UPDATE '.getTableName('sections_relation').' sr
	           LEFT JOIN '.getTableName('sections').' s ON s.id=sr.id_topsection
	           SET id_topsection=NULL
	           WHERE s.id IS NULL');

        sqlQuery('ALTER TABLE '.getTableName('sections_relation').'
	           ADD CONSTRAINT `sections_ibfk_2` FOREIGN KEY (`id_topsection`) REFERENCES '.getTableName('sections').' (`id`) ON DELETE CASCADE ON UPDATE CASCADE');

        sqlQuery('ALTER TABLE '.getTableName('sections_relation').' DROP FOREIGN KEY `sections_ibfk_2`');

        $this->upgradeOK();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_4()
    {
        return !$this->checkColumnExists('sections', 'menu_figure', true);
    }

    // provest samotny upgrade
    public function makeChanges_4()
    {
        sqlQuery('ALTER TABLE '.getTableName('sections').' DROP `menu_figure`');

        $this->upgradeOK();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_5()
    {
        return !$this->checkColumnExists('sections', 'menu_order', true);
    }

    // provest samotny upgrade
    public function makeChanges_5()
    {
        sqlQuery('ALTER TABLE '.getTableName('sections').' DROP `menu_order`');

        $this->upgradeOK();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_6()
    {
        return $this->checkColumnIsNull('menu_links', 'parent', true);
    }

    // provest samotny upgrade
    public function makeChanges_6()
    {
        sqlQuery('ALTER TABLE '.getTableName('menu_links').' CHANGE `parent` `parent` INT(11) NULL DEFAULT NULL');

        sqlQuery('UPDATE '.getTableName('menu_links').' ml
	           LEFT JOIN '.getTableName('menu_links').' ml2 ON ml.parent=ml2.id
	           SET ml.parent=NULL
	           WHERE ml2.id IS NULL');

        sqlQuery('ALTER TABLE '.getTableName('menu_links').'
	           ADD CONSTRAINT `menu_links_ibfk_1` FOREIGN KEY (`parent`) REFERENCES '.getTableName('menu_links').' (`id`) ON DELETE CASCADE ON UPDATE CASCADE');

        $this->upgradeOK();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_7()
    {
        return $this->checkColumnExists('sections_relation', 'position');
    }

    // provest samotny upgrade
    public function makeChanges_7()
    {
        $SQL = sqlQuery('SELECT id_topsection FROM '.getTableName('sections_relation').' GROUP BY id_topsection');

        global $cfg;
        require_once $cfg['Path']['shared_version'].'admin/list/SectionsList.php';

        foreach ($SQL as $row) {
            SectionsList::orderTreeLevel($row['id_topsection']);
        }

        $this->upgradeOK();
    }
}
