forked from deblan/gist
#4 Account: more features to manage gists - Filters
This commit is contained in:
parent
5e331aa29c
commit
f89e84585a
|
@ -71,14 +71,17 @@ form:
|
||||||
alert: 'By enabling cipher, fork will be not possible.'
|
alert: 'By enabling cipher, fork will be not possible.'
|
||||||
label: 'Cipher: %value%'
|
label: 'Cipher: %value%'
|
||||||
choice:
|
choice:
|
||||||
|
anyway: 'Anyway'
|
||||||
yes: 'Yes'
|
yes: 'Yes'
|
||||||
no: 'No'
|
no: 'No'
|
||||||
submit: 'Send'
|
submit: 'Send'
|
||||||
|
filter: 'Filter'
|
||||||
success:
|
success:
|
||||||
gist: 'Gist removed.'
|
gist: 'Gist removed.'
|
||||||
type:
|
type:
|
||||||
label: 'Language: %value%'
|
label: 'Language: %value%'
|
||||||
choice:
|
choice:
|
||||||
|
all: 'Tous'
|
||||||
html: 'HTML'
|
html: 'HTML'
|
||||||
xml: 'XML'
|
xml: 'XML'
|
||||||
css: 'CSS'
|
css: 'CSS'
|
||||||
|
|
|
@ -72,14 +72,17 @@ form:
|
||||||
alert: 'En activant le chiffrement, le fork deviendra impossible.'
|
alert: 'En activant le chiffrement, le fork deviendra impossible.'
|
||||||
label: 'Chiffrer : %value%'
|
label: 'Chiffrer : %value%'
|
||||||
choice:
|
choice:
|
||||||
|
anyway: 'Peu importe'
|
||||||
yes: 'Oui'
|
yes: 'Oui'
|
||||||
no: 'Non'
|
no: 'Non'
|
||||||
submit: 'Envoyer'
|
submit: 'Envoyer'
|
||||||
|
filter: 'Filtrer'
|
||||||
success:
|
success:
|
||||||
gist: 'Votre Gist a bien a été supprimé.'
|
gist: 'Votre Gist a bien a été supprimé.'
|
||||||
type:
|
type:
|
||||||
label: 'Langage : %value%'
|
label: 'Langage : %value%'
|
||||||
choice:
|
choice:
|
||||||
|
all: 'Tous'
|
||||||
html: 'HTML'
|
html: 'HTML'
|
||||||
xml: 'XML'
|
xml: 'XML'
|
||||||
css: 'CSS'
|
css: 'CSS'
|
||||||
|
|
|
@ -5,6 +5,7 @@ namespace Gist\Controller;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Gist\Model\GistQuery;
|
use Gist\Model\GistQuery;
|
||||||
use Gist\Form\DeleteGistForm;
|
use Gist\Form\DeleteGistForm;
|
||||||
|
use Gist\Form\FilterGistForm;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class MyController
|
* Class MyController
|
||||||
|
@ -14,24 +15,55 @@ class MyController extends Controller
|
||||||
{
|
{
|
||||||
public function myAction(Request $request, $page)
|
public function myAction(Request $request, $page)
|
||||||
{
|
{
|
||||||
$page = (int) $page;
|
$page = (int) $page;
|
||||||
$gists = $this->getUser()->getGistsPager($page);
|
|
||||||
|
|
||||||
$app = $this->getApp();
|
$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')) {
|
if ($request->isMethod('post')) {
|
||||||
$form->submit($request);
|
$form->submit($request);
|
||||||
|
|
||||||
if ($form->isValid()) {
|
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) {
|
foreach ($gists as $gist) {
|
||||||
if ($gist->getId() === $id) {
|
if ($gist->getId() === $id) {
|
||||||
$gist->delete();
|
$gist->delete();
|
||||||
$deleted = true;
|
$deleted = true;
|
||||||
$gists = $this->getUser()->getGistsPager($page);
|
$gists = $this->getUser()->getGistsPager($page, $options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,7 +77,8 @@ class MyController extends Controller
|
||||||
array(
|
array(
|
||||||
'gists' => $gists,
|
'gists' => $gists,
|
||||||
'page' => $page,
|
'page' => $page,
|
||||||
'form' => $form->createView(),
|
'deleteForm' => $deleteForm->createView(),
|
||||||
|
'filterForm' => $filterForm->createView(),
|
||||||
'deleted' => !empty($deleted),
|
'deleted' => !empty($deleted),
|
||||||
'nextPage' => $nextPage,
|
'nextPage' => $nextPage,
|
||||||
'previousPage' => $previousPage,
|
'previousPage' => $previousPage,
|
||||||
|
|
|
@ -23,6 +23,13 @@ class DeleteGistForm extends AbstractForm
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$this->builder->setMethod('POST');
|
||||||
|
|
||||||
return $this->builder;
|
return $this->builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return 'delete';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
79
src/Gist/Form/FilterGistForm.php
Normal file
79
src/Gist/Form/FilterGistForm.php
Normal 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';
|
||||||
|
}
|
||||||
|
}
|
|
@ -28,11 +28,25 @@ class User extends BaseUser implements UserInterface
|
||||||
return parent::getGists($criteria, $con);
|
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)
|
->filterByUser($this)
|
||||||
->orderByCreatedAt(Criteria::DESC)
|
->orderByCreatedAt(Criteria::DESC);
|
||||||
->paginate($page, $maxPerPage);
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,9 +25,70 @@
|
||||||
<div class="panel-body">
|
<div class="panel-body">
|
||||||
<div class="tab-content">
|
<div class="tab-content">
|
||||||
<div id="form-deletion">
|
<div id="form-deletion">
|
||||||
{{ form(form) }}
|
{{ form(deleteForm) }}
|
||||||
</div>
|
</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 %}
|
{% if gists.nbResults == 0 %}
|
||||||
{{ 'my.nothing'|trans }}
|
{{ 'my.nothing'|trans }}
|
||||||
{% else %}
|
{% else %}
|
||||||
|
@ -45,7 +106,7 @@
|
||||||
|
|
||||||
<li>
|
<li>
|
||||||
{% set params = app.request.attributes.get('_route_params')|merge({page: previousPage}) %}
|
{% 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 aria-hidden="true">
|
||||||
<span class="glyphicon glyphicon glyphicon-chevron-left"></span>
|
<span class="glyphicon glyphicon glyphicon-chevron-left"></span>
|
||||||
</span>
|
</span>
|
||||||
|
@ -70,7 +131,7 @@
|
||||||
|
|
||||||
<li>
|
<li>
|
||||||
{% set params = app.request.attributes.get('_route_params')|merge({page: 1}) %}
|
{% 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 aria-hidden="true">
|
||||||
<span class="glyphicon glyphicon-step-forward"></span>
|
<span class="glyphicon glyphicon-step-forward"></span>
|
||||||
</span>
|
</span>
|
||||||
|
|
|
@ -82,3 +82,7 @@ div.diff {
|
||||||
background: #DE3336;
|
background: #DE3336;
|
||||||
color: #fff;
|
color: #fff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.btn-error:active, .btn-error:hover, .btn-error:focus {
|
||||||
|
color: #000;
|
||||||
|
}
|
||||||
|
|
|
@ -79,7 +79,7 @@ var editorEvents = function() {
|
||||||
|
|
||||||
var myEvents = function() {
|
var myEvents = function() {
|
||||||
$('.btn-delete').click(function() {
|
$('.btn-delete').click(function() {
|
||||||
$('#form_id').val($(this).data('id'));
|
$('#delete_id').val($(this).data('id'));
|
||||||
$('#form-deletion form').submit();
|
$('#form-deletion form').submit();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue