<?php
header('Cache-Control: no cache'); //no cache
session_cache_limiter('private_no_expire'); // works

// SYMFONY CALCULATOR CONNECTION
$calculatorFirstWpUrl = get_permalink(6);
$calculatorSecondWpUrl = get_permalink(15);
$calculatorThirdWpUrl = get_permalink(17);
$calculatorFourthWpUrl = get_permalink(882);
$calculatorSymfonyFirstStepUrl = get_site_url() . '/symfony/web/order/';
$calculatorSymfonySecondStepUrl = get_site_url() . '/symfony/web/order/second';
$calculatorSymfonyThirdStepUrl = get_site_url() . '/symfony/web/order/third';
$calculatorSymfonyFourStepUrl = get_site_url() . '/symfony/web/order/four';

if (have_posts()) {
    if (get_the_id() == 653 || get_the_id() == 6 || get_the_id() == 15 || get_the_id() == 17 || get_the_id() == 882 || get_post_type() == 'adwords') {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        if (get_the_id() == 653 || get_the_id() == 6 || get_post_type() == 'adwords') {
            if (get_post_type() == 'adwords') {
                $adwordPost = get_post();
                $calculatorSymfonyFirstStepUrl .= '?' . http_build_query(
                        array(
                            'loading' => $adwordPost->port_zaladunkowy,
                            'unloading' => $adwordPost->port_wyladunkowy
                        ));
            }

            curl_setopt($ch, CURLOPT_URL, $calculatorSymfonyFirstStepUrl);

            $symfonyOutput = curl_exec($ch);
            curl_close($ch);

            // edit form url to wp url
            $symfonyOutput = str_replace('//first-step-url//', $calculatorSecondWpUrl, $symfonyOutput);
        } elseif (get_the_id() == 15) {
            $_POST['initial'] = true;
            curl_setopt($ch, CURLOPT_URL, $calculatorSymfonyFirstStepUrl);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
            $symfonyOutput = curl_exec($ch);
            curl_close($ch);

            // validate
            if ($symfonyOutput == '//not-valid//') {
                header('Location: ' . $calculatorFirstWpUrl);
                die;
            }

            // edit form url to wp url
            $symfonyOutput = str_replace('//second-step-url//', $calculatorThirdWpUrl, $symfonyOutput);
        } elseif (get_the_id() == 17) {
            $_POST['initial'] = true;
            curl_setopt($ch, CURLOPT_URL, $calculatorSymfonySecondStepUrl);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
            $symfonyOutput = curl_exec($ch);
            curl_close($ch);

            $symfonyOutput = str_replace('//rules-url//', get_permalink(184), $symfonyOutput);

            // validate
            if ($symfonyOutput == '//not-valid//') {
                header('Location: ' . $calculatorFirstWpUrl);
                die;
            }
        }
        elseif (get_the_id() == 882) {
            $_POST['initial'] = true;
            curl_setopt($ch, CURLOPT_URL, $calculatorSymfonyFourStepUrl);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
            $symfonyOutput = curl_exec($ch);
            curl_close($ch);

            $symfonyOutput = str_replace('//rules-url//', get_permalink(184), $symfonyOutput);

            // validate
            if ($symfonyOutput == '//not-valid//') {
                header('Location: ' . $calculatorFirstWpUrl);
                die;
            }
        }
    }
}

get_header();

if (is_search()) {
    require('page/search-result.php');
} elseif (get_the_id() == 6 || get_post_type() == 'adwords') {
    require('page/home.php');
} elseif (get_the_id() == 15) {
    require('page/calculator.php');
} elseif (get_the_id() == 17) {
    require('page/calculator2.php');
} elseif (get_the_id() == 882) {
    require('page/calculator3.php');
} elseif (get_the_id() == 24 || get_post_type() == 'faq') {
    require('page/faq.php');
} elseif (get_the_id() == 74 || get_post_type() == 'slownik_pojec') {
    require('page/glossary.php');
} elseif (get_the_id() == 43) {
    require('page/contact.php');
} elseif (get_the_id() == 1181) {
    require('page/blog.php');
} elseif (get_the_id() == 653) {
    require('page/kalkulator-iframe.php');
} elseif (get_post_type() == 'post') {
    require('page/post.php');
} elseif (get_post_type() == 'page') {
    require('page/article.php');
}

get_footer();