r109256 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109255‎ | r109256 | r109257 >
Date:00:17, 18 January 2012
Author:awjrichards
Status:ok
Tags:sopa 
Comment:
Added logic for rudimentary zip code validation and provided a mechanism to make it possible to display an error on the zip code submission form in the event that the zip is invalid
Modified paths:
  • /trunk/extensions/CongressLookup/SpecialCongressLookup.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CongressLookup/SpecialCongressLookup.php
@@ -5,7 +5,7 @@
66 * to the user.
77 */
88 class SpecialCongressLookup extends UnlistedSpecialPage {
9 - var $zip;
 9+ protected $zip = null;
1010
1111 public function __construct() {
1212 // Register special page
@@ -19,7 +19,7 @@
2020 global $wgRequest, $wgOut;
2121
2222 // Pull in query string parameters
23 - $this->zip = $wgRequest->getVal( 'zip' );
 23+ $this->setZip( $wgRequest->getVal( 'zip' ));
2424
2525 // Setup
2626 $wgOut->disable();
@@ -147,7 +147,10 @@
148148 <div id="contacts">
149149
150150 HTML;
151 - if ( $this->zip ) {
 151+
 152+ if ( $this->getZip() === false ) {
 153+ $htmlOut .= $this->getZipFOrm( true );
 154+ } elseif ( !is_null( $this->getZip() )) {
152155 $htmlOut .= $this->getCongressTables();
153156 } else {
154157 $htmlOut .= $this->getZipForm();
@@ -267,7 +270,7 @@
268271 * Get HTML for a Zip Code form
269272 * @return string HTML
270273 */
271 - private function getZipForm() {
 274+ private function getZipForm( $isError=false ) {
272275 $htmlOut = <<<HTML
273276 <h4>Contact your representatives</h4>
274277 <div class="sopaActionDiv">
@@ -316,4 +319,42 @@
317320 HTML;
318321 return $htmlOut;
319322 }
 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+ }
320361 }

Status & tagging log