Simple PHP postSaveHook Method redirect after save

36 views
Skip to first unread message

cardman3000

unread,
Oct 23, 2020, 6:19:20 AM10/23/20
to Joomla Component Builder
Hello,

could you please tell me what's wrong or missing hier (PHP postSaveHook Method):

if($model->getState('asset.printer') == 3) {
$link = 'index.php?option=com_rsform&view=rsform&formId=5';
}

I just need (after save) to redirect  to an form site, only when printer value = 3.

Thanks

Peter Evgeniev

unread,
Oct 23, 2020, 6:40:00 AM10/23/20
to cardman3000, Joomla Component Builder
Hey Zdravko,

Your code is kind-a incomplete. It will not work just to define the redirect link. You need to add something like that:



//assuming you have that $app var defined
if ($app->isSite())
{
$this->redirect = $link;
}


I hope it helps.

Regards,
Peter


--
You received this message because you are subscribed to the Google Groups "Joomla Component Builder" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jcb+uns...@vdm.io.
To view this discussion on the web visit https://groups.google.com/a/vdm.io/d/msgid/jcb/515ff24b-9c3e-4c55-bafe-dda99a4763f3n%40vdm.io.


--
Warm Regards
Peter Evgeniev
+359 877 876 089
Message has been deleted

Peter Evgeniev

unread,
Oct 23, 2020, 9:58:37 AM10/23/20
to cardman3000, Joomla Component Builder
Hey, 

no it should either one or nested:


if($model->getState('asset.printer') == 3) {
$link = 'index.php?option=com_rsform&view=rsform&formId=5';
$this->redirect = $link;
}

or ->

if($model->getState('asset.printer') == 3) {
$link = 'index.php?option=com_rsform&view=rsform&formId=5';

if ($app->isSite())
{
$this->redirect = $link;
}

}


There are two reasons. You might want to redirect only if the var is equal to the value and only in the case that is the front end. In any other cases you might end up the backend doing it as well or redirect in every case but to the wrong address etc.

I hope this helps.

Regards,
Peter



On Fri, Oct 23, 2020 at 2:05 PM cardman3000 <zdravk...@gmail.com> wrote:
Hi Peter,

do you mean with two if statements:

if($model->getState('asset.printer') == 3) {
$link = 'index.php?option=com_rsform&view=rsform&formId=5';
}

cardman3000

unread,
Oct 23, 2020, 10:07:00 AM10/23/20
to Joomla Component Builder, peter.e...@gmail.com, Joomla Component Builder, cardman3000
Hello Peter,

I'm trying and trying.... without success :-(

that's all I can achieve with my JCB/PHP skills and of course it doesn't work:

$app = JFactory::getApplication();
$link = 'index.php?option=com_rsform&view=rsform&formId=5';

if($app->isSite() && $model->getState('asset.printer') == 3) {
$this->redirect = $link;
}

What else should I consider?

peter.e...@gmail.com schrieb am Freitag, 23. Oktober 2020 um 12:40:00 UTC+2:

Peter Evgeniev

unread,
Oct 23, 2020, 10:14:20 AM10/23/20
to cardman3000, Joomla Component Builder
Your skills are fine. 

What i would troubleshoot is if i get the correct values. So for instance try that:

$app = JFactory::getApplication();
$link = 'index.php?option=com_rsform&view=rsform&formId=5';

var_dump($model->getState('asset.printer'));
jexit();

if($app->isSite() && $model->getState('asset.printer') == 3) {
$this->redirect = $link;
}


This will output the value of the state and you will see if it is correct.Then if not you should troubleshoot why it is not working. If it has the correct value maybe you have to use different method to equalize it based on the type of the var eventually.

So start with dumping it out and then you can move forward with the next step.

I hope this helps.

Regards,
Peter

cardman3000

unread,
Oct 23, 2020, 10:50:37 AM10/23/20
to Joomla Component Builder, peter.e...@gmail.com, Joomla Component Builder, cardman3000
dump shows NULL with asset.printer.
I did a test with asset.id and voila: there I get an 'int (24)' result. The redirection also works for this data record.

My field type for "printer" is varchar50. This field is also listed in components/assetmanagement/models/asset.php with corrected name "printer".  
Can it be that "$model-> getState" can only read integer values?

Peter Evgeniev

unread,
Oct 23, 2020, 10:56:31 AM10/23/20
to cardman3000, Joomla Component Builder
This means only that you are not assigning that parameter and value to it. So can you explain in bit more details exactly what you are trying to do there? Maybe i could help with it.

Peter

cardman3000

unread,
Oct 23, 2020, 11:44:55 AM10/23/20
to Joomla Component Builder, peter.e...@gmail.com, Joomla Component Builder, cardman3000
Thank you Peter for this offer:

so, the table 'assetmanagement" contains few fields with most common purpose: customername, address, client, display, printer, . . . All of them are var(50) type.
Assetlist (Site view) works properly. Edit also.

Field 'printer' is as List (dropdown) configured and contains 6 items 1|USB Printer,2|BW Printer,3|Color Printer,4|Multifunction Printer, . . . and so on.
Also i have one form for each type of printer (toner, equipment order,...).
What I want to achieve is when the user selects printer 2 and clicks Save, that he will be redirected to form 4, wenn printer 3 then form 5, and so on.


Because I learn a lot from your help, here's one more question: what do you mean with:  " not assigning that parameter and value to it "?

Best regards and many thanks

Peter Evgeniev

unread,
Oct 23, 2020, 11:58:30 AM10/23/20
to cardman3000, Joomla Component Builder
I will answer backwards.

$model->getState('asset.printer') - this group consist of few parts. $model has all data related to the model and the getState function contains the state of different items. You were trying to get of an item that was not assigned at all.

With regarding to what you want i guess this will help:

$item = $model->getItem();
$printerVal = $item->get('printer');

if ($printerVal  == 3) {


$app = JFactory::getApplication();
$link = 'index.php?option=com_rsform&view=rsform&formId=5';

if ($app->isSite()) {
$this->redirect = $link;
}

}


What the above code does is takes the values from the model currently added record and assigns custom variable for the printer value. Then we compare it and execute the actions if it is equal to 3. You may have to dump as i described above the $item var in case you are not sure which is the correct printer parameter name. It should be the name of the field but i am not sure so just check if the above code doesn't work.

Give it a try and let me know if it works.

Peter

cardman3000

unread,
Oct 23, 2020, 12:15:40 PM10/23/20
to Joomla Component Builder, peter.e...@gmail.com, Joomla Component Builder, cardman3000
That's exactly what I wanted. It works perfectly!!!
Peter, thank you so much for your support..

Peter Evgeniev

unread,
Oct 23, 2020, 12:56:51 PM10/23/20
to cardman3000, Joomla Component Builder
Cool. Glad i was able to help.

P
Reply all
Reply to author
Forward
0 new messages