Skip to content
Snippets Groups Projects
Commit f58723c6 authored by Taddeüs Kroes's avatar Taddeüs Kroes
Browse files

Added filter form

parent 2febdf14
No related branches found
No related tags found
No related merge requests found
<?php
use Nette\Forms\Form;
use Instante\Bootstrap3Renderer\BootstrapRenderer;
function pack_scores($scores) {
$blob = '';
foreach ($scores as $score)
......@@ -43,16 +46,68 @@ function render_match_action($action) {
};
}
$app->get('/match', function () use ($app, $db, $user) {
function filter_form($matches) {
global $db;
$form = new Form;
$form->setRenderer(new BootstrapRenderer);
$form->setAction('match#results');
$form->setMethod('get');
$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);
$form->addSelect('distance', 'Distance', $distances)
->setPrompt('Select a distance');
$form->addSelect('tag', 'Tag', $tags)
->setPrompt('Select a tag');
$form->addText('name', 'Name');
$form->addSubmit('send', 'Filter');
return $form;
}
$app->get('/match', function ($filter=null) use ($app, $db, $user) {
$user_id = $user->getId();
$matches = $db->table('match')->where(compact('user_id'))->order('created_at', 'desc');
$app->render('match/list', compact('matches'));
$matches = $db->table('match')->where(compact('user_id'))->order('created_at DESC');
$filter_form = filter_form($matches);
if ($filter_form->isSubmitted()) {
$values = $filter_form->getValues();
if ($values->tag) {
$ids = $db->table('tag')->select('match_id')->where('name', $values->tag);
$matches->where('id', $ids);
}
if ($values->distance)
$matches->where('distance', $values->distance);
if ($values->name)
$matches->where('name LIKE ?', "%$values->name%");
//foreach ($filter_form->getValues() as $prop => $value) {
//$matches = $matches->where($filter_form->getValues();
}
$app->render('match/list', compact('matches', 'filter_form'));
});
$app->get('/match/:id', render_match_action('view'));
$app->get('/match/:id/edit', render_match_action('edit'));
$app->get('/match/:id/delete', render_match_action('delete'));
$app->delete('/match/:id', function ($id) use ($app, $db) {
$db->table('user')->delete();
//find_match($id)->delete();
$app->delete('/match/:id', function ($id) {
find_match($id)->delete();
});
$app->put('/match/:id', function ($id) {
// TODO
});
{extends '../layout.latte'}
{block content}
<table class="table table-hover matches">
<h3 class="page-header">
<a data-toggle="collapse" href="#filter">
Filter matches <span class="caret"></span>
</a>
</h3>
<div id="filter" class="collapse {$filter_form->isSubmitted() ? in}">
{form $filter_form}
{form errors}
{form controls}
<div class="form-group">
<div class="form-actions col-sm-offset-2 col-sm-10">
<button type="submit" name="send" class="btn btn-primary">Filter</button>
<a href="match" class="btn btn-default">Clear filters</a>
</div>
</div>
{/form}
<h3>Results</h3>
</div>
<table id="results" class="table table-hover matches">
<thead>
<tr>
<th>Name</th>
......@@ -25,7 +44,13 @@
<td>{match_score($match)} (~{match_avg_score($match)|number:1})</td>
</tr>
<tr n:if="!$matches->count()">
<td colspan="5">You have not saved any matches yet.</td>
<td colspan="5">
{if $filter_form->isSubmitted()}
No search results.
{else}
You have not saved any matches yet.
{/if}
</td>
</tr>
</tbody>
</table>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment