1
0
mirror of https://gitnet.fr/deblan/gist.git synced 2021-08-14 08:30:49 +02:00
This commit is contained in:
Simon Vieille 2015-05-07 14:10:23 +02:00
parent de8de3fa18
commit 88fb39fbab
10 changed files with 215 additions and 29 deletions

View File

@ -2,6 +2,10 @@ home:
path: /
defaults: {_controller: Gist\Controller\EditController::createAction, _locale: en}
clone:
path: /clone/{gist}/{commit}
defaults: {_controller: Gist\Controller\EditController::cloneAction, _locale: en, commit: 0}
view:
path: /view/{gist}/{commit}
defaults: {_controller: Gist\Controller\ViewController::viewAction, _locale: en, commit: 0}

View File

@ -18,7 +18,7 @@ gist:
history: 'Commit(s)'
raw: 'RAW'
download: 'Download'
fork: 'Fork'
clone: 'Clone'
date:
format: 'Y-m-d h:i:s'
@ -34,6 +34,7 @@ form:
choice:
yes: 'Yes'
no: 'No'
submit: 'Send'
type:
label: 'Language: %value%'
choice:

View File

@ -34,6 +34,7 @@ form:
choice:
yes: 'Oui'
no: 'Non'
submit: 'Send'
type:
label: 'Langage : %value%'
choice:

View File

@ -5,7 +5,10 @@ namespace Gist\Controller;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Gist\Form\CreateGistForm;
use Gist\Form\CloneGistForm;
use Gist\Model\Gist;
use GitWrapper\GitException;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Class HomeController
@ -32,7 +35,7 @@ class EditController extends Controller
}
return $app['twig']->render(
'Home/index.html.twig',
'Edit/index.html.twig',
array(
'gist' => isset($gist) ? $gist : null,
'form' => $form->createView(),
@ -44,10 +47,39 @@ class EditController extends Controller
{
$viewOptions = $this->getViewOptions($request, $app, $gist, $commit);
if (is_array($viewOptions)) {
return $app['twig']->render('View/view.html.twig', $viewOptions);
} else {
return $this->notFoundResponse($app);
$data = array(
'type' => $viewOptions['gist']->getType(),
'content' => $viewOptions['raw_content'],
'cipher' => 'no',
);
$form = new CloneGistForm($app['form.factory'], $app['translator'], $data);
$form = $form->build();
if ($request->isMethod('post')) {
$form->submit($request);
if ($form->isValid()) {
try {
$gist = $app['gist']->commit($viewOptions['gist'], $form->getData());
} catch (GitException $e) {
}
$history = $app['gist']->getHistory($gist);
return new RedirectResponse($app['url_generator']->generate(
'view',
array(
'gist' => $gist->getFile(),
'commit' => array_pop($history)['commit'],
)
));
}
}
$viewOptions['form'] = $form->createView();
return $app['twig']->render('Edit/clone.html.twig', $viewOptions);
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace Gist\Form;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* Class CreateGistForm
* @author Simon Vieille <simon@deblan.fr>
*/
class CloneGistForm extends AbstractForm
{
public function build(array $options = array())
{
$this->builder->add(
'content',
'textarea',
array(
'required' => true,
'attr' => array(
'class' => 'form-control',
'rows' => 10,
),
'constraints' => array(
new NotBlank(array(
'message' => $this->translator->trans('form.error.not_blank'),
)),
),
)
);
$this->builder->add(
'type',
'choice',
array(
'required' => true,
'choices' => $this->getTypes(),
'constraints' => array(
new NotBlank(),
),
)
);
return $this->builder->getForm();
}
protected function getTypes()
{
$types = array(
'xml' => '',
'css' => '',
'javascript' => '',
'php' => '',
'sql' => '',
'yaml'=> '',
'perl' => '',
'c' => '',
'asp' => '',
'python' => '',
'bash' => '',
'actionscript3' => '',
'text' => '',
);
foreach ($types as $k => $v) {
$types[$k] = $this->translator->trans('form.type.choice.'.$k);
}
return $types;
}
}

View File

@ -0,0 +1,66 @@
{% extends 'base.html.twig' %}
{% block body %}
<div class="row">
<form action="" method="post" id="main-form">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
{{ gist.title ? gist.title : 'gist.untitled'|trans }}
</div>
<div class="panel-body">
<p class="text-primary hide" id="cipher-alert">
<span class="glyphicon glyphicon-info-sign"></span>
{{ 'form.cipher.alert'|trans }}
</p>
<div class="btn-toolbar">
<div class="btn-group" id="options">
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span id="type-label" data-tpl="{{ 'form.type.label'|trans }}">
</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
{% for item in form.type.vars.choices %}
<li>
<input
{% if item.value == form.type.vars.value %}checked{% endif %}
data-id="#type-label" type="radio" class="hide"
data-title="{{ item.label }}"
value="{{ item.value }}"
name="form[type]"
id="type-{{ loop.index }}" />
<a href="#">
<label for="type-{{ loop.index }}">
{{ item.label }}
</label>
</a>
</li>
{% endfor %}
</ul>
</div>
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span id="cipher-label" data-tpl="{{ 'form.cipher.label'|trans }}">
</span>
<span class="caret"></span>
</button>
</div>
</div>
</div>
<p>
{{ form_errors(form.content) }}
{{ form_widget(form.content) }}
</p>
<p>
<input type="submit" class="btn btn-primary" value="{{ 'form.submit'|trans }}">
</p>
</div>
</div>
</div>
{{ form_widget(form._token) }}
</form>
</div>
{% endblock %}

View File

@ -74,7 +74,7 @@
{{ form_widget(form.content) }}
</p>
<p>
<input type="submit" class="btn btn-primary" value="Envoyer">
<input type="submit" class="btn btn-primary" value="{{ 'form.submit'|trans }}">
</p>
</div>
</div>

View File

@ -36,9 +36,9 @@
<span class="glyphicon glyphicon-save-file"></span>
{{ 'gist.action.download'|trans }}
</a>
<a class="btn btn-success btn-sm">
<a href="{{ path('clone', app.request.attributes.get('_route_params')) }}" class="btn btn-success btn-sm">
<span class="glyphicon glyphicon-copy"></span>
{{ 'gist.action.fork'|trans }}
{{ 'gist.action.clone'|trans }}
</a>
</div>
{% endif %}

View File

@ -1,7 +1,7 @@
{% extends 'base.html.twig' %}
{% block title %}
Error {{ code }} - {{ name }}
Error {{ code }} - {{ name }}
{% endblock %}
{% block langs %}
@ -9,24 +9,24 @@
{% endblock %}
{% block body %}
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
Error: {{ name }}
</div>
<div class="panel-body">
{% if app.env == 'dev' %}
<p><em>In file "{{ exception.file }}" at line {{ exception.line }}</em></p>
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
Error: {{ name }}
</div>
<div class="panel-body">
{% if app.env == 'dev' %}
<p><em>In file "{{ exception.file }}" at line {{ exception.line }}</em></p>
<h2>Stacktrace</h2>
<p>{{ exception.message }}</p>
<pre>{{ exception.traceAsString }}</pre>
{% else %}
<p>{{ exception.message }}</p>
{% endif %}
</div>
</div>
</div>
</div>
<h2>Stacktrace</h2>
<p>{{ exception.message }}</p>
<pre>{{ exception.traceAsString }}</pre>
{% else %}
<p>{{ exception.message }}</p>
{% endif %}
</div>
</div>
</div>
</div>
{% endblock %}

View File

@ -89,6 +89,17 @@ class GistService
return $gist;
}
public function commit(Gist $gist, array $data)
{
file_put_contents($this->gistPath.'/'.$gist->getFile(), $data['content']);
$this->gitWorkingCopy
->add($gist->getFile())
->commit('Update');
return $gist;
}
public function highlight($type, $content)
{
$this->geshi->set_source($content);