<?php

function compose_view($article_id = null)
{
    if (!empty($article_id)) {
        $conn = get_connection();
        $stmt = $conn->prepare('SELECT * FROM article WHERE article_id = ?');
        $stmt->bind_param('i', $article_id);
        $stmt->execute();
        $article = $stmt->get_result()->fetch_assoc();

        $stmt = $conn->prepare('SELECT tag_id FROM article_tags WHERE article_id = ?');
        $stmt->bind_param('i', $article_id);
        $stmt->execute();
        $result = $stmt->get_result();
        $tags = [];
        while ($row = $result->fetch_assoc())
            $tags[] = $row['tag_id'];

        $title = $article['title'];
        $excerpt = $article['excerpt'];
        $existing_markdown = read_file_one_string($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/article.md');
    }

    $view = '
      <div class="grid-2x1">
      ' . (!empty($existing_markdown) ? form($article_id, $title, $excerpt, $existing_markdown, $tags) : form()) . '
      ' . preview() . '
      </div>
    ';

    return $view;
}
