gist/src/Gist/Controller/EditController.php

87 lines
2.4 KiB
PHP
Raw Normal View History

2015-05-05 20:33:05 +02:00
<?php
namespace Gist\Controller;
use Silex\Application;
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;
2015-05-05 20:33:05 +02:00
/**
2015-07-19 16:45:04 +02: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
{
2015-05-06 20:35:30 +02:00
public function createAction(Request $request, Application $app)
2015-05-05 20:33:05 +02:00
{
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);
if ($form->isValid()) {
2015-05-06 20:35:30 +02:00
$gist = $app['gist']->create(new Gist(), $form->getData());
2015-05-05 23:24:37 +02:00
}
}
return $this->render(
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(),
),
$app
2015-05-05 22:04:04 +02:00
);
2015-05-05 20:33:05 +02:00
}
2015-05-07 13:50:13 +02:00
public function cloneAction(Request $request, Application $app, $gist, $commit)
2015-05-07 13:49:31 +02:00
{
$viewOptions = $this->getViewOptions($request, $app, $gist, $commit);
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();
return $this->render('Edit/clone.html.twig', $viewOptions, $app);
2015-05-07 13:49:31 +02:00
}
2015-05-05 20:33:05 +02:00
}