Add a basic "harbormaster.step.edit" API method
[phabricator/blender.git] / src / view / form / AphrontFormView.php
blob3022a91f9cf16f603d3aff8c0b3973944e971b93
1 <?php
3 final class AphrontFormView extends AphrontView {
5 private $action;
6 private $method = 'POST';
7 private $header;
8 private $data = array();
9 private $encType;
10 private $workflow;
11 private $id;
12 private $sigils = array();
13 private $metadata;
14 private $controls = array();
15 private $fullWidth = false;
16 private $classes = array();
18 public function setMetadata($metadata) {
19 $this->metadata = $metadata;
20 return $this;
23 public function getMetadata() {
24 return $this->metadata;
27 public function setID($id) {
28 $this->id = $id;
29 return $this;
32 public function setAction($action) {
33 $this->action = $action;
34 return $this;
37 public function setMethod($method) {
38 $this->method = $method;
39 return $this;
42 public function setEncType($enc_type) {
43 $this->encType = $enc_type;
44 return $this;
47 public function addHiddenInput($key, $value) {
48 $this->data[$key] = $value;
49 return $this;
52 public function setWorkflow($workflow) {
53 $this->workflow = $workflow;
54 return $this;
57 public function addSigil($sigil) {
58 $this->sigils[] = $sigil;
59 return $this;
62 public function addClass($class) {
63 $this->classes[] = $class;
64 return $this;
67 public function setFullWidth($full_width) {
68 $this->fullWidth = $full_width;
69 return $this;
72 public function getFullWidth() {
73 return $this->fullWidth;
76 public function appendInstructions($text) {
77 return $this->appendChild(
78 phutil_tag(
79 'div',
80 array(
81 'class' => 'aphront-form-instructions',
83 $text));
86 public function appendRemarkupInstructions($remarkup) {
87 $view = $this->newInstructionsRemarkupView($remarkup);
88 return $this->appendInstructions($view);
91 public function newInstructionsRemarkupView($remarkup) {
92 $viewer = $this->getViewer();
93 $view = new PHUIRemarkupView($viewer, $remarkup);
95 $view->setRemarkupOptions(
96 array(
97 PHUIRemarkupView::OPTION_PRESERVE_LINEBREAKS => false,
98 ));
100 return $view;
103 public function buildLayoutView() {
104 foreach ($this->controls as $control) {
105 $control->setViewer($this->getViewer());
106 $control->willRender();
109 return id(new PHUIFormLayoutView())
110 ->setFullWidth($this->getFullWidth())
111 ->appendChild($this->renderDataInputs())
112 ->appendChild($this->renderChildren());
117 * Append a control to the form.
119 * This method behaves like @{method:appendChild}, but it only takes
120 * controls. It will propagate some information from the form to the
121 * control to simplify rendering.
123 * @param AphrontFormControl Control to append.
124 * @return this
126 public function appendControl(AphrontFormControl $control) {
127 $this->controls[] = $control;
128 return $this->appendChild($control);
132 public function render() {
133 require_celerity_resource('phui-form-view-css');
135 $layout = $this->buildLayoutView();
137 if (!$this->hasViewer()) {
138 throw new Exception(
139 pht(
140 'You must pass the user to %s.',
141 __CLASS__));
144 $sigils = $this->sigils;
145 if ($this->workflow) {
146 $sigils[] = 'workflow';
149 return phabricator_form(
150 $this->getViewer(),
151 array(
152 'class' => implode(' ', $this->classes),
153 'action' => $this->action,
154 'method' => $this->method,
155 'enctype' => $this->encType,
156 'sigil' => $sigils ? implode(' ', $sigils) : null,
157 'meta' => $this->metadata,
158 'id' => $this->id,
160 $layout->render());
163 private function renderDataInputs() {
164 $inputs = array();
165 foreach ($this->data as $key => $value) {
166 if ($value === null) {
167 continue;
169 $inputs[] = phutil_tag(
170 'input',
171 array(
172 'type' => 'hidden',
173 'name' => $key,
174 'value' => $value,
177 return $inputs;