diff --git a/app/config/routing.yml b/app/config/routing.yml index 1fe48e7..666ca81 100644 --- a/app/config/routing.yml +++ b/app/config/routing.yml @@ -45,8 +45,13 @@ logout: path: /my/logout my: - path: /my - defaults: {_controller: Gist\Controller\MyController::myAction, _locale: en} + path: /my/{page} + defaults: + _controller: Gist\Controller\MyController::myAction + _locale: en + page: 1 + requirements: + page: \d+ revisions: path: /revs/{gist} diff --git a/app/locales/en.yml b/app/locales/en.yml index c02fc04..5be4648 100644 --- a/app/locales/en.yml +++ b/app/locales/en.yml @@ -33,6 +33,7 @@ gist: download: 'Download' clone: 'Clone' embed: 'Embed:' + add: 'New' date: format: 'Y-m-d h:i:s' @@ -73,6 +74,8 @@ form: yes: 'Yes' no: 'No' submit: 'Send' + success: + gist: 'Gist removed.' type: label: 'Language: %value%' choice: diff --git a/app/locales/fr.yml b/app/locales/fr.yml index d35bf7e..bbfece7 100644 --- a/app/locales/fr.yml +++ b/app/locales/fr.yml @@ -33,6 +33,7 @@ gist: download: 'Télécharger' clone: 'Clôner' embed: 'Insérer : ' + add: 'Ajouter' date: format: 'd/m/Y H\hi s\s' @@ -74,6 +75,8 @@ form: yes: 'Oui' no: 'Non' submit: 'Envoyer' + success: + gist: 'Votre Gist a bien a été supprimé.' type: label: 'Langage : %value%' choice: diff --git a/src/Gist/Controller/MyController.php b/src/Gist/Controller/MyController.php index 73a705e..fb7dbf9 100644 --- a/src/Gist/Controller/MyController.php +++ b/src/Gist/Controller/MyController.php @@ -3,6 +3,8 @@ namespace Gist\Controller; use Symfony\Component\HttpFoundation\Request; +use Gist\Model\GistQuery; +use Gist\Form\DeleteGistForm; /** * Class MyController @@ -10,8 +12,44 @@ use Symfony\Component\HttpFoundation\Request; */ class MyController extends Controller { - public function myAction(Request $request) + public function myAction(Request $request, $page) { - return $this->render('My/my.html.twig'); + $page = (int) $page; + $gists = $this->getUser()->getGistsPager($page); + + $app = $this->getApp(); + $form = new DeleteGistForm($app['form.factory'], $app['translator']); + $form = $form->build()->getForm(); + + if ($request->isMethod('post')) { + $form->submit($request); + + if ($form->isValid()) { + $id = (int) $form->getData()['id']; + + foreach ($gists as $gist) { + if ($gist->getId() === $id) { + $gist->delete(); + $deleted = true; + $gists = $this->getUser()->getGistsPager($page); + } + } + } + } + + $nextPage = min($page + 1, $gists->getLastPage()); + $previousPage = max($page - 1, 1); + + return $this->render( + 'My/my.html.twig', + array( + 'gists' => $gists, + 'page' => $page, + 'form' => $form->createView(), + 'deleted' => !empty($deleted), + 'nextPage' => $nextPage, + 'previousPage' => $previousPage, + ) + ); } } diff --git a/src/Gist/Form/DeleteGistForm.php b/src/Gist/Form/DeleteGistForm.php new file mode 100644 index 0000000..c7c2350 --- /dev/null +++ b/src/Gist/Form/DeleteGistForm.php @@ -0,0 +1,28 @@ + + */ +class DeleteGistForm extends AbstractForm +{ + public function build(array $options = array()) + { + $this->builder->add( + 'id', + 'hidden', + array( + 'required' => true, + 'constraints' => array( + new NotBlank(), + ), + ) + ); + + return $this->builder; + } +} diff --git a/src/Gist/Model/User.php b/src/Gist/Model/User.php index fca1a40..6ce65fb 100644 --- a/src/Gist/Model/User.php +++ b/src/Gist/Model/User.php @@ -27,4 +27,12 @@ class User extends BaseUser implements UserInterface return parent::getGists($criteria, $con); } + + public function getGistsPager($page, $maxPerPage = 10) + { + return GistQuery::create() + ->filterByUser($this) + ->orderByCreatedAt(Criteria::DESC) + ->paginate($page, $maxPerPage); + } } diff --git a/src/Gist/Resources/views/My/my.html.twig b/src/Gist/Resources/views/My/my.html.twig index 237940a..b0bec8e 100644 --- a/src/Gist/Resources/views/My/my.html.twig +++ b/src/Gist/Resources/views/My/my.html.twig @@ -4,19 +4,85 @@ {% block body %}
{{ 'form.success.gist'|trans }}
+{{ gist.title ? gist.title : 'gist.untitled'|trans }}, @@ -37,9 +103,15 @@ {% endif %} + +