<?php

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

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

        if (findModule('producers') && $this->checkRightfulness_2()) {
            $this->makeChanges_2();
            $changed = true;
        }

        if (findModule('products_variations') && $this->checkRightfulness_3()) {
            $this->makeChanges_3();
            $changed = true;
        }

        if (findModule('eshop_delivery') && $this->checkRightfulness_4()) {
            $this->makeChanges_4();
            $changed = true;
        }

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

        $this->printResult();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_1()
    {
        return $this->checkTableExists('parameters_producers');
    }

    // provest samotny upgrade
    public function makeChanges_1()
    {
        sqlQuery('CREATE TABLE '.getTableName('parameters_producers')." (
           `id_parameter` INT(10) UNSIGNED NOT NULL DEFAULT '0',
           `id_producer` INT(11) UNSIGNED NOT NULL DEFAULT '0',
           `weight` INT(11) NOT NULL DEFAULT '0',
           PRIMARY KEY (`id_parameter`,`id_producer`),
           KEY `weight` (`weight`),
           KEY `id_producer` (`id_producer`)
         ) ENGINE=InnoDB ");

        sqlQuery('ALTER TABLE '.getTableName('parameters_producers').'
           ADD CONSTRAINT `parameters_producers_ibfk_1` FOREIGN KEY (`id_parameter`) REFERENCES `parameters` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
           ADD CONSTRAINT `parameters_producers_ibfk_2` FOREIGN KEY (`id_producer`) REFERENCES `producers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE');

        $this->upgradeOK();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_2()
    {
        // return $this->checkColumnType('producers', 'descr', 'text') && $this->checkColumnType('producers', 'descr', 'mediumtext');
        return false;
    }

    // provest samotny upgrade
    public function makeChanges_2()
    {
        sqlQuery('ALTER TABLE '.getTableName('producers').' CHANGE `descr` `descr` TEXT NOT NULL');

        $this->upgradeOK();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_3()
    {
        return $this->checkColumnExists('products_variations', 'figure');
    }

    // provest samotny upgrade
    public function makeChanges_3()
    {
        sqlQuery('ALTER TABLE '.getTableName('products_variations')." ADD `figure` ENUM( 'Y', 'N' ) NOT NULL DEFAULT 'Y';");

        $this->upgradeOK();
    }

    // overi, jestli je opravnene provest upgrade
    public function checkRightfulness_4()
    {
        return $this->checkColumnExists('delivery_type', 'format');
    }

    // provest samotny upgrade
    public function makeChanges_4()
    {
        sqlQuery('ALTER TABLE '.getTableName('delivery_type').' ADD `format` VARCHAR(20) NULL DEFAULT NULL;');

        $this->upgradeOK();
    }
}
