@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 PhabricatorMacroAudioController extends PhabricatorMacroController {
4
5 public function handleRequest(AphrontRequest $request) {
6 $viewer = $request->getViewer();
7 $id = $request->getURIData('id');
8
9 $this->requireApplicationCapability(
10 PhabricatorMacroManageCapability::CAPABILITY);
11
12 $macro = id(new PhabricatorMacroQuery())
13 ->setViewer($viewer)
14 ->requireCapabilities(
15 array(
16 PhabricatorPolicyCapability::CAN_VIEW,
17 ))
18 ->withIDs(array($id))
19 ->executeOne();
20
21 if (!$macro) {
22 return new Aphront404Response();
23 }
24
25 $errors = array();
26 $view_uri = $this->getApplicationURI('/view/'.$macro->getID().'/');
27
28 $e_file = null;
29 $file = null;
30
31 if ($request->isFormPost()) {
32 $xactions = array();
33
34 if ($request->getBool('behaviorForm')) {
35 $xactions[] = id(new PhabricatorMacroTransaction())
36 ->setTransactionType(
37 PhabricatorMacroAudioBehaviorTransaction::TRANSACTIONTYPE)
38 ->setNewValue($request->getStr('audioBehavior'));
39 } else {
40 $file = null;
41 if ($request->getFileExists('file')) {
42 $file = PhabricatorFile::newFromPHPUpload(
43 $_FILES['file'],
44 array(
45 'name' => $request->getStr('name'),
46 'authorPHID' => $viewer->getPHID(),
47 'isExplicitUpload' => true,
48 ));
49 }
50
51 if ($file) {
52 if (!$file->isAudio()) {
53 $errors[] = pht(
54 'The file you uploaded is invalid: it is not recognizable as '.
55 'a valid audio file.');
56 $e_file = pht('Invalid');
57 } else {
58 $xactions[] = id(new PhabricatorMacroTransaction())
59 ->setTransactionType(
60 PhabricatorMacroAudioTransaction::TRANSACTIONTYPE)
61 ->setNewValue($file->getPHID());
62 }
63 } else {
64 $errors[] = pht(
65 'To change the audio for a macro, you must upload an audio '.
66 'file.');
67 $e_file = pht('Required');
68 }
69 }
70
71 if (!$errors) {
72 id(new PhabricatorMacroEditor())
73 ->setActor($viewer)
74 ->setContinueOnNoEffect(true)
75 ->setContentSourceFromRequest($request)
76 ->applyTransactions($macro, $xactions);
77
78 return id(new AphrontRedirectResponse())->setURI($view_uri);
79 }
80 }
81
82 $suggestion_volume = pht(
83 'If you set one of the Play options, audio volume will automatically '.
84 'increase when scrolling closer to the Macro, and decrease when '.
85 'scrolling away.');
86
87 $suggestion_click = pht(
88 'If the audio does not play, you may need to reload the page and '.
89 'interact with it (for example, click somewhere), or check your web '.
90 'permissions as modern browsers may block audio autoplay.');
91
92 $form = id(new AphrontFormView())
93 ->addHiddenInput('behaviorForm', 1)
94 ->setUser($viewer);
95
96 $options = id(new AphrontFormRadioButtonControl())
97 ->setLabel(pht('Audio Behavior'))
98 ->setName('audioBehavior')
99 ->setValue(
100 nonempty(
101 $macro->getAudioBehavior(),
102 PhabricatorFileImageMacro::AUDIO_BEHAVIOR_NONE));
103
104 $options->addButton(
105 PhabricatorFileImageMacro::AUDIO_BEHAVIOR_NONE,
106 pht('Do Not Play'),
107 pht('Do not play audio.'));
108
109 $options->addButton(
110 PhabricatorFileImageMacro::AUDIO_BEHAVIOR_ONCE,
111 pht('Play Once'),
112 pht('Play audio once, when the viewer looks at the macro.'));
113
114 $options->addButton(
115 PhabricatorFileImageMacro::AUDIO_BEHAVIOR_LOOP,
116 pht('Play Continuously'),
117 pht(
118 'Play audio continuously, treating the macro as an audio source. '.
119 'Best for ambient sounds.'));
120
121 $options->setCaption(
122 implode(' ', array(
123 $suggestion_volume,
124 $suggestion_click,
125 )));
126
127 $form->appendChild($options);
128 $form->appendChild(
129 id(new AphrontFormSubmitControl())
130 ->setValue(pht('Save Audio Behavior'))
131 ->addCancelButton($view_uri));
132
133 $crumbs = $this->buildApplicationCrumbs();
134
135 $title = pht('Edit Audio: %s', $macro->getName());
136 $crumb = pht('Edit Audio');
137
138 $crumbs->addTextCrumb(pht('Macro "%s"', $macro->getName()), $view_uri);
139 $crumbs->addTextCrumb($crumb, $request->getRequestURI());
140 $crumbs->setBorder(true);
141
142 $upload_form = id(new AphrontFormView())
143 ->setEncType('multipart/form-data')
144 ->setUser($viewer)
145 ->appendChild(
146 id(new AphrontFormFileControl())
147 ->setLabel(pht('Audio File'))
148 ->setName('file'))
149 ->appendChild(
150 id(new AphrontFormSubmitControl())
151 ->setValue(pht('Upload File')));
152
153 $upload = id(new PHUIObjectBoxView())
154 ->setHeaderText(pht('Upload New Audio'))
155 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
156 ->setForm($upload_form);
157
158 $form_box = id(new PHUIObjectBoxView())
159 ->setHeaderText(pht('Behavior'))
160 ->setFormErrors($errors)
161 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
162 ->setForm($form);
163
164 $header = id(new PHUIHeaderView())
165 ->setHeader($title)
166 ->setHeaderIcon('fa-pencil');
167
168 $view = id(new PHUITwoColumnView())
169 ->setHeader($header)
170 ->setFooter(array(
171 $form_box,
172 $upload,
173 ));
174
175 return $this->newPage()
176 ->setTitle($title)
177 ->setCrumbs($crumbs)
178 ->appendChild($view);
179 }
180
181}