<?php declare(strict_types=1); ?>

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title><?php echo xss($viewModel->title()); ?></title>
    <link><?php echo xss($viewModel->link()); ?></link>
    <description><?php echo xss($viewModel->description()); ?></description>
    <managingEditor><?php echo xss($viewModel->siteEmail()); ?></managingEditor>
    <webMaster><?php echo xss($viewModel->webMaster()); ?></webMaster>
    <copyright><?php echo xss($viewModel->copyright()); ?></copyright>
    <atom:link href="<?php echo xss($viewModel->link()); ?>" rel="self" type="application/rss+xml" />
    <?php foreach ($viewModel->items() as $item): ?>
    <item>
      <?php if ($item->title()): ?>
      <title><?php echo xss($item->title()); ?></title>
      <?php endif; ?>
      <link><?php echo xss($item->url()); ?></link>
      <?php if ($item->teaser()): ?>
      <description>
        <![CDATA[<?php if (!empty($item->image())): ?><img src="<?php echo $item->cachedImage(400); ?>" /><?php endif; ?>
        <?php echo $item->teaserForDisplay(); ?>]]>
      </description>
      <?php endif; ?>
      <?php if (($item->media()) && is_readable(TFISH_MEDIA_PATH . $item->media())):
      $length = filesize(TFISH_MEDIA_PATH . $item->media());
      $mimetype = $viewModel->listMimetypes()[pathinfo($item->media(), PATHINFO_EXTENSION)]; ?>
      <enclosure url="<?php echo TFISH_ENCLOSURE_URL . xss($item->id()); ?>" length="<?php echo xss($length); ?>" type="<?php echo xss($mimetype); ?>" />
      <?php unset($length, $mimetype); endif; ?>
      <?php if ($item->submissionTime()): ?>
      <pubDate><?php echo xss(gmdate(DATE_RSS, (int) $item->submissionTime())); ?></pubDate>
      <?php endif; ?>
      <guid isPermaLink="true"><?php echo TFISH_PERMALINK_URL . '?id=' . xss($item->id()); ?></guid>
    </item>
    <?php endforeach; ?>
  </channel>
</rss>
