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