aboutsummaryrefslogtreecommitdiff
path: root/_functions/loadJSON.php
blob: c8540dcb7aa1823da4d3fbcb936139b4dacd9265 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php

function loadHomepageJSON(string $fileName): string
{
    // Get the file contents and decode JSON
    $fileContents = file_get_contents($fileName);
    $data = json_decode($fileContents);

    // Loop through the JSON object
    $stack = [];
    $metadata = '';
    foreach ($data as $postObject) {
        // Only load published posts
        if ($postObject->published == 'Yes') {
            // Add relevant metadata to the output template
            $year = date_format(date_create($postObject->created), "Y");
            if (!in_array($year, $stack)) {
                // If any posts have been created in a list, close the existing <ul> and open a new <ul> after the heading
                if (!empty($stack)) {
                    $metadata .= '</ul>';
                }
                $metadata .= '<h2 class="text-center">' . $year . '</h2><ul class="blog-posts">';
                array_push($stack, $year);
            }
            $metadata .= '<li><time datetime=' . date_format(date_create($postObject->created), "Y-m-d") . '>' . date_format(date_create($postObject->created), "Y-m-d") . '</time><a href=' . $postObject->link . ' aria-label="Read ' . $postObject->title . ' blog post">' . $postObject->title . '</a></li>';
        }
    }
    $metadata .= '</ul>';
    return $metadata;
}

function loadCategoryJSON(string $fileName): string
{
    // Get the file contents and decode JSON
    $fileContents = file_get_contents($fileName);
    $data = json_decode($fileContents);

    // Sort the JSON data by the `tag`, then by `create`.
    // I had to sort a -> b to get an "A-Z" sort for `tag`,
    // but b -> a to get a "first-last" sort for `create`.
    uasort($data, function ($a, $b) {
        return strcmp($a->tag, $b->tag) ?: strcmp($b->created, $a->created);
    });

    // Loop through the JSON object
    $stack = [];
    $metadata = '';
    foreach ($data as $postObject) {
        // Only load published posts
        if ($postObject->published == 'Yes') {
            // Add relevant metadata to the output template
            $category = str_replace('-', ' ', $postObject->tag);
            if (!in_array($category, $stack)) {
                // If any posts have been created in a list, close the existing <ul> and open a new <ul> after the heading
                if (!empty($stack)) {
                    $metadata .= '</ul>';
                }
                $metadata .= '<h2 id=' . $category . ' class="text-center">' . ucwords($category) . '</h2><ul class="blog-posts">';
                array_push($stack, $category);
            }

            $metadata .= '<li><time datetime=' . date_format(date_create($postObject->created), "Y-m-d") . '>' . date_format(date_create($postObject->created), "Y-m-d") . '</time><a href=' . $postObject->link . ' aria-label="Read ' . $postObject->title . ' blog post">' . $postObject->title . '</a></li>';
        }
    }
    $metadata .= '</ul>';
    return $metadata;
}

function loadPostJSON(string $fileName, string $query)
{
    // Get the file contents and decode JSON
    $fileContents = file_get_contents($fileName);
    $data = json_decode($fileContents);

    // Loop through the JSON object
    foreach ($data as $postObject) {
        // If this function is called to get a specific post's metadata, return the whole object
        if ($postObject->link == $GLOBALS['fullDomain'] . "/post/" . $query) {
            return $postObject;
        }
    }
}

function loadCommentJSON(string $fileName, string $query = null): string
{
    // Set up an empty comment section
    $commentSection = '';

    // Load the file and decode the JSON
    $fileContents = file_get_contents($fileName);
    $data = json_decode($fileContents);

    // Sort comments by date - latest comments will appear at the top
    uasort($data, function ($a, $b) {
        return strcmp($b->timestamp, $a->timestamp);
    });

    // Loop through all comments add to returned string if it matches criteria
    foreach ($data as $commentObject) {
        if (($query !== null) && ($commentObject->postURL == $GLOBALS['fullDomain'] . "/post/" . $query)) {
            $securedHTML = parseMarkdown(null, $commentObject->comment);
            $commentSection .= '<div class="user-comment"><div class="row"><label>Timestamp:</label><p>' . $commentObject->timestamp . '</p></div><div class="row"><label>Name:</label><p>' . $commentObject->username . '</p></div><div class="row markdown"><label>Comment:</label><div class="comment-markdown">' . $securedHTML . '</div></div></div>';
        } else if ($query == null) {
            $securedHTML = parseMarkdown(null, $commentObject->comment);
            $pageURL = str_replace($GLOBALS['fullDomain'] . '/post/', '', $commentObject->postURL);
            $postObject = loadPostJSON('_data/metadata.json', $pageURL);
            $commentSection .= '<div class="user-comment"><div class="row"><label>Post:</label><a href="' . $commentObject->postURL . '#comments">' . $postObject->title . '</a></div><div class="row"><label>Timestamp:</label><p>' . $commentObject->timestamp . '</p></div><div class="row"><label>Name:</label><p>' . $commentObject->username . '</p></div><div class="row markdown"><label>Comment:</label><div class="comment-markdown">' . $securedHTML . '</div></div></div>';
        }
    }
    return $commentSection;
}