1
0
Fork 0
forked from deblan/gist

#4 Account: more features to manage gists

This commit is contained in:
Simon Vieille 2016-06-18 16:37:45 +02:00
parent f36c0b8eeb
commit 143299fad8
9 changed files with 177 additions and 7 deletions

View file

@ -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}

View file

@ -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:

View file

@ -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:

View file

@ -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,
)
);
}
}

View file

@ -0,0 +1,28 @@
<?php
namespace Gist\Form;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* Class DeleteGistForm
* @author Simon Vieille <simon@deblan.fr>
*/
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;
}
}

View file

@ -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);
}
}

View file

@ -4,19 +4,85 @@
{% block body %}
<div class="row">
{% if deleted %}
<div class="col-md-12">
<div class="alert alert-success">
<p>{{ 'form.success.gist'|trans }}</p>
</div>
</div>
{% endif %}
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
{{ 'my.title'|trans }}
<div class="pull-right actions">
<a href="{{ path('home', app.request.attributes.get('_route_params')) }}" class="btn btn-success btn-sm">
<span class="glyphicon glyphicon-copy"></span>
{{ 'gist.action.add'|trans }}
</a>
</div>
</div>
<div class="panel-body">
<div class="tab-content">
{% set gists = user.gists %}
<div id="form-deletion">
{{ form(form) }}
</div>
{% if gists|length == 0 %}
{% if gists.nbResults == 0 %}
{{ 'my.nothing'|trans }}
{% else %}
{% for gist in user.gists %}
{% set pager %}
{% if gists.haveToPaginate %}
<ul class="pagination">
<li>
{% set params = app.request.attributes.get('_route_params')|merge({page: 1}) %}
<a href="{{ path('my', params) }}" aria-label="Previous">
<span aria-hidden="true">
<span class="glyphicon glyphicon-step-backward"></span>
</span>
</a>
</li>
<li>
{% set params = app.request.attributes.get('_route_params')|merge({page: previousPage}) %}
<a href="{{ path('my', params) }}" aria-label="Previous">
<span aria-hidden="true">
<span class="glyphicon glyphicon glyphicon-chevron-left"></span>
</span>
</a>
</li>
{% for p in gists.links(10) %}
<li {% if p == page %}class="active"{% endif %}>
{% set params = app.request.attributes.get('_route_params')|merge({page: p}) %}
<a href="{{ path('my', params) }}">{{ p }}</a>
</li>
{% endfor %}
<li>
{% set params = app.request.attributes.get('_route_params')|merge({page: nextPage}) %}
<a href="{{ path('my', params) }}" aria-label="Previous">
<span aria-hidden="true">
<span class="glyphicon glyphicon glyphicon-chevron-right"></span>
</span>
</a>
</li>
<li>
{% set params = app.request.attributes.get('_route_params')|merge({page: 1}) %}
<a href="{{ path('my', params) }}" aria-label="Previous">
<span aria-hidden="true">
<span class="glyphicon glyphicon-step-forward"></span>
</span>
</a>
</li>
</ul>
{% endif %}
{% endset %}
{{ pager }}
{% for gist in gists %}
<div class="commit">
<p>
<strong>{{ gist.title ? gist.title : 'gist.untitled'|trans }}</strong>,
@ -37,9 +103,15 @@
<span class="glyphicon glyphicon-lock"></span>
</button>
{% endif %}
<button class="btn btn-delete btn-sm" data-id="{{ gist.id }}">
<span class="glyphicon btn-delete glyphicon-remove" data-id="{{ gist.id }}"></span>
</button>
</p>
</div>
{% endfor %}
{{ pager }}
{% endif %}
</div>
</div>

View file

@ -77,3 +77,8 @@ div.diff {
#options {
margin-bottom: 17px;
}
.btn-delete {
background: #DE3336;
color: #fff;
}

View file

@ -77,6 +77,13 @@ var editorEvents = function() {
}
}
var myEvents = function() {
$('.btn-delete').click(function() {
$('#form_id').val($(this).data('id'));
$('#form-deletion form').submit();
});
}
var mainEditorEvents = function() {
$('.cipher-input').change(function() {
if ($('.cipher-input:checked').val() === 'yes') {
@ -142,6 +149,7 @@ var viewerEvents = function() {
var bootstrap = function() {
editorEvents();
viewerEvents();
myEvents();
mainEditorEvents();
}