<?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(); ?>

<div class="subpage articles">
    <?php require(get_template_directory() . '/subpage-top.php'); ?>

    <div id="blog" class="content">
        <h1>Blog - drobnicamorska.pl</h1>
        
        
            <div id="posty">
                <main>

                    <?php

                    global $wp_query;
                    $wp_query = new WP_Query(array(
                        'posts_per_page' => 3,
                        'paged' => getPaginationPageNumber()
                    ));

                    if (have_posts()) {
                        while (have_posts()) {
                            the_post();
                    ?>
                        <div class="post">
                            <a href="<?php the_permalink(); ?>"><div class="post-thumbnail"><?php the_post_thumbnail( 'full' ); ?></div></a>
                            <div class="blog-title"><p><?php the_category( ', ' ); ?> <br><?php the_tags( 'Tagi: ', ', ', '<br />' ); ?>
                            <a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a><p></div>

                            <p><?php echo get_excerpt(500); ?>...</p>
                            <div class="form-group buttons"><a href="<?php the_permalink(); ?>"><button class="button orange"> Czytaj dalej<div class="arrow-right"></div></button></a></div><div class="orange-square bottom"></div>
                        </div>            
                </main>       
                <?php
                }
                pagination_nav();}
                ?>
            </div>
            <?php get_sidebar('blog'); ?>  
        
    </div>
    <?php require(get_template_directory() . '/subpage-calculator.php'); ?>
    </div>
<?php
get_footer();