From 7f13ca69bb71a0eb477cbf4f4bfcd08b2843bf9b Mon Sep 17 00:00:00 2001 From: polo Date: Mon, 31 Mar 2025 22:50:11 +0200 Subject: inversion de deux articles --- public/js/main.js | 47 +++++++++++++++++++++++++++++++++++++++++++++ src/controller/ajax.php | 17 ++++++++++++++++ src/model/entities/Node.php | 4 ++-- src/view/ArticleBuilder.php | 8 ++++---- src/view/NewBuilder.php | 12 ++++++------ 5 files changed, 76 insertions(+), 12 deletions(-) diff --git a/public/js/main.js b/public/js/main.js index c86ec51..d985b71 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -27,4 +27,51 @@ function copyInClipBoard(link){ element.parentNode.removeChild(element); alert('Cette adresse a été copiée dans le presse-papier:\n\n' + link); +} + +// complète les fonctions dans tinymce.js +function switchPositions(articleId, direction) +{ + const current_article = document.getElementById(articleId).parentElement.parentElement; + var other_article = current_article; + + if(direction == 'down'){ + other_article = current_article.nextElementSibling; + } + else if(direction == 'up'){ + other_article = current_article.previousElementSibling; + } + const other_article_id = other_article.querySelector('div[id]').id; + + fetch('index.php?action=switch_positions', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ id1: articleId, id2: other_article_id }) + }) + .then(response => response.json()) + .then(data => { + if(data.success) + { + if(direction == 'down'){ + current_article.parentElement.insertBefore(other_article, current_article); + console.log('Inversion réussie'); + } + else if(direction == 'up'){ + other_article.parentElement.insertBefore(current_article, other_article); + console.log('Inversion réussie'); + } + else{ + console.log('Échec de l\'inversion'); + } + } + else { + + console.log('Échec de l\'inversion'); + } + }) + .catch(error => { + console.error('Erreur:', error); + }); } \ No newline at end of file diff --git a/src/controller/ajax.php b/src/controller/ajax.php index 130c4c6..bcba3f2 100644 --- a/src/controller/ajax.php +++ b/src/controller/ajax.php @@ -58,6 +58,23 @@ if($_SERVER['CONTENT_TYPE'] === 'application/json' && isset($_GET['action'])) } die; } + // inversion de la position de deux noeuds + elseif($_GET['action'] === 'switch_positions' && isset($json['id1']) && isset($json['id2'])) + { + $director = new Director($entityManager); + $director->makeArticleNode($json['id1']); + $node1 = $director->getRootNode(); + $director->makeArticleNode($json['id2']); + $node2 = $director->getRootNode(); + + $tmp = $node1->getPosition(); + $node1->setPosition($node2->getPosition()); + $node2->setPosition($tmp); + $entityManager->flush(); + + echo json_encode(['success' => true]); + die; + } } // détection des requêtes d'upload d'image de tinymce diff --git a/src/model/entities/Node.php b/src/model/entities/Node.php index 49e16ba..9240413 100644 --- a/src/model/entities/Node.php +++ b/src/model/entities/Node.php @@ -128,10 +128,10 @@ class Node { return $this->position; } - /*public function setPosition(int $position): void + public function setPosition(int $position): void { $this->position = $position; - }*/ + } public function getPage(): Page { return $this->page; diff --git a/src/view/ArticleBuilder.php b/src/view/ArticleBuilder.php index 989da0d..6c2f63c 100644 --- a/src/view/ArticleBuilder.php +++ b/src/view/ArticleBuilder.php @@ -34,11 +34,11 @@ class ArticleBuilder extends AbstractBuilder $modify_js = 'onclick="openEditor(\'' . $id . '\')"'; $modify_article = '

' . "\n"; - $up_link = new URL(['page' => CURRENT_PAGE, 'id' => $id, 'action' => 'position_up']); - $up_button = '

' . "\n"; + $up_js = 'onclick="switchPositions(\'' . $id . '\', \'up\')"'; + $up_button = '

' . "\n"; - $down_link = new URL(['page' => CURRENT_PAGE, 'id' => $id, 'action' => 'position_down']); - $down_button = '

' . "\n"; + $down_js = 'onclick="switchPositions(\'' . $id . '\', \'down\')"'; + $down_button = '

' . "\n"; $delete_js = 'onclick="deleteArticle(\'' . $id . '\')"'; $delete_article = '

' . "\n"; diff --git a/src/view/NewBuilder.php b/src/view/NewBuilder.php index 605c174..56d1f4d 100644 --- a/src/view/NewBuilder.php +++ b/src/view/NewBuilder.php @@ -53,8 +53,8 @@ class NewBuilder extends AbstractBuilder $modify_js = 'onclick="openEditor(\'' . $id . '\')"'; $modify_article = '

' . "\n"; - $up_button = '

' . "\n"; - $down_button = '

' . "\n"; + $up_button = ''; + $down_button = ''; $delete_js = 'onclick="deleteArticle(\'' . $id . '\', \'' . CURRENT_PAGE . '\')"'; $delete_article = '

' . "\n"; @@ -68,11 +68,11 @@ class NewBuilder extends AbstractBuilder else{ $modify_article = '

' . "\n"; - $up_link = new URL(['page' => CURRENT_PAGE, 'id' => $id, 'action' => 'position_up']); - $up_button = '

' . "\n"; + $up_js = 'onclick="switchPositions(\'' . $id . '\', \'up\')"'; + $up_button = '

' . "\n"; - $down_link = new URL(['page' => CURRENT_PAGE, 'id' => $id, 'action' => 'position_down']); - $down_button = '

' . "\n"; + $down_js = 'onclick="switchPositions(\'' . $id . '\', \'down\')"'; + $down_button = '

' . "\n"; $delete_js = 'onclick="deleteArticle(\'' . $id . '\')"'; $delete_article = '

' . "\n"; -- cgit v1.2.3