[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @concrete-extensible 5 */ 6 class PHUIFormPageView extends AphrontView { 7 8 private $key; 9 private $form; 10 private $controls = array(); 11 private $content = array(); 12 private $values = array(); 13 private $isValid; 14 private $validateFormPageCallback; 15 private $adjustFormPageCallback; 16 private $pageErrors = array(); 17 private $pageName; 18 19 20 public function setPageName($page_name) { 21 $this->pageName = $page_name; 22 return $this; 23 } 24 25 public function getPageName() { 26 return $this->pageName; 27 } 28 29 public function addPageError($page_error) { 30 $this->pageErrors[] = $page_error; 31 return $this; 32 } 33 34 public function getPageErrors() { 35 return $this->pageErrors; 36 } 37 38 public function setAdjustFormPageCallback($adjust_form_page_callback) { 39 $this->adjustFormPageCallback = $adjust_form_page_callback; 40 return $this; 41 } 42 43 public function setValidateFormPageCallback($validate_form_page_callback) { 44 $this->validateFormPageCallback = $validate_form_page_callback; 45 return $this; 46 } 47 48 public function addInstructions($text, $before = null) { 49 $tag = phutil_tag( 50 'div', 51 array( 52 'class' => 'aphront-form-instructions', 53 ), 54 $text); 55 56 $append = true; 57 if ($before !== null) { 58 for ($ii = 0; $ii < count($this->content); $ii++) { 59 if ($this->content[$ii] instanceof AphrontFormControl) { 60 if ($this->content[$ii]->getName() == $before) { 61 array_splice($this->content, $ii, 0, array($tag)); 62 $append = false; 63 break; 64 } 65 } 66 } 67 } 68 69 if ($append) { 70 $this->content[] = $tag; 71 } 72 73 return $this; 74 } 75 76 public function addRemarkupInstructions($remarkup, $before = null) { 77 return $this->addInstructions( 78 PhabricatorMarkupEngine::renderOneObject( 79 id(new PhabricatorMarkupOneOff())->setContent($remarkup), 80 'default', 81 $this->getUser()), $before); 82 } 83 84 public function addControl(AphrontFormControl $control) { 85 $name = $control->getName(); 86 87 if (!strlen($name)) { 88 throw new Exception('Form control has no name!'); 89 } 90 91 if (isset($this->controls[$name])) { 92 throw new Exception( 93 "Form page contains duplicate control with name '{$name}'!"); 94 } 95 96 $this->controls[$name] = $control; 97 $this->content[] = $control; 98 $control->setFormPage($this); 99 100 return $this; 101 } 102 103 public function getControls() { 104 return $this->controls; 105 } 106 107 public function getControl($name) { 108 if (empty($this->controls[$name])) { 109 throw new Exception("No page control '{$name}'!"); 110 } 111 return $this->controls[$name]; 112 } 113 114 protected function canAppendChild() { 115 return false; 116 } 117 118 public function setPagedFormView(PHUIPagedFormView $view, $key) { 119 if ($this->key) { 120 throw new Exception('This page is already part of a form!'); 121 } 122 $this->form = $view; 123 $this->key = $key; 124 return $this; 125 } 126 127 public function adjustFormPage() { 128 if ($this->adjustFormPageCallback) { 129 call_user_func($this->adjustFormPageCallback, $this); 130 } 131 return $this; 132 } 133 134 protected function validateFormPage() { 135 if ($this->validateFormPageCallback) { 136 return call_user_func($this->validateFormPageCallback, $this); 137 } 138 return true; 139 } 140 141 public function getKey() { 142 return $this->key; 143 } 144 145 public function render() { 146 return $this->content; 147 } 148 149 public function getForm() { 150 return $this->form; 151 } 152 153 public function getRequestKey($key) { 154 return $this->getForm()->getRequestKey('p:'.$this->key.':'.$key); 155 } 156 157 public function validateObjectType($object) { 158 return true; 159 } 160 161 public function validateResponseType($response) { 162 return true; 163 } 164 165 protected function validateControls() { 166 $result = true; 167 foreach ($this->getControls() as $name => $control) { 168 if (!$control->isValid()) { 169 $result = false; 170 break; 171 } 172 } 173 174 return $result; 175 } 176 177 public function isValid() { 178 if ($this->isValid === null) { 179 $this->isValid = $this->validateControls() && $this->validateFormPage(); 180 } 181 return $this->isValid; 182 } 183 184 public function readFromRequest(AphrontRequest $request) { 185 foreach ($this->getControls() as $name => $control) { 186 $control->readValueFromRequest($request); 187 } 188 189 return $this; 190 } 191 192 public function readFromObject($object) { 193 foreach ($this->getControls() as $name => $control) { 194 if (is_array($object)) { 195 $control->readValueFromDictionary($object); 196 } 197 } 198 199 return $this; 200 } 201 202 public function writeToResponse($response) { 203 return $this; 204 } 205 206 public function readSerializedValues(AphrontRequest $request) { 207 foreach ($this->getControls() as $name => $control) { 208 $key = $this->getRequestKey($name); 209 $control->readSerializedValue($request->getStr($key)); 210 } 211 212 return $this; 213 } 214 215 public function getSerializedValues() { 216 $dict = array(); 217 foreach ($this->getControls() as $name => $control) { 218 $key = $this->getRequestKey($name); 219 $dict[$key] = $control->getSerializedValue(); 220 } 221 return $dict; 222 } 223 224 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |