forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhabricatorProjectController.php
231 lines (189 loc) · 6.31 KB
/
PhabricatorProjectController.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
abstract class PhabricatorProjectController extends PhabricatorController {
private $project;
private $profileMenu;
private $profileMenuEngine;
protected function setProject(PhabricatorProject $project) {
$this->project = $project;
return $this;
}
protected function getProject() {
return $this->project;
}
protected function loadProject() {
return $this->loadProjectWithCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
));
}
protected function loadProjectForEdit() {
return $this->loadProjectWithCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
));
}
private function loadProjectWithCapabilities(array $capabilities) {
$viewer = $this->getViewer();
$request = $this->getRequest();
$id = nonempty(
$request->getURIData('projectID'),
$request->getURIData('id'));
$slug = $request->getURIData('slug');
if ($slug) {
$normal_slug = PhabricatorSlug::normalizeProjectSlug($slug);
$is_abnormal = ($slug !== $normal_slug);
$normal_uri = "/tag/{$normal_slug}/";
} else {
$is_abnormal = false;
}
$query = id(new PhabricatorProjectQuery())
->setViewer($viewer)
->requireCapabilities($capabilities)
->needMembers(true)
->needWatchers(true)
->needImages(true)
->needSlugs(true);
if ($slug) {
$query->withSlugs(array($slug));
} else {
$query->withIDs(array($id));
}
$policy_exception = null;
try {
$project = $query->executeOne();
} catch (PhabricatorPolicyException $ex) {
$policy_exception = $ex;
$project = null;
}
if (!$project) {
// This project legitimately does not exist, so just 404 the user.
if (!$policy_exception) {
return new Aphront404Response();
}
// Here, the project exists but the user can't see it. If they are
// using a non-canonical slug to view the project, redirect to the
// canonical slug. If they're already using the canonical slug, rethrow
// the exception to give them the policy error.
if ($is_abnormal) {
return id(new AphrontRedirectResponse())->setURI($normal_uri);
} else {
throw $policy_exception;
}
}
// The user can view the project, but is using a noncanonical slug.
// Redirect to the canonical slug.
$primary_slug = $project->getPrimarySlug();
if ($slug && ($slug !== $primary_slug)) {
$primary_uri = "/tag/{$primary_slug}/";
return id(new AphrontRedirectResponse())->setURI($primary_uri);
}
$this->setProject($project);
return null;
}
protected function buildApplicationCrumbs() {
return $this->newApplicationCrumbs('profile');
}
protected function newWorkboardCrumbs() {
return $this->newApplicationCrumbs('workboard');
}
private function newApplicationCrumbs($mode) {
$crumbs = parent::buildApplicationCrumbs();
$project = $this->getProject();
if ($project) {
$ancestors = $project->getAncestorProjects();
$ancestors = array_reverse($ancestors);
$ancestors[] = $project;
foreach ($ancestors as $ancestor) {
if ($ancestor->getPHID() === $project->getPHID()) {
// Link the current project's crumb to its profile no matter what,
// since we're already on the right context page for it and linking
// to the current page isn't helpful.
$crumb_uri = $ancestor->getProfileURI();
} else {
switch ($mode) {
case 'workboard':
if ($ancestor->getHasWorkboard()) {
$crumb_uri = $ancestor->getWorkboardURI();
} else {
$crumb_uri = $ancestor->getProfileURI();
}
break;
case 'profile':
default:
$crumb_uri = $ancestor->getProfileURI();
break;
}
}
$crumbs->addTextCrumb($ancestor->getName(), $crumb_uri);
}
}
return $crumbs;
}
protected function getProfileMenuEngine() {
if (!$this->profileMenuEngine) {
$viewer = $this->getViewer();
$project = $this->getProject();
if ($project) {
$engine = id(new PhabricatorProjectProfileMenuEngine())
->setViewer($viewer)
->setController($this)
->setProfileObject($project);
$this->profileMenuEngine = $engine;
}
}
return $this->profileMenuEngine;
}
protected function setProfileMenuEngine(
PhabricatorProjectProfileMenuEngine $engine) {
$this->profileMenuEngine = $engine;
return $this;
}
protected function newCardResponse(
$board_phid,
$object_phid,
PhabricatorProjectColumnOrder $ordering = null,
$sounds = array()) {
$viewer = $this->getViewer();
$request = $this->getRequest();
$visible_phids = $request->getStrList('visiblePHIDs');
if (!$visible_phids) {
$visible_phids = array();
}
$engine = id(new PhabricatorBoardResponseEngine())
->setViewer($viewer)
->setBoardPHID($board_phid)
->setUpdatePHIDs(array($object_phid))
->setVisiblePHIDs($visible_phids)
->setSounds($sounds);
if ($ordering) {
$engine->setOrdering($ordering);
}
return $engine->buildResponse();
}
public function renderHashtags(array $tags) {
$result = array();
foreach ($tags as $key => $tag) {
$result[] = '#'.$tag;
}
return implode(', ', $result);
}
final protected function newNavigation(
PhabricatorProject $project,
$item_identifier) {
$engine = $this->getProfileMenuEngine();
$view_list = $engine->newProfileMenuItemViewList();
// See PHI1247. If the "Workboard" item is removed from the menu, we will
// not be able to select it. This can happen if a user removes the item,
// then manually navigate to the workboard URI (or follows an older link).
// In this case, just render the menu with no selected item.
if ($view_list->getViewsWithItemIdentifier($item_identifier)) {
$view_list->setSelectedViewWithItemIdentifier($item_identifier);
}
$navigation = $view_list->newNavigationView();
if ($item_identifier === PhabricatorProject::ITEM_WORKBOARD) {
$navigation->addClass('project-board-nav');
}
return $navigation;
}
}