diff --git a/app/locales/en.yml b/app/locales/en.yml index 5be4648..a1c92cd 100644 --- a/app/locales/en.yml +++ b/app/locales/en.yml @@ -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' diff --git a/app/locales/fr.yml b/app/locales/fr.yml index bbfece7..5adb093 100644 --- a/app/locales/fr.yml +++ b/app/locales/fr.yml @@ -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' diff --git a/src/Gist/Controller/MyController.php b/src/Gist/Controller/MyController.php index fb7dbf9..22e4fb8 100644 --- a/src/Gist/Controller/MyController.php +++ b/src/Gist/Controller/MyController.php @@ -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, diff --git a/src/Gist/Form/DeleteGistForm.php b/src/Gist/Form/DeleteGistForm.php index c7c2350..01baab0 100644 --- a/src/Gist/Form/DeleteGistForm.php +++ b/src/Gist/Form/DeleteGistForm.php @@ -23,6 +23,13 @@ class DeleteGistForm extends AbstractForm ) ); + $this->builder->setMethod('POST'); + return $this->builder; } + + public function getName() + { + return 'delete'; + } } diff --git a/src/Gist/Form/FilterGistForm.php b/src/Gist/Form/FilterGistForm.php new file mode 100644 index 0000000..ce2bcce --- /dev/null +++ b/src/Gist/Form/FilterGistForm.php @@ -0,0 +1,79 @@ + + */ +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'; + } +} diff --git a/src/Gist/Model/User.php b/src/Gist/Model/User.php index 6ce65fb..2d29bc5 100644 --- a/src/Gist/Model/User.php +++ b/src/Gist/Model/User.php @@ -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); } } diff --git a/src/Gist/Resources/views/My/my.html.twig b/src/Gist/Resources/views/My/my.html.twig index b0bec8e..10e3539 100644 --- a/src/Gist/Resources/views/My/my.html.twig +++ b/src/Gist/Resources/views/My/my.html.twig @@ -25,9 +25,70 @@
- {{ form(form) }} + {{ form(deleteForm) }}
+
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ {% if gists.nbResults == 0 %} {{ 'my.nothing'|trans }} {% else %} @@ -45,7 +106,7 @@
  • {% set params = app.request.attributes.get('_route_params')|merge({page: previousPage}) %} - + @@ -70,7 +131,7 @@
  • {% set params = app.request.attributes.get('_route_params')|merge({page: 1}) %} - + diff --git a/web/app/css/app.css b/web/app/css/app.css index bcf679d..1d4ab1c 100644 --- a/web/app/css/app.css +++ b/web/app/css/app.css @@ -82,3 +82,7 @@ div.diff { background: #DE3336; color: #fff; } + +.btn-error:active, .btn-error:hover, .btn-error:focus { + color: #000; +} diff --git a/web/app/js/app.js b/web/app/js/app.js index ba95913..fe2a6eb 100644 --- a/web/app/js/app.js +++ b/web/app/js/app.js @@ -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(); }); }