wizard.php 7.58 KB
Newer Older
David Morley's avatar
David Morley 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';
David Morley's avatar
David Morley committed
13 14 15 16

$page         = (int) ($_GET['page'] ?? 1);
$softwarename = $_GET['softwarename'] ?? null;
$country      = $_GET['country'] ?? null;
David Morley's avatar
David Morley committed
17
$users        = $_GET['users'] ?? null;
David Morley's avatar
David Morley committed
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

$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 () {
David Morley's avatar
David Morley committed
45 46 47
            var users = $('input:text[name="users"]').val();
            if (users) {
                $.get('wizard.php?page=4&softwarename=<?php echo $softwarename; ?>&country=<?php echo $country; ?>&users=' + users, function (html) {
David Morley's avatar
David Morley committed
48 49 50
                    $('.wizard').replaceWith(html);
                });
            } else {
David Morley's avatar
David Morley committed
51
                $('.wizardnext4').text('Select Users, then. Next..');
David Morley's avatar
David Morley committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65
            }
        });
        $('.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; ?>';
David Morley's avatar
David Morley committed
66 67 68 69 70 71
            var users = '<?php echo $users; ?>';
            if (users) {
                columns[8] = '>= ' + users;
            } else {
                columns[8] = '>= 0';
            }
David Morley's avatar
David Morley committed
72
            $('table')
David Morley's avatar
David Morley committed
73
                .trigger('refreshColumnSelector', ['columns', [3, 4, 13, 18, 8, 22, 28]])
David Morley's avatar
David Morley committed
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
                .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>
109
    <button type="button" class="wizardnext2 btn bg-grey">Next</button>
David Morley's avatar
David Morley committed
110 111 112 113 114 115 116 117 118 119 120 121

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(
122
            '<label class="m-0"><input class="ml-2" type="radio" name="country" value="%1$s" /> %2$s %3$s</label><br>',
David Morley's avatar
David Morley committed
123 124 125 126 127
            $country,
            country($country)->getName(),
            country($country)->getEmoji()
        );
    }
128
    echo '</div></div><button type="button" class="wizardnext3 btn bg-grey">Next</button>';
David Morley's avatar
David Morley committed
129
} elseif ($page === 3) {
David Morley's avatar
David Morley committed
130 131 132
    $users   = data('total_users', $softwarename, $country);
    $umin    = min($users);
    $umax    = max($users);
David Morley's avatar
David Morley committed
133

David Morley's avatar
David Morley committed
134 135 136 137 138 139 140
    echo '<div class="row m-1 p-1"><div class="col-9">Users we found:</div><div class="col-8 p-3">';
    if ($umin === $umax) {
        echo '<div class="row m-1 p-1"><div class="col-9"></div><div class="col-8">';
        printf('<input type="text" name="users" value="%d" hidden>', $umin);
    } else {
        echo '<div class="row m-1 p-1"><div class="col-9">Users Minimum:</div><div class="col-8">';
        printf('<input type="text" name="users" id="month" data-min="%d" data-max="%d" data-step="1">', $umin, $umax);
David Morley's avatar
David Morley committed
141
    }
142
    echo '</div></div><button type="button" class="wizardnext4 btn bg-grey">Next</button>';
David Morley's avatar
David Morley committed
143
} elseif ($page === 4) {
David Morley's avatar
David Morley committed
144
    $months = data('monthsmonitored', $softwarename, $country, $users);
David Morley's avatar
David Morley committed
145 146 147 148 149 150 151 152 153
    $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);
    }
154
    echo '</div></div><button type="button" class="suggestpod btn bg-grey">Suggest a Pod</button>';
David Morley's avatar
David Morley committed
155 156 157
}


David Morley's avatar
David Morley committed
158
function data($column, $software = null, $country = null, $users = null)
David Morley's avatar
David Morley committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
{
    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;
        }
David Morley's avatar
David Morley committed
174 175 176
        if ($users) {
            $sql      .= ' AND total_users >= ?';
            $params[] = $users;
David Morley's avatar
David Morley committed
177 178 179 180 181 182 183
        }
        $data = R::getAll($sql, $params);
    } catch (\RedBeanPHP\RedException $e) {
        die('Error in SQL query: ' . $e->getMessage());
    }
    return array_filter(array_column($data, $column));
}