Path: blob/master/src/view/form/AphrontFormView.php
12249 views
<?php12final class AphrontFormView extends AphrontView {34private $action;5private $method = 'POST';6private $header;7private $data = array();8private $encType;9private $workflow;10private $id;11private $sigils = array();12private $metadata;13private $controls = array();14private $fullWidth = false;15private $classes = array();1617public function setMetadata($metadata) {18$this->metadata = $metadata;19return $this;20}2122public function getMetadata() {23return $this->metadata;24}2526public function setID($id) {27$this->id = $id;28return $this;29}3031public function setAction($action) {32$this->action = $action;33return $this;34}3536public function setMethod($method) {37$this->method = $method;38return $this;39}4041public function setEncType($enc_type) {42$this->encType = $enc_type;43return $this;44}4546public function addHiddenInput($key, $value) {47$this->data[$key] = $value;48return $this;49}5051public function setWorkflow($workflow) {52$this->workflow = $workflow;53return $this;54}5556public function addSigil($sigil) {57$this->sigils[] = $sigil;58return $this;59}6061public function addClass($class) {62$this->classes[] = $class;63return $this;64}6566public function setFullWidth($full_width) {67$this->fullWidth = $full_width;68return $this;69}7071public function getFullWidth() {72return $this->fullWidth;73}7475public function appendInstructions($text) {76return $this->appendChild(77phutil_tag(78'div',79array(80'class' => 'aphront-form-instructions',81),82$text));83}8485public function appendRemarkupInstructions($remarkup) {86$view = $this->newInstructionsRemarkupView($remarkup);87return $this->appendInstructions($view);88}8990public function newInstructionsRemarkupView($remarkup) {91$viewer = $this->getViewer();92$view = new PHUIRemarkupView($viewer, $remarkup);9394$view->setRemarkupOptions(95array(96PHUIRemarkupView::OPTION_PRESERVE_LINEBREAKS => false,97));9899return $view;100}101102public function buildLayoutView() {103foreach ($this->controls as $control) {104$control->setViewer($this->getViewer());105$control->willRender();106}107108return id(new PHUIFormLayoutView())109->setFullWidth($this->getFullWidth())110->appendChild($this->renderDataInputs())111->appendChild($this->renderChildren());112}113114115/**116* Append a control to the form.117*118* This method behaves like @{method:appendChild}, but it only takes119* controls. It will propagate some information from the form to the120* control to simplify rendering.121*122* @param AphrontFormControl Control to append.123* @return this124*/125public function appendControl(AphrontFormControl $control) {126$this->controls[] = $control;127return $this->appendChild($control);128}129130131public function render() {132require_celerity_resource('phui-form-view-css');133134$layout = $this->buildLayoutView();135136if (!$this->hasViewer()) {137throw new Exception(138pht(139'You must pass the user to %s.',140__CLASS__));141}142143$sigils = $this->sigils;144if ($this->workflow) {145$sigils[] = 'workflow';146}147148return phabricator_form(149$this->getViewer(),150array(151'class' => implode(' ', $this->classes),152'action' => $this->action,153'method' => $this->method,154'enctype' => $this->encType,155'sigil' => $sigils ? implode(' ', $sigils) : null,156'meta' => $this->metadata,157'id' => $this->id,158),159$layout->render());160}161162private function renderDataInputs() {163$inputs = array();164foreach ($this->data as $key => $value) {165if ($value === null) {166continue;167}168$inputs[] = phutil_tag(169'input',170array(171'type' => 'hidden',172'name' => $key,173'value' => $value,174));175}176return $inputs;177}178179}180181182