Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Formit RedirectByValue Hook
<?php
/**
* RedirectByValue
*
* Copyright 2016 by Thomas Jakobi <thomas.jakobi@partout.info>
*
* Description:
* This FormIt Hook redirects to different targets on base of the
* formItRedirectByValue property. The property has to contain a json encoded
* object of fieldnames, values, redirects and redirect parameters. Only a
* third level
*
* Usage:
* [[!FormIt? &preHooks=`redirectbyvalue` &redirectByValue=`{"send_back":"123","send_forward":"456","send_submit":{"1":"135","2":{"params":{"user":"123","success":"1"},"redirect":"246"}}}`]]
*
* Indented JSON value from above:
* [
* {
* "send_back": "123",
* "send_forward": "456",
* "send_submit": {
* "1": "135",
* "2": {
* "params": {
* "user": "123",
* "success": "1"
* },
* "redirect": "246"
* }
* }
* }
* ]
*
* @var modX $modx
* @var FormIt $formit
* @var fiHooks $hook
*/
$postValues = count($_POST) ? $_POST : array();
$redirectByValue = json_decode($modx->getOption('redirectByValue', $formit->config, ''), true);
$redirect = false;
$redirectParams = array();
if ($redirectByValue && $postValues) {
foreach ($redirectByValue as $fieldname => $value) {
if (isset($postValues[$fieldname])) {
if (is_string($value)) {
$redirect = $value;
} elseif (is_array($value)) {
foreach ($value as $fieldvalue => $fieldredirect) {
if ($postValues[$fieldname] == $fieldvalue) {
if (is_string($fieldredirect)) {
$redirect = $fieldredirect;
} elseif (is_array($fieldredirect)) {
$redirect = $fieldredirect['redirect'];
$redirectParams = array();
foreach ($fieldredirect['params'] as $param) {
if (isset($postValues[$param]) && $postValues[$param] != '') {
$redirectParams[$param] = $postValues[$param];
}
}
}
}
}
}
}
}
if ($redirect) {
$url = $modx->makeUrl($redirect, '', $redirectParams);
$modx->sendRedirect($url);
}
}
return true;
@Jako

This comment has been minimized.

Copy link
Owner Author

Jako commented Aug 30, 2016

Usage:

[[!FormIt?
&preHooks=`redirectbyvalue`
&redirectByValue=`{"send_back":"123","send_forward":"456","send_submit":{"1":"135","2":{"params":{"user":"123","success":"1"},"redirect":"246"}}}`
]]

Indented JSON value from above:

[
    {
        "send_back": "123",
        "send_forward": "456",
        "send_submit": {
            "1": "135",
            "2": {
                "params": {
                    "user": "123",
                    "success": "1"
                },
                "redirect": "246"
            }
        }
    }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.