<?php

use Symfony\Component\Filesystem\Filesystem;

class ShopFoldersUpgrade extends UpgradeNew
{
    protected $priority = -1001;

    protected function isAllowed()
    {
        return !defined('UPGRADE_DB_ONLY');
    }

    public function check_webBundlesFolder()
    {
        $path = $this->getShopDir().'web';

        if (!file_exists($path)) {
            return true;
        }

        return false;
    }

    /** Create './shop/web' folder for bundles */
    public function upgrade_webBundlesFolder()
    {
        $path = $this->getShopDir().'web';

        if (!file_exists($path)) {
            mkdir($path);
            chmod($path, 0777);
        }
        $this->upgradeOK();
    }

    public function check_engineFolder()
    {
        global $cfg;

        $target = @readlink($this->getShopDir().'engine');

        return $target != '../engine';
    }

    /** Update './engine' link in web directory */
    public function upgrade_engineFolder()
    {
        global $cfg;

        $file = $this->getShopDir().'engine';
        $fs = new Filesystem();
        $fs->remove($file);
        symlink('../engine', $file);

        $this->upgradeOK();
    }

    public function check_staticDir()
    {
        if (isFunctionalTests() || findModule(\Modules::COMPONENTS)) {
            return false;
        }

        global $cfg;

        $target = @readlink($this->getShopDir().'static');

        return $target != "../{$cfg['Program']['version']['folder']}/web/templates/{$cfg['Path']['smarty_tpl']['theme']}/static/";
    }

    /** Update './static' link in web directory */
    public function upgrade_staticDir()
    {
        global $cfg;

        $file = $this->getShopDir().'static';
        $fs = new Filesystem();
        $fs->remove($file);
        symlink("../{$cfg['Program']['version']['folder']}/web/templates/{$cfg['Path']['smarty_tpl']['theme']}/static/", $file);

        $this->upgradeOK();
    }

    public function check_removeStaticDir()
    {
        if (!findModule(\Modules::COMPONENTS)) {
            return false;
        }

        return file_exists($this->getShopDir().'static');
    }

    /** Remove './static' link in web directory */
    public function upgrade_removeStaticDir()
    {
        $file = $this->getShopDir().'static';
        $fs = new Filesystem();
        $fs->remove($file);

        $this->upgradeOK();
    }

    public function check_commonFolder()
    {
        if (defined('TEST_SUITE')) {
            return false;
        }

        global $cfg;

        $target = @readlink(getcwd().'/'.$cfg['Path']['web_root'].'common');

        return $target != "../{$cfg['Program']['version']['folder']}/web/common/";
    }

    /** Update './common' link in web directory */
    public function upgrade_commonFolder()
    {
        global $cfg;

        $file = $cfg['Path']['web_root'].'common';
        $fs = new Filesystem();
        $fs->remove($file);
        symlink("../{$cfg['Program']['version']['folder']}/web/common/", $file);

        $this->upgradeOK();
    }

    public function check_adminStatic()
    {
        if (defined('TEST_SUITE')) {
            return false;
        }

        global $cfg;

        $target = @readlink($this->getShopDir().'admin/static');

        return $target != "../../{$cfg['Program']['version']['folder']}/admin/static/";
    }

    /** Update './static' link in admin directory */
    public function upgrade_adminStatic()
    {
        global $cfg;

        $file = $this->getShopDir().'admin/static';
        $fs = new Filesystem();
        $fs->remove($file);
        symlink("../../{$cfg['Program']['version']['folder']}/admin/static/", $file);

        $this->upgradeOK();
    }

    public function check_ckeditor()
    {
        $target = @readlink($this->getShopDir().'ckeditor');

        return $target != '../ckeditor4/';
    }

    /** Update './ckeditor' symlink */
    public function upgrade_ckeditor()
    {
        @unlink($this->getShopDir().'ckeditor');
        @symlink('../ckeditor4/', $this->getShopDir().'ckeditor');

        $this->upgradeOK();
    }
}
