<?php
/**
 * 2022 Anvanto
 *
 * NOTICE OF LICENSE
 *
 * This file is not open source! Each license that you purchased is only available for 1 wesite only.
 * If you want to use this file on more websites (or projects), you need to purchase additional licenses. 
 * You are not allowed to redistribute, resell, lease, license, sub-license or offer our resources to any third party.
 *
 *  @author Anvanto <anvantoco@gmail.com>
 *  @copyright  2022 Anvanto
 *  @license    Valid for 1 website (or project) for each purchase of license
 *  International Registered Trademark & Property of Anvanto
 */

include_once _PS_MODULE_DIR_.'anblog/loader.php';

class anblogsitemapModuleFrontController extends ModuleFrontController
{
    public function initContent()
    { 
        if(!(Configuration::get(anblog::PREFIX . 'enable_google_sitemap'))){
            Tools::redirect('index.php?controller=404');
        }

        if (Tools::isSubmit('id_lang')){
            $this->getSitemapAction();
        } else {
            $this->getMainSitemapAction();
        }
    }    
    
    public function getMainSitemapAction()
    {
        $context = Context::getContext()->language;
        $languages = Language::getLanguages();

        $sitemapLinks = [];
        foreach ($languages as $language){
            $sitemapLinks[$language['iso_code']] = $this->context->link->getModuleLink('anblog', 'sitemap', [], true, $language['id_lang']) . '';
        }

        $this->context->smarty->assign('anblogsitemap', ['sitemapLinks' => $sitemapLinks]);

        header('Content-Type:text/xml; charset=utf-8');
        echo $this->module->display($this->module->name, 'sitemap_old.tpl');
        die;
    }

    public function getSitemapAction()
    {
        $posts = $this->getPosts();
        $categories = $this->getCategories();

        $lastmod = date('Y-m-d');

        $this->context->smarty->assign('anblogsitemap', [
            'posts' => $posts,
            'categories' => $categories,
            'lastmod' => $lastmod,
            'linkSiteMap' => $this->context->link->getModuleLink('anblog', 'sitemap', [], true)
        ]);
    
        header('Content-Type:text/xml; charset=utf-8');
        echo $this->module->display($this->module->name, 'sitemap_old.tpl');
        die;
    }

    public function getPosts()
    {
        $helper = AnblogHelper::getInstance();
        $config = AnblogConfig::getInstance();

        $blogs = AnblogBlog::getListBlogs(
            null,
            Context::getContext()->language->id,
            0,
            'all',
            'id_anblog_blog',
            'DESC',
            array(),
            true
        );

        foreach ($blogs as $key => $blog) {
            
            $blog = AnblogHelper::buildBlog($helper, $blog, 'anblog_listing_leading_img', $config);
            $blogs[$key] = $blog;
        }

        return $blogs;
    }

    public function getCategories()
    {
        $categories = Anblogcat::getCategories();
        $helper = AnblogHelper::getInstance();

        foreach ($categories as $key => $category) {
            $category['thumb'] = '';
            if ($category['image'] !=''){
                $category['thumb'] = _PS_BASE_URL_ ._ANBLOG_BLOG_IMG_URI_.'c/'.$category['image'];
            }
            $category['category_link'] = $helper->getBlogCatLink(['rewrite' => $category['link_rewrite'], 'id' => $category['id_anblogcat']]);
            $categories[$key] = $category;
        }
        return $categories;
    }

    
}