gist/src/Gist/Controller/EditController.php

118 lines
3.2 KiB
PHP
Raw Normal View History

2015-05-05 20:33:05 +02:00
<?php
namespace Gist\Controller;
use Symfony\Component\HttpFoundation\Request;
2015-05-05 22:04:04 +02:00
use Gist\Form\CreateGistForm;
2015-05-07 14:10:23 +02:00
use Gist\Form\CloneGistForm;
2015-05-06 14:11:00 +02:00
use Gist\Model\Gist;
2015-05-07 14:10:23 +02:00
use GitWrapper\GitException;
use Symfony\Component\HttpFoundation\RedirectResponse;
2017-06-19 23:55:50 +02:00
use Symfony\Component\Form\FormError;
2018-08-20 17:05:36 +02:00
use Symfony\Component\HttpFoundation\Response;
2015-05-05 20:33:05 +02:00
/**
2016-11-13 00:44:23 +01:00
* Class EditController.
*
2015-05-05 22:04:04 +02:00
* @author Simon Vieille <simon@deblan.fr>
2015-05-05 20:33:05 +02:00
*/
2015-05-07 13:49:31 +02:00
class EditController extends Controller
2015-05-05 20:33:05 +02:00
{
2016-11-13 00:44:23 +01:00
/**
* Creation page.
*
* @param Request $request
*
2018-08-20 17:05:36 +02:00
* @return Response
2016-11-13 00:44:23 +01:00
*/
public function createAction(Request $request)
2015-05-05 20:33:05 +02:00
{
$app = $this->getApp();
2015-05-05 23:24:37 +02:00
$data = array(
2015-05-09 12:30:33 +02:00
'type' => 'html',
2015-05-05 23:24:37 +02:00
'cipher' => 'no',
);
$form = new CreateGistForm($app['form.factory'], $app['translator'], $data);
2015-05-09 01:03:51 +02:00
$form = $form->build()->getForm();
2015-05-05 22:04:04 +02:00
2015-05-05 23:24:37 +02:00
if ($request->isMethod('post')) {
$form->submit($request);
2017-06-19 23:55:50 +02:00
$data = $form->getData();
2017-06-20 10:03:40 +02:00
if (empty($form->getData()['content']) && $form->get('file')->getData()) {
$data['content'] = file_get_contents($form->get('file')->getData()->getPathName());
unset($data['file']);
2017-10-15 13:28:49 +02:00
}
if (empty($data['content'])) {
2017-06-19 23:55:50 +02:00
$form->get('content')->addError(new FormError($app['translator']->trans('form.error.not_blank')));
}
2015-05-05 23:24:37 +02:00
if ($form->isValid()) {
2017-06-19 23:55:50 +02:00
$gist = $app['gist']->create(new Gist(), $data, $this->getUser());
2015-05-05 23:24:37 +02:00
}
}
2018-08-21 09:56:51 +02:00
return $this->createResponse(
2015-05-07 14:10:23 +02:00
'Edit/index.html.twig',
2015-05-05 22:04:04 +02:00
array(
2015-05-06 20:35:30 +02:00
'gist' => isset($gist) ? $gist : null,
2015-05-05 22:04:04 +02:00
'form' => $form->createView(),
2018-09-11 13:15:12 +02:00
'no_cache' => true,
)
2015-05-05 22:04:04 +02:00
);
2015-05-05 20:33:05 +02:00
}
2015-05-07 13:50:13 +02:00
2016-11-13 00:44:23 +01:00
/**
* Cloning page.
*
* @param Request $request
*
2018-08-20 17:05:36 +02:00
* @return Response
2016-11-13 00:44:23 +01:00
*/
public function cloneAction(Request $request, $gist, $commit)
2015-05-07 13:49:31 +02:00
{
$app = $this->getApp();
$viewOptions = $this->getViewOptions($request, $gist, $commit);
2015-05-07 13:49:31 +02:00
2015-05-07 14:10:23 +02:00
$data = array(
'type' => $viewOptions['gist']->getType(),
'content' => $viewOptions['raw_content'],
'cipher' => 'no',
);
$form = new CloneGistForm($app['form.factory'], $app['translator'], $data);
2015-05-09 01:03:51 +02:00
$form = $form->build()->getForm();
2015-05-07 14:10:23 +02:00
if ($request->isMethod('post')) {
$form->submit($request);
if ($form->isValid()) {
try {
$gist = $app['gist']->commit($viewOptions['gist'], $form->getData());
} catch (GitException $e) {
2015-05-09 01:03:51 +02:00
$gist = $viewOptions['gist'];
2015-05-07 14:10:23 +02:00
}
$history = $app['gist']->getHistory($gist);
return new RedirectResponse($app['url_generator']->generate(
'view',
array(
'gist' => $gist->getFile(),
'commit' => array_pop($history)['commit'],
)
));
}
2015-05-07 13:49:31 +02:00
}
2015-05-07 14:10:23 +02:00
$viewOptions['form'] = $form->createView();
2018-09-11 13:15:12 +02:00
$viewOptions['no_cache'] = true;
2015-05-07 14:10:23 +02:00
2018-08-21 09:56:51 +02:00
return $this->createResponse('Edit/clone.html.twig', $viewOptions);
2015-05-07 13:49:31 +02:00
}
2015-05-05 20:33:05 +02:00
}