user.php 5.38 KB
Newer Older
1 2
<?php

Taddeüs Kroes's avatar
Taddeüs Kroes committed
3 4
use Nette\Forms\Form;
use Instante\Bootstrap3Renderer\BootstrapRenderer;
5
use Nette\Security\Passwords;
Taddeüs Kroes's avatar
Taddeüs Kroes committed
6

7
function find_user($id) {
8
    global $app, $db;
Taddeus Kroes's avatar
Taddeus Kroes committed
9

10
    require_user_access($id);
Taddeus Kroes's avatar
Taddeus Kroes committed
11 12 13 14 15 16
    $dbuser = $db->table('user')->get($id);

    if (!$dbuser)
        $app->halt(403, _('User not found'));

    return $dbuser;
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
}

function edit_user_form($dbuser) {
    $form = new Form;
    $form->setRenderer(new BootstrapRenderer);
    $form->setAction("user/$dbuser->id");
    $form->addHidden('_METHOD', 'PUT');

    $check_current_password = function ($password) use ($dbuser) {
        return Passwords::verify($password->getValue(), $dbuser->password);
    };

    $form->addPassword('current_password', _('Current password'))
        ->setRequired()
        ->addRule($check_current_password, _('Password incorrect'));
32
    $form->addPassword('new_password', _('New password'))
33 34 35
        ->addCondition(Form::FILLED)
            ->addRule(Form::MIN_LENGTH, sprintf(_('Password must be at least %d charachers'),
                MIN_PASSWORD_CHARS), MIN_PASSWORD_CHARS);
36 37 38
    $form->addPassword('new_password_repeat', _('Confirm new password'))
        ->addConditionOn($form['new_password'], Form::FILLED)
            ->addRule(Form::EQUAL, _('Passwords must match'), $form['new_password']);
39
    $form->addSubmit('send', _('Save'))->setAttribute('class', 'btn-primary');
40

41 42 43 44 45
    return $form;
}

$app->get('/user/:id', function ($id) use ($app, $db) {
    $app->render('user/view', array('dbuser' => find_user($id)));
46 47
});

48
$app->get('/user/:id/edit', function ($id) use ($app) {
49 50 51
    $dbuser = find_user($id);
    $form = edit_user_form($dbuser);
    $app->render('user/edit', compact('dbuser', 'form'));
52
});
53
$app->put('/user/:id', function ($id) use ($app, $user) {
54 55 56 57 58 59 60
    $dbuser = find_user($id);
    $form = edit_user_form($dbuser);
    $form->validate();

    if (!$form->hasErrors()) {
        $values = $form->getValues();

61
        if ($values->new_password) {
62
            $dbuser->update(array(
63
                'password' => Passwords::hash($values->new_password)
64 65
            ));
            //$user->logout();
66
            //$user->login($dbuser->username, $values->new_password);
67 68 69 70
        }

        $app->redirect(ROOT_URL . "/user/$id");
    }
71

72
    $app->render('user/edit', compact('dbuser', 'form'));
73 74 75 76 77 78 79 80 81
});

$app->put('/user/:id', function ($id) {
    echo "update user $id";
});

$app->delete('/user/:id', function ($id) {
    echo "delete user $id";
});
Taddeüs Kroes's avatar
Taddeüs Kroes committed
82

83
function plot_filter_form($dbuser, $matches, $default_discipline) {
Taddeüs Kroes's avatar
Taddeüs Kroes committed
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
    global $db;

    $disciplines = array('barebow', 'recurve', 'compound');
    $disciplines = array_combine($disciplines, array_map('ucfirst', $disciplines));

    $match_ids = array_unique($matches->fetchPairs(null, 'id'));
    $tags = $db->table('tag')
        ->select('DISTINCT name AS name')
        ->where(array('match_id' => $match_ids))
        ->fetchPairs('name', 'name');
    asort($tags);

    $distances = $matches->fetchPairs('distance', 'distance');
    asort($distances);
    $distances = map_format(_('%d meters'), $distances);

    $form = new Form;
    $form->setRenderer(new BootstrapRenderer);
    $form->setAction("user/$dbuser->id/plot");
    $form->setMethod('get');

    $form->addText('from', _('From'))
        ->setType('date')
        ->setAttribute('placeholder', _('YYYY-MM-DD'));
    $form->addText('until', _('Until'))
        ->setType('date')
        ->setAttribute('placeholder', _('YYYY-MM-DD'))
        ->setDefaultValue(strftime('%Y-%m-%d'));
    $form->addMultiSelect('tags', _('Tags'), $tags);
    $form->addSelect('distance', _('Distance'), $distances)
        ->setPrompt(_('Select a distance'));
    $form->addRadioList('discipline', _('Discipline'), $disciplines)
        ->setAttribute('data-inline', true)
117
        ->setDefaultValue($default_discipline);
Taddeüs Kroes's avatar
Taddeüs Kroes committed
118 119 120 121 122 123
    $form->addSubmit('send', _('Filter'));

    return $form;
}

$app->get('/user/:id/plot', function ($id) use ($app, $db, $user) {
Taddeus Kroes's avatar
Taddeus Kroes committed
124
    $dbuser = find_user($id);
Taddeüs Kroes's avatar
Taddeüs Kroes committed
125 126 127 128 129

    $matches = $db->table('match')
        ->where(array('user_id' => $id))
        ->order('created_at DESC');

130 131 132 133 134 135 136 137 138
    if ($matches->count()) {
        $last_match = $matches->fetch();
        $default_discipline = $last_match->discipline;
        $matches->rewind();
    } else {
        $default_discipline = 'recurve';
    }

    $form = plot_filter_form($dbuser, $matches, $default_discipline);
Taddeüs Kroes's avatar
Taddeüs Kroes committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

    if ($form->isSubmitted()) {
        $v = $form->getValues();

        if ($v->tags) {
            $match_ids = $db->table('tag')
                ->select('match_id')
                ->where('name', $v->tags)
                ->fetchPairs(null, 'match_id');
            $matches->where('id', $match_ids);
        }

        if ($v->from && $v->until)
            $matches->where('DATE(created_at) BETWEEN ? AND ?', $v->from, $v->until);
        elseif ($v->from)
            $matches->where('DATE(created_at) >= ?', $v->from);
        elseif ($v->until)
            $matches->where('DATE(created_at) <= ?', $v->until);

        if ($v->distance)
            $matches->where('distance', $v->distance);

        if ($v->discipline)
            $matches->where('discipline', $v->discipline);
163 164
    } else {
        $matches->where('discipline', $default_discipline);
Taddeüs Kroes's avatar
Taddeüs Kroes committed
165 166 167 168
    }

    $app->render('user/plot', compact('dbuser', 'form', 'matches'));
});
169 170 171 172 173 174

$app->get('/user', function () use ($app, $db) {
    require_admin_access();
    $users = $db->table('user')->order('id');
    $app->render('user/list', compact('users'));
});