wizard.php 7.45 KB
Newer Older
dmorley's avatar
dmorley committed
1 2 3 4 5 6 7 8 9 10 11
<?php

/**
 * help users find a pod with Q&A wizard
 */

declare(strict_types=1);

use Poduptime\PodStatus;
use RedBeanPHP\R;

12
require_once __DIR__ . '/boot.php';
dmorley's avatar
dmorley committed
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 112 113 114 115 116

$page         = (int) ($_GET['page'] ?? 1);
$softwarename = $_GET['softwarename'] ?? null;
$country      = $_GET['country'] ?? null;
$language     = $_GET['language'] ?? null;

$iso = new Matriphe\ISO639\ISO639;

?>
<script>
    $(document).ready(function () {
        $('.wizardnext2').on('click', function () {
            var software = $('input:radio[name="software"]:checked').val();
            if (software) {
                $.get('wizard.php?page=2&softwarename=' + software, function (html) {
                    $('.wizard').replaceWith(html);
                });
            } else {
                $('.wizardnext2').text('Select software, then. Next..');
            }
        });
        $('.wizardnext3').on('click', function () {
            var country = $('input:radio[name="country"]:checked').val();
            if (country) {
                $.get('wizard.php?page=3&softwarename=<?php echo $softwarename; ?>&country=' + country, function (html) {
                    $('.wizard').replaceWith(html);
                });
            } else {
                $('.wizardnext3').text('Select Country, then. Next..');
            }
        });
        $('.wizardnext4').on('click', function () {
            var language = $('input:radio[name="language"]:checked').val();
            if (language) {
                $.get('wizard.php?page=4&softwarename=<?php echo $softwarename; ?>&country=<?php echo $country; ?>&language=' + language, function (html) {
                    $('.wizard').replaceWith(html);
                });
            } else {
                $('.wizardnext4').text('Select Language, then. Next..');
            }
        });
        $('.suggestpod').on('click', function () {
            $('.suggestpod').text('Loading');
            $.featherlight.close();
            var columns = [];
            columns[3] = '<?php echo $softwarename; ?>';
            var month = $('input:text[name="month"]').val();
            if (month) {
                columns[13] = '>= ' + month;
            } else {
                columns[13] = '>= 0';
            }
            columns[18] = '<?php echo $country; ?>';
            columns[21] = '<?php echo $language; ?>';
            $('table')
                .trigger('refreshColumnSelector', ['columns', [3, 4, 13, 18, 21, 22, 28]])
                .trigger('search', [columns])
                .trigger('update', [true]);
        });
        $('#month').ionRangeSlider();
    });
</script>
<div class="container wizard">
    <h4>Pod Wizard</h4>
<?php
if ($page === 1) {
    echo <<<EOF

    <div class="row m-1 p-1">
        <div class="col-6 p-2">Software we recommend:</div>
        <div class="col-8">
            <label>
                <input type="radio" name="software" value="diaspora" aria-describedby="diasporahelp"/> diaspora*<br>
                <p id="diasporahelp" class="form-text text-muted">
                    Instead of everyone’s data being held on huge central servers owned by a large organization, diaspora* exists on independently run servers (“pods”) all over the world. You choose which pod to register with, and you can then connect seamlessly with the diaspora* community worldwide.
                </p>
            </label>
            <label>
                <input type="radio" name="software" value="hubzilla" aria-describedby="hubzillahelp"/> Hubzilla<br>
                <p id="hubzillahelp" class="form-text text-muted">
                    Hubzilla is a free and open source set of web applications and services, called a "hub", that can connect to other hubs in a decentralised network, "the grid", providing communications, identity, and access control services. It allows anybody to publicly or privately publish content via "channels", which are the fundamental, secured identities that provide authentication independently of the hubs.
                </p>
            </label>
            <label>
                <input type="radio" name="software" value="friendica" aria-describedby="friendicahelp"/> Friendica<br>
                <p id="freindicahelp" class="form-text text-muted">
                    Decentralized architecture with no central authority or ownership. Relationships can be made across any compatible system, creating a network of Internet scale made up of smaller sites. Seamless wall-to-wall posts and remote comments, even across different network nodes.
                </p>
            </label>
        </div>
    </div>
    <button type="button" class="wizardnext2 btn btn-info">Next</button>

EOF;
} elseif ($page === 2) {
    $countries = data('country', $softwarename);
    // Sort country names correctly.
    uasort($countries, function ($c1, $c2) {
        return country($c1)->getName() <=> country($c2)->getName();
    });

    echo '<div class="row m-1 p-1"><div class="col-9">Countries we found pods in:</div><div class="col-8 p-3">';
    foreach ($countries as $country) {
        printf(
117
            '<label class="m-0"><input class="ml-2" type="radio" name="country" value="%1$s" /> %2$s %3$s</label><br>',
dmorley's avatar
dmorley committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
            $country,
            country($country)->getName(),
            country($country)->getEmoji()
        );
    }
    echo '</div></div><button type="button" class="wizardnext3 btn btn-info">Next</button>';
} elseif ($page === 3) {
    $languages = data('detectedlanguage', $softwarename, $country);
    // Sort language names correctly.
    uasort($languages, function ($l1, $l2) use ($iso) {
        return $iso->languageByCode1($l1) <=> $iso->languageByCode1($l2);
    });

    echo '<div class="row m-1 p-1"><div class="col-9">Languages we found:</div><div class="col-8 p-3">';
    foreach ($languages as $language) {
        printf(
            '<label><input class="ml-2" type="radio" name="language" value="%1$s" /> %2$s</label><br>',
135
            strtoupper($language),
dmorley's avatar
dmorley committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
            $iso->languageByCode1($language)
        );
    }
    echo '</div></div><button type="button" class="wizardnext4 btn btn-info">Next</button>';
} elseif ($page === 4) {
    $months = data('monthsmonitored', $softwarename, $country, $language);
    $min    = min($months);
    $max    = max($months);
    if ($min === $max) {
        echo '<div class="row m-1 p-1"><div class="col-9"></div><div class="col-8">';
        printf('<input type="text" name="month" value="%d" hidden>', $min);
    } else {
        echo '<div class="row m-1 p-1"><div class="col-9">Months Online Minimum:</div><div class="col-8">';
        printf('<input type="text" name="month" id="month" data-min="%d" data-max="%d" data-step="1">', $min, $max);
    }
    echo '</div></div><button type="button" class="suggestpod btn btn-info">Suggest a Pod</button>';
}


function data($column, $software = null, $country = null, $language = null)
{
    try {
        $sql    = "
            SELECT DISTINCT {$column}
            FROM pods
            WHERE status < ?
            AND signup
            AND softwarename = ?
        ";
        $params = [PodStatus::SYSTEM_DELETED, $software];

        if ($country) {
            $sql      .= ' AND country = ?';
            $params[] = $country;
        }
        if ($language) {
            $sql      .= ' AND detectedlanguage = ?';
            $params[] = $language;
        }
        $data = R::getAll($sql, $params);
    } catch (\RedBeanPHP\RedException $e) {
        die('Error in SQL query: ' . $e->getMessage());
    }
    return array_filter(array_column($data, $column));
}