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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
<?php
namespace Icinga\Module\Businessprocess\Forms;
use Exception;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Storage\LegacyConfigParser;
use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
use Icinga\Web\Notification;
class BpUploadForm extends BpConfigBaseForm
{
protected $node;
protected $objectList = array();
protected $processList = array();
protected $deleteButtonName;
private $sourceCode;
/** @var BpConfig */
private $uploadedConfig;
public function setup()
{
$this->showUpload();
if ($this->hasSource()) {
$this->showDetails();
}
}
protected function showDetails()
{
$this->addElement('text', 'name', array(
'label' => $this->translate('Name'),
'required' => true,
'description' => $this->translate(
'This is the unique identifier of this process'
),
'validators' => array(
array(
'validator' => 'StringLength',
'options' => array(
'min' => 2,
'max' => 40
)
),
[
'validator' => 'Regex',
'options' => [
'pattern' => '/^[a-zA-Z0-9](?:[\w\h._-]*)?\w$/',
'messages' => [
'regexNotMatch' => $this->translate(
'Id must only consist of alphanumeric characters.'
. ' Underscore at the beginning and space, dot and hyphen at the beginning'
. ' and end are not allowed.'
)
]
]
]
),
));
$this->addElement('textarea', 'source', array(
'label' => $this->translate('Source'),
'description' => $this->translate(
'Business process source code'
),
'value' => $this->sourceCode,
'class' => 'preformatted smaller',
'rows' => 7,
));
$this->getUploadedConfig();
$this->setSubmitLabel(
$this->translate('Store')
);
}
public function getUploadedConfig()
{
if ($this->uploadedConfig === null) {
$this->uploadedConfig = $this->parseSubmittedSourceCode();
}
return $this->uploadedConfig;
}
protected function parseSubmittedSourceCode()
{
$code = $this->getSentValue('source');
$name = $this->getSentValue('name', '<new config>');
if (empty($code)) {
$code = $this->sourceCode;
}
try {
$config = LegacyConfigParser::parseString($name, $code);
if ($config->hasErrors()) {
foreach ($config->getErrors() as $error) {
$this->addError($error);
}
}
} catch (Exception $e) {
$this->addError($e->getMessage());
return null;
}
return $config;
}
protected function hasSource()
{
if ($this->hasBeenSent() && $source = $this->getSentValue('source')) {
$this->sourceCode = $source;
} else {
$this->processUploadedSource();
}
if (empty($this->sourceCode)) {
return false;
} else {
$this->removeElement('uploaded_file');
return true;
}
}
protected function showUpload()
{
$this->setAttrib('enctype', 'multipart/form-data');
$this->addElement('file', 'uploaded_file', array(
'label' => $this->translate('File'),
'destination' => $this->getTempDir(),
'required' => true,
));
/** @var \Zend_Form_Element_File $el */
$el = $this->getElement('uploaded_file');
$el->setValueDisabled(true);
$this->setSubmitLabel(
$this->translate('Next')
);
}
protected function getTempDir()
{
return sys_get_temp_dir();
}
protected function processUploadedSource()
{
/** @var ?\Zend_Form_Element_File $el */
$el = $this->getElement('uploaded_file');
if ($el && $this->hasBeenSent()) {
$tmpdir = $this->getTempDir();
$tmpfile = tempnam($tmpdir, 'bpupload_');
// TODO: race condition, try to do this without unlinking here
unlink($tmpfile);
$el->addFilter('Rename', $tmpfile);
if ($el->receive()) {
$this->sourceCode = file_get_contents($tmpfile);
unlink($tmpfile);
} else {
foreach ($el->file->getMessages() as $error) {
$this->addError($error);
}
}
}
return $this;
}
public function onSuccess()
{
$config = $this->getUploadedConfig();
$name = $config->getName();
if ($this->storage->hasProcess($name)) {
$this->addError(sprintf(
$this->translate('A process named "%s" already exists'),
$name
));
return;
}
if (! $this->prepareMetadata($config)) {
return;
}
$this->storage->storeProcess($config);
Notification::success(sprintf('Process %s has been stored', $name));
$this->getSuccessUrl()->setParam('config', $name);
parent::onSuccess();
}
}
|