Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/conpherence/controller/ConpherenceRoomPictureController.php
12256 views
1
<?php
2
3
final class ConpherenceRoomPictureController
4
extends ConpherenceController {
5
6
public function handleRequest(AphrontRequest $request) {
7
$viewer = $request->getViewer();
8
$id = $request->getURIData('id');
9
10
$conpherence = id(new ConpherenceThreadQuery())
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 (!$conpherence) {
21
return new Aphront404Response();
22
}
23
24
$monogram = $conpherence->getMonogram();
25
26
$supported_formats = PhabricatorFile::getTransformableImageFormats();
27
$e_file = true;
28
$errors = array();
29
30
if ($request->isFormPost()) {
31
$phid = $request->getStr('phid');
32
$is_default = false;
33
if ($phid == PhabricatorPHIDConstants::PHID_VOID) {
34
$phid = null;
35
$is_default = true;
36
} else if ($phid) {
37
$file = id(new PhabricatorFileQuery())
38
->setViewer($viewer)
39
->withPHIDs(array($phid))
40
->executeOne();
41
} else {
42
if ($request->getFileExists('picture')) {
43
$file = PhabricatorFile::newFromPHPUpload(
44
$_FILES['picture'],
45
array(
46
'authorPHID' => $viewer->getPHID(),
47
'canCDN' => true,
48
));
49
} else {
50
$e_file = pht('Required');
51
$errors[] = pht(
52
'You must choose a file when uploading a new room picture.');
53
}
54
}
55
56
if (!$errors && !$is_default) {
57
if (!$file->isTransformableImage()) {
58
$e_file = pht('Not Supported');
59
$errors[] = pht(
60
'This server only supports these image formats: %s.',
61
implode(', ', $supported_formats));
62
} else {
63
$xform = PhabricatorFileTransform::getTransformByKey(
64
PhabricatorFileThumbnailTransform::TRANSFORM_PROFILE);
65
$xformed = $xform->executeTransform($file);
66
}
67
}
68
69
if (!$errors) {
70
if ($is_default) {
71
$new_value = null;
72
} else {
73
$xformed->attachToObject($conpherence->getPHID());
74
$new_value = $xformed->getPHID();
75
}
76
77
$xactions = array();
78
$xactions[] = id(new ConpherenceTransaction())
79
->setTransactionType(
80
ConpherenceThreadPictureTransaction::TRANSACTIONTYPE)
81
->setNewValue($new_value);
82
83
$editor = id(new ConpherenceEditor())
84
->setActor($viewer)
85
->setContentSourceFromRequest($request)
86
->setContinueOnMissingFields(true)
87
->setContinueOnNoEffect(true);
88
89
$editor->applyTransactions($conpherence, $xactions);
90
91
return id(new AphrontRedirectResponse())->setURI('/'.$monogram);
92
}
93
}
94
95
$title = pht('Edit Room Picture');
96
97
$form = id(new PHUIFormLayoutView())
98
->setUser($viewer);
99
100
$default_image = PhabricatorFile::loadBuiltin($viewer, 'conpherence.png');
101
102
$images = array();
103
104
$current = $conpherence->getProfileImagePHID();
105
$has_current = false;
106
if ($current) {
107
$file = id(new PhabricatorFileQuery())
108
->setViewer($viewer)
109
->withPHIDs(array($current))
110
->executeOne();
111
if ($file) {
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
$images[PhabricatorPHIDConstants::PHID_VOID] = array(
123
'uri' => $default_image->getBestURI(),
124
'tip' => pht('Default Picture'),
125
);
126
127
require_celerity_resource('people-profile-css');
128
Javelin::initBehavior('phabricator-tooltips', array());
129
130
$buttons = array();
131
foreach ($images as $phid => $spec) {
132
$button = javelin_tag(
133
'button',
134
array(
135
'class' => 'button-grey profile-image-button',
136
'sigil' => 'has-tooltip',
137
'meta' => array(
138
'tip' => $spec['tip'],
139
'size' => 300,
140
),
141
),
142
phutil_tag(
143
'img',
144
array(
145
'height' => 50,
146
'width' => 50,
147
'src' => $spec['uri'],
148
)));
149
150
$button = array(
151
phutil_tag(
152
'input',
153
array(
154
'type' => 'hidden',
155
'name' => 'phid',
156
'value' => $phid,
157
)),
158
$button,
159
);
160
161
$button = phabricator_form(
162
$viewer,
163
array(
164
'class' => 'profile-image-form',
165
'method' => 'POST',
166
),
167
$button);
168
169
$buttons[] = $button;
170
}
171
172
if ($has_current) {
173
$form->appendChild(
174
id(new AphrontFormMarkupControl())
175
->setLabel(pht('Current Picture'))
176
->setValue(array_shift($buttons)));
177
}
178
179
$form->appendChild(
180
id(new AphrontFormMarkupControl())
181
->setLabel(pht('Use Picture'))
182
->setValue($buttons));
183
184
$form_box = id(new PHUIObjectBoxView())
185
->setHeaderText($title)
186
->setFormErrors($errors)
187
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
188
->setForm($form);
189
190
$upload_form = id(new AphrontFormView())
191
->setUser($viewer)
192
->setEncType('multipart/form-data')
193
->appendChild(
194
id(new AphrontFormFileControl())
195
->setName('picture')
196
->setLabel(pht('Upload Picture'))
197
->setError($e_file)
198
->setCaption(
199
pht('Supported formats: %s', implode(', ', $supported_formats))))
200
->appendChild(
201
id(new AphrontFormSubmitControl())
202
->addCancelButton('/'.$monogram)
203
->setValue(pht('Upload Picture')));
204
205
$upload_box = id(new PHUIObjectBoxView())
206
->setHeaderText(pht('Upload New Picture'))
207
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
208
->setForm($upload_form);
209
210
$crumbs = $this->buildApplicationCrumbs();
211
$crumbs->addTextCrumb($conpherence->getTitle(), '/'.$monogram);
212
$crumbs->addTextCrumb(pht('Room Picture'));
213
$crumbs->setBorder(true);
214
215
$header = id(new PHUIHeaderView())
216
->setHeader(pht('Edit Room Picture'))
217
->setHeaderIcon('fa-camera');
218
219
$view = id(new PHUITwoColumnView())
220
->setHeader($header)
221
->setFooter(array(
222
$form_box,
223
$upload_box,
224
));
225
226
return $this->newPage()
227
->setTitle($title)
228
->setCrumbs($crumbs)
229
->appendChild(
230
array(
231
$view,
232
));
233
234
}
235
}
236
237