Index: trunk/extensions/CongressLookup/SpecialCongressLookup.php |
— | — | @@ -5,7 +5,7 @@ |
6 | 6 | * to the user. |
7 | 7 | */ |
8 | 8 | class SpecialCongressLookup extends UnlistedSpecialPage { |
9 | | - var $zip; |
| 9 | + protected $zip = null; |
10 | 10 | |
11 | 11 | public function __construct() { |
12 | 12 | // Register special page |
— | — | @@ -19,7 +19,7 @@ |
20 | 20 | global $wgRequest, $wgOut; |
21 | 21 | |
22 | 22 | // Pull in query string parameters |
23 | | - $this->zip = $wgRequest->getVal( 'zip' ); |
| 23 | + $this->setZip( $wgRequest->getVal( 'zip' )); |
24 | 24 | |
25 | 25 | // Setup |
26 | 26 | $wgOut->disable(); |
— | — | @@ -147,7 +147,10 @@ |
148 | 148 | <div id="contacts"> |
149 | 149 | |
150 | 150 | HTML; |
151 | | - if ( $this->zip ) { |
| 151 | + |
| 152 | + if ( $this->getZip() === false ) { |
| 153 | + $htmlOut .= $this->getZipFOrm( true ); |
| 154 | + } elseif ( !is_null( $this->getZip() )) { |
152 | 155 | $htmlOut .= $this->getCongressTables(); |
153 | 156 | } else { |
154 | 157 | $htmlOut .= $this->getZipForm(); |
— | — | @@ -267,7 +270,7 @@ |
268 | 271 | * Get HTML for a Zip Code form |
269 | 272 | * @return string HTML |
270 | 273 | */ |
271 | | - private function getZipForm() { |
| 274 | + private function getZipForm( $isError=false ) { |
272 | 275 | $htmlOut = <<<HTML |
273 | 276 | <h4>Contact your representatives</h4> |
274 | 277 | <div class="sopaActionDiv"> |
— | — | @@ -316,4 +319,42 @@ |
317 | 320 | HTML; |
318 | 321 | return $htmlOut; |
319 | 322 | } |
| 323 | + |
| 324 | + /** |
| 325 | + * |
| 326 | + * Enter description here ... |
| 327 | + * @param $zip |
| 328 | + */ |
| 329 | + public function setZip( $zip ) { |
| 330 | + if ( !$this->isValidZip( $zip )) { |
| 331 | + $this->zip = false; |
| 332 | + } else { |
| 333 | + $this->zip = $zip; |
| 334 | + } |
| 335 | + } |
| 336 | + |
| 337 | + public function getZip() { |
| 338 | + return $this->zip; |
| 339 | + } |
| 340 | + |
| 341 | + /** |
| 342 | + * Make sure the zip code entered was valid-ish |
| 343 | + * @param $zip |
| 344 | + * @return bool |
| 345 | + */ |
| 346 | + public function isValidZip( $zip ) { |
| 347 | + $zipPieces = explode( '-', $zip, 2 ); |
| 348 | + |
| 349 | + if ( strlen( $zipPieces[0] ) < 5 || !is_numeric( $zipPieces[0] )) { |
| 350 | + return false; |
| 351 | + } |
| 352 | + |
| 353 | + if ( isset( $zipPieces[1] )) { |
| 354 | + if ( strlen( $zipPieces[1] ) < 4 || !is_numeric( $zipPieces[1] )) { |
| 355 | + return false; |
| 356 | + } |
| 357 | + } |
| 358 | + |
| 359 | + return true; |
| 360 | + } |
320 | 361 | } |