<?php
/**
 * @link https://github.com/himiklab/yii2-sitemap-module
 * @copyright Copyright (c) 2014 HimikLab
 * @license http://opensource.org/licenses/MIT MIT
 */

namespace frontend\modules\sitemap;

use Yii;
use yii\base\InvalidConfigException;
use yii\base\Module;
use yii\caching\Cache;
use common\modules\languages\models\Languages;

/**
 * Yii2 module for automatically generating XML Sitemap.
 *
 * @author HimikLab
 * @package himiklab\sitemap
 */
class Sitemap extends Module
{
    public $controllerNamespace = 'frontend\modules\sitemap\controllers';

    /** @var int */
    public $cacheExpire = 86400;

    /** @var Cache|string */
    public $cacheProvider = 'cache';

    /** @var string */
    public $cacheKey = 'sitemap';

    /** @var boolean Use php's gzip compressing. */
    public $enableGzip = false;

    /** @var array */
    public $models = [];

    /** @var array */
    public $urls = [];

    public function init()
    {
        parent::init();

        if (is_string($this->cacheProvider)) {
            $this->cacheProvider = Yii::$app->{$this->cacheProvider};
        }

        if (!$this->cacheProvider instanceof Cache) {
            throw new InvalidConfigException('Invalid `cacheKey` parameter was specified.');
        }
    }

    /**
     * Build and cache a site map.
     * @return string
     * @throws \yii\base\InvalidConfigException
     */
    public function buildSitemap($links = false)
    {
        $urls = $this->urls;

        foreach ($this->models as $modelName) 
        {
            /** @var behaviors\SitemapBehavior $model */
            if (is_array($modelName)) 
            {
                $model = new $modelName['class'];
                if (isset($modelName['behaviors'])) {
                    $model->attachBehaviors($modelName['behaviors']);
                }
            } 
            else 
            {
                $model = new $modelName;
            }

            $urls = array_merge($urls, $model->generateSiteMap());
        }

        $langs = Languages::find()->all();

        $sitemapData = $this->createControllerByID('default')->renderPartial('index', [
            'urls' => $urls,
            'langs' => $langs,
            'links' => $links
        ]);

        $this->cacheProvider->set($this->cacheKey, $sitemapData, $this->cacheExpire);

        return $sitemapData;
    }
}
