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