This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
/
snippet.ajaxform.php
76 lines (67 loc) · 2.66 KB
/
snippet.ajaxform.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/** @var modX $modx */
/** @var AjaxForm $AjaxForm */
/** @var array $scriptProperties */
if (!$modx->loadClass('ajaxform', MODX_CORE_PATH . 'components/ajaxform/model/ajaxform/', false, true)) {
return false;
}
$AjaxForm = new AjaxForm($modx, $scriptProperties);
$snippet = $modx->getOption('snippet', $scriptProperties, 'FormIt', true);
$tpl = $modx->getOption('form', $scriptProperties, 'tpl.AjaxForm.example', true);
$formSelector = $modx->getOption('formSelector', $scriptProperties, 'ajax_form', true);
$objectName = $modx->getOption('objectName', $scriptProperties, 'AjaxForm', true);
$AjaxForm->loadJsCss($objectName);
/** @var pdoTools $pdo */
if (class_exists('pdoTools') && $pdo = $modx->getService('pdoTools')) {
$content = $pdo->getChunk($tpl, $scriptProperties);
} else {
$content = $modx->getChunk($tpl, $scriptProperties);
}
if (empty($content)) {
return $modx->lexicon('af_err_chunk_nf', array('name' => $tpl));
}
// Add selector to tag form
if (preg_match('#<form.*?class=(?:"|\')(.*?)(?:"|\')#i', $content, $matches)) {
$classes = explode(' ', $matches[1]);
if (!in_array($formSelector, $classes)) {
$classes[] = $formSelector;
$classes = preg_replace(
'#class=(?:"|\')' . $matches[1] . '(?:"|\')#i',
'class="' . implode(' ', $classes) . '"',
$matches[0]
);
$content = str_ireplace($matches[0], $classes, $content);
}
} else {
$content = str_ireplace('<form', '<form class="' . $formSelector . '"', $content);
}
// Add method = post
if (preg_match('#<form.*?method=(?:"|\')(.*?)(?:"|\')#i', $content)) {
$content = preg_replace('#<form(.*?)method=(?:"|\')(.*?)(?:"|\')#i', '<form\\1method="post"', $content);
} else {
$content = str_ireplace('<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 ((stripos($content, '</form>') !== false)) {
if (preg_match('#<input.*?name=(?:"|\')af_action(?:"|\').*?>#i', $content, $matches)) {
$content = str_ireplace($matches[0], '', $content);
}
$content = str_ireplace('</form>', "\n\t$action\n</form>", $content);
}
// Save snippet properties
if (!empty(session_id())) {
// ... to user`s session
$_SESSION['AjaxForm'][$hash] = $scriptProperties;
} else {
// ... to cache file
$modx->cacheManager->set('ajaxform/props_' . $hash, $scriptProperties, 3600);
}
// Call snippet for preparation of form
$action = !empty($_REQUEST['af_action'])
? $_REQUEST['af_action']
: $hash;
$AjaxForm->process($action, $_REQUEST);
// Return chunk
return $content;