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