|
5 | 5 | namespace CustomFields\Controller\Admin; |
6 | 6 |
|
7 | 7 | use CustomFields\Form\CustomFieldValueForm; |
| 8 | +use CustomFields\Model\CustomFieldImage; |
| 9 | +use CustomFields\Model\CustomFieldImageQuery; |
8 | 10 | use CustomFields\Model\CustomFieldQuery; |
9 | 11 | use CustomFields\Model\CustomFieldValueQuery; |
10 | 12 | use CustomFields\Model\Map\CustomFieldTableMap; |
11 | 13 | use Propel\Runtime\Exception\PropelException; |
| 14 | +use Symfony\Component\HttpFoundation\File\UploadedFile; |
12 | 15 | use Symfony\Component\HttpFoundation\RedirectResponse; |
13 | 16 | use Symfony\Component\HttpFoundation\Response; |
14 | 17 | use Symfony\Component\HttpKernel\Attribute\AsController; |
@@ -59,25 +62,85 @@ public function saveCustomFieldValues(): Response |
59 | 62 |
|
60 | 63 | foreach ($customFields as $customField) { |
61 | 64 | $fieldKey = 'custom_field_'.$customField->getId(); |
62 | | - $value = $this->getRequest()->request->get($fieldKey); |
63 | | - |
64 | | - if (null !== $value) { |
65 | | - // Find or create custom field value |
66 | | - $customFieldValue = CustomFieldValueQuery::create() |
67 | | - ->filterByCustomFieldId($customField->getId()) |
68 | | - ->filterBySource($source) |
69 | | - ->filterBySourceId($sourceId) |
70 | | - ->findOneOrCreate(); |
71 | | - |
72 | | - if (in_array($customField->getType(), self::CUSTOM_FIELD_SIMPLE_VALUES)) { |
73 | | - $customFieldValue |
74 | | - ->setSimpleValue($value) |
75 | | - ->save(); |
76 | | - } else { |
77 | | - $customFieldValue |
78 | | - ->setLocale($locale) |
79 | | - ->setValue($value) |
80 | | - ->save(); |
| 65 | + |
| 66 | + // Handle image type separately |
| 67 | + if ($customField->getType() === CustomFieldTableMap::COL_TYPE_IMAGE) { |
| 68 | + // Skip if no file field exists in the request |
| 69 | + if (!$this->getRequest()->files->has($fieldKey)) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + try { |
| 74 | + /** @var UploadedFile|null $uploadedFile */ |
| 75 | + $uploadedFile = $this->getRequest()->files->get($fieldKey); |
| 76 | + |
| 77 | + // Skip if no valid file was uploaded (empty file input) |
| 78 | + if (!$uploadedFile instanceof UploadedFile || $uploadedFile->getError() !== UPLOAD_ERR_OK) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + // Create upload directory if it doesn't exist |
| 83 | + $uploadDir = THELIA_LOCAL_DIR . 'media' . DS . 'images' . DS . 'customField'; |
| 84 | + if (!is_dir($uploadDir)) { |
| 85 | + mkdir($uploadDir, 0777, true); |
| 86 | + } |
| 87 | + |
| 88 | + // Generate unique filename |
| 89 | + $fileName = uniqid() . '_' . $uploadedFile->getClientOriginalName(); |
| 90 | + $uploadedFile->move($uploadDir, $fileName); |
| 91 | + |
| 92 | + // Find or create custom field value first |
| 93 | + $customFieldValue = CustomFieldValueQuery::create() |
| 94 | + ->filterByCustomFieldId($customField->getId()) |
| 95 | + ->filterBySource($source) |
| 96 | + ->filterBySourceId($sourceId) |
| 97 | + ->findOneOrCreate(); |
| 98 | + |
| 99 | + $customFieldValue->save(); |
| 100 | + |
| 101 | + // Check if image already exists for this custom field value |
| 102 | + $customFieldImage = CustomFieldImageQuery::create() |
| 103 | + ->filterByCustomFieldValueId($customFieldValue->getId()) |
| 104 | + ->findOne(); |
| 105 | + |
| 106 | + if (!$customFieldImage) { |
| 107 | + $customFieldImage = new CustomFieldImage(); |
| 108 | + $customFieldImage->setCustomFieldValueId($customFieldValue->getId()); |
| 109 | + } else { |
| 110 | + // Delete old file if exists |
| 111 | + $oldFile = $uploadDir . DS . $customFieldImage->getFile(); |
| 112 | + if (file_exists($oldFile)) { |
| 113 | + unlink($oldFile); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + $customFieldImage->setFile($fileName); |
| 118 | + $customFieldImage->save(); |
| 119 | + } catch (\Exception $e) { |
| 120 | + // Skip this field if there's an error with the file |
| 121 | + continue; |
| 122 | + } |
| 123 | + } else { |
| 124 | + $value = $this->getRequest()->request->get($fieldKey); |
| 125 | + |
| 126 | + if (null !== $value) { |
| 127 | + // Find or create custom field value |
| 128 | + $customFieldValue = CustomFieldValueQuery::create() |
| 129 | + ->filterByCustomFieldId($customField->getId()) |
| 130 | + ->filterBySource($source) |
| 131 | + ->filterBySourceId($sourceId) |
| 132 | + ->findOneOrCreate(); |
| 133 | + |
| 134 | + if (in_array($customField->getType(), self::CUSTOM_FIELD_SIMPLE_VALUES)) { |
| 135 | + $customFieldValue |
| 136 | + ->setSimpleValue($value) |
| 137 | + ->save(); |
| 138 | + } else { |
| 139 | + $customFieldValue |
| 140 | + ->setLocale($locale) |
| 141 | + ->setValue($value) |
| 142 | + ->save(); |
| 143 | + } |
81 | 144 | } |
82 | 145 | } |
83 | 146 | } |
@@ -137,4 +200,46 @@ public function saveCustomFieldValues(): Response |
137 | 200 | ]) |
138 | 201 | ); |
139 | 202 | } |
| 203 | + |
| 204 | + #[Route(path: '/image/delete/{id}', name: 'image_delete', methods: ['POST', 'GET'])] |
| 205 | + public function deleteCustomFieldImage(int $id): Response |
| 206 | + { |
| 207 | + if (null !== $response = $this->checkAuth(AdminResources::MODULE, 'CustomFields', AccessManager::DELETE)) { |
| 208 | + return $response; |
| 209 | + } |
| 210 | + |
| 211 | + $customFieldImage = CustomFieldImageQuery::create()->findPk($id); |
| 212 | + |
| 213 | + if (!$customFieldImage) { |
| 214 | + return new RedirectResponse( |
| 215 | + URL::getInstance()->absoluteUrl($this->getRequest()->headers->get('referer', '/admin/module/customfields/list'), [ |
| 216 | + 'error' => Translator::getInstance()->trans('Image not found', [], 'customfields'), |
| 217 | + ]) |
| 218 | + ); |
| 219 | + } |
| 220 | + |
| 221 | + try { |
| 222 | + // Delete physical file |
| 223 | + $uploadDir = $customFieldImage->getUploadDir(); |
| 224 | + $file = $uploadDir . DS . $customFieldImage->getFile(); |
| 225 | + if (file_exists($file)) { |
| 226 | + unlink($file); |
| 227 | + } |
| 228 | + |
| 229 | + // Delete database entry |
| 230 | + $customFieldImage->delete(); |
| 231 | + |
| 232 | + return new RedirectResponse( |
| 233 | + URL::getInstance()->absoluteUrl($this->getRequest()->headers->get('referer', '/admin/module/customfields/list'), [ |
| 234 | + 'success' => Translator::getInstance()->trans('Image deleted successfully', [], 'customfields'), |
| 235 | + ]) |
| 236 | + ); |
| 237 | + } catch (PropelException $e) { |
| 238 | + return new RedirectResponse( |
| 239 | + URL::getInstance()->absoluteUrl($this->getRequest()->headers->get('referer', '/admin/module/customfields/list'), [ |
| 240 | + 'error' => Translator::getInstance()->trans('An error occurred while deleting the image', [], 'customfields'), |
| 241 | + ]) |
| 242 | + ); |
| 243 | + } |
| 244 | + } |
140 | 245 | } |
0 commit comments