Advertisement

Custom Extension -> Upload File (Controller / Model)

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
MarcusR
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Fri Jan 08, 2021 4:07 pm

Custom Extension -> Upload File (Controller / Model)

Post by MarcusR » Sat Nov 02, 2024 1:49 pm

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:

Code: Select all

    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');
    }
DownloadModel.php:

Code: Select all

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;
    }
Thank you very much for helping me
Marcus

Advertisement
User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 31407
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: Custom Extension -> Upload File (Controller / Model)

Post by Per Yngve Berg » Sat Nov 02, 2024 2:11 pm

Mod. Note: Relocated the topic to the Coding Forum.

SharkyKZ
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 3105
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Custom Extension -> Upload File (Controller / Model)

Post by SharkyKZ » Sun Nov 03, 2024 8:34 am

The model's save method must not return an ID, it must be return a boolean. If the method had a return type, this would result in an error. The model should also not be reading request data. What you could do instead is merge the file data with form data and pass it to the model.

MarcusR
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Fri Jan 08, 2021 4:07 pm

Re: Custom Extension -> Upload File (Controller / Model)

Post by MarcusR » Sun Nov 03, 2024 8:03 pm

I think, I got it with smaller code only in my controller. The model will be standard:
Here is my controller:

Code: Select all

public function save($key = null, $urlVar = null)
    {
        $data       = $this->input->post->get('jform', [], 'array');
        $file       = $this->input->files->get('jform')['file_name'] ?? null;
        $fileName   = File::makeSafe($file['name']);
        $tempPath   = $file['tmp_name'];
        $uploadDir  = JPATH_SITE . '/images/downloads/';
        $uploadPath = $uploadDir . $fileName;
        $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 {
            $data['modified_by'] = Factory::getUser()->id;
            $data['modified'] = date('Y-m-d H:i:s');
        }

        $this->input->post->set('jform', $data);
        if ($file && !empty($file['name'])) {
            if (File::upload($tempPath, $uploadPath)) {
            } 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;
            }
        }
        return parent::save($key, $urlVar);
    }

Advertisement

Post Reply

Return to “Joomla! 4.x Coding”