<?php
/**
 * Copyright since 2007 PrestaShop SA and Contributors
 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License version 3.0
 * that is bundled with this package in the file LICENSE.md.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/AFL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
 * @copyright Since 2007 PrestaShop SA and Contributors
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
 */
if (!defined('_PS_VERSION_')) {
    exit;
}

class SpmreviewsadvRssModuleFrontController extends ModuleFrontController
{

    public function initContent()
    {

        $_name = "spmreviewsadv";


        include_once(_PS_MODULE_DIR_.$_name.'/classes/spmreviewsadvhelp.class.php');
        $obj_spmreviewsadvhelp = new spmreviewsadvhelp();





        $cookie = Context::getContext()->cookie;

        $id_lang = (int)$cookie->id_lang;
        $data_language = $obj_spmreviewsadvhelp->getfacebooklib($id_lang);
        $rss_title =  Configuration::get($_name.'rssname_'.$id_lang);
        $rss_description =  Configuration::get($_name.'rssdesc_'.$id_lang);

        $_http_host = Tools::getShopDomainSsl(true, true).__PS_BASE_URI__;


        // Lets build the page
        $rootURL = $_http_host."feeds/";
        $latestBuild = date("r");




        // Lets define the the type of doc we're creating.
        header('Content-Type:text/xml; charset=utf-8');
        $createXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";


        if(Configuration::get($_name.'rsson') == 1){



            $createXML .= "<rss version=\"0.92\">\n";
            $createXML .= "<channel>
	<title><![CDATA[".str_replace('&','&amp;', $rss_title)."]]></title>
	<link>$rootURL</link>
	<description>".str_replace('&','&amp;', $rss_description)."</description>
	<lastBuildDate>$latestBuild</lastBuildDate>
	<docs>http://backend.userland.com/rss092</docs>
	<language>".$data_language['rss_language_iso']."</language>
	<image>
			<title><![CDATA[".$rss_title."]]></title>
			<url>".$_http_host."img/logo.jpg</url>
			<link>$_http_host</link>
	</image>
";

            $data_rss_items = $obj_spmreviewsadvhelp->getItemsForRSS();

//echo "<pre>"; var_dump($data_rss_items); exit;

            foreach($data_rss_items['items'] as $_item)
            {
                $page = $_item['page'];
                $description = $_item['seo_description'];
                $title = $_item['title'];
                $img = $_item['img'];

                $createXML .= $obj_spmreviewsadvhelp->createRSSFile($title,$description,$page, $img);
            }
            $createXML .= "</channel>\n </rss>";
// Finish it up
        }

        echo $createXML;
        exit;
    }

}