Created
August 30, 2016 06:48
-
-
Save Jako/8ba13f097b36e94fe315f505d3003d8b to your computer and use it in GitHub Desktop.
Formit RedirectByValue Hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Indented JSON value from above: