2011-01-16 22:51:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class AphrontFormView extends AphrontView {
|
|
|
|
|
|
|
|
private $action;
|
|
|
|
private $method = 'POST';
|
|
|
|
private $header;
|
|
|
|
private $data = array();
|
2011-01-23 03:33:00 +01:00
|
|
|
private $encType;
|
2011-02-06 07:36:21 +01:00
|
|
|
private $workflow;
|
2011-03-23 01:08:08 +01:00
|
|
|
private $id;
|
2013-08-26 20:53:11 +02:00
|
|
|
private $shaded = false;
|
2012-12-11 23:02:29 +01:00
|
|
|
private $sigils = array();
|
2013-07-29 03:21:22 +02:00
|
|
|
private $metadata;
|
2015-03-31 23:10:32 +02:00
|
|
|
private $controls = array();
|
2015-03-31 23:10:55 +02:00
|
|
|
private $fullWidth = false;
|
2013-07-29 03:21:22 +02:00
|
|
|
|
|
|
|
public function setMetadata($metadata) {
|
|
|
|
$this->metadata = $metadata;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMetadata() {
|
|
|
|
return $this->metadata;
|
|
|
|
}
|
2012-08-15 23:15:12 +02:00
|
|
|
|
2011-03-23 01:08:08 +01:00
|
|
|
public function setID($id) {
|
|
|
|
$this->id = $id;
|
|
|
|
return $this;
|
2011-01-31 03:52:29 +01:00
|
|
|
}
|
2011-01-16 22:51:39 +01:00
|
|
|
|
|
|
|
public function setAction($action) {
|
|
|
|
$this->action = $action;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setMethod($method) {
|
|
|
|
$this->method = $method;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-01-23 03:33:00 +01:00
|
|
|
public function setEncType($enc_type) {
|
|
|
|
$this->encType = $enc_type;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-26 20:53:11 +02:00
|
|
|
public function setShaded($shaded) {
|
|
|
|
$this->shaded = $shaded;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-01-26 02:17:19 +01:00
|
|
|
public function addHiddenInput($key, $value) {
|
|
|
|
$this->data[$key] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-02-06 07:36:21 +01:00
|
|
|
public function setWorkflow($workflow) {
|
|
|
|
$this->workflow = $workflow;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-12-11 23:02:29 +01:00
|
|
|
public function addSigil($sigil) {
|
|
|
|
$this->sigils[] = $sigil;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-03-31 23:10:55 +02:00
|
|
|
public function setFullWidth($full_width) {
|
|
|
|
$this->fullWidth = $full_width;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFullWidth() {
|
|
|
|
return $this->fullWidth;
|
|
|
|
}
|
|
|
|
|
2013-05-24 21:38:27 +02:00
|
|
|
public function appendInstructions($text) {
|
|
|
|
return $this->appendChild(
|
|
|
|
phutil_tag(
|
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'class' => 'aphront-form-instructions',
|
|
|
|
),
|
|
|
|
$text));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function appendRemarkupInstructions($remarkup) {
|
|
|
|
return $this->appendInstructions(
|
|
|
|
PhabricatorMarkupEngine::renderOneObject(
|
|
|
|
id(new PhabricatorMarkupOneOff())->setContent($remarkup),
|
|
|
|
'default',
|
|
|
|
$this->getUser()));
|
|
|
|
}
|
|
|
|
|
2014-02-24 21:20:49 +01:00
|
|
|
public function buildLayoutView() {
|
2015-04-01 21:41:23 +02:00
|
|
|
foreach ($this->controls as $control) {
|
|
|
|
$control->setUser($this->getUser());
|
2015-04-02 22:42:01 +02:00
|
|
|
$control->willRender();
|
2015-04-01 21:41:23 +02:00
|
|
|
}
|
|
|
|
|
2014-02-24 21:20:49 +01:00
|
|
|
return id(new PHUIFormLayoutView())
|
2015-03-31 23:10:55 +02:00
|
|
|
->setFullWidth($this->getFullWidth())
|
2011-06-10 00:28:29 +02:00
|
|
|
->appendChild($this->renderDataInputs())
|
2013-02-13 23:50:15 +01:00
|
|
|
->appendChild($this->renderChildren());
|
2014-02-24 21:20:49 +01:00
|
|
|
}
|
|
|
|
|
2015-03-31 23:10:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Append a control to the form.
|
|
|
|
*
|
|
|
|
* This method behaves like @{method:appendChild}, but it only takes
|
|
|
|
* controls. It will propagate some information from the form to the
|
|
|
|
* control to simplify rendering.
|
|
|
|
*
|
|
|
|
* @param AphrontFormControl Control to append.
|
|
|
|
* @return this
|
|
|
|
*/
|
|
|
|
public function appendControl(AphrontFormControl $control) {
|
|
|
|
$this->controls[] = $control;
|
|
|
|
return $this->appendChild($control);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-24 21:20:49 +01:00
|
|
|
public function render() {
|
|
|
|
require_celerity_resource('phui-form-view-css');
|
|
|
|
|
|
|
|
$layout = $this->buildLayoutView();
|
2011-06-10 00:28:29 +02:00
|
|
|
|
2012-02-03 03:09:51 +01:00
|
|
|
if (!$this->user) {
|
2013-03-02 00:37:32 +01:00
|
|
|
throw new Exception(pht('You must pass the user to AphrontFormView.'));
|
2012-02-03 03:09:51 +01:00
|
|
|
}
|
|
|
|
|
2012-12-11 23:02:29 +01:00
|
|
|
$sigils = $this->sigils;
|
|
|
|
if ($this->workflow) {
|
|
|
|
$sigils[] = 'workflow';
|
|
|
|
}
|
|
|
|
|
2013-02-08 00:18:34 +01:00
|
|
|
return phabricator_form(
|
2012-02-03 03:09:51 +01:00
|
|
|
$this->user,
|
2011-01-16 22:51:39 +01:00
|
|
|
array(
|
2013-08-26 20:53:11 +02:00
|
|
|
'class' => $this->shaded ? 'phui-form-shaded' : null,
|
2011-01-23 03:33:00 +01:00
|
|
|
'action' => $this->action,
|
|
|
|
'method' => $this->method,
|
|
|
|
'enctype' => $this->encType,
|
2012-12-11 23:02:29 +01:00
|
|
|
'sigil' => $sigils ? implode(' ', $sigils) : null,
|
2013-07-29 03:21:22 +02:00
|
|
|
'meta' => $this->metadata,
|
2011-03-23 01:08:08 +01:00
|
|
|
'id' => $this->id,
|
2011-01-16 22:51:39 +01:00
|
|
|
),
|
2011-06-10 00:28:29 +02:00
|
|
|
$layout->render());
|
2011-01-16 22:51:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function renderDataInputs() {
|
|
|
|
$inputs = array();
|
2012-02-03 03:09:51 +01:00
|
|
|
foreach ($this->data as $key => $value) {
|
2011-01-26 02:17:19 +01:00
|
|
|
if ($value === null) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-18 03:39:02 +01:00
|
|
|
$inputs[] = phutil_tag(
|
2011-01-16 22:51:39 +01:00
|
|
|
'input',
|
|
|
|
array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'name' => $key,
|
|
|
|
'value' => $value,
|
|
|
|
));
|
|
|
|
}
|
2013-02-08 00:18:34 +01:00
|
|
|
return $inputs;
|
2011-01-16 22:51:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|