mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-15 11:22:40 +01:00
40680e459f
Summary: Ref T2232. Very busy day on IRC so I feel like I've made 20 minutes of progress in 1-minute spurts here, but this adds the basics for a form that can have multiple pages and automatically handle pagination and reading to/from the request, objects and responses. The UIExample is reasonably instructive. Basically, you make a form, add pages to the form, and add controls to the pages. The core flow control looks like this: if ($request->isFormPost()) { $form->readFromRequest($request); // (1) if ($form->isComplete()) { // (2) $response = $form->writeToResponse($response); // (3) // Process result here. // (4) } } else { $form->readFromObject($object); // (5) } The key parts are: # This reads the form state from the request, including reading all the inactive pages. # This tests if all pages are valid and the user just clicked "Done" on the last page. # This produces a "response", which might be writing to an object (for simpler forms) or creating a transaction record (for more complex forms). # Here, we would save the object or apply the transactions. # When the user views the form for the first time, we preload all the values from some object (which might just be empty). Ultimate goal here is to fix repository creation to not be a terrible pit of awfulness. There are probably a lot of rough edges and missing features still, but this seems to not be totally crazy. I'm using two submit buttons with different names which doesn't work on IE7 or something, but we can JS our way out of that if we need to. Test Plan: Paged forward and backward through the form. Reviewers: btrahan, chad Reviewed By: btrahan CC: aran Maniphest Tasks: T2232 Differential Revision: https://secure.phabricator.com/D6003
252 lines
5.4 KiB
PHP
252 lines
5.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
*
|
|
* @task page Managing Pages
|
|
*/
|
|
final class PHUIPagedFormView extends AphrontTagView {
|
|
|
|
private $name = 'pages';
|
|
private $pages = array();
|
|
private $selectedPage;
|
|
private $choosePage;
|
|
private $nextPage;
|
|
private $prevPage;
|
|
private $complete;
|
|
|
|
protected function canAppendChild() {
|
|
return false;
|
|
}
|
|
|
|
|
|
/* -( Managing Pages )----------------------------------------------------- */
|
|
|
|
|
|
/**
|
|
* @task page
|
|
*/
|
|
public function addPage($key, PHUIFormPageView $page) {
|
|
if (isset($this->pages[$key])) {
|
|
throw new Exception("Duplicate page with key '{$key}'!");
|
|
}
|
|
$this->pages[$key] = $page;
|
|
$page->setPagedFormView($this, $key);
|
|
return $this;
|
|
}
|
|
|
|
|
|
/**
|
|
* @task page
|
|
*/
|
|
public function getPage($key) {
|
|
if (!$this->pageExists($key)) {
|
|
throw new Exception("No page '{$key}' exists!");
|
|
}
|
|
return $this->pages[$key];
|
|
}
|
|
|
|
|
|
/**
|
|
* @task page
|
|
*/
|
|
public function pageExists($key) {
|
|
return isset($this->pages[$key]);
|
|
}
|
|
|
|
|
|
/**
|
|
* @task page
|
|
*/
|
|
protected function getPageIndex($key) {
|
|
$page = $this->getPage($key);
|
|
|
|
$index = 0;
|
|
foreach ($this->pages as $target_page) {
|
|
if ($page === $target_page) {
|
|
break;
|
|
}
|
|
$index++;
|
|
}
|
|
|
|
return $index;
|
|
}
|
|
|
|
|
|
/**
|
|
* @task page
|
|
*/
|
|
protected function getPageByIndex($index) {
|
|
foreach ($this->pages as $page) {
|
|
if (!$index) {
|
|
return $page;
|
|
}
|
|
$index--;
|
|
}
|
|
|
|
throw new Exception("Requesting out-of-bounds page '{$index}'.");
|
|
}
|
|
|
|
protected function getLastIndex() {
|
|
return count($this->pages) - 1;
|
|
}
|
|
|
|
protected function isFirstPage(PHUIFormPageView $page) {
|
|
return ($this->getPageIndex($page->getKey()) === 0);
|
|
|
|
}
|
|
|
|
protected function isLastPage(PHUIFormPageView $page) {
|
|
return ($this->getPageIndex($page->getKey()) === (count($this->pages) - 1));
|
|
}
|
|
|
|
public function getSelectedPage() {
|
|
return $this->selectedPage;
|
|
}
|
|
|
|
public function readFromObject($object) {
|
|
foreach ($this->pages as $page) {
|
|
$page->validateObjectType($object);
|
|
$page->readFromObject($object);
|
|
}
|
|
|
|
return $this->processForm();
|
|
}
|
|
|
|
public function writeToResponse($response) {
|
|
foreach ($this->pages as $page) {
|
|
$page->validateResponseType($response);
|
|
$response = $page->writeToResponse($page);
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
public function readFromRequest(AphrontRequest $request) {
|
|
$active_page = $request->getStr($this->getRequestKey('page'));
|
|
|
|
foreach ($this->pages as $key => $page) {
|
|
if ($key == $active_page) {
|
|
$page->readFromRequest($request);
|
|
} else {
|
|
$page->readSerializedValues($request);
|
|
}
|
|
}
|
|
|
|
$this->choosePage = $active_page;
|
|
$this->nextPage = $request->getStr('__submit__');
|
|
$this->prevPage = $request->getStr('__back__');
|
|
|
|
return $this->processForm();
|
|
}
|
|
|
|
public function setName($name) {
|
|
$this->name = $name;
|
|
return $this;
|
|
}
|
|
|
|
|
|
public function getValue($page, $key, $default = null) {
|
|
return $this->getPage($page)->getValue($key, $default);
|
|
}
|
|
|
|
public function setValue($page, $key, $value) {
|
|
$this->getPage($page)->setValue($key, $value);
|
|
return $this;
|
|
}
|
|
|
|
public function processForm() {
|
|
foreach ($this->pages as $key => $page) {
|
|
if (!$page->isValid()) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($this->pageExists($this->choosePage)) {
|
|
$selected = $this->getPage($this->choosePage);
|
|
} else {
|
|
$selected = $this->getPageByIndex(0);
|
|
}
|
|
|
|
$is_attempt_complete = false;
|
|
if ($this->prevPage) {
|
|
$prev_index = $this->getPageIndex($selected->getKey()) - 1;;
|
|
$index = max(0, $prev_index);
|
|
$selected = $this->getPageByIndex($index);
|
|
} else if ($this->nextPage) {
|
|
$next_index = $this->getPageIndex($selected->getKey()) + 1;
|
|
if ($next_index > $this->getLastIndex()) {
|
|
$is_attempt_complete = true;
|
|
}
|
|
$index = min($this->getLastIndex(), $next_index);
|
|
$selected = $this->getPageByIndex($index);
|
|
}
|
|
|
|
$validation_error = false;
|
|
foreach ($this->pages as $key => $page) {
|
|
if (!$page->isValid()) {
|
|
$validation_error = true;
|
|
break;
|
|
}
|
|
if ($page === $selected) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($is_attempt_complete && !$validation_error) {
|
|
$this->complete = true;
|
|
} else {
|
|
$this->selectedPage = $page;
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function isComplete() {
|
|
return $this->complete;
|
|
}
|
|
|
|
public function getRequestKey($key) {
|
|
return $this->name.':'.$key;
|
|
}
|
|
|
|
public function getTagContent() {
|
|
$form = id(new AphrontFormView())
|
|
->setUser($this->getUser());
|
|
|
|
$selected_page = $this->getSelectedPage();
|
|
if (!$selected_page) {
|
|
throw new Exception("No selected page!");
|
|
}
|
|
|
|
$form->addHiddenInput(
|
|
$this->getRequestKey('page'),
|
|
$selected_page->getKey());
|
|
|
|
foreach ($this->pages as $page) {
|
|
if ($page == $selected_page) {
|
|
continue;
|
|
}
|
|
foreach ($page->getSerializedValues() as $key => $value) {
|
|
$form->addHiddenInput($key, $value);
|
|
}
|
|
}
|
|
|
|
$submit = id(new PHUIFormMultiSubmitControl());
|
|
|
|
if (!$this->isFirstPage($selected_page)) {
|
|
$submit->addBackButton();
|
|
}
|
|
|
|
if ($this->isLastPage($selected_page)) {
|
|
$submit->addSubmitButton(pht("Save"));
|
|
} else {
|
|
$submit->addSubmitButton(pht("Continue \xC2\xBB"));
|
|
}
|
|
|
|
$form->appendChild($selected_page);
|
|
$form->appendChild($submit);
|
|
|
|
return $form;
|
|
}
|
|
|
|
}
|