Skip to content

Instantly share code, notes, and snippets.

@Jako
Created August 30, 2016 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jako/8ba13f097b36e94fe315f505d3003d8b to your computer and use it in GitHub Desktop.
Save Jako/8ba13f097b36e94fe315f505d3003d8b to your computer and use it in GitHub Desktop.
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
Copy link
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