Last active
November 27, 2016 12:06
-
-
Save goldsky/da7bd03a815b9a093905 to your computer and use it in GitHub Desktop.
Helper snippet to render the dynamic values from FormIt
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 | |
/** | |
* fiDynamicFields snippet | |
* | |
* the snippet to manipulate this dynamic form, to parse the HTML code and | |
* to hold the submitted values if the page goes back to the same page. | |
* | |
* @link http://www.virtudraft.com/blog/dynamic-fields-in-multiple-pages-for-formit.html | |
* @author goldsky <[email protected]> | |
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 | |
*/ | |
/** | |
* &fields=`field_name_1, field_name_2, field_name_3` | |
* Name of fields to be captcured | |
*/ | |
$fields = $modx->getOption('fields', $scriptProperties); | |
if (empty($fields)) { | |
$modx->log(modX::LOG_LEVEL_ERROR, '[fiDynamicFields] is missing &fields'); | |
return; | |
} | |
/** | |
* &itemTpl=`a chunk name` | |
* Chunk name as template to render each row | |
*/ | |
$itemTpl = $modx->getOption('itemTpl', $scriptProperties); | |
/** | |
* &wrapperTpl=`a chunk name` | |
* (optional) | |
* Chunk name to wrap rendered items and place the javascript code. | |
* Leave this empty if uneeded. | |
*/ | |
$wrapperTpl = $modx->getOption('wrapperTpl', $scriptProperties); | |
if (empty($itemTpl)) { | |
$modx->log(modX::LOG_LEVEL_ERROR, '[fiDynamicFields] is missing &itemTpl'); | |
return; | |
} | |
$phsPrefix = $modx->getOption('phsPrefix', $scriptProperties, 'dynfield.'); | |
if (!isset($_POST)) { | |
return; | |
} | |
$fieldsArray = array_map('trim', @explode(',', $fields)); | |
$assoc = array(); | |
/** | |
* You MUST sanitize the submitted data manually in here, | |
* because the expected inputs are based on specific cases | |
* | |
* @link http://php.net/manual/en/function.filter-input-array.php filter_input_array | |
*/ | |
$data = $_POST; | |
foreach ($data as $k => $v) { | |
if (in_array($k, $fieldsArray)) { | |
$assoc[$k] = array(); | |
if (is_array($v)) { | |
foreach($v as $x) { | |
$assoc[$k][] = $x; | |
} | |
} elseif (is_string($v)) { | |
$valArray = array_map('trim', @explode(',', $v)); | |
foreach ($valArray as $x) { | |
$assoc[$k][] = $x; | |
} | |
} | |
} | |
} | |
$reverting = array(); | |
foreach ($assoc as $k => $v) { | |
foreach ($v as $key => $val) { | |
$reverting[$key][$phsPrefix . $k] = $val; | |
} | |
} | |
$items = array(); | |
foreach ($reverting as $phs) { | |
$items[] = $modx->getChunk($itemTpl, $phs); | |
} | |
if (empty($wrapper)) { | |
return @implode("\n", $items); | |
} else { | |
$wrapper = array( | |
$phsPrefix . 'items' => @implode("\n", $items), | |
); | |
$output = $modx->getChunk($wrapperTpl, $wrapper); | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why you check $wrapper?