-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPicoSymfonyForm.php
101 lines (82 loc) · 2.71 KB
/
PicoSymfonyForm.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
use PicoSymfonyForm\FormHandler;
/**
* Created by 4/16/18 6:02 PM.
* @author Mediengstalt Heimbach - Johannes Heimbach
*/
class PicoSymfonyForm extends AbstractPicoPlugin
{
const API_VERSION = 2;
/**
* @var FormHandler
*/
private $formHandler;
private $globalConfig;
private $config;
private function initForms()
{
try {
$this->formHandler = new FormHandler($this->getPico(), $this->config);
} catch (ReflectionException $e) {
throw new RuntimeException($e->getMessage());
}
}
private function generateForm($formName)
{
$form = $this->formHandler->generateForm($formName);
$form->handleRequest();
$alert = $_GET['send'] ?? null;
if ($form->isSubmitted() && $form->isValid()) {
$sent = $this->formHandler->sendData($formName, $form->getData());
$alert = $sent === 0 ? 'error' : 'success';
header('Location: ' . $this->getPico()->getRequestUrl() . '?send=' . $alert);
}
return $this->formHandler->generateView($form, $alert);
}
public function onConfigLoaded($config)
{
$this->globalConfig = $config;
if (!array_key_exists('PicoSymfonyForm', $this->globalConfig)) {
$this->globalConfig['PicoSymfonyForm'] = [];
}
$conf = $this->globalConfig['PicoSymfonyForm'];
$this->config = array_merge_recursive_distinct([
'confDir' => $this->getPico()->getConfigDir() . 'forms/',
'view_dir' => __DIR__ . '/templates/',
'form_view' => 'form.html.twig',
'form_theme' => 'form_div_layout.html.twig',
'locale' => 'en',
'handler' => 'smtp',
'smtp' => [
'host' => 'smtp.mailgun.org',
'username' => '',
'password' => ''
],
'email' => [
'sender' => '[email protected]',
'view_dir' => '',
],
'translation_dir' => __DIR__ . '/translations/',
'translations' => [
'messages' => [
'file' => 'messages.en.yml',
'format' => 'yaml',
'locale' => 'en'
]
]
], $conf);
}
/**
* @param $content
*/
public function onContentParsed(&$content)
{
$includeFormPattern = '/<p>%include_form\((.*)\)%<\/p>/';
preg_match($includeFormPattern, $content, $matches);
if (count($matches) < 1) {
return;
}
$this->initForms();
$content = preg_replace($includeFormPattern, $this->generateForm($matches[1]), $content);
}
}