|
3 | 3 | namespace BitCode\FI\Triggers\BitForm; |
4 | 4 |
|
5 | 5 | use BitCode\FI\Flow\Flow; |
| 6 | +use BitCode\FI\Core\Util\Common; |
| 7 | +use BitCode\BitForm\Core\Util\FileHandler; |
6 | 8 |
|
7 | 9 | final class BitFormController |
8 | 10 | { |
@@ -100,24 +102,48 @@ public static function fields($form_id) |
100 | 102 | return $fields; |
101 | 103 | } |
102 | 104 |
|
103 | | - public static function handle_bitform_submit($formId, $entryId, $formData) |
| 105 | + public static function handle_bitform_submit($formId, $entryId, $formData, $files) |
104 | 106 | { |
105 | | - if (!empty($formId)) { |
106 | | - $data = []; |
107 | | - if ($entryId) { |
108 | | - $data['entry_id'] = $entryId; |
109 | | - } |
110 | | - foreach ($formData as $key => $value) { |
111 | | - if (\is_string($value) && str_contains($value, '__bf__')) { |
112 | | - $data[$key] = explode('__bf__', $value); |
113 | | - } else { |
114 | | - $data[$key] = $value; |
| 107 | + if (empty($formId)) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + $flows = Flow::exists('BitForm', $formId); |
| 112 | + |
| 113 | + if (empty($flows)) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + $data = [ |
| 118 | + 'form_id' => $formId, |
| 119 | + 'entry_id' => $entryId |
| 120 | + ]; |
| 121 | + |
| 122 | + foreach ($formData as $key => $value) { |
| 123 | + $data[$key] = (\is_string($value) && str_contains($value, '__bf__')) |
| 124 | + ? explode('__bf__', $value) |
| 125 | + : $value; |
| 126 | + } |
| 127 | + |
| 128 | + if (class_exists(FileHandler::class) && !empty($files)) { |
| 129 | + foreach ($files as $fieldKey => $fileGroup) { |
| 130 | + if (empty($fileGroup['new_name']) || !\is_array($fileGroup['new_name'])) { |
| 131 | + continue; |
115 | 132 | } |
116 | | - } |
117 | | - if (!empty($formId) && $flows = Flow::exists('BitForm', $formId)) { |
118 | | - Flow::execute('BitForm', $formId, $data, $flows); |
| 133 | + |
| 134 | + $uploadDir = FileHandler::getEntriesFileUploadDir($formId, $entryId); |
| 135 | + $fileUrls = []; |
| 136 | + |
| 137 | + foreach ($fileGroup['new_name'] as $filename) { |
| 138 | + $filePath = $uploadDir . DIRECTORY_SEPARATOR . $filename; |
| 139 | + $fileUrls[] = Common::fileUrl($filePath); |
| 140 | + } |
| 141 | + |
| 142 | + $data[$fieldKey] = \count($fileUrls) > 1 ? $fileUrls : reset($fileUrls); |
119 | 143 | } |
120 | 144 | } |
| 145 | + |
| 146 | + Flow::execute('BitForm', $formId, $data, $flows); |
121 | 147 | } |
122 | 148 |
|
123 | 149 | private static function isPluginActive() |
|
0 commit comments