Hello together,
at the moment I script an custom upload extension for my website and it works now (more or less).
Unfortunately, I am not satisfied with the result, as I have adapted a lot of the standard code in the model and in my controller. Maybe someone here can help me how I can make it “clean”. The problem here is that I have to intercept the data from the uploaded file separately and process it in the model. This in turn causes problems in the controller, which then fails to recognize the task that was triggered, so I have to return the new $id, which was created after save in my model. Here is my shortened Joomla 4 / 5 code:
DownloadController:DownloadModel.php:
Thank you very much for helping me
Marcus
at the moment I script an custom upload extension for my website and it works now (more or less).
Unfortunately, I am not satisfied with the result, as I have adapted a lot of the standard code in the model and in my controller. Maybe someone here can help me how I can make it “clean”. The problem here is that I have to intercept the data from the uploaded file separately and process it in the model. This in turn causes problems in the controller, which then fails to recognize the task that was triggered, so I have to return the new $id, which was created after save in my model. Here is my shortened Joomla 4 / 5 code:
DownloadController:
Code:
public function save($key = null, $urlVar = null) { $input = Factory::getApplication()->input; $data = $input->get('jform', [], 'array'); $file = $input->files->get('jform')['file_name'] ?? null; $model = $this->getModel('Download'); $id = $model->save($data); if ($file && !empty($file['name'])) { // Dateiname sicherstellen und Upload-Pfad festlegen $fileName = File::makeSafe($file['name']); $tempPath = $file['tmp_name']; $uploadDir = JPATH_SITE . '/images/downloads/'; $uploadPath = $uploadDir . $fileName; // Datei hochladen if (File::upload($tempPath, $uploadPath)) { // Speichere den Pfad zur Datei in den Daten $data['file_name'] = 'images/downloads/' . $fileName; //$input->set('jform', $data); } else { $errorMessage = Factory::getApplication()->enqueueMessage(Text::_('Fehler beim Hochladen der Datei.'), 'error'); Factory::getApplication()->enqueueMessage($errorMessage, 'error'); $this->setRedirect(Route::_('index.php?option=com_downloads&view=downloads', false)); return false; } } $task = $input->getCmd('task'); if ($id) { switch ($task) { case 'apply': $successMessage = Text::_('COM_DOWNLOADS_ITEM_UPDATED'); $this->setRedirect(Route::_('index.php?option=com_downloads&view=download&layout=edit&id=' . (int) $id, false)); break; case 'save2new': // Wenn 'save2new', zurück zur Edit-Seite mit leeren Feldern $successMessage = Text::_('COM_DOWNLOADS_SUCCESS_MESSAGE'); $this->setRedirect(Route::_('index.php?option=com_downloads&view=download&layout=edit', false)); break; case 'save': default: $successMessage = Text::_('COM_DOWNLOADS_ITEM_UPDATED'); $this->setRedirect(Route::_('index.php?option=com_downloads&view=downloads&layout=success', false)); break; } } else { $errorMessage = $model->getError(); Factory::getApplication()->enqueueMessage($errorMessage, 'error'); $this->setRedirect(Route::_('index.php?option=com_downloads&view=downloads', false)); } Factory::getApplication()->enqueueMessage($successMessage, 'message'); }
Code:
public function save($data) { $input = Factory::getApplication()->input; $file = $input->files->get('jform')['file_name'] ?? null; $fileName = File::makeSafe($file['name']); $data['file_name'] = 'images/downloads/' . $fileName; if (empty($data['id'])) { $data['created_by'] = Factory::getUser()->id; $data['created'] = date('Y-m-d H:i:s'); } else { unset($data['created']); $data['modified_by'] = Factory::getUser()->id; $data['modified'] = date('Y-m-d H:i:s'); } //$input->set('jform', $data); $result = parent::save($data); // Überprüfe, ob das Speichern erfolgreich war if ($result) { // Gib die ID des gespeicherten Datensatzes zurück return $this->getState($this->getName() . '.id'); } return false; }
Marcus
Statistics: Posted by MarcusR — Sat Nov 02, 2024 1:49 pm