@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
3/**
4 * Renders the "HTTP Parameters" help page for edit engines.
5 *
6 * This page has a ton of text and specialized rendering on it, this class
7 * just pulls it out of the main @{class:PhabricatorEditEngine}.
8 */
9final class PhabricatorApplicationEditHTTPParameterHelpView
10 extends AphrontView {
11
12 private $object;
13 private $fields;
14
15 public function setObject($object) {
16 $this->object = $object;
17 return $this;
18 }
19
20 public function getObject() {
21 return $this->object;
22 }
23
24 public function setFields(array $fields) {
25 $this->fields = $fields;
26 return $this;
27 }
28
29 public function getFields() {
30 return $this->fields;
31 }
32
33 public function render() {
34 $object = $this->getObject();
35 $fields = $this->getFields();
36
37 $uri = 'https://phorge.example.com/application/edit/';
38
39 // Remove fields which do not expose an HTTP parameter type.
40 $types = array();
41 foreach ($fields as $key => $field) {
42 if (!$field->shouldGenerateTransactionsFromSubmit()) {
43 unset($fields[$key]);
44 continue;
45 }
46
47 $type = $field->getHTTPParameterType();
48 if ($type === null) {
49 unset($fields[$key]);
50 continue;
51 }
52
53 $types[$type->getTypeName()] = $type;
54 }
55
56 $intro = pht(<<<EOTEXT
57When creating objects in the web interface, you can use HTTP parameters to
58prefill fields in the form. This allows you to quickly create a link to a
59form with some of the fields already filled in with default values.
60
61To prefill a form, start by finding the URI for the form you want to prefill.
62Do this by navigating to the relevant application, clicking the "Create" button
63for the type of object you want to create, and then copying the URI out of your
64browser's address bar. It will usually look something like this:
65
66```
67%s
68```
69
70However, `phorge.example.com` will be the domain where your copy of this
71software is installed, and `application/` will be the URI for an application.
72Some applications have multiple forms for creating objects or URIs that look a
73little different than this example, so the URI may not look exactly like this.
74
75To prefill the form, add properly encoded HTTP parameters to the URI. You
76should end up with something like this:
77
78```
79%s?title=Platypus&body=Ornithopter
80```
81
82If the form has `title` and `body` fields of the correct types, visiting this
83link will prefill those fields with the values "Platypus" and "Ornithopter"
84respectively.
85
86The rest of this document shows which parameters you can add to this form and
87how to format them.
88
89
90Supported Fields
91----------------
92
93This form supports these fields:
94
95EOTEXT
96 ,
97 $uri,
98 $uri);
99
100 $rows = array();
101 foreach ($fields as $field) {
102 $rows[] = array(
103 $field->getLabel(),
104 head($field->getAllReadValueFromRequestKeys()),
105 $field->getHTTPParameterType()->getTypeName(),
106 $field->getDescription(),
107 );
108 }
109
110 $main_table = id(new AphrontTableView($rows))
111 ->setHeaders(
112 array(
113 pht('Label'),
114 pht('Key'),
115 pht('Type'),
116 pht('Description'),
117 ))
118 ->setColumnClasses(
119 array(
120 'pri',
121 null,
122 null,
123 'wide',
124 ));
125
126 $aliases_text = pht(<<<EOTEXT
127Aliases
128-------
129
130Aliases are alternate recognized keys for a field. For example, a field with
131a complex key like `examplePHIDs` might be have a simple version of that key
132as an alias, like `example`.
133
134Aliases work just like the primary key when prefilling forms. They make it
135easier to remember and use HTTP parameters by providing more natural ways to do
136some prefilling.
137
138For example, if a field has `examplePHIDs` as a key but has aliases `example`
139and `examples`, these three URIs will all do the same thing:
140
141```
142%s?examplePHIDs=...
143%s?examples=...
144%s?example=...
145```
146
147If a URI specifies multiple default values for a field, the value using the
148primary key has precedence. Generally, you can not mix different aliases in
149a single URI.
150
151EOTEXT
152 ,
153 $uri,
154 $uri,
155 $uri);
156
157 $rows = array();
158 foreach ($fields as $field) {
159 $aliases = array_slice($field->getAllReadValueFromRequestKeys(), 1);
160 if (!$aliases) {
161 continue;
162 }
163 $rows[] = array(
164 $field->getLabel(),
165 $field->getKey(),
166 implode(', ', $aliases),
167 );
168 }
169
170 $alias_table = id(new AphrontTableView($rows))
171 ->setNoDataString(pht('This object has no fields with aliases.'))
172 ->setHeaders(
173 array(
174 pht('Label'),
175 pht('Key'),
176 pht('Aliases'),
177 ))
178 ->setColumnClasses(
179 array(
180 'pri',
181 null,
182 'wide',
183 ));
184
185 $template_text = pht(<<<EOTEXT
186Template Objects
187----------------
188
189Instead of specifying each field value individually, you can specify another
190object to use as a template. Some of the initial fields will be copied from the
191template object.
192
193Specify a template object with the `template` parameter. You can use an ID,
194PHID, or monogram (for objects which have monograms). For example, you might
195use URIs like these:
196
197```
198%s?template=123
199%s?template=PHID-WXYZ-abcdef...
200%s?template=T123
201```
202
203You can combine the `template` parameter with HTTP parameters: the template
204object will be copied first, then any HTTP parameters will be read.
205
206When using `template`, these fields will be copied:
207EOTEXT
208 ,
209 $uri,
210 $uri,
211 $uri);
212
213 $yes = id(new PHUIIconView())->setIcon('fa-check-circle green');
214 $no = id(new PHUIIconView())->setIcon('fa-times grey');
215
216 $rows = array();
217 foreach ($fields as $field) {
218 $rows[] = array(
219 $field->getLabel(),
220 $field->getIsCopyable() ? $yes : $no,
221 );
222 }
223
224 $template_table = id(new AphrontTableView($rows))
225 ->setNoDataString(
226 pht('None of the fields on this object support templating.'))
227 ->setHeaders(
228 array(
229 pht('Field'),
230 pht('Will Copy'),
231 ))
232 ->setColumnClasses(
233 array(
234 'pri',
235 'wide',
236 ));
237
238 $select_text = pht(<<<EOTEXT
239Select Fields
240-------------
241
242Some fields support selection from a specific set of values. When prefilling
243these fields, use the value in the **Value** column to select the appropriate
244setting.
245
246EOTEXT
247 );
248
249 $rows = array();
250 foreach ($fields as $field) {
251 if (!($field instanceof PhabricatorSelectEditField)) {
252 continue;
253 }
254
255 $options = $field->getOptions();
256 $label = $field->getLabel();
257 foreach ($options as $option_key => $option_value) {
258 if (strlen($option_key)) {
259 $option_display = $option_key;
260 } else {
261 $option_display = phutil_tag('em', array(), pht('<empty>'));
262 }
263
264 $rows[] = array(
265 $label,
266 $option_display,
267 $option_value,
268 );
269 $label = null;
270 }
271 }
272
273 $select_table = id(new AphrontTableView($rows))
274 ->setNoDataString(pht('This object has no select fields.'))
275 ->setHeaders(
276 array(
277 pht('Field'),
278 pht('Value'),
279 pht('Label'),
280 ))
281 ->setColumnClasses(
282 array(
283 'pri',
284 null,
285 'wide',
286 ));
287
288 $types_text = pht(<<<EOTEXT
289Field Types
290-----------
291
292Fields in this form have the types described in the table below. This table
293shows how to format values for each field type.
294EOTEXT
295 );
296
297 $types_table = id(new PhabricatorHTTPParameterTypeTableView())
298 ->setHTTPParameterTypes($types);
299
300 return array(
301 $this->renderInstructions($intro),
302 $main_table,
303 $this->renderInstructions($aliases_text),
304 $alias_table,
305 $this->renderInstructions($template_text),
306 $template_table,
307 $this->renderInstructions($select_text),
308 $select_table,
309 $this->renderInstructions($types_text),
310 $types_table,
311 );
312 }
313
314 protected function renderInstructions($corpus) {
315 $viewer = $this->getUser();
316 $view = new PHUIRemarkupView($viewer, $corpus);
317
318 $view->setRemarkupOptions(
319 array(
320 PHUIRemarkupView::OPTION_PRESERVE_LINEBREAKS => false,
321 ));
322
323 return $view;
324 }
325
326}