summaryrefslogtreecommitdiff
path: root/includes/keywords.php
blob: 270d9056d1cead7bafea9110f81a5e73275b32c7 (plain)
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
<?php

function getKeyWords() {
    $actions = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/actions.json"), true);
    $toys    = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/toys.json"), true);
    $pages   = [];

    foreach ($actions as $action) {
        $base = strtolower($action["name"]);
        $addKeywords = $action["keywords"] ?? [];
        $keywords = [
            $base,
            ucfirst($base),
            ucwords($base)
        ];

        for ($i = 0; $i < strlen($base); $i++) {
            $keywords[] = substr($base, 0, $i) . strtoupper(substr($base, $i, 1)) . substr($base, $i + 1, strlen($base) - $i - 1);

            for ($j = 0; $j < strlen($base); $j++) {
                $keywords[] = substr($base, 0, $i) . strtoupper(substr($base, $i,$j)) . substr($base, $i + $j, strlen($base) - $i - $j);
            }
        }

        foreach ($addKeywords as $keyword) {
            $keywords[] = $keyword;
            $keywords[] = ucfirst($keyword);
            $keywords[] = ucwords($keyword);

            for ($i = 0; $i < strlen($keyword); $i++) {
                $keywords[] = substr($keyword, 0, $i) . strtoupper(substr($keyword, $i, 1)) . substr($keyword, $i + 1, strlen($keyword) - $i - 1);

                for ($j = 0; $j < strlen($keyword); $j++) {
                    $keywords[] = substr($keyword, 0, $i) . strtoupper(substr($keyword, $i, $j)) . substr($keyword, $i + $j, strlen($keyword) - $i - $j);
                }
            }
        }

        $pages[$action["id"]] = [
            "keywords" => array_unique($keywords),
            "link" => "/-/actions/$action[id]"
        ];
    }

    foreach ($toys as $toy) {
        $base = strtolower($toy["name"]);
        $addKeywords = $toy["keywords"] ?? [];
        $keywords = [
            $base,
            ucfirst($base),
            ucwords($base)
        ];

        for ($i = 0; $i < strlen($base); $i++) {
            $keywords[] = substr($base, 0, $i) . strtoupper(substr($base, $i, 1)) . substr($base, $i + 1, strlen($base) - $i - 1);

            for ($j = 0; $j < strlen($base); $j++) {
                $keywords[] = substr($base, 0, $i) . strtoupper(substr($base, $i, $j)) . substr($base, $i + $j, strlen($base) - $i - $j);
            }
        }

        foreach ($addKeywords as $keyword) {
            $keywords[] = $keyword;
            $keywords[] = ucfirst($keyword);
            $keywords[] = ucwords($keyword);

            for ($i = 0; $i < strlen($keyword); $i++) {
                $keywords[] = substr($keyword, 0, $i) . strtoupper(substr($keyword, $i, 1)) . substr($keyword, $i + 1, strlen($keyword) - $i - 1);

                for ($j = 0; $j < strlen($keyword); $j++) {
                    $keywords[] = substr($keyword, 0, $i) . strtoupper(substr($keyword, $i, $j)) . substr($keyword, $i + $j, strlen($keyword) - $i - $j);
                }
            }
        }

        $pages[$toy["id"]] = [
            "keywords" => array_unique($keywords),
            "link" => "/-/toys/$toy[id]"
        ];
    }

    $keywords = [];
    foreach ($pages as $page) {
        foreach ($page["keywords"] as $keyword) {
            $keywords[] = [
                "keyword" => $keyword,
                "link" => $page["link"]
            ];
        }
    }

    uasort($keywords, function ($a, $b) {
        return strlen($b["keyword"]) - strlen($a["keyword"]);
    });

    return $keywords;
}

function replaceKeyWords(string $input): string {
    $keywords = getKeyWords();

    foreach ($keywords as $data) {
        $keyword = $data["keyword"];
        $url = $data["link"];

        $input = str_replace($keyword, "<a href='$url'>$keyword</a>", $input);
    }

    return $input;
}