forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhabricatorTokenGiveController.php
142 lines (114 loc) · 3.56 KB
/
PhabricatorTokenGiveController.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
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
<?php
final class PhabricatorTokenGiveController extends PhabricatorTokenController {
public function handleRequest(AphrontRequest $request) {
$viewer = $request->getViewer();
$phid = $request->getURIData('phid');
$handle = id(new PhabricatorHandleQuery())
->setViewer($viewer)
->withPHIDs(array($phid))
->executeOne();
if (!$handle->isComplete()) {
return new Aphront404Response();
}
$object = id(new PhabricatorObjectQuery())
->setViewer($viewer)
->withPHIDs(array($phid))
->executeOne();
if (!($object instanceof PhabricatorTokenReceiverInterface)) {
return new Aphront400Response();
}
if (!PhabricatorPolicyFilter::canInteract($viewer, $object)) {
$lock = PhabricatorEditEngineLock::newForObject($viewer, $object);
$dialog = $this->newDialog()
->addCancelButton($handle->getURI());
return $lock->willBlockUserInteractionWithDialog($dialog);
}
$current = id(new PhabricatorTokenGivenQuery())
->setViewer($viewer)
->withAuthorPHIDs(array($viewer->getPHID()))
->withObjectPHIDs(array($handle->getPHID()))
->execute();
if ($current) {
$is_give = false;
$title = pht('Rescind Token');
} else {
$is_give = true;
$title = pht('Give Token');
}
$done_uri = $handle->getURI();
if ($request->isDialogFormPost()) {
$content_source = PhabricatorContentSource::newFromRequest($request);
$editor = id(new PhabricatorTokenGivenEditor())
->setActor($viewer)
->setContentSource($content_source);
if ($is_give) {
$token_phid = $request->getStr('tokenPHID');
$editor->addToken($handle->getPHID(), $token_phid);
} else {
$editor->deleteToken($handle->getPHID());
}
return id(new AphrontReloadResponse())->setURI($done_uri);
}
if ($is_give) {
$dialog = $this->buildGiveTokenDialog();
} else {
$dialog = $this->buildRescindTokenDialog(head($current));
}
$dialog->setUser($viewer);
$dialog->addCancelButton($done_uri);
return id(new AphrontDialogResponse())->setDialog($dialog);
}
private function buildGiveTokenDialog() {
$viewer = $this->getViewer();
$tokens = id(new PhabricatorTokenQuery())
->setViewer($viewer)
->execute();
$buttons = array();
$ii = 0;
foreach ($tokens as $token) {
$aural = javelin_tag(
'span',
array(
'aural' => true,
),
pht('Award "%s" Token', $token->getName()));
$buttons[] = javelin_tag(
'button',
array(
'class' => 'token-button',
'name' => 'tokenPHID',
'value' => $token->getPHID(),
'type' => 'submit',
'sigil' => 'has-tooltip',
'meta' => array(
'tip' => $token->getName(),
),
),
array(
$aural,
$token->renderIcon(),
));
if ((++$ii % 6) == 0) {
$buttons[] = phutil_tag('br');
}
}
$buttons = phutil_tag(
'div',
array(
'class' => 'token-grid',
),
$buttons);
$dialog = new AphrontDialogView();
$dialog->setTitle(pht('Give Token'));
$dialog->appendChild($buttons);
return $dialog;
}
private function buildRescindTokenDialog(PhabricatorTokenGiven $token_given) {
$dialog = new AphrontDialogView();
$dialog->setTitle(pht('Rescind Token'));
$dialog->appendChild(
pht('Really rescind this lovely token?'));
$dialog->addSubmitButton(pht('Rescind Token'));
return $dialog;
}
}