diff options
| author | polo <ordipolo@gmx.fr> | 2025-10-29 21:42:39 +0100 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2025-10-29 21:42:39 +0100 |
| commit | bce9fda51d334a547ec5a48f0b7699ed3b5d7944 (patch) | |
| tree | f4b3b57414a22c2b9542195bfd76eaf6e2853293 | |
| parent | 822f526fd7f4e89043e64b435961720b622bdb6e (diff) | |
| download | cms-bce9fda51d334a547ec5a48f0b7699ed3b5d7944.tar.gz cms-bce9fda51d334a547ec5a48f0b7699ed3b5d7944.tar.bz2 cms-bce9fda51d334a547ec5a48f0b7699ed3b5d7944.zip | |
classe Fetcher, gestion réseaux sociaux présents/absents, partie 2
| -rw-r--r-- | public/css/head.css | 2 | ||||
| -rw-r--r-- | public/js/Fetcher.js | 51 | ||||
| -rw-r--r-- | public/js/Input.js | 105 | ||||
| -rw-r--r-- | public/js/main.js | 21 | ||||
| -rw-r--r-- | src/controller/HeadFootController.php | 30 | ||||
| -rw-r--r-- | src/router.php | 3 | ||||
| -rw-r--r-- | src/view/HeadBuilder.php | 3 | ||||
| -rw-r--r-- | src/view/HeaderBuilder.php | 13 |
8 files changed, 161 insertions, 67 deletions
diff --git a/public/css/head.css b/public/css/head.css index b5f1bd1..1844f93 100644 --- a/public/css/head.css +++ b/public/css/head.css | |||
| @@ -87,7 +87,7 @@ header a | |||
| 87 | gap: 4px; | 87 | gap: 4px; |
| 88 | flex-wrap: wrap; | 88 | flex-wrap: wrap; |
| 89 | } | 89 | } |
| 90 | #header_social_content img | 90 | #header_social_content a img |
| 91 | { | 91 | { |
| 92 | width: 28px; | 92 | width: 28px; |
| 93 | background-color: #ffffffb3; | 93 | background-color: #ffffffb3; |
diff --git a/public/js/Fetcher.js b/public/js/Fetcher.js new file mode 100644 index 0000000..0f56628 --- /dev/null +++ b/public/js/Fetcher.js | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | class Fetcher{ | ||
| 2 | constructor(options = {}) { | ||
| 3 | this.endpoint = options.endpoint || 'index.php'; | ||
| 4 | this.method = options.method || 'POST'; // normalement c'est GET par défaut | ||
| 5 | |||
| 6 | // Callbacks optionnels | ||
| 7 | this.onSuccess = options.onSuccess || null; | ||
| 8 | this.onFailure = options.onFailure || null; | ||
| 9 | //this.onError = options.onError || null; // Pour les erreurs réseau/parsing | ||
| 10 | } | ||
| 11 | |||
| 12 | send(body){ | ||
| 13 | const options = { method: this.method }; | ||
| 14 | |||
| 15 | if(this.method !== 'GET'){ // si GET, ni body ni headers | ||
| 16 | if(body instanceof FormData){ // pas de headers | ||
| 17 | options.body = body; | ||
| 18 | } | ||
| 19 | else if(body !== null && typeof body === 'object'){ // objet => json | ||
| 20 | options.headers = { 'Content-Type': 'application/json' }; | ||
| 21 | options.body = JSON.stringify(body); | ||
| 22 | } | ||
| 23 | else{ // blob? | ||
| 24 | options.body = body; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | return fetch(this.endpoint, options) | ||
| 29 | .then(response => response.json()) | ||
| 30 | .then(data => this.onResponse(data)) | ||
| 31 | .catch(error => { | ||
| 32 | console.error('Erreur:', error); | ||
| 33 | }); | ||
| 34 | } | ||
| 35 | |||
| 36 | onResponse(data){ | ||
| 37 | if(data.success){ | ||
| 38 | if(this.onSuccess){ | ||
| 39 | this.onSuccess(data); | ||
| 40 | } | ||
| 41 | return{ success: true, data }; | ||
| 42 | } | ||
| 43 | else{ | ||
| 44 | if(this.onFailure){ | ||
| 45 | this.onFailure(data); | ||
| 46 | } | ||
| 47 | console.error(data.message || "Erreur serveur"); | ||
| 48 | return { success: false, data }; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } \ No newline at end of file | ||
diff --git a/public/js/Input.js b/public/js/Input.js index 54872d1..0ebbbbb 100644 --- a/public/js/Input.js +++ b/public/js/Input.js | |||
| @@ -1,9 +1,10 @@ | |||
| 1 | // mère | ||
| 1 | class InputToggler{ | 2 | class InputToggler{ |
| 2 | constructor(name, options = {}){ | 3 | constructor(name, options = {}){ |
| 3 | this.name = name; | 4 | this.name = name; |
| 4 | this.parent = document.getElementById(name); | 5 | this.parent = document.getElementById(name); |
| 5 | 6 | ||
| 6 | // ids alternatifs optionnels | 7 | // des ids alternatifs sont possibles |
| 7 | this.content_elem = this.parent.querySelector(options.content_selector || `#${name}_content`); | 8 | this.content_elem = this.parent.querySelector(options.content_selector || `#${name}_content`); |
| 8 | this.input_elem = this.parent.querySelector(options.input_selector || `#${name}_input`); | 9 | this.input_elem = this.parent.querySelector(options.input_selector || `#${name}_input`); |
| 9 | this.open_elem = this.parent.querySelector(options.open_selector || `#${name}_open`); | 10 | this.open_elem = this.parent.querySelector(options.open_selector || `#${name}_open`); |
| @@ -39,34 +40,26 @@ class InputToggler{ | |||
| 39 | } | 40 | } |
| 40 | } | 41 | } |
| 41 | 42 | ||
| 43 | |||
| 44 | // enfants | ||
| 42 | class InputText extends InputToggler{ | 45 | class InputText extends InputToggler{ |
| 43 | constructor(name, options = {}){ | 46 | constructor(name, options = {}){ |
| 44 | super(name, options); | 47 | super(name, options); |
| 45 | this.fetch_endpoint = options.endpoint || 'index.php'; | 48 | this.fetcher = new Fetcher({ |
| 46 | this.fetch_key = options.fetch_key || 'head_foot_text'; | 49 | endpoint: (options.endpoint || 'index.php?') + (options.fetch_key || 'head_foot_text=') + this.name, |
| 50 | method: 'POST', | ||
| 51 | onSuccess: (data) => this.onSuccess(data) | ||
| 52 | }); | ||
| 47 | } | 53 | } |
| 48 | submit(){ | 54 | submit(){ |
| 49 | fetch(this.fetch_endpoint + '?' + this.fetch_key + '=' + this.name, { | 55 | this.fetcher.send({new_text: this.input_elem.value}) |
| 50 | method: 'POST', | 56 | .then(result => { |
| 51 | headers: { 'Content-Type': 'application/json' }, | 57 | toastNotify(result.success ? 'texte modifié avec succès' : "erreur: la modification des données côté serveur a échoué"); |
| 52 | body: JSON.stringify({new_text: this.input_elem.value}) | 58 | }); |
| 53 | }) | ||
| 54 | .then(response => response.json()) | ||
| 55 | .then(data => { | ||
| 56 | if(data.success){ | ||
| 57 | this.onSuccess(data); | ||
| 58 | this.close(); | ||
| 59 | } | ||
| 60 | else{ | ||
| 61 | console.error("Erreur: le serveur n'a pas enregistré le nouveau texte."); | ||
| 62 | } | ||
| 63 | }) | ||
| 64 | .catch(error => { | ||
| 65 | console.error('Erreur:', error); | ||
| 66 | }); | ||
| 67 | } | 59 | } |
| 68 | onSuccess(data){ | 60 | onSuccess(data){ |
| 69 | this.content_elem.innerHTML = this.input_elem.value; | 61 | this.content_elem.innerHTML = this.input_elem.value; |
| 62 | this.close(); | ||
| 70 | } | 63 | } |
| 71 | cancel(){ | 64 | cancel(){ |
| 72 | this.input_elem.value = this.content_elem.innerHTML; | 65 | this.input_elem.value = this.content_elem.innerHTML; |
| @@ -74,29 +67,15 @@ class InputText extends InputToggler{ | |||
| 74 | } | 67 | } |
| 75 | } | 68 | } |
| 76 | 69 | ||
| 77 | class InputTextSocialNetwork extends InputText{ | ||
| 78 | open(){ | ||
| 79 | const elem_parent = this.content_elem.parentNode; | ||
| 80 | if(elem_parent.tagName.toLowerCase() === 'a'){ | ||
| 81 | this.input_elem.value = elem_parent.href; | ||
| 82 | } | ||
| 83 | super.open(); | ||
| 84 | } | ||
| 85 | onSuccess(data){ | ||
| 86 | if(this.input_elem.value){ | ||
| 87 | this.content_elem.parentNode.href = this.input_elem.value; | ||
| 88 | } | ||
| 89 | else{ | ||
| 90 | this.content_elem.parentNode.removeAttribute('href'); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | class InputFile extends InputToggler{ | 70 | class InputFile extends InputToggler{ |
| 96 | constructor(name, options = {}){ | 71 | constructor(name, options = {}){ |
| 97 | super(name, options); | 72 | super(name, options); |
| 98 | this.fetch_endpoint = options.endpoint || 'index.php'; | 73 | this.fetcher = new Fetcher({ |
| 99 | this.fetch_key = options.fetch_key || 'head_foot_image'; | 74 | endpoint: (options.endpoint || 'index.php?') + (options.fetch_key || 'head_foot_image=') + this.name, |
| 75 | method: 'POST', | ||
| 76 | onSuccess: (data) => this.onSuccess(data), | ||
| 77 | onFailure: (data) => this.onFailure(data) | ||
| 78 | }); | ||
| 100 | } | 79 | } |
| 101 | submit(){ | 80 | submit(){ |
| 102 | const file = this.input_elem.files[0]; | 81 | const file = this.input_elem.files[0]; |
| @@ -108,27 +87,14 @@ class InputFile extends InputToggler{ | |||
| 108 | const form_data = new FormData(); | 87 | const form_data = new FormData(); |
| 109 | form_data.append('file', file); | 88 | form_data.append('file', file); |
| 110 | 89 | ||
| 111 | fetch(this.fetch_endpoint + '?' + this.fetch_key + '=' + this.name, { | 90 | this.fetcher.send(form_data) |
| 112 | method: 'POST', // apparemment il faudrait utiliser PUT | 91 | .then(result => { |
| 113 | body: form_data | 92 | toastNotify(result.success ? 'fichier téléchargé avec succès' : "erreur: la modification des données côté serveur a échoué"); |
| 114 | }) | 93 | }); |
| 115 | .then(response => response.json()) | ||
| 116 | .then(data => { | ||
| 117 | if(data.success){ | ||
| 118 | this.onSuccess(data); | ||
| 119 | this.close(); | ||
| 120 | } | ||
| 121 | else{ | ||
| 122 | this.onFailure(data); | ||
| 123 | console.error(data.message); | ||
| 124 | } | ||
| 125 | }) | ||
| 126 | .catch(error => { | ||
| 127 | console.error('Erreur:', error); | ||
| 128 | }); | ||
| 129 | } | 94 | } |
| 130 | onSuccess(data){ | 95 | onSuccess(data){ |
| 131 | this.content_elem.src = data.location; | 96 | this.content_elem.src = data.location; |
| 97 | this.close(); | ||
| 132 | } | 98 | } |
| 133 | onFailure(data){ | 99 | onFailure(data){ |
| 134 | if(data.format === 'ico'){ | 100 | if(data.format === 'ico'){ |
| @@ -137,6 +103,8 @@ class InputFile extends InputToggler{ | |||
| 137 | } | 103 | } |
| 138 | } | 104 | } |
| 139 | 105 | ||
| 106 | |||
| 107 | // petits enfants | ||
| 140 | class InputFileFavicon extends InputFile{ | 108 | class InputFileFavicon extends InputFile{ |
| 141 | onSuccess(data){ | 109 | onSuccess(data){ |
| 142 | const link = document.querySelector('link[rel="icon"]'); | 110 | const link = document.querySelector('link[rel="icon"]'); |
| @@ -150,4 +118,23 @@ class InputFileHeaderBackground extends InputFile{ | |||
| 150 | document.querySelector('header').style.backgroundImage = `url('${data.location}')`; | 118 | document.querySelector('header').style.backgroundImage = `url('${data.location}')`; |
| 151 | super.onSuccess(data); | 119 | super.onSuccess(data); |
| 152 | } | 120 | } |
| 121 | } | ||
| 122 | |||
| 123 | class InputTextSocialNetwork extends InputText{ | ||
| 124 | open(){ | ||
| 125 | const elem_parent = this.content_elem.parentNode; | ||
| 126 | if(elem_parent.tagName.toLowerCase() === 'a'){ | ||
| 127 | this.input_elem.value = elem_parent.href; | ||
| 128 | } | ||
| 129 | super.open(); | ||
| 130 | } | ||
| 131 | onSuccess(data){ | ||
| 132 | if(this.input_elem.value){ | ||
| 133 | this.content_elem.parentNode.href = this.input_elem.value; | ||
| 134 | } | ||
| 135 | else{ | ||
| 136 | this.content_elem.parentNode.removeAttribute('href'); | ||
| 137 | } | ||
| 138 | this.close(); // vu qu'on n'appelle pas super.onSuccess | ||
| 139 | } | ||
| 153 | } \ No newline at end of file | 140 | } \ No newline at end of file |
diff --git a/public/js/main.js b/public/js/main.js index 59a9331..3cc144a 100644 --- a/public/js/main.js +++ b/public/js/main.js | |||
| @@ -199,6 +199,27 @@ function findParentByTagName(element, tag_name){ | |||
| 199 | return null; | 199 | return null; |
| 200 | } | 200 | } |
| 201 | 201 | ||
| 202 | function checkSocialNetwork(id){ | ||
| 203 | const checkbox = document.getElementById(id).querySelector('input[type="checkbox"]'); | ||
| 204 | |||
| 205 | new Fetcher({ | ||
| 206 | endpoint: 'index.php?head_foot_social_check=' + id, | ||
| 207 | method: 'POST', | ||
| 208 | onSuccess: (data) => { | ||
| 209 | console.log(data); | ||
| 210 | |||
| 211 | checkbox.checked = data.checked; | ||
| 212 | |||
| 213 | /*const color = checkbox.checked ? "#ff1d04" : "grey"; | ||
| 214 | clicked_menu_entry.querySelector("button").style.color = color;*/ | ||
| 215 | }, | ||
| 216 | onFailure: (data) => { | ||
| 217 | console.log(data); | ||
| 218 | } | ||
| 219 | }) | ||
| 220 | .send({checked: checkbox.checked}); | ||
| 221 | } | ||
| 222 | |||
| 202 | 223 | ||
| 203 | /* -- fonctions spécifiques à la gestion des dates -- */ | 224 | /* -- fonctions spécifiques à la gestion des dates -- */ |
| 204 | 225 | ||
diff --git a/src/controller/HeadFootController.php b/src/controller/HeadFootController.php index 7597683..a4727e2 100644 --- a/src/controller/HeadFootController.php +++ b/src/controller/HeadFootController.php | |||
| @@ -22,15 +22,17 @@ class HeadFootController | |||
| 22 | if($model->findWhateverNode('name_node', $params_array[0])){ | 22 | if($model->findWhateverNode('name_node', $params_array[0])){ |
| 23 | $node_data = $model->getNode()->getNodeData(); | 23 | $node_data = $model->getNode()->getNodeData(); |
| 24 | 24 | ||
| 25 | // liens réseaux sociaux | ||
| 25 | if(in_array($params_array[1], NodeData::$social_networks)){ | 26 | if(in_array($params_array[1], NodeData::$social_networks)){ |
| 26 | $social = $node_data->getData()['social']; | 27 | $social = $node_data->getData()['social'] ?? []; |
| 27 | $social[$params_array[1]] = $json['new_text']; | 28 | $social[$params_array[1]] = $json['new_text']; |
| 28 | $node_data->updateData('social', $social); | 29 | $node_data->updateData('social', $social); |
| 29 | } | 30 | } |
| 31 | // autres textes | ||
| 30 | else{ | 32 | else{ |
| 31 | $node_data->updateData($params_array[1], $json['new_text']); // $params_array[1] n'est pas contrôlé | 33 | $node_data->updateData($params_array[1], $json['new_text']); // $params_array[1] n'est pas contrôlé |
| 32 | } | 34 | } |
| 33 | 35 | ||
| 34 | $entityManager->flush(); | 36 | $entityManager->flush(); |
| 35 | echo json_encode(['success' => true]); | 37 | echo json_encode(['success' => true]); |
| 36 | } | 38 | } |
| @@ -120,5 +122,29 @@ class HeadFootController | |||
| 120 | die; | 122 | die; |
| 121 | } | 123 | } |
| 122 | 124 | ||
| 125 | static public function displaySocialNetwork(EntityManager $entityManager, string $request_params, array $json): void | ||
| 126 | { | ||
| 127 | $params_array = explode('_', $request_params); | ||
| 128 | if(count($params_array) !== 2){ | ||
| 129 | echo json_encode(['success' => false]); | ||
| 130 | die; | ||
| 131 | } | ||
| 132 | |||
| 133 | $model = new Model($entityManager); | ||
| 134 | if(in_array($params_array[1], NodeData::$social_networks) && $model->findWhateverNode('name_node', $params_array[0])){ | ||
| 135 | $node_data = $model->getNode()->getNodeData(); | ||
| 136 | $social_show = $node_data->getData()['social_show'] ?? []; | ||
| 137 | $social_show[$params_array[1]] = $json['checked']; | ||
| 138 | $node_data->updateData('social_show', $social_show); | ||
| 139 | |||
| 140 | $entityManager->flush(); | ||
| 141 | echo json_encode(['success' => true, 'checked' => $json['checked']]); | ||
| 142 | } | ||
| 143 | else{ | ||
| 144 | echo json_encode(['success' => false]); | ||
| 145 | } | ||
| 146 | die; | ||
| 147 | } | ||
| 148 | |||
| 123 | //static public function uploadImage(EntityManager $entityManager, array $request_params): void | 149 | //static public function uploadImage(EntityManager $entityManager, array $request_params): void |
| 124 | } \ No newline at end of file | 150 | } \ No newline at end of file |
diff --git a/src/router.php b/src/router.php index ebe645f..2ba3b88 100644 --- a/src/router.php +++ b/src/router.php | |||
| @@ -145,6 +145,9 @@ elseif($request->getMethod() === 'POST'){ | |||
| 145 | elseif($request->query->has('head_foot_text')){ | 145 | elseif($request->query->has('head_foot_text')){ |
| 146 | HeadFootController::setTextData($entityManager, $request->query->get('head_foot_text'), $json); | 146 | HeadFootController::setTextData($entityManager, $request->query->get('head_foot_text'), $json); |
| 147 | } | 147 | } |
| 148 | elseif($request->query->has('head_foot_social_check')){ | ||
| 149 | HeadFootController::displaySocialNetwork($entityManager, $request->query->get('head_foot_social_check'), $json); | ||
| 150 | } | ||
| 148 | 151 | ||
| 149 | /* -- page Menu et chemins -- */ | 152 | /* -- page Menu et chemins -- */ |
| 150 | elseif(isset($_GET['menu_edit'])) | 153 | elseif(isset($_GET['menu_edit'])) |
diff --git a/src/view/HeadBuilder.php b/src/view/HeadBuilder.php index 88e69fb..e3d620c 100644 --- a/src/view/HeadBuilder.php +++ b/src/view/HeadBuilder.php | |||
| @@ -38,6 +38,9 @@ class HeadBuilder extends AbstractBuilder | |||
| 38 | // édition éléments sur toutes les pages (header, footer et favicon) | 38 | // édition éléments sur toutes les pages (header, footer et favicon) |
| 39 | $js .= '<script src="' . self::versionedFileURL('js', 'Input') . '"></script>' . "\n"; | 39 | $js .= '<script src="' . self::versionedFileURL('js', 'Input') . '"></script>' . "\n"; |
| 40 | 40 | ||
| 41 | // sert partout? | ||
| 42 | $js .= '<script src="' . self::versionedFileURL('js', 'Fetcher') . '"></script>' . "\n"; | ||
| 43 | |||
| 41 | // tinymce, nécéssite un script de copie dans composer.json | 44 | // tinymce, nécéssite un script de copie dans composer.json |
| 42 | $css .= '<link rel="stylesheet" href="' . self::versionedFileURL('css', 'tinymce') . '">' . "\n"; | 45 | $css .= '<link rel="stylesheet" href="' . self::versionedFileURL('css', 'tinymce') . '">' . "\n"; |
| 43 | $js .= '<script src="' . self::versionedFileURL('js', 'tinymce/tinymce.min') . '"></script>' . "\n"; // pour js/tinymce/tinymce.min.js | 46 | $js .= '<script src="' . self::versionedFileURL('js', 'tinymce/tinymce.min') . '"></script>' . "\n"; // pour js/tinymce/tinymce.min.js |
diff --git a/src/view/HeaderBuilder.php b/src/view/HeaderBuilder.php index 1cc4fc3..f988156 100644 --- a/src/view/HeaderBuilder.php +++ b/src/view/HeaderBuilder.php | |||
| @@ -84,10 +84,10 @@ class HeaderBuilder extends AbstractBuilder | |||
| 84 | // boucle sur la liste complète de réseaux sociaux | 84 | // boucle sur la liste complète de réseaux sociaux |
| 85 | foreach(NodeData::$social_networks as $network){ | 85 | foreach(NodeData::$social_networks as $network){ |
| 86 | $checked = (isset($social_show[$network]) && $social_show[$network]) ? 'checked' : ''; | 86 | $checked = (isset($social_show[$network]) && $social_show[$network]) ? 'checked' : ''; |
| 87 | $href = isset($social[$network]) ? 'href="' . $social[$network] . '"' : ''; | 87 | $href = (isset($social[$network]) && $social[$network] !== '') ? 'href="' . $social[$network] . '"' : ''; |
| 88 | 88 | ||
| 89 | $social_networks .= '<div id="header_' . $network . '"> | 89 | $social_networks .= '<div id="header_' . $network . '"> |
| 90 | <input type="checkbox" onclick="" ' . $checked . '> | 90 | <input type="checkbox" onclick="checkSocialNetwork(\'header_' . $network . '\')" ' . $checked . '> |
| 91 | <a ' . $href . ' target="_blank" rel="noopener noreferrer"> | 91 | <a ' . $href . ' target="_blank" rel="noopener noreferrer"> |
| 92 | <img id="header_' . $network . '_content" src="assets/' . $network . '.svg" alt="'. $network . '_alt"> | 92 | <img id="header_' . $network . '_content" src="assets/' . $network . '.svg" alt="'. $network . '_alt"> |
| 93 | </a> | 93 | </a> |
| @@ -110,9 +110,12 @@ class HeaderBuilder extends AbstractBuilder | |||
| 110 | if(isset($social_show)){ | 110 | if(isset($social_show)){ |
| 111 | // boucle sur les réseaux sociaux "activés" | 111 | // boucle sur les réseaux sociaux "activés" |
| 112 | foreach(array_keys($social_show) as $network){ | 112 | foreach(array_keys($social_show) as $network){ |
| 113 | $social_networks .= '<div id="header_' . $network . '"> | 113 | if($social_show[$network]){ |
| 114 | <a href="' . $social[$network] . '" target="_blank" rel="noopener noreferrer"><img id="header_' . $network . '_content" src="assets/' . $network . '.svg" alt="'. $network . '_alt"></a> | 114 | $href = (isset($social[$network]) && $social[$network] !== '') ? 'href="' . $social[$network] . '"' : ''; |
| 115 | </div>'; | 115 | $social_networks .= '<div id="header_' . $network . '"> |
| 116 | <a ' . $href . ' target="_blank" rel="noopener noreferrer"><img id="header_' . $network . '_content" src="assets/' . $network . '.svg" alt="'. $network . '_alt"></a> | ||
| 117 | </div>'; | ||
| 118 | } | ||
| 116 | } | 119 | } |
| 117 | } | 120 | } |
| 118 | } | 121 | } |
