aboutsummaryrefslogtreecommitdiff
path: root/src/controller/HeadFootController.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/controller/HeadFootController.php')
-rw-r--r--src/controller/HeadFootController.php141
1 files changed, 0 insertions, 141 deletions
diff --git a/src/controller/HeadFootController.php b/src/controller/HeadFootController.php
deleted file mode 100644
index 72487d8..0000000
--- a/src/controller/HeadFootController.php
+++ /dev/null
@@ -1,141 +0,0 @@
1<?php
2// src/controller/HeadFootController.php
3
4declare(strict_types=1);
5
6use App\Entity\NodeData;
7use App\Entity\Asset;
8use App\Entity\AssetEmployment;
9use Doctrine\ORM\EntityManager;
10use Symfony\Component\HttpFoundation\JsonResponse;
11
12class HeadFootController
13{
14 static public function setTextData(EntityManager $entityManager, string $request_params, array $json): JsonResponse
15 {
16 $params_array = explode('_', $request_params); // header_title, header_description, footer_name, footer_address, footer_email
17 if(count($params_array) !== 2){
18 return new JsonResponse(['success' => false]);
19 }
20
21 $model = new Model($entityManager);
22 if($model->findWhateverNode('name_node', $params_array[0])){
23 $node_data = $model->getNode()->getNodeData();
24
25 // liens réseaux sociaux
26 if(in_array($params_array[1], NodeData::$social_networks)){
27 $social = $node_data->getData()['social'] ?? [];
28 $social[$params_array[1]] = $json['new_text'];
29 $node_data->updateData('social', $social);
30 }
31 // autres textes
32 else{
33 $node_data->updateData($params_array[1], $json['new_text']); // $params_array[1] n'est pas contrôlé
34 }
35
36 $entityManager->flush();
37 return new JsonResponse(['success' => true]);
38 }
39 else{
40 return new JsonResponse(['success' => false]);
41 }
42 }
43 static public function uploadAsset(EntityManager $entityManager, string $request_params): JsonResponse
44 {
45 if(empty($_FILES)){
46 return new JsonResponse(['success' => false], JsonResponse::HTTP_BAD_REQUEST); // code 400
47 }
48 else{
49 if(!is_dir(Asset::USER_PATH)){
50 mkdir(Asset::USER_PATH, 0777, true);
51 }
52
53 /* -- téléchargement -- */
54 $file = $_FILES['file'];
55 $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'tif', 'ico', 'bmp']; // pas de SVG
56 $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
57 if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){
58 $extension = 'jpeg';
59 }
60 $mime_type = mime_content_type($file['tmp_name']);
61 $hash = hash_file('sha256', $file['tmp_name']);
62
63 /* -- instance d'Asset -- */
64 $model = new Model($entityManager);
65 $result = $model->getWhatever('App\Entity\Asset', 'hash', $hash);
66
67 if(count($result) > 0){ // asset existant trouvé
68 $asset = $result[0];
69
70 // correction des informations
71 $name = $asset->getFileName(); // permet à priori de réécrire par dessus le précédent fichier
72 //$asset->setFileName($name);
73 $asset->setMimeType($mime_type);
74 }
75 else{
76 $name = Security::secureFileName(pathinfo($file['name'], PATHINFO_FILENAME));
77 $name = uniqid($name . '_') . '.' . $extension;
78 $asset = new Asset($name, $mime_type, $hash);
79 }
80
81 /* -- écriture du fichier sur le disque -- */
82 if(!ImageUploadController::imagickCleanAndWriteImage(file_get_contents($file['tmp_name']), Asset::USER_PATH . $name)){ // recréer l’image pour la nettoyer
83 return new JsonResponse(['success' => false, 'message' => "Erreur de l'enregistrement de l'image: problème de permission ou format non valide.", 'format' => $extension], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); // code 500
84 }
85 else{
86 $params_array = explode('_', $request_params); // head_favicon, header_logo, header_background, footer_logo
87
88 /* -- table intermédiaire node_data/asset-- */
89 if($model->findWhateverNode('name_node', $params_array[0])){ // noeud (head, header ou footer)
90 $node_data = $model->getNode()->getNodeData();
91
92 // recherche à l'aide du rôle
93 $old_nda = null;
94 foreach($node_data->getNodeDataAssets() as $nda){
95 if($nda->getRole() === $request_params){
96 $old_nda = $nda;
97 $old_nda->setAsset($asset);
98 break;
99 }
100 }
101 // entrée pas trouvée
102 if($old_nda === null){
103 $new_nda = new AssetEmployment($node_data, $asset, $request_params); // $request_params sera le rôle de l'asset
104 $entityManager->persist($new_nda);
105 }
106
107 if(count($result) === 0){
108 $entityManager->persist($asset);
109 }
110 $entityManager->flush();
111 return new JsonResponse(['success' => true, 'location' => Asset::USER_PATH . $name, 'mime_type' => $mime_type]);
112 }
113 else{
114 return new JsonResponse(['success' => false, 'message' => "Erreur noeud non trouvé, c'est pas du tout normal!"], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); // code 500
115 }
116 }
117 }
118 }
119
120 static public function displaySocialNetwork(EntityManager $entityManager, string $request_params, array $json): JsonResponse
121 {
122 $params_array = explode('_', $request_params);
123 if(count($params_array) !== 2){
124 return new JsonResponse(['success' => false]);
125 }
126
127 $model = new Model($entityManager);
128 if(in_array($params_array[1], NodeData::$social_networks) && $model->findWhateverNode('name_node', $params_array[0])){
129 $node_data = $model->getNode()->getNodeData();
130 $social_show = $node_data->getData()['social_show'] ?? [];
131 $social_show[$params_array[1]] = $json['checked'];
132 $node_data->updateData('social_show', $social_show);
133
134 $entityManager->flush();
135 return new JsonResponse(['success' => true, 'checked' => $json['checked']]);
136 }
137 else{
138 return new JsonResponse(['success' => false]);
139 }
140 }
141} \ No newline at end of file