<?php

class Upgrade25 extends Upgrade
{
    public function upgrade()
    {
        $changed = false;

        // jestlize je opravnene udelat upgrade, tak provest
        if (findModule('stock_in') && $this->checkRightfulness_1()) {
            $this->makeChanges_1();
            $changed = true;
        }

        // jestlize je opravnene udelat upgrade, tak provest
        if ($this->checkRightfulness_2()) {
            $this->makeChanges_2();
            $changed = true;
        }

        // jestlize je opravnene udelat upgrade, tak provest
        if ($this->checkRightfulness_3()) {
            $this->makeChanges_3();
            $changed = true;
        }

        // jestlize je opravnene udelat upgrade, tak provest
        if ($this->checkRightfulness_4()) {
            $this->makeChanges_4();
            $changed = true;
        }

        global $cfg;
        if (!empty($cfg['Order']['Flags'])) {
            // jestlize je opravnene udelat upgrade, tak provest
            if ($this->checkRightfulness_5()) {
                $this->makeChanges_5();
                $changed = true;
            }

            // jestlize je opravnene udelat upgrade, tak provest
            if ($this->checkRightfulness_6()) {
                $this->makeChanges_6();
                $changed = true;
            }
        }

        // jestlize je opravnene udelat upgrade, tak provest
        if ($this->checkRightfulness_7()) {
            $this->makeChanges_7();
            $changed = true;
        }

        if ($changed == false) {
            $this->noChanges();
        }

        $this->printResult();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_1()
    {
        return false;
    }

    // provest samotny upgrade
    public function makeChanges_1()
    {
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_2()
    {
        global $cfg;

        $type = '';
        $this->checkEnumExists('products', 'campaign', 'bžet', $type);

        foreach ($cfg['Products']['Flags'] as $name => $flag) {
            if (strstr($type, "'{$name}'") === false) {
                return true;
            }
        }
    }

    // provest samotny upgrade
    public function makeChanges_2()
    {
        global $cfg;

        $type = '';
        $this->checkEnumExists('products', 'campaign', 'bžet', $type);

        foreach ($cfg['Products']['Flags'] as $name => $flag) {
            if (strstr($type, "'{$name}'") === false) {
                $type = str_replace(')', ",'{$name}')", $type);
            }
        }

        sqlQuery('ALTER TABLE '.getTableName('products')." CHANGE `campaign` `campaign` {$type} NOT NULL");

        $this->upgradeOK();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_3()
    {
        return $this->checkConstraintWithColumnExists('products_related', 'products_related_ibfk_1', 'id_top_product');
    }

    // provest samotny upgrade
    public function makeChanges_3()
    {
        sqlQuery('DELETE pr FROM '.getTableName('products_related').' pr
	             LEFT JOIN '.getTableName('products').' p ON pr.id_top_product=p.id
	             WHERE p.id IS NULL');

        sqlQuery('ALTER TABLE '.getTableName('products_related').' CHANGE `id_top_product` `id_top_product` INT(11) NOT NULL');

        sqlQuery('ALTER TABLE '.getTableName('products_related').'
	           ADD CONSTRAINT `products_related_ibfk_1` FOREIGN KEY (`id_top_product`) REFERENCES `products` (`id`) ON DELETE CASCADE ON UPDATE CASCADE');

        $this->upgradeOK();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_4()
    {
        return $this->checkConstraintWithColumnExists('products_related', 'products_related_ibfk_2', 'id_rel_product');
    }

    // provest samotny upgrade
    public function makeChanges_4()
    {
        sqlQuery('DELETE pr FROM '.getTableName('products_related').' pr
	             LEFT JOIN '.getTableName('products').' p ON pr.id_rel_product=p.id
	             WHERE p.id IS NULL');

        sqlQuery('ALTER TABLE '.getTableName('products_related').' CHANGE `id_rel_product` `id_rel_product` INT(11) NOT NULL');

        sqlQuery('ALTER TABLE '.getTableName('products_related').'
	           ADD CONSTRAINT `products_related_ibfk_2` FOREIGN KEY (`id_rel_product`) REFERENCES `products` (`id`) ON DELETE CASCADE ON UPDATE CASCADE');

        $this->upgradeOK();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_5()
    {
        return $this->checkColumnExists('orders', 'flags');
    }

    // provest samotny upgrade
    public function makeChanges_5()
    {
        global $cfg;

        sqlQuery('ALTER TABLE '.getTableName('orders')." ADD `flags` SET('".join("','", array_keys($cfg['Order']['Flags']))."')  NOT NULL");

        sqlQuery('ALTER TABLE '.getTableName('orders').' ADD INDEX(`flags`)');

        $this->upgradeOK();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_6()
    {
        global $cfg;

        $type = '';
        $this->checkEnumExists('orders', 'flags', 'bžet', $type);

        foreach ($cfg['Order']['Flags'] as $name => $flag) {
            if (strstr($type, "'{$name}'") === false) {
                return true;
            }
        }
    }

    // provest samotny upgrade
    public function makeChanges_6()
    {
        global $cfg;

        $type = '';
        $this->checkEnumExists('orders', 'flags', 'bžet', $type);

        foreach ($cfg['Order']['Flags'] as $name => $flag) {
            if (strstr($type, "'{$name}'") === false) {
                $type = str_replace(')', ",'{$name}')", $type);
            }
        }

        sqlQuery('ALTER TABLE '.getTableName('orders')." CHANGE `flags` `flags` {$type} NOT NULL");

        $this->upgradeOK();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_7()
    {
        return $this->checkEnumExists('products', 'campaign', 'M');
    }

    // provest samotny upgrade
    public function makeChanges_7()
    {
        $type = '';
        $this->checkEnumExists('products', 'campaign', 'M', $type);

        $type = str_replace(')', ",'M')", $type);

        sqlQuery('ALTER TABLE '.getTableName('products')." CHANGE `campaign` `campaign` {$type} NOT NULL");

        $this->upgradeOK();
    }
}
