<?php
// -------------------------------------------
// kirby template FOR jannikbeyerstedt.de

// copyright: Jannik Beyerstedt | https://jannikbeyerstedt.de
// license: http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 License
// -------------------------------------------

$base_url   = $site->find('blog')->url();
function tags($base_url, $article) {
  $tags = explode(',',(string)$article->tags());

  if (!empty($tags[0])) {
    $return_val = '<span class="tags">';
    foreach ($tags as $tag) {
      $url = $base_url.'/tag:'.$tag;
      $return_val .= '<a href="'.$url.'">' . $tag . '</a> ';
    }
    $return_val .= '</span>';
    return $return_val;
  } else {
    return '';
  }
}

$articles   = $page->children()->listed()->flip();

$title      = $page->title()->html();

if($tag = param('tag')) {
  $articles = $articles->filterBy('tags', $tag, ',');
  $title   .= ' - '.$tag;
}

$articles   = $articles->paginate(6);
$pagination = $articles->pagination();

snippet('blog-htmlhead');
snippet('base/cont-header');
?>

<main class="section blog">
  <div class="container">

    <h1><a href="<?php echo $base_url ?>"><?php echo $title ?></a></h1>
    <?php echo $page->text()->kirbytext(); ?>

<?php if($pagination->hasPages() && $pagination->page() != 1): ?>
    <nav class="pagination">
      <?php if($pagination->hasNextPage()): ?>
      <a class="button older" href="<?php echo $pagination->nextPageURL() ?>">&lsaquo; älter</a>
      <?php endif ?>
      <span class="num-pages">Seite <?php echo $pagination->page() ?> von <?php echo $pagination->pages() ?></span>
      <?php if($pagination->hasPrevPage()): ?>
      <a class="button newer" href="<?php echo $pagination->prevPageURL() ?>">neuer &rsaquo;</a>
      <?php endif ?>
    </nav>
<?php endif ?>


<?php foreach($articles as $a) {
  echo snippet('blogexcerpt',["article"=>$a,"length"=>200,"base_url"=>$base_url]);
}?>


<?php if($pagination->hasPages()): ?>
    <nav class="pagination">
      <?php if($pagination->hasNextPage()): ?>
      <a class="button older" href="<?php echo $pagination->nextPageURL() ?>">&lsaquo; älter</a>
      <?php endif ?>
      <span class="num-pages">Seite <?php echo $pagination->page() ?> von <?php echo $pagination->pages() ?></span>
      <?php if($pagination->hasPrevPage()): ?>
      <a class="button newer" href="<?php echo $pagination->prevPageURL() ?>">neuer &rsaquo;</a>
      <?php endif ?>
    </nav>
<?php endif ?>

  </div>
</main>

<?php snippet('base/cont-footer') ?>
<?php snippet('base/html-end', []) ?>