3 final class AphrontDialogView
5 implements AphrontResponseProducerInterface
{
11 private $cancelText = 'Cancel';
13 private $hidden = array();
15 private $renderAsForm = true;
17 private $footers = array();
18 private $isStandalone;
19 private $method = 'POST';
20 private $disableWorkflowOnSubmit;
21 private $disableWorkflowOnCancel;
22 private $width = 'default';
23 private $errors = array();
25 private $validationException;
31 const WIDTH_DEFAULT
= 'default';
32 const WIDTH_FORM
= 'form';
33 const WIDTH_FULL
= 'full';
35 public function setMethod($method) {
36 $this->method
= $method;
40 public function setIsStandalone($is_standalone) {
41 $this->isStandalone
= $is_standalone;
45 public function setErrors(array $errors) {
46 $this->errors
= $errors;
50 public function getIsStandalone() {
51 return $this->isStandalone
;
54 public function setSubmitURI($uri) {
55 $this->submitURI
= $uri;
59 public function setTitle($title) {
60 $this->title
= $title;
64 public function getTitle() {
68 public function setShortTitle($short_title) {
69 $this->shortTitle
= $short_title;
73 public function getShortTitle() {
74 return $this->shortTitle
;
77 public function setResizeY($resize_y) {
78 $this->resizeY
= $resize_y;
82 public function getResizeY() {
83 return $this->resizeY
;
86 public function setResizeX($resize_x) {
87 $this->resizeX
= $resize_x;
91 public function getResizeX() {
92 return $this->resizeX
;
95 public function addSubmitButton($text = null) {
100 $this->submitButton
= $text;
104 public function addCancelButton($uri, $text = null) {
106 $text = pht('Cancel');
109 $this->cancelURI
= $uri;
110 $this->cancelText
= $text;
114 public function addFooter($footer) {
115 $this->footers
[] = $footer;
119 public function addHiddenInput($key, $value) {
120 if (is_array($value)) {
121 foreach ($value as $hidden_key => $hidden_value) {
122 $this->hidden
[] = array($key.'['.$hidden_key.']', $hidden_value);
125 $this->hidden
[] = array($key, $value);
130 public function setClass($class) {
131 $this->class = $class;
135 public function setFlush($flush) {
136 $this->flush
= $flush;
140 public function setRenderDialogAsDiv() {
141 // TODO: This API is awkward.
142 $this->renderAsForm
= false;
146 public function setFormID($id) {
151 public function setWidth($width) {
152 $this->width
= $width;
156 public function setObjectList(PHUIObjectItemListView
$list) {
157 $this->objectList
= true;
158 $box = id(new PHUIObjectBoxView())
159 ->setObjectList($list);
160 return $this->appendChild($box);
163 public function appendRemarkup($remarkup) {
164 $viewer = $this->getViewer();
165 $view = new PHUIRemarkupView($viewer, $remarkup);
167 $view_tag = phutil_tag(
170 'class' => 'aphront-dialog-view-paragraph',
174 return $this->appendChild($view_tag);
177 public function appendParagraph($paragraph) {
178 return $this->appendParagraphTag($paragraph);
181 public function appendCommand($command) {
182 $command_tag = phutil_tag('tt', array(), $command);
183 return $this->appendParagraphTag(
185 'aphront-dialog-view-command');
188 private function appendParagraphTag($content, $classes = null) {
190 $classes = (array)$classes;
195 array_unshift($classes, 'aphront-dialog-view-paragraph');
197 $paragraph_tag = phutil_tag(
200 'class' => implode(' ', $classes),
204 return $this->appendChild($paragraph_tag);
208 public function appendList(array $items) {
209 $listitems = array();
210 foreach ($items as $item) {
211 $listitems[] = phutil_tag(
214 'class' => 'remarkup-list-item',
218 return $this->appendChild(
222 'class' => 'remarkup-list',
227 public function appendForm(AphrontFormView
$form) {
228 return $this->appendChild($form->buildLayoutView());
231 public function setDisableWorkflowOnSubmit($disable_workflow_on_submit) {
232 $this->disableWorkflowOnSubmit
= $disable_workflow_on_submit;
236 public function getDisableWorkflowOnSubmit() {
237 return $this->disableWorkflowOnSubmit
;
240 public function setDisableWorkflowOnCancel($disable_workflow_on_cancel) {
241 $this->disableWorkflowOnCancel
= $disable_workflow_on_cancel;
245 public function getDisableWorkflowOnCancel() {
246 return $this->disableWorkflowOnCancel
;
249 public function setValidationException(
250 PhabricatorApplicationTransactionValidationException
$ex = null) {
251 $this->validationException
= $ex;
255 public function render() {
256 require_celerity_resource('aphront-dialog-view-css');
259 if ($this->submitButton
) {
261 if ($this->disableWorkflowOnSubmit
) {
262 $meta['disableWorkflow'] = true;
265 $buttons[] = javelin_tag(
268 'name' => '__submit__',
269 'sigil' => '__default__',
273 $this->submitButton
);
276 if ($this->cancelURI
) {
278 if ($this->disableWorkflowOnCancel
) {
279 $meta['disableWorkflow'] = true;
282 $buttons[] = javelin_tag(
285 'href' => $this->cancelURI
,
286 'class' => 'button button-grey',
287 'name' => '__cancel__',
288 'sigil' => 'jx-workflow-button',
294 if (!$this->hasViewer()) {
297 'You must call %s when rendering an %s.',
303 $classes[] = 'aphront-dialog-view';
304 $classes[] = $this->class;
306 $classes[] = 'aphront-dialog-flush';
309 switch ($this->width
) {
310 case self
::WIDTH_FORM
:
311 case self
::WIDTH_FULL
:
312 $classes[] = 'aphront-dialog-view-width-'.$this->width
;
314 case self
::WIDTH_DEFAULT
:
319 "Unknown dialog width '%s'!",
323 if ($this->isStandalone
) {
324 $classes[] = 'aphront-dialog-view-standalone';
327 if ($this->objectList
) {
328 $classes[] = 'aphront-dialog-object-list';
332 'class' => implode(' ', $classes),
333 'sigil' => 'jx-dialog',
337 $form_attributes = array(
338 'action' => $this->submitURI
,
339 'method' => $this->method
,
340 'id' => $this->formID
,
343 $hidden_inputs = array();
344 $hidden_inputs[] = phutil_tag(
348 'name' => '__dialog__',
352 foreach ($this->hidden
as $desc) {
353 list($key, $value) = $desc;
354 $hidden_inputs[] = javelin_tag(
360 'sigil' => 'aphront-dialog-application-input',
364 if (!$this->renderAsForm
) {
369 array_merge($hidden_inputs, $buttons)),
373 $children = $this->renderChildren();
375 $errors = $this->errors
;
377 $ex = $this->validationException
;
378 $exception_errors = null;
380 foreach ($ex->getErrors() as $error) {
381 $errors[] = $error->getMessage();
387 id(new PHUIInfoView())->setErrors($errors),
392 $header = new PHUIHeaderView();
393 $header->setHeader($this->title
);
396 if ($this->footers
) {
397 $footer = phutil_tag(
400 'class' => 'aphront-dialog-foot',
406 if ($this->resizeX ||
$this->resizeY
) {
407 $resize = javelin_tag(
410 'class' => 'aphront-dialog-resize',
411 'sigil' => 'jx-dialog-resize',
413 'resizeX' => $this->resizeX
,
414 'resizeY' => $this->resizeY
,
420 if ($buttons ||
$footer) {
424 'class' => 'aphront-dialog-tail grouped',
437 'class' => 'aphront-dialog-head',
442 'class' => 'aphront-dialog-body grouped',
448 if ($this->renderAsForm
) {
449 return phabricator_form(
451 $form_attributes +
$attributes,
452 array($hidden_inputs, $content));
462 /* -( AphrontResponseProducerInterface )----------------------------------- */
465 public function produceAphrontResponse() {
466 return id(new AphrontDialogResponse())