Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/metamta/applicationpanel/PhabricatorMetaMTAApplicationEmailPanel.php
12256 views
1
<?php
2
3
final class PhabricatorMetaMTAApplicationEmailPanel
4
extends PhabricatorApplicationConfigurationPanel {
5
6
public function getPanelKey() {
7
return 'email';
8
}
9
10
public function shouldShowForApplication(
11
PhabricatorApplication $application) {
12
return $application->supportsEmailIntegration();
13
}
14
15
public function buildConfigurationPagePanel() {
16
$viewer = $this->getViewer();
17
$application = $this->getApplication();
18
19
$table = $this->buildEmailTable($is_edit = false, null);
20
21
$can_edit = PhabricatorPolicyFilter::hasCapability(
22
$viewer,
23
$application,
24
PhabricatorPolicyCapability::CAN_EDIT);
25
26
$header = id(new PHUIHeaderView())
27
->setHeader(pht('Application Emails'))
28
->addActionLink(
29
id(new PHUIButtonView())
30
->setTag('a')
31
->setText(pht('Edit Application Emails'))
32
->setIcon('fa-pencil')
33
->setHref($this->getPanelURI())
34
->setDisabled(!$can_edit)
35
->setWorkflow(!$can_edit));
36
37
38
$box = id(new PHUIObjectBoxView())
39
->setHeader($header)
40
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
41
->setTable($table);
42
43
return $box;
44
}
45
46
public function handlePanelRequest(
47
AphrontRequest $request,
48
PhabricatorController $controller) {
49
$viewer = $request->getViewer();
50
$application = $this->getApplication();
51
52
$path = $request->getURIData('path');
53
if (strlen($path)) {
54
return new Aphront404Response();
55
}
56
57
$uri = new PhutilURI($request->getPath());
58
59
$new = $request->getStr('new');
60
$edit = $request->getInt('edit');
61
$delete = $request->getInt('delete');
62
63
if ($new) {
64
return $this->returnNewAddressResponse($request, $uri, $application);
65
}
66
67
if ($edit) {
68
return $this->returnEditAddressResponse($request, $uri, $edit);
69
}
70
71
if ($delete) {
72
return $this->returnDeleteAddressResponse($request, $uri, $delete);
73
}
74
75
$table = $this->buildEmailTable(
76
$is_edit = true,
77
$request->getInt('id'));
78
79
$form = id(new AphrontFormView())
80
->setUser($viewer);
81
82
$crumbs = $controller->buildPanelCrumbs($this);
83
$crumbs->addTextCrumb(pht('Edit Application Emails'));
84
$crumbs->setBorder(true);
85
86
$header = id(new PHUIHeaderView())
87
->setHeader(pht('Edit Application Emails: %s', $application->getName()))
88
->setSubheader($application->getAppEmailBlurb())
89
->setHeaderIcon('fa-pencil');
90
91
$icon = id(new PHUIIconView())
92
->setIcon('fa-plus');
93
$button = new PHUIButtonView();
94
$button->setText(pht('Add New Address'));
95
$button->setTag('a');
96
$button->setHref($uri->alter('new', 'true'));
97
$button->setIcon($icon);
98
$button->addSigil('workflow');
99
$header->addActionLink($button);
100
101
$object_box = id(new PHUIObjectBoxView())
102
->setHeaderText(pht('Emails'))
103
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
104
->setTable($table);
105
106
$title = $application->getName();
107
$view = id(new PHUITwoColumnView())
108
->setHeader($header)
109
->setFooter($object_box);
110
111
return $controller->buildPanelPage(
112
$this,
113
$title,
114
$crumbs,
115
$view);
116
}
117
118
private function returnNewAddressResponse(
119
AphrontRequest $request,
120
PhutilURI $uri,
121
PhabricatorApplication $application) {
122
123
$viewer = $request->getUser();
124
$email_object =
125
PhabricatorMetaMTAApplicationEmail::initializeNewAppEmail($viewer)
126
->setApplicationPHID($application->getPHID());
127
128
return $this->returnSaveAddressResponse(
129
$request,
130
$uri,
131
$email_object,
132
$is_new = true);
133
}
134
135
private function returnEditAddressResponse(
136
AphrontRequest $request,
137
PhutilURI $uri,
138
$email_object_id) {
139
140
$viewer = $request->getUser();
141
$email_object = id(new PhabricatorMetaMTAApplicationEmailQuery())
142
->setViewer($viewer)
143
->withIDs(array($email_object_id))
144
->requireCapabilities(
145
array(
146
PhabricatorPolicyCapability::CAN_VIEW,
147
PhabricatorPolicyCapability::CAN_EDIT,
148
))
149
->executeOne();
150
if (!$email_object) {
151
return new Aphront404Response();
152
}
153
154
return $this->returnSaveAddressResponse(
155
$request,
156
$uri,
157
$email_object,
158
$is_new = false);
159
}
160
161
private function returnSaveAddressResponse(
162
AphrontRequest $request,
163
PhutilURI $uri,
164
PhabricatorMetaMTAApplicationEmail $email_object,
165
$is_new) {
166
167
$viewer = $request->getUser();
168
169
$config_default =
170
PhabricatorMetaMTAApplicationEmail::CONFIG_DEFAULT_AUTHOR;
171
172
$e_email = true;
173
$v_email = $email_object->getAddress();
174
$e_space = null;
175
$v_space = $email_object->getSpacePHID();
176
$v_default = $email_object->getConfigValue($config_default);
177
178
$validation_exception = null;
179
if ($request->isDialogFormPost()) {
180
$e_email = null;
181
182
$v_email = trim($request->getStr('email'));
183
$v_space = $request->getStr('spacePHID');
184
$v_default = $request->getArr($config_default);
185
$v_default = nonempty(head($v_default), null);
186
187
$type_address =
188
PhabricatorMetaMTAApplicationEmailTransaction::TYPE_ADDRESS;
189
$type_space = PhabricatorTransactions::TYPE_SPACE;
190
$type_config =
191
PhabricatorMetaMTAApplicationEmailTransaction::TYPE_CONFIG;
192
193
$key_config = PhabricatorMetaMTAApplicationEmailTransaction::KEY_CONFIG;
194
195
$xactions = array();
196
197
$xactions[] = id(new PhabricatorMetaMTAApplicationEmailTransaction())
198
->setTransactionType($type_address)
199
->setNewValue($v_email);
200
201
$xactions[] = id(new PhabricatorMetaMTAApplicationEmailTransaction())
202
->setTransactionType($type_space)
203
->setNewValue($v_space);
204
205
$xactions[] = id(new PhabricatorMetaMTAApplicationEmailTransaction())
206
->setTransactionType($type_config)
207
->setMetadataValue($key_config, $config_default)
208
->setNewValue($v_default);
209
210
$editor = id(new PhabricatorMetaMTAApplicationEmailEditor())
211
->setActor($viewer)
212
->setContentSourceFromRequest($request)
213
->setContinueOnNoEffect(true);
214
215
try {
216
$editor->applyTransactions($email_object, $xactions);
217
218
return id(new AphrontRedirectResponse())->setURI(
219
$uri->alter('highlight', $email_object->getID()));
220
} catch (PhabricatorApplicationTransactionValidationException $ex) {
221
$validation_exception = $ex;
222
$e_email = $ex->getShortMessage($type_address);
223
$e_space = $ex->getShortMessage($type_space);
224
}
225
}
226
227
if ($v_default) {
228
$v_default = array($v_default);
229
} else {
230
$v_default = array();
231
}
232
233
$form = id(new AphrontFormView())
234
->setUser($viewer)
235
->appendChild(
236
id(new AphrontFormTextControl())
237
->setLabel(pht('Email'))
238
->setName('email')
239
->setValue($v_email)
240
->setError($e_email));
241
242
if (PhabricatorSpacesNamespaceQuery::getViewerSpacesExist($viewer)) {
243
$form->appendControl(
244
id(new AphrontFormSelectControl())
245
->setLabel(pht('Space'))
246
->setName('spacePHID')
247
->setValue($v_space)
248
->setError($e_space)
249
->setOptions(
250
PhabricatorSpacesNamespaceQuery::getSpaceOptionsForViewer(
251
$viewer,
252
$v_space)));
253
}
254
255
$form
256
->appendControl(
257
id(new AphrontFormTokenizerControl())
258
->setDatasource(new PhabricatorPeopleDatasource())
259
->setLabel(pht('Default Author'))
260
->setName($config_default)
261
->setLimit(1)
262
->setValue($v_default)
263
->setCaption(
264
pht(
265
'Used if the "From:" address does not map to a user account. '.
266
'Setting a default author will allow anyone on the public '.
267
'internet to create objects by sending email to '.
268
'this address.')));
269
270
if ($is_new) {
271
$title = pht('New Address');
272
} else {
273
$title = pht('Edit Address');
274
}
275
276
$dialog = id(new AphrontDialogView())
277
->setUser($viewer)
278
->setWidth(AphrontDialogView::WIDTH_FORM)
279
->setTitle($title)
280
->setValidationException($validation_exception)
281
->appendForm($form)
282
->addSubmitButton(pht('Save'))
283
->addCancelButton($uri);
284
285
if ($is_new) {
286
$dialog->addHiddenInput('new', 'true');
287
}
288
289
return id(new AphrontDialogResponse())->setDialog($dialog);
290
}
291
292
private function returnDeleteAddressResponse(
293
AphrontRequest $request,
294
PhutilURI $uri,
295
$email_object_id) {
296
297
$viewer = $this->getViewer();
298
299
$email_object = id(new PhabricatorMetaMTAApplicationEmailQuery())
300
->setViewer($viewer)
301
->withIDs(array($email_object_id))
302
->requireCapabilities(
303
array(
304
PhabricatorPolicyCapability::CAN_VIEW,
305
PhabricatorPolicyCapability::CAN_EDIT,
306
))
307
->executeOne();
308
if (!$email_object) {
309
return new Aphront404Response();
310
}
311
312
if ($request->isDialogFormPost()) {
313
$engine = new PhabricatorDestructionEngine();
314
$engine->destroyObject($email_object);
315
return id(new AphrontRedirectResponse())->setURI($uri);
316
}
317
318
$dialog = id(new AphrontDialogView())
319
->setUser($viewer)
320
->addHiddenInput('delete', $email_object_id)
321
->setTitle(pht('Delete Address'))
322
->appendParagraph(pht(
323
'Are you sure you want to delete this email address?'))
324
->addSubmitButton(pht('Delete'))
325
->addCancelButton($uri);
326
327
return id(new AphrontDialogResponse())->setDialog($dialog);
328
}
329
330
private function buildEmailTable($is_edit, $highlight) {
331
$viewer = $this->getViewer();
332
$application = $this->getApplication();
333
$uri = new PhutilURI($this->getPanelURI());
334
335
$emails = id(new PhabricatorMetaMTAApplicationEmailQuery())
336
->setViewer($viewer)
337
->withApplicationPHIDs(array($application->getPHID()))
338
->execute();
339
340
$rowc = array();
341
$rows = array();
342
foreach ($emails as $email) {
343
344
$button_edit = javelin_tag(
345
'a',
346
array(
347
'class' => 'button small button-grey',
348
'href' => $uri->alter('edit', $email->getID()),
349
'sigil' => 'workflow',
350
),
351
pht('Edit'));
352
353
$button_remove = javelin_tag(
354
'a',
355
array(
356
'class' => 'button small button-grey',
357
'href' => $uri->alter('delete', $email->getID()),
358
'sigil' => 'workflow',
359
),
360
pht('Delete'));
361
362
if ($highlight == $email->getID()) {
363
$rowc[] = 'highlighted';
364
} else {
365
$rowc[] = null;
366
}
367
368
$space_phid = PhabricatorSpacesNamespaceQuery::getObjectSpacePHID($email);
369
if ($space_phid) {
370
$email_space = $viewer->renderHandle($space_phid);
371
} else {
372
$email_space = null;
373
}
374
375
$default_author_phid = $email->getDefaultAuthorPHID();
376
if (!$default_author_phid) {
377
$default_author = phutil_tag('em', array(), pht('None'));
378
} else {
379
$default_author = $viewer->renderHandle($default_author_phid);
380
}
381
382
$rows[] = array(
383
$email_space,
384
$email->getAddress(),
385
$default_author,
386
$button_edit,
387
$button_remove,
388
);
389
}
390
391
$table = id(new AphrontTableView($rows))
392
->setNoDataString(pht('No application emails created yet.'));
393
$table->setHeaders(
394
array(
395
pht('Space'),
396
pht('Email'),
397
pht('Default'),
398
pht('Edit'),
399
pht('Delete'),
400
));
401
$table->setColumnClasses(
402
array(
403
'',
404
'',
405
'wide',
406
'action',
407
'action',
408
));
409
$table->setRowClasses($rowc);
410
$table->setColumnVisibility(
411
array(
412
PhabricatorSpacesNamespaceQuery::getViewerSpacesExist($viewer),
413
true,
414
true,
415
$is_edit,
416
$is_edit,
417
));
418
419
return $table;
420
}
421
422
}
423
424