Search code examples
phpmodel-view-controllerjoomla3.0joomla-component

Joomla Call another Model and fill data


I am Developing a Joomla Component for Joomla 3.x.

The Component is for Accounting and managing Properties.

In property view i've made a button where it goes to another view, for uploading a File.

I've given the object ID in the URL, but this is a little insecure i think and harder to manage also with redirecting.

I could not find how to do it right. My thought is in the property view getting the model and set the property id, then when clicking the button it jumps to that view.

In the View iv'e added this line, but it is wrong because it's not storing the id of property if an error occures, all other data is stored.

$jinput     = JFactory::getApplication()->input;

    if (empty($this->item->object_id))
    {
        $this->item->object_id = $jinput->get('object_id', '');
        $this->form->setValue('object_id', null, $this->item->object_id);
    }

I think i should insert this data in the model, but i don't know where and how.

can anyone give me a hint how to do it the right way?

Also is there an easy way to redirect back to the called view? I've done so far like this:

View detail of property:

<div class="action-bt">
        <?php $upload_pdf = JRoute::_('index.php?option=com_immo&back=object_detail&view=pdfupload&layout=edit&object_id=' . (int) $this->object->id); ?>
        <a href="<?php echo $upload_pdf; ?>"><?php echo JText::_('PDF Upload') ?></a>
    </div>

Controller of upload

$return = parent::cancel($key);

    $back = $this->input->get('jform', array(), 'array');

    if (!empty($this->input->get('back')))
    {
        $url
            = \JRoute::_('index.php?option=com_immo&view=object&layout=detail&task=object.detail&id=' . $back['object_id'], false);

        // Redirect back to the edit screen.
        $this->setRedirect(
            $url
        );
    }

    return $return;

Edit tmpl

    <input type="hidden" name="back" value="<?php echo isset($_GET['back']) ? $_GET['back'] : '' ?>"/>

Thanks for your help

Edit

I got 2 Views: 1. Property View, which redirects to the PDFUpload (edit) page for adding a new PDF 2. The PDF List view which also opens the PDFUpload (edit) view when clicking on new

The userState was a good hint.

But now my problem is how to make it right to redirect to the calling view I think this could also be done with a userState

But i am not sure if there is a proper way of doing this. On the cancel function i have seen that there is a code line:

// Check if there is a return value
$return = $this->input->get('return', null, 'base64');

if (!is_null($return) && \JUri::isInternal(base64_decode($return)))
{
    $url = base64_decode($return);
}

Solution

  • Your button in the view will link to the controller. Note that you don't set the view or layout here.

    use \Joomla\CMS\Router\Route;    
    
    <a class="btn btn-primary" href="<?php echo Route::_('index.php?option=com_immo&task=object.edit&id=' . $back['object_id'], false); ?>">Edit Object</a>
    

    In your controller function, you can set the current id being edited to the State.

    use \Joomla\CMS\Factory;
    use \Joomla\CMS\Router\Route;    
    
    public function edit($key = NULL, $urlVar = NULL)
    {
        $app = Factory::getApplication();
    
        // Get the previous edit id (if any) and the current edit id.
        $previousId = (int) $app->getUserState('com_immo.edit.object.id');
        $editId     = $app->input->getInt('id', 0);
    
        // Set the user id for the user to edit in the session.
        $app->setUserState('com_immo.edit.object.id', $editId);
    
        // Get the model.
        $model = $this->getModel('ObjectForm', 'ImmoModel');
    
        // Check out the item
        if ($editId)
        {
            $model->checkout($editId);
        }
    
        // Check in the previous user.
        if ($previousId)
        {
            $model->checkin($previousId);
        }
    
        // Redirect to the edit screen.
        $this->setRedirect(Route::_('index.php?option=com_immo&view=object&layout=edit', false));
    }
    

    The task will then redirect the user to the edit page. You can access the id with:

    $editId = $app->getUserState('com_immo.edit.object.id');
    

    You can read more at the Joomla API page https://api.joomla.org/cms-3/classes/JApplication.html#method_getUserState