| Total Complexity | 57 |
| Total Lines | 376 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TValidationSummary often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TValidationSummary, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class TValidationSummary extends \Prado\Web\UI\WebControls\TWebControl |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var TClientSideValidationSummaryOptions validation client side options. |
||
| 44 | */ |
||
| 45 | private $_clientSide; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructor. |
||
| 49 | * This method sets the foreground color to red. |
||
| 50 | */ |
||
| 51 | public function __construct() |
||
| 52 | { |
||
| 53 | parent::__construct(); |
||
| 54 | $this->setForeColor('red'); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return TValidationSummaryDisplayStyle the style of displaying the error messages. Defaults to TValidationSummaryDisplayStyle::Fixed. |
||
| 59 | */ |
||
| 60 | public function getDisplay() |
||
| 61 | { |
||
| 62 | return $this->getViewState('Display', TValidationSummaryDisplayStyle::Fixed); |
||
|
|
|||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param TValidationSummaryDisplayStyle the style of displaying the error messages |
||
| 67 | */ |
||
| 68 | public function setDisplay($value) |
||
| 69 | { |
||
| 70 | $this->setViewState('Display', TPropertyValue::ensureEnum($value, 'Prado\\Web\\UI\\WebControls\\TValidationSummaryDisplayStyle'), TValidationSummaryDisplayStyle::Fixed); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return string the header text displayed at the top of the summary |
||
| 75 | */ |
||
| 76 | public function getHeaderText() |
||
| 77 | { |
||
| 78 | return $this->getViewState('HeaderText', ''); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Sets the header text to be displayed at the top of the summary |
||
| 83 | * @param string the header text |
||
| 84 | */ |
||
| 85 | public function setHeaderText($value) |
||
| 86 | { |
||
| 87 | $this->setViewState('HeaderText', $value, ''); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return TValidationSummaryDisplayMode the mode of displaying error messages. Defaults to TValidationSummaryDisplayMode::BulletList. |
||
| 92 | */ |
||
| 93 | public function getDisplayMode() |
||
| 94 | { |
||
| 95 | return $this->getViewState('DisplayMode', TValidationSummaryDisplayMode::BulletList); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param TValidationSummaryDisplayMode the mode of displaying error messages |
||
| 100 | */ |
||
| 101 | public function setDisplayMode($value) |
||
| 102 | { |
||
| 103 | $this->setViewState('DisplayMode', TPropertyValue::ensureEnum($value, 'Prado\\Web\\UI\\WebControls\\TValidationSummaryDisplayMode'), TValidationSummaryDisplayMode::BulletList); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @return boolean whether the TValidationSummary component updates itself using client-side script. Defaults to true. |
||
| 108 | */ |
||
| 109 | public function getEnableClientScript() |
||
| 110 | { |
||
| 111 | return $this->getViewState('EnableClientScript', true); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param boolean whether the TValidationSummary component updates itself using client-side script. |
||
| 116 | */ |
||
| 117 | public function setEnableClientScript($value) |
||
| 118 | { |
||
| 119 | $this->setViewState('EnableClientScript', TPropertyValue::ensureBoolean($value), true); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return boolean whether the validation summary is displayed in a message box. Defaults to false. |
||
| 124 | */ |
||
| 125 | public function getShowMessageBox() |
||
| 126 | { |
||
| 127 | return $this->getViewState('ShowMessageBox', false); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param boolean whether the validation summary is displayed in a message box. |
||
| 132 | */ |
||
| 133 | public function setShowMessageBox($value) |
||
| 134 | { |
||
| 135 | $this->setViewState('ShowMessageBox', TPropertyValue::ensureBoolean($value), false); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return boolean whether the validation summary is displayed inline. Defaults to true. |
||
| 140 | */ |
||
| 141 | public function getShowSummary() |
||
| 142 | { |
||
| 143 | return $this->getViewState('ShowSummary', true); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param boolean whether the validation summary is displayed inline. |
||
| 148 | */ |
||
| 149 | public function setShowSummary($value) |
||
| 150 | { |
||
| 151 | $this->setViewState('ShowSummary', TPropertyValue::ensureBoolean($value), true); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return boolean whether scroll summary into viewport or not. Defaults to true. |
||
| 156 | */ |
||
| 157 | public function getScrollToSummary() |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param boolean whether scroll summary into viewport or not. |
||
| 164 | */ |
||
| 165 | public function setScrollToSummary($value) |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return boolean whether the validation summary should be anchored. Defaults to false. |
||
| 172 | */ |
||
| 173 | public function getShowAnchor() |
||
| 174 | { |
||
| 175 | return $this->getViewState('ShowAnchor', false); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param boolean whether the validation summary should be anchored. |
||
| 180 | */ |
||
| 181 | public function setShowAnchor($value) |
||
| 182 | { |
||
| 183 | $this->setViewState('ShowAnchor', TPropertyValue::ensureBoolean($value), false); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Gets the auto-update for this summary. |
||
| 188 | * @return boolean automatic client-side summary updates. Defaults to true. |
||
| 189 | */ |
||
| 190 | public function getAutoUpdate() |
||
| 191 | { |
||
| 192 | return $this->getViewState('AutoUpdate', true); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Sets the summary to auto-update on the client-side |
||
| 197 | * @param boolean true for automatic summary updates. |
||
| 198 | */ |
||
| 199 | public function setAutoUpdate($value) |
||
| 200 | { |
||
| 201 | $this->setViewState('AutoUpdate', TPropertyValue::ensureBoolean($value), true); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return string the group which this validator belongs to |
||
| 206 | */ |
||
| 207 | public function getValidationGroup() |
||
| 208 | { |
||
| 209 | return $this->getViewState('ValidationGroup', ''); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string the group which this validator belongs to |
||
| 214 | */ |
||
| 215 | public function setValidationGroup($value) |
||
| 216 | { |
||
| 217 | $this->setViewState('ValidationGroup', $value, ''); |
||
| 218 | } |
||
| 219 | |||
| 220 | protected function addAttributesToRender($writer) |
||
| 221 | { |
||
| 222 | $display = $this->getDisplay(); |
||
| 223 | $visible = $this->getEnabled(true) && count($this->getErrorMessages()) > 0; |
||
| 224 | if(!$visible) |
||
| 225 | { |
||
| 226 | if($display === TValidationSummaryDisplayStyle::None || $display === TValidationSummaryDisplayStyle::Dynamic) |
||
| 227 | $writer->addStyleAttribute('display', 'none'); |
||
| 228 | else |
||
| 229 | $writer->addStyleAttribute('visibility', 'hidden'); |
||
| 230 | } |
||
| 231 | $writer->addAttribute('id', $this->getClientID()); |
||
| 232 | parent::addAttributesToRender($writer); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Render the javascript for validation summary. |
||
| 237 | * @param array list of options for validation summary. |
||
| 238 | */ |
||
| 239 | protected function renderJsSummary() |
||
| 240 | { |
||
| 241 | if(!$this->getEnabled(true) || !$this->getEnableClientScript()) |
||
| 242 | return; |
||
| 243 | $cs = $this->getPage()->getClientScript(); |
||
| 244 | $cs->registerPradoScript('validator'); |
||
| 245 | |||
| 246 | //need to register the validation manager is validation summary is alone. |
||
| 247 | $formID = $this->getPage()->getForm()->getClientID(); |
||
| 248 | $scriptKey = "TBaseValidator:$formID"; |
||
| 249 | if($this->getEnableClientScript() && !$cs->isEndScriptRegistered($scriptKey)) |
||
| 250 | { |
||
| 251 | $manager['FormID'] = $formID; |
||
| 252 | $options = TJavaScript::encode($manager); |
||
| 253 | $cs->registerPradoScript('validator'); |
||
| 254 | $cs->registerEndScript($scriptKey, "new Prado.ValidationManager({$options});"); |
||
| 255 | } |
||
| 256 | |||
| 257 | |||
| 258 | $options = TJavaScript::encode($this->getClientScriptOptions()); |
||
| 259 | $script = "new Prado.WebUI.TValidationSummary({$options});"; |
||
| 260 | $cs->registerEndScript($this->getClientID(), $script); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get a list of options for the client-side javascript validation summary. |
||
| 265 | * @return array list of options for the summary |
||
| 266 | */ |
||
| 267 | protected function getClientScriptOptions() |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @return TClientSideValidationSummaryOptions client-side validation summary |
||
| 292 | * event options. |
||
| 293 | */ |
||
| 294 | public function getClientSide() |
||
| 295 | { |
||
| 296 | if($this->_clientSide === null) |
||
| 297 | $this->_clientSide = $this->createClientScript(); |
||
| 298 | return $this->_clientSide; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return TClientSideValidationSummaryOptions javascript validation summary |
||
| 303 | * event options. |
||
| 304 | */ |
||
| 305 | protected function createClientScript() |
||
| 306 | { |
||
| 307 | return new TClientSideValidationSummaryOptions; |
||
| 308 | } |
||
| 309 | /** |
||
| 310 | * Get the list of validation error messages. |
||
| 311 | * @return array list of validator error messages. |
||
| 312 | */ |
||
| 313 | protected function getErrorMessages() |
||
| 314 | { |
||
| 315 | $validators = $this->getPage()->getValidators($this->getValidationGroup()); |
||
| 316 | $messages = []; |
||
| 317 | foreach($validators as $validator) |
||
| 318 | { |
||
| 319 | if(!$validator->getIsValid() && ($msg = $validator->getErrorMessage()) !== '') |
||
| 320 | //$messages[] = $validator->getAnchoredMessage($msg); |
||
| 321 | $messages[] = $msg; |
||
| 322 | } |
||
| 323 | return $messages; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Overrides parent implementation by rendering TValidationSummary-specific presentation. |
||
| 328 | * @return string the rendering result |
||
| 329 | */ |
||
| 330 | public function renderContents($writer) |
||
| 331 | { |
||
| 332 | $this->renderJsSummary(); |
||
| 333 | if($this->getShowSummary()) |
||
| 334 | { |
||
| 335 | // $this->setStyle('display:block'); |
||
| 336 | switch($this->getDisplayMode()) |
||
| 337 | { |
||
| 338 | case TValidationSummaryDisplayMode::SimpleList: |
||
| 339 | $this->renderList($writer); |
||
| 340 | break; |
||
| 341 | case TValidationSummaryDisplayMode::SingleParagraph: |
||
| 342 | $this->renderSingleParagraph($writer); |
||
| 343 | break; |
||
| 344 | case TValidationSummaryDisplayMode::BulletList: |
||
| 345 | $this->renderBulletList($writer); |
||
| 346 | break; |
||
| 347 | case TValidationSummaryDisplayMode::HeaderOnly: |
||
| 348 | $this->renderHeaderOnly($writer); |
||
| 349 | break; |
||
| 350 | } |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Render the validation summary as a simple list. |
||
| 356 | * @param array list of messages |
||
| 357 | * @param string the header text |
||
| 358 | * @return string summary list |
||
| 359 | */ |
||
| 360 | protected function renderList($writer) |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Render the validation summary as a paragraph. |
||
| 374 | * @param array list of messages |
||
| 375 | * @param string the header text |
||
| 376 | * @return string summary paragraph |
||
| 377 | */ |
||
| 378 | protected function renderSingleParagraph($writer) |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Render the validation summary as a bullet list. |
||
| 390 | * @param array list of messages |
||
| 391 | * @param string the header text |
||
| 392 | * @return string summary bullet list |
||
| 393 | */ |
||
| 394 | protected function renderBulletList($writer) |
||
| 395 | { |
||
| 396 | $header = $this->getHeaderText(); |
||
| 397 | $messages = $this->getErrorMessages(); |
||
| 398 | $content = $header; |
||
| 399 | if(count($messages) > 0) |
||
| 400 | { |
||
| 401 | $content .= "<ul>\n"; |
||
| 402 | foreach($messages as $message) |
||
| 403 | $content .= '<li>' . $message . "</li>\n"; |
||
| 404 | $content .= "</ul>\n"; |
||
| 405 | } |
||
| 406 | $writer->write($content); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Render the validation summary header text only. |
||
| 411 | * @param THtmlWriter the writer used for the rendering purpose |
||
| 412 | */ |
||
| 413 | protected function renderHeaderOnly($writer) |
||
| 416 | } |
||
| 417 | } |
||
| 418 |