@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
at upstream/main 180 lines 4.0 kB view raw
1<?php 2 3final class AphrontFormView extends AphrontView { 4 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(); 17 18 public function setMetadata($metadata) { 19 $this->metadata = $metadata; 20 return $this; 21 } 22 23 public function getMetadata() { 24 return $this->metadata; 25 } 26 27 public function setID($id) { 28 $this->id = $id; 29 return $this; 30 } 31 32 public function setAction($action) { 33 $this->action = $action; 34 return $this; 35 } 36 37 public function setMethod($method) { 38 $this->method = $method; 39 return $this; 40 } 41 42 public function setEncType($enc_type) { 43 $this->encType = $enc_type; 44 return $this; 45 } 46 47 public function addHiddenInput($key, $value) { 48 $this->data[$key] = $value; 49 return $this; 50 } 51 52 public function setWorkflow($workflow) { 53 $this->workflow = $workflow; 54 return $this; 55 } 56 57 public function addSigil($sigil) { 58 $this->sigils[] = $sigil; 59 return $this; 60 } 61 62 public function addClass($class) { 63 $this->classes[] = $class; 64 return $this; 65 } 66 67 public function setFullWidth($full_width) { 68 $this->fullWidth = $full_width; 69 return $this; 70 } 71 72 public function getFullWidth() { 73 return $this->fullWidth; 74 } 75 76 public function appendInstructions($text) { 77 return $this->appendChild( 78 phutil_tag( 79 'div', 80 array( 81 'class' => 'aphront-form-instructions', 82 ), 83 $text)); 84 } 85 86 public function appendRemarkupInstructions($remarkup) { 87 $view = $this->newInstructionsRemarkupView($remarkup); 88 return $this->appendInstructions($view); 89 } 90 91 public function newInstructionsRemarkupView($remarkup) { 92 $viewer = $this->getViewer(); 93 $view = new PHUIRemarkupView($viewer, $remarkup); 94 95 $view->setRemarkupOptions( 96 array( 97 PHUIRemarkupView::OPTION_PRESERVE_LINEBREAKS => false, 98 )); 99 100 return $view; 101 } 102 103 public function buildLayoutView() { 104 foreach ($this->controls as $control) { 105 $control->setViewer($this->getViewer()); 106 $control->willRender(); 107 } 108 109 return id(new PHUIFormLayoutView()) 110 ->setFullWidth($this->getFullWidth()) 111 ->appendChild($this->renderDataInputs()) 112 ->appendChild($this->renderChildren()); 113 } 114 115 116 /** 117 * Append a control to the form. 118 * 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. 122 * 123 * @param AphrontFormControl $control Control to append. 124 * @return $this 125 */ 126 public function appendControl(AphrontFormControl $control) { 127 $this->controls[] = $control; 128 return $this->appendChild($control); 129 } 130 131 132 public function render() { 133 require_celerity_resource('phui-form-view-css'); 134 135 $layout = $this->buildLayoutView(); 136 137 if (!$this->hasViewer()) { 138 throw new Exception( 139 pht( 140 'You must pass the user to %s.', 141 self::class)); 142 } 143 144 $sigils = $this->sigils; 145 if ($this->workflow) { 146 $sigils[] = 'workflow'; 147 } 148 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, 159 ), 160 $layout->render()); 161 } 162 163 private function renderDataInputs() { 164 $inputs = array(); 165 foreach ($this->data as $key => $value) { 166 if ($value === null) { 167 continue; 168 } 169 $inputs[] = phutil_tag( 170 'input', 171 array( 172 'type' => 'hidden', 173 'name' => $key, 174 'value' => $value, 175 )); 176 } 177 return $inputs; 178 } 179 180}