Commit 85e4a5bc authored by arnaudbey's avatar arnaudbey

wip pattern

parent 9d32c9d2
......@@ -54,11 +54,39 @@ class GridPatternController extends Controller
*/
public function savePatternAction(GridPattern $gridPattern, Request $request)
{
$this->get('mw_manager.grid_pattern')->savePatternString($gridPattern, $request);
$this->get('mw_manager.grid_pattern')->save($gridPattern, $request);
$form = $this->get('mw_manager.grid_pattern')->getForm($gridPattern);
$template = $this->get('templating')->render('MagicWordBundle:Grid:grid-pattern-edit.html.twig', array('form' => $form, 'pattern' => $gridPattern ));
return new JsonResponse();
}
/**
* @Route("/pattern/create-form", name="pattern_create_form", options={"expose"=true})
* @Method("GET")
*/
public function patternCreateFormAction()
{
$gridPattern = new GridPattern();
$form = $this->get('mw_manager.grid_pattern')->getForm($gridPattern);
$template = $this->get('templating')->render('MagicWordBundle:Grid:grid-pattern-edit.html.twig', array('form' => $form, 'pattern' => $gridPattern ));
return new Response($template);
}
/**
* @Route("/pattern/create-form", name="pattern_create", options={"expose"=true})
* @Method("POST")
*/
public function patternCreateAction(Request $request)
{
$gridPattern = new GridPattern();
$this->get('mw_manager.grid_pattern')->save($gridPattern, $request);
$form = $this->get('mw_manager.grid_pattern')->getForm($gridPattern);
$template = $this->get('templating')->render('MagicWordBundle:Grid:grid-pattern-edit.html.twig', array('form' => $form, 'pattern' => $gridPattern ));
return new Response($template);
}
}
......@@ -54,31 +54,34 @@ class GridPatternManager
return $form;
}
public function savePatternString(GridPattern $gridPattern, Request $request)
public function save(GridPattern $gridPattern, Request $request)
{
$form = $this->formFactory->createBuilder(GridPatternType::class, $gridPattern)->getForm();
if (!$gridPattern->getAuthor() || $gridPattern->getAuthor() == $this->currentUser) {
$form = $this->formFactory->createBuilder(GridPatternType::class, $gridPattern)->getForm();
$strings = new ArrayCollection();
foreach ($gridPattern->getStrings() as $string) {
$strings->add($string);
}
$strings = new ArrayCollection();
foreach ($gridPattern->getStrings() as $string) {
$strings->add($string);
}
$form->handleRequest($request);
$form->handleRequest($request);
if ($form->isValid()) {
foreach ($strings as $string) {
if ($gridPattern->getStrings()->contains($string) === false) {
$this->em->remove($string);
if ($form->isValid()) {
$gridPattern->setAuthor($this->currentUser);
foreach ($strings as $string) {
if ($gridPattern->getStrings()->contains($string) === false) {
$this->em->remove($string);
}
}
foreach ($gridPattern->getStrings() as $string) {
$string->setGridPattern($gridPattern);
}
}
foreach ($gridPattern->getStrings() as $string) {
$string->setGridPattern($gridPattern);
}
$this->em->persist($gridPattern);
$this->em->flush();
$this->em->persist($gridPattern);
$this->em->flush();
}
}
return;
}
}
......@@ -9,6 +9,7 @@ var gridPatternHandler = {
modalHandler.createModale("patterns", "patterns_list", data);
});
},
edit: function(id){
var url = Routing.generate('pattern_form', { id : id});
$.ajax({
......@@ -19,6 +20,35 @@ var gridPatternHandler = {
modalHandler.createModale("patterns", "pattern_edit", data);
});
},
displayCreateForm: function(){
var url = Routing.generate('pattern_create_form');
$.ajax({
type: 'GET',
url: url,
})
.done(function(data) {
modalHandler.createModale("patterns", "pattern_create", data);
});
},
create: function(){
var formId = "gridpattern";
if(editor.isFormValid(formId)){
$("#pattern-save-btn").attr("onclick", "");
var url = Routing.generate('pattern_create');
var data = $("#"+formId).serializeArray();
$.ajax({
type: 'POST',
url: url,
data: data,
})
.done(function() {
gridPatternHandler.list();
});
}
},
addString: function(){
var strings = $('#strings');
var string = strings.data('prototype');
......@@ -26,12 +56,15 @@ var gridPatternHandler = {
string = string.replace(/__name__/g, length);
strings.append(string);
},
removeString: function(btn){
btn.closest('li').remove();
},
save: function(){
var formId = "gridpattern";
if(editor.isFormValid(formId)){
$("#pattern-save-btn").attr("onclick", "");
var url = Routing.generate('pattern_save', {id: $('#pattern-id').val()});
var data = $("#"+formId).serializeArray();
$.ajax({
......@@ -40,9 +73,11 @@ var gridPatternHandler = {
data: data,
})
.done(function() {
gridPatternHandler.list();
});
}
},
use: function(btn){
var strings = btn.getAttribute("data-strings");
var strings = strings.split(",");
......
......@@ -208,3 +208,9 @@ accuracy_text: Votre précision est de %accuracy%% (%totalFound% formes correcte
no_massive_create: Il n'y a pas de partie massive disponible mais vous pouvez en créer une.
save_all: Tout sauvegarder
piwikUrl: URL piwik (sans le http(s)://)
patterns_list: Liste des patterns
back_to_patterns_list: Retour aux patterns
pattern_edit: Edition d'un pattern
use_pattern: Utiliser
add_string: Ajouter une chaîne
pattern_create: Créer un pattern
<span onclick="gridPatternHandler.list();" class="btn btn-default">
{{"back_to_list" | trans}}
<i class="fa fa-chevron-left" aria-hidden="true"></i>
{{"back_to_patterns_list" | trans}}
</span>
<span onclick="gridPatternHandler.save()" class="btn btn-primary">
<span id="pattern-save-btn" onclick="{{ pattern.id ? "gridPatternHandler.save()" : "gridPatternHandler.create()"}}" class="btn btn-primary">
<i class="fa fa-floppy-o" aria-hidden="true"></i>
{{ "save" | trans }}
</span>
......
<span class="btn btn-default btn-default" onclick="gridPatternHandler.displayCreateForm()">
<i class="fa fa-plus" aria-hidden="true"></i>
{{ "pattern_create" | trans }}
</span>
<ul class="list-group">
{% for pattern in patterns %}
{% for pattern in patterns if pattern.strings |length > 0 or pattern.author == app.user %}
<li class="list-group-item">
<span onclick="gridPatternHandler.use(this)" class="btn btn-default btn-xs" data-strings="{% for string in pattern.strings %}{{string.value}},{% endfor %}">
Use it !
</span>
{{ pattern.name }} <span class="text-muted">{{ pattern.description }}</span>
{% if pattern.author == app.user %}<span class="btn btn-default btn-xs pull-right" onclick="gridPatternHandler.edit({{ pattern.id }})"> edit</span>{% endif %}
<span class="pull-right">
{% if pattern.author == app.user %}
<span class="btn btn-default btn-xs" onclick="gridPatternHandler.edit({{ pattern.id }})">
<i class="fa fa-pencil-square-o" aria-hidden="true"></i>
{{ "edit" | trans }}
</span>
{% endif %}
{% if pattern.strings | length > 0 %}
<span onclick="gridPatternHandler.use(this)" class="btn btn-default btn-xs" data-strings="{% for string in pattern.strings %}{{string.value}},{% endfor %}">
<i class="fa fa-crosshairs" aria-hidden="true"></i>
{{ "use_pattern" | trans }}
</span>
{% endif %}
</span>
</li>
{% endfor %}
</ul>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment