Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/people/controller/PhabricatorPeopleProfilePictureController.php
12262 views
1
<?php
2
3
final class PhabricatorPeopleProfilePictureController
4
extends PhabricatorPeopleProfileController {
5
6
public function handleRequest(AphrontRequest $request) {
7
$viewer = $this->getViewer();
8
$id = $request->getURIData('id');
9
10
$user = id(new PhabricatorPeopleQuery())
11
->setViewer($viewer)
12
->withIDs(array($id))
13
->needProfileImage(true)
14
->requireCapabilities(
15
array(
16
PhabricatorPolicyCapability::CAN_VIEW,
17
PhabricatorPolicyCapability::CAN_EDIT,
18
))
19
->executeOne();
20
if (!$user) {
21
return new Aphront404Response();
22
}
23
24
$this->setUser($user);
25
$name = $user->getUserName();
26
27
$done_uri = '/p/'.$name.'/';
28
29
$supported_formats = PhabricatorFile::getTransformableImageFormats();
30
$e_file = true;
31
$errors = array();
32
33
if ($request->isFormPost()) {
34
$phid = $request->getStr('phid');
35
$is_default = false;
36
if ($phid == PhabricatorPHIDConstants::PHID_VOID) {
37
$phid = null;
38
$is_default = true;
39
} else if ($phid) {
40
$file = id(new PhabricatorFileQuery())
41
->setViewer($viewer)
42
->withPHIDs(array($phid))
43
->executeOne();
44
} else {
45
if ($request->getFileExists('picture')) {
46
$file = PhabricatorFile::newFromPHPUpload(
47
$_FILES['picture'],
48
array(
49
'authorPHID' => $viewer->getPHID(),
50
'canCDN' => true,
51
));
52
} else {
53
$e_file = pht('Required');
54
$errors[] = pht(
55
'You must choose a file when uploading a new profile picture.');
56
}
57
}
58
59
if (!$errors && !$is_default) {
60
if (!$file->isTransformableImage()) {
61
$e_file = pht('Not Supported');
62
$errors[] = pht(
63
'This server only supports these image formats: %s.',
64
implode(', ', $supported_formats));
65
} else {
66
$xform = PhabricatorFileTransform::getTransformByKey(
67
PhabricatorFileThumbnailTransform::TRANSFORM_PROFILE);
68
$xformed = $xform->executeTransform($file);
69
}
70
}
71
72
if (!$errors) {
73
if ($is_default) {
74
$user->setProfileImagePHID(null);
75
} else {
76
$user->setProfileImagePHID($xformed->getPHID());
77
$xformed->attachToObject($user->getPHID());
78
}
79
$user->save();
80
return id(new AphrontRedirectResponse())->setURI($done_uri);
81
}
82
}
83
84
$title = pht('Edit Profile Picture');
85
86
$form = id(new PHUIFormLayoutView())
87
->setUser($viewer);
88
89
$default_image = $user->getDefaultProfileImagePHID();
90
if ($default_image) {
91
$default_image = id(new PhabricatorFileQuery())
92
->setViewer($viewer)
93
->withPHIDs(array($default_image))
94
->executeOne();
95
}
96
97
if (!$default_image) {
98
$default_image = PhabricatorFile::loadBuiltin($viewer, 'profile.png');
99
}
100
101
$images = array();
102
103
$current = $user->getProfileImagePHID();
104
$has_current = false;
105
if ($current) {
106
$files = id(new PhabricatorFileQuery())
107
->setViewer($viewer)
108
->withPHIDs(array($current))
109
->execute();
110
if ($files) {
111
$file = head($files);
112
if ($file->isTransformableImage()) {
113
$has_current = true;
114
$images[$current] = array(
115
'uri' => $file->getBestURI(),
116
'tip' => pht('Current Picture'),
117
);
118
}
119
}
120
}
121
122
$builtins = array(
123
'user1.png',
124
'user2.png',
125
'user3.png',
126
'user4.png',
127
'user5.png',
128
'user6.png',
129
'user7.png',
130
'user8.png',
131
'user9.png',
132
);
133
foreach ($builtins as $builtin) {
134
$file = PhabricatorFile::loadBuiltin($viewer, $builtin);
135
$images[$file->getPHID()] = array(
136
'uri' => $file->getBestURI(),
137
'tip' => pht('Builtin Image'),
138
);
139
}
140
141
// Try to add external account images for any associated external accounts.
142
$accounts = id(new PhabricatorExternalAccountQuery())
143
->setViewer($viewer)
144
->withUserPHIDs(array($user->getPHID()))
145
->needImages(true)
146
->requireCapabilities(
147
array(
148
PhabricatorPolicyCapability::CAN_VIEW,
149
PhabricatorPolicyCapability::CAN_EDIT,
150
))
151
->execute();
152
153
foreach ($accounts as $account) {
154
$file = $account->getProfileImageFile();
155
if ($account->getProfileImagePHID() != $file->getPHID()) {
156
// This is a default image, just skip it.
157
continue;
158
}
159
160
$config = $account->getProviderConfig();
161
$provider = $config->getProvider();
162
163
$tip = pht('Picture From %s', $provider->getProviderName());
164
165
if ($file->isTransformableImage()) {
166
$images[$file->getPHID()] = array(
167
'uri' => $file->getBestURI(),
168
'tip' => $tip,
169
);
170
}
171
}
172
173
$images[PhabricatorPHIDConstants::PHID_VOID] = array(
174
'uri' => $default_image->getBestURI(),
175
'tip' => pht('Default Picture'),
176
);
177
178
require_celerity_resource('people-profile-css');
179
Javelin::initBehavior('phabricator-tooltips', array());
180
181
$buttons = array();
182
foreach ($images as $phid => $spec) {
183
$style = null;
184
if (isset($spec['style'])) {
185
$style = $spec['style'];
186
}
187
$button = javelin_tag(
188
'button',
189
array(
190
'class' => 'button-grey profile-image-button',
191
'sigil' => 'has-tooltip',
192
'meta' => array(
193
'tip' => $spec['tip'],
194
'size' => 300,
195
),
196
),
197
phutil_tag(
198
'img',
199
array(
200
'height' => 50,
201
'width' => 50,
202
'src' => $spec['uri'],
203
)));
204
205
$button = array(
206
phutil_tag(
207
'input',
208
array(
209
'type' => 'hidden',
210
'name' => 'phid',
211
'value' => $phid,
212
)),
213
$button,
214
);
215
216
$button = phabricator_form(
217
$viewer,
218
array(
219
'class' => 'profile-image-form',
220
'method' => 'POST',
221
),
222
$button);
223
224
$buttons[] = $button;
225
}
226
227
if ($has_current) {
228
$form->appendChild(
229
id(new AphrontFormMarkupControl())
230
->setLabel(pht('Current Picture'))
231
->setValue(array_shift($buttons)));
232
}
233
234
$form->appendChild(
235
id(new AphrontFormMarkupControl())
236
->setLabel(pht('Use Picture'))
237
->setValue($buttons));
238
239
$form_box = id(new PHUIObjectBoxView())
240
->setHeaderText($title)
241
->setFormErrors($errors)
242
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
243
->setForm($form);
244
245
$upload_form = id(new AphrontFormView())
246
->setUser($viewer)
247
->setEncType('multipart/form-data')
248
->appendChild(
249
id(new AphrontFormFileControl())
250
->setName('picture')
251
->setLabel(pht('Upload Picture'))
252
->setError($e_file)
253
->setCaption(
254
pht('Supported formats: %s', implode(', ', $supported_formats))))
255
->appendChild(
256
id(new AphrontFormSubmitControl())
257
->addCancelButton($done_uri)
258
->setValue(pht('Upload Picture')));
259
260
$upload_box = id(new PHUIObjectBoxView())
261
->setHeaderText(pht('Upload New Picture'))
262
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
263
->setForm($upload_form);
264
265
$crumbs = $this->buildApplicationCrumbs();
266
$crumbs->addTextCrumb(pht('Edit Profile Picture'));
267
$crumbs->setBorder(true);
268
269
$nav = $this->newNavigation(
270
$user,
271
PhabricatorPeopleProfileMenuEngine::ITEM_MANAGE);
272
273
$header = $this->buildProfileHeader();
274
275
$view = id(new PHUITwoColumnView())
276
->setHeader($header)
277
->addClass('project-view-home')
278
->addClass('project-view-people-home')
279
->setFooter(array(
280
$form_box,
281
$upload_box,
282
));
283
284
return $this->newPage()
285
->setTitle($title)
286
->setCrumbs($crumbs)
287
->setNavigation($nav)
288
->appendChild($view);
289
}
290
}
291
292