<?php
/* version */
define('JOURNAL_VERSION', '2.3.3');


/* check if journal should be loaded */
$load_theme = !(defined('BLOCK_JOURNAL') && BLOCK_JOURNAL === true);

/* detect if is cli call */
if (php_sapi_name() === "cli") {
    $load_theme = false;
}

/* detect if is admin access */
if (Front::$IS_ADMIN) {
    $load_theme = false;
}

/* detect if is install access */
if (Front::$IS_INSTALLER) {
    $load_theme = false;
}

/* check if current theme is journal2 */
if (!Front::$IS_JOURNAL) {
    $load_theme = false;
}

/* block OpenCart 2.0 */
if (Front::$IS_JOURNAL && Front::$IS_OC2 && !defined('J2ENV')) {
    echo '
        <h3>Journal Installation Error</h3>
        <p>OpenCart 2.0 is not yet supported. We are working on this.</p>
    ';
    exit();
}

/* OpenCart 2.0 development */
if (Front::$IS_JOURNAL && Front::$IS_OC2 && !Front::$IS_ADMIN && !defined('J2ENV')) {
    global $config;
    $config->set('config_template', 'journal2_oc2');
}

/* check if OpenCart 2.0 system/engine/action.php is modified */
if ($load_theme && Front::$IS_OC2 && !defined('JOURNAL_ACTION_LOADED')) {
    echo '
        <h3>Journal Installation Error</h3>
        <p>Make sure you have uploaded all Journal files to your server and successfully replaced <b>system/modification/system/engine/action.php</b> file.</p>
        <p>You can find more information <a href="http://docs.digital-atelier.com/opencart/journal/#/settings/install" target="_blank">here</a>.</p>
    ';
    exit();
}

/* check if OpenCart 2.0 system/engine/loader.php is modified */
if ($load_theme && Front::$IS_OC2 && !defined('JOURNAL_LOADER_LOADED')) {
    echo '
        <h3>Journal Installation Error</h3>
        <p>Make sure you have uploaded all Journal files to your server and successfully replaced <b>system/modification/system/engine/loader.php</b> file.</p>
        <p>You can find more information <a href="http://docs.digital-atelier.com/opencart/journal/#/settings/install" target="_blank">here</a>.</p>
    ';
    exit();
}

/* check if Journal 1.x is active */
if ($load_theme) {
    foreach ($this->pre_action as $act) {
        if ($act->getMethod() === 'journal_cp') {
            echo '
                <h3>Journal Installation Error</h3>
                <p>Open <b>index.php</b> from the root directory of your setup and remove this line of code as it is not needed anymore in Journal2:</p>
                <pre>$controller->addPreAction(new Action(\'module/journal_cp\'));</pre>
            ';
            exit();
        }
    }
}

/* Check if Modules are enabled */
if ($load_theme && $this->registry->get('db')->query('show tables like "' . DB_PREFIX . 'journal2_config"')->num_rows === 0) {
    echo '
        <h3>Journal Installation Error</h3>
        <p>Journal2 module is not installed.</p>
    ';
    exit();
}

/* start Journal 2 engine */
if ($load_theme) {
    /* load journal2 */
    require_once(DIR_SYSTEM . 'journal2/classes/journal2.php');
    Journal2::startTimer('Engine');
    define('JOURNAL_INSTALLED', true);

    /* Get current route */
    $current_route = isset($this->registry->get('request')->get['route']) ? $this->registry->get('request')->get['route'] : null;

    /* Utils */
    require_once(DIR_SYSTEM . 'journal2/classes/journal2_utils.php');

    /* Load journal2 class */
    $journal2 = new Journal2();
    $this->registry->set('journal2', $journal2);

    /* Load Cache class */
    require_once(DIR_SYSTEM . 'journal2/classes/journal2_cache.php');
    $journal2->cache = new Journal2Cache($this->registry);

    /* Html Classes */
    require_once(DIR_SYSTEM . 'journal2/classes/journal2_html_classes.php');
    $journal2->html_classes = new Journal2HtmlClasses($this->registry);

    /* Add mobile / tablet class */
    if (Journal2Cache::$mobile_detect->isMobile()) {
        if (Journal2Cache::$mobile_detect->isTablet()) {
            $journal2->html_classes->addClass('tablet');
        } else {
            $journal2->html_classes->addClass('mobile');
        }
    } else {
        $journal2->html_classes->addClass('journal-desktop');
    }

    /* Route Parser */
    require_once(DIR_SYSTEM . 'journal2/classes/journal2_page.php');
    $journal2->page = new Journal2Page($this->registry, $journal2->html_classes);

    /* Load journal2 config */
    require_once(DIR_SYSTEM . 'journal2/classes/journal2_config.php');
    $journal2->config = new Journal2Config($this->registry);

    /* Load journal2 settings */
    require_once(DIR_SYSTEM . 'journal2/classes/journal2_settings.php');
    $journal2->settings = new Journal2Settings($this->registry);

    /* Load journal2 minifier */
    require_once(DIR_SYSTEM . 'journal2/classes/journal2_minifier.php');
    $journal2->minifier = new Journal2Minifier($journal2->cache);

    /* Google Fonts */
    require_once(DIR_SYSTEM . 'journal2/classes/journal2_google_fonts.php');
    $journal2->google_fonts = new Journal2GoogleFonts();

    /* Controllers */
    if ($current_route === 'module/journal2_popup/show') {
        $this->execute(new Action('module/journal2_popup/show'));
        die();
    }

    if ($current_route !== 'module/journal2_side_blocks/load') {
        $this->execute(new Action('journal2/settings'));
        if (!$journal2->cache->getDeveloperMode()) {
            if ($journal2->minifier->getMinifyCss()) {
                $this->execute(new Action('journal2/assets/css'));
            }
            if ($journal2->minifier->getMinifyJs()) {
                $this->execute(new Action('journal2/assets/js'));
            }
        }
    }

    if (!in_array($current_route, array('module/journal2_side_blocks/load', 'journal2/assets/js', 'journal2/assets/css', 'journal2/assets/outofstock', 'product/product/captcha', 'information/contact/captcha'))) {
        $this->execute(new Action('journal2/product_tabs'));
        $this->execute(new Action('journal2/product_tabs/enquiry'));
        $this->execute(new Action('journal2/modules'));
        if (Front::$IS_OC2) {
            $this->execute(new Action('journal2/menu/header',   'primary_menu'));
            $this->execute(new Action('journal2/menu/header',   'secondary_menu'));
            $this->execute(new Action('journal2/menu/mega',     'mega_menu'));
            $this->execute(new Action('journal2/menu/footer',   'footer_menu'));
        } else {
            $this->execute(new Action('journal2/menu/header',   (array('primary_menu'))));
            $this->execute(new Action('journal2/menu/header',   (array('secondary_menu'))));
            $this->execute(new Action('journal2/menu/mega',     (array('mega_menu'))));
            $this->execute(new Action('journal2/menu/footer',   (array('footer_menu'))));
        }

        $this->execute(new Action('journal2/snippets'));
        $this->execute(new Action('journal2/category/refine_images'));
    }

    Journal2::stopTimer('Engine');
}