Skip to content

Instantly share code, notes, and snippets.

@grachov
Created June 13, 2014 19:27
Show Gist options
  • Save grachov/80d3b6572a439667fbf6 to your computer and use it in GitHub Desktop.
Save grachov/80d3b6572a439667fbf6 to your computer and use it in GitHub Desktop.
Extended version of AjaxForm that supports formConstruct
<?php
/**
* @var array $scriptProperties
* @var AjaxForm $AjaxForm
*/
$AjaxForm = $modx->getService('ajaxform', 'AjaxForm', $modx->getOption('ajaxform_core_path', null, $modx->getOption('core_path') . 'components/ajaxform/') . 'model/ajaxform/', $scriptProperties);
if (!($AjaxForm instanceof AjaxForm)) {
return '';
}
$AjaxForm->initialize($modx->context->key);
$snippet = $modx->getOption('snippet', $scriptProperties, 'FormIt', true);
if (!isset($placeholderPrefix)) {
$placeholderPrefix = 'fi.';
}
if (strtolower($snippet) == 'formconstruct') {
$scriptProperties['submitVar'] = $modx->getOption('submitVar', $scriptProperties, 'f_submit');
require_once MODX_CORE_PATH . 'components/form_construct/model/form_construct/formconstruct.class.php';
$formConstruct = new formConstruct($modx, $scriptProperties);
$formTpl = $modx->getOption('tpl', $scriptProperties, 'formConstruct_tpl');
$formId = (int)$modx->getOption('form_id', $scriptProperties, 0);
if ($formId < 1) {
return '';
}
$form = $modx->getObject('formConstructItem', $formId);
if (!$form) {
return '';
}
$formFieldsData = $form->get('fields_data');
$formFieldsData = !empty($formFieldsData) ? json_decode($formFieldsData, true) : array();
$scriptProperties['emailTo'] = $form->get('email');
$scriptProperties['validate'] = $formConstruct->getValidateString($formFieldsData);
$scriptProperties = $formConstruct->getFormItProperties($scriptProperties, $formFieldsData);
foreach ($modx->placeholders as $key => $val) {
if (strpos($key, $placeholderPrefix) === 0) {
unset($modx->placeholders[$key]);
}
}
$content = $formConstruct->makeForm($formConstruct->YAMLLoadString($modx->getChunk($formTpl)), $formFieldsData, $form->get('description'));
$scriptProperties['snippet'] = 'FormIt';
} else {
$tpl = $modx->getOption('form', $scriptProperties, 'tpl.AjaxForm.example', true);
/** @var modChunk $chunk */
$condition = array(
'name' => $tpl,
);
if (!$chunk = $modx->getObject('modChunk', $condition)) {
return $modx->lexicon('af_err_chunk_nf', $condition);
}
$content = $chunk->getContent();
}
$formSelector = $modx->getOption('formSelector', $scriptProperties, 'ajax_form', true);
// Add selector to tag form
if (preg_match('/form.*?class="(.*?)"/', $content, $matches)) {
$classes = explode(' ', $matches[1]);
if (!in_array($formSelector, $classes)) {
$classes[] = $formSelector;
$classes = str_replace('class="' . $matches[1] . '"', 'class="' . implode(' ', $classes) . '"', $matches[0]);
$content = str_replace($matches[0], $classes, $content);
}
} else {
$content = str_replace('<form', '<form class="' . $formSelector . '"', $content);
}
// Add method = post
if (preg_match('/form.*?method="(.*?)"/', $content)) {
$content = preg_replace('/form(.*?)method="(.*?)"/', 'form\\1method="post"', $content);
} else {
$content = str_replace('<form', '<form method="post"', $content);
}
// Add action for form processing
$hash = md5(http_build_query($scriptProperties));
$action = '<input type="hidden" name="af_action" value="' . $hash . '" />';
if ((strpos($content, '</form>') !== false)) {
if (preg_match('/<input.*?name="af_action".*?>/', $content, $matches)) {
$content = str_replace($matches[0], '', $content);
}
$content = str_replace('</form>', "\n\t$action\n</form>", $content);
}
// Save settings to user`s session
$_SESSION['AjaxForm'][$hash] = $scriptProperties;
// Call snippet for preparation of form
$action = !empty($_REQUEST['af_action']) ? $_REQUEST['af_action'] : $hash;
$AjaxForm->process($action, $_REQUEST);
// Return chunk
return $content;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment