<?php
/*
 * to do
 * https://dzen.ru/help/news/export-content/export.html
 * https://yandex.ru/dev/turbo/doc/rss/simple-rss.html
 *
 */
use yii\helpers\Html;
header('Content-Type: application/rss+xml; charset=utf-8');
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;

?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL ?>
<rss version="2.0"
	xml:base="https://www.amic.ru/"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
>
    <channel>
<?php if (isset($channel['title'])) : ?>
        <title><![CDATA[<?= $channel['title']; ?>]]></title>
<?php endif; ?>
<?php if (isset($channel['link'])) : ?>
        <link><?= Html::encode($channel['link']); ?></link>
<?php endif; ?>
<?php if (isset($channel['description'])) : ?>
        <description><![CDATA[<?= $channel['description']; ?>]]></description>
<?php endif; ?>
<?php if (isset($channel['language'])) : ?>
        <language><?= $channel['language']; ?></language>
<?php endif; ?>
    <image>
      <url>https://www.amic.ru/img/amic-logo.svg</url>
      <link>https://www.amic.ru</link>
      <title>Новости Алтайского края</title>
    </image>
<?php foreach ($items as $item): ?>
<?php if (isset($item['url']) ) : ?>
        <item>
			<guid><?= rtrim(Html::encode($channel['link']),'/'); ?><?= Html::encode($item['url']); ?></guid>
            <link><?= rtrim(Html::encode($channel['link']),'/'); ?><?= Html::encode($item['url']); ?></link>
            <pubDate><?= date( "r", strtotime( $item['updated_at'] ) ); ?></pubDate>
<?php if (isset($item['title']) ) : ?>
            <title><![CDATA[<?= $item['title']; ?>]]></title>
<?php endif; ?>
<?php if (isset($item['description']) ) : ?>
            <description>
            <![CDATA[<?= $item['description']; ?>]]>
            </description>
<?php endif; ?>
<?php if (isset($item['newscontent']) ) : ?>
            <content:encoded>
            <![CDATA[
				<p><?= ($item['description']) ? $item['description'] : ''; ?></p>
				<?= $item['newscontent']; ?>
            ]]>
            </content:encoded>
<?php endif; ?>
<?php if (isset($item['image'])) :?>
<?
				$r = explode(".", $item['image']);
				if( is_array($r) ){
					if( array_pop($r) == 'webp' ){
						$mtype = "image/webp";
					}else{
						$mtype = "image/jpeg";
					}
				}
?>
			<enclosure url="<?=$item['image']?>" type="<?=$mtype?>" />
<?php endif; ?>
<?
	   preg_match_all('/src="([^"]+\.(jpg|png)).*"/i', $item['content'], $ax );
       foreach ( $ax[1] as $aa ){
            if( !preg_match( "/(\/manager\/|small)/", $aa) ){
				$r = explode(".", $aa);
				if( is_array($r) ){
					if( array_pop($r) == 'png' ){
						$mtype = "image/png";
					}else{
						$mtype = "image/jpeg";
					}
				}
    ?>
			<enclosure url="<?=$aa?>" type="<?=$mtype?>" />
    <?
            }
        }
?>
		<dc:creator>ИА Амител</dc:creator>
        </item>
<?php endif; ?><?php endforeach; ?>
    </channel>
</rss>
