1
0
Fork 0
forked from deblan/gist

#4 Account: more features to manage gists - Filters

This commit is contained in:
Simon Vieille 2016-06-18 17:44:07 +02:00
parent 5e331aa29c
commit f89e84585a
9 changed files with 220 additions and 16 deletions

View file

@ -71,14 +71,17 @@ form:
alert: 'By enabling cipher, fork will be not possible.'
label: 'Cipher: %value%'
choice:
anyway: 'Anyway'
yes: 'Yes'
no: 'No'
submit: 'Send'
filter: 'Filter'
success:
gist: 'Gist removed.'
type:
label: 'Language: %value%'
choice:
all: 'Tous'
html: 'HTML'
xml: 'XML'
css: 'CSS'

View file

@ -72,14 +72,17 @@ form:
alert: 'En activant le chiffrement, le fork deviendra impossible.'
label: 'Chiffrer : %value%'
choice:
anyway: 'Peu importe'
yes: 'Oui'
no: 'Non'
submit: 'Envoyer'
filter: 'Filtrer'
success:
gist: 'Votre Gist a bien a été supprimé.'
type:
label: 'Langage : %value%'
choice:
all: 'Tous'
html: 'HTML'
xml: 'XML'
css: 'CSS'

View file

@ -5,6 +5,7 @@ namespace Gist\Controller;
use Symfony\Component\HttpFoundation\Request;
use Gist\Model\GistQuery;
use Gist\Form\DeleteGistForm;
use Gist\Form\FilterGistForm;
/**
* Class MyController
@ -14,24 +15,55 @@ class MyController extends Controller
{
public function myAction(Request $request, $page)
{
$page = (int) $page;
$gists = $this->getUser()->getGistsPager($page);
$page = (int) $page;
$app = $this->getApp();
$form = new DeleteGistForm($app['form.factory'], $app['translator']);
$form = $form->build()->getForm();
$deleteForm = new DeleteGistForm($app['form.factory'], $app['translator']);
$deleteForm = $deleteForm->build()->getForm();
$options = array(
'type' => 'all',
'cipher' => 'anyway',
);
$filterForm = new FilterGistForm(
$app['form.factory'],
$app['translator'],
$options,
['csrf_protection' => false]
);
$filterForm = $filterForm->build()->getForm();
if ($request->query->has('filter')) {
$filterForm->submit($request);
if ($filterForm->isValid()) {
$options = $filterForm->getData();
}
}
$gists = $this->getUser()->getGistsPager($page, $options);
if ($request->isMethod('post')) {
$form->submit($request);
if ($form->isValid()) {
$id = (int) $form->getData()['id'];
$gist = $app['gist']->create(new Gist(), $form->getData(), $this->getUser());
}
}
if ($request->isMethod('post')) {
$deleteForm->submit($request);
if ($deleteForm->isValid()) {
$id = (int) $deleteForm->getData()['id'];
foreach ($gists as $gist) {
if ($gist->getId() === $id) {
$gist->delete();
$deleted = true;
$gists = $this->getUser()->getGistsPager($page);
$gists = $this->getUser()->getGistsPager($page, $options);
}
}
}
@ -45,7 +77,8 @@ class MyController extends Controller
array(
'gists' => $gists,
'page' => $page,
'form' => $form->createView(),
'deleteForm' => $deleteForm->createView(),
'filterForm' => $filterForm->createView(),
'deleted' => !empty($deleted),
'nextPage' => $nextPage,
'previousPage' => $previousPage,

View file

@ -23,6 +23,13 @@ class DeleteGistForm extends AbstractForm
)
);
$this->builder->setMethod('POST');
return $this->builder;
}
public function getName()
{
return 'delete';
}
}

View file

@ -0,0 +1,79 @@
<?php
namespace Gist\Form;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* Class CreateGistForm
* @author Simon Vieille <simon@deblan.fr>
*/
class FilterGistForm extends AbstractForm
{
public function build(array $options = array())
{
$this->builder->add(
'type',
'choice',
array(
'required' => true,
'choices' => $this->getTypes(),
'constraints' => array(
new NotBlank(),
),
)
);
$this->builder->add(
'cipher',
'choice',
array(
'required' => true,
'choices' => array(
'anyway' => $this->translator->trans('form.cipher.choice.anyway'),
'no' => $this->translator->trans('form.cipher.choice.no'),
'yes' => $this->translator->trans('form.cipher.choice.yes'),
),
'constraints' => array(
new NotBlank(),
),
)
);
$this->builder->setMethod('GET');
return $this->builder;
}
protected function getTypes()
{
$types = array(
'all' => '',
'html' => '',
'css' => '',
'javascript' => '',
'php' => '',
'sql' => '',
'xml' => '',
'yaml'=> '',
'perl' => '',
'c' => '',
'asp' => '',
'python' => '',
'bash' => '',
'actionscript3' => '',
'text' => '',
);
foreach ($types as $k => $v) {
$types[$k] = $this->translator->trans('form.type.choice.'.$k);
}
return $types;
}
public function getName()
{
return 'filter';
}
}

View file

@ -28,11 +28,25 @@ class User extends BaseUser implements UserInterface
return parent::getGists($criteria, $con);
}
public function getGistsPager($page, $maxPerPage = 10)
public function getGistsPager($page, $options = array(), $maxPerPage = 10)
{
return GistQuery::create()
$query = GistQuery::create()
->filterByUser($this)
->orderByCreatedAt(Criteria::DESC)
->paginate($page, $maxPerPage);
->orderByCreatedAt(Criteria::DESC);
if (!empty($options['type']) && $options['type'] !== 'all') {
$query->filterByType($options['type']);
}
if (!empty($options['cipher']) && $options['cipher'] !== 'anyway') {
$bools = array(
'yes' => true,
'no' => false,
);
$query->filterByCipher($bools[$options['cipher']]);
}
return $query->paginate($page, $maxPerPage);
}
}

View file

@ -25,9 +25,70 @@
<div class="panel-body">
<div class="tab-content">
<div id="form-deletion">
{{ form(form) }}
{{ form(deleteForm) }}
</div>
<form action="" method="GET">
<div class="btn-toolbar">
<div class="btn-group" id="options">
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span id="type-label" data-tpl="{{ 'form.type.label'|trans }}">
</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
{% for item in filterForm.type.vars.choices %}
<li>
<input
{% if item.value == filterForm.type.vars.value %}checked{% endif %}
data-id="#type-label" type="radio" class="hide"
data-title="{{ item.label }}"
value="{{ item.value }}"
name="filter[type]"
id="type-{{ loop.index }}" />
<a href="#">
<label for="type-{{ loop.index }}">
{{ item.label }}
</label>
</a>
</li>
{% endfor %}
</ul>
</div>
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span id="cipher-label" data-tpl="{{ 'form.cipher.label'|trans }}">
</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
{% for item in filterForm.cipher.vars.choices %}
<li>
<input
{% if item.value == filterForm.cipher.vars.value %}checked{% endif %}
data-id="#cipher-label" type="radio" class="hide cipher-input"
data-title="{{ item.label }}"
value="{{ item.value }}"
name="filter[cipher]"
id="cipher-{{ loop.index }}" />
<a href="#">
<label for="cipher-{{ loop.index }}">
{{ item.label }}
</label>
</a>
</li>
{% endfor %}
</ul>
</div>
</div>
<input type="submit" class="btn btn-success" value="{{ 'form.filter'|trans }}" />
</div>
</form>
{% if gists.nbResults == 0 %}
{{ 'my.nothing'|trans }}
{% else %}
@ -45,7 +106,7 @@
<li>
{% set params = app.request.attributes.get('_route_params')|merge({page: previousPage}) %}
<a href="{{ path('my', params) }}" aria-label="Previous">
<a href="{{ path('my', params) }}">
<span aria-hidden="true">
<span class="glyphicon glyphicon glyphicon-chevron-left"></span>
</span>
@ -70,7 +131,7 @@
<li>
{% set params = app.request.attributes.get('_route_params')|merge({page: 1}) %}
<a href="{{ path('my', params) }}" aria-label="Previous">
<a href="{{ path('my', params) }}">
<span aria-hidden="true">
<span class="glyphicon glyphicon-step-forward"></span>
</span>

View file

@ -82,3 +82,7 @@ div.diff {
background: #DE3336;
color: #fff;
}
.btn-error:active, .btn-error:hover, .btn-error:focus {
color: #000;
}

View file

@ -79,7 +79,7 @@ var editorEvents = function() {
var myEvents = function() {
$('.btn-delete').click(function() {
$('#form_id').val($(this).data('id'));
$('#delete_id').val($(this).data('id'));
$('#form-deletion form').submit();
});
}