gist/src/Gist/Controller/MyController.php

84 lines
2.1 KiB
PHP
Raw Normal View History

2015-11-21 18:28:48 +01:00
<?php
namespace Gist\Controller;
use Symfony\Component\HttpFoundation\Request;
use Gist\Form\DeleteGistForm;
use Gist\Form\FilterGistForm;
2015-11-21 18:28:48 +01:00
/**
2016-11-13 00:44:23 +01:00
* Class MyController.
*
2015-11-21 18:28:48 +01:00
* @author Simon Vieille <simon@deblan.fr>
*/
class MyController extends Controller
{
2016-11-13 00:44:23 +01:00
/**
* "My" page.
*
* @param Request $request
* @param int $page
*
* @return string
*/
public function myAction(Request $request, $page)
2015-11-21 18:28:48 +01:00
{
2016-11-13 00:44:23 +01:00
$page = (int) $page;
$app = $this->getApp();
$deleteForm = new DeleteGistForm($app['form.factory'], $app['translator']);
$deleteForm = $deleteForm->build()->getForm();
2016-11-13 00:44:23 +01:00
$options = array(
2016-11-13 00:44:23 +01:00
'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();
}
}
2016-11-13 00:44:23 +01:00
$gists = $this->getUser()->getGistsPager($page, $options);
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, $options);
}
}
}
}
return $this->render(
'My/my.html.twig',
array(
2016-11-13 00:44:23 +01:00
'gists' => $gists,
'page' => $page,
'deleteForm' => $deleteForm->createView(),
'filterForm' => $filterForm->createView(),
'deleted' => !empty($deleted),
)
);
2015-11-21 18:28:48 +01:00
}
}