http://www.mediawiki.org/wiki/Special:Code/MediaWiki/74124
Revision: 74124
Author: awjrichards
Date: 2010-10-01 23:17:17 +0000 (Fri, 01 Oct 2010)
Log Message:
-----------
Increased flexibility of form generation by reducing three abstract methods in
abstract form to one - getForm(); Added methods to get/set html for a captcha;
updated recaptcha plugin code to use new form methods; updated payflow gateway
code to use new form methods; improved comments in existing form code
Modified Paths:
--------------
trunk/extensions/DonationInterface/payflowpro_gateway/extras/recaptcha/recaptcha.body.php
trunk/extensions/DonationInterface/payflowpro_gateway/forms/Form.php
trunk/extensions/DonationInterface/payflowpro_gateway/forms/TwoColumn.php
trunk/extensions/DonationInterface/payflowpro_gateway/payflowpro_gateway.body.php
Modified:
trunk/extensions/DonationInterface/payflowpro_gateway/extras/recaptcha/recaptcha.body.php
===================================================================
---
trunk/extensions/DonationInterface/payflowpro_gateway/extras/recaptcha/recaptcha.body.php
2010-10-01 23:14:32 UTC (rev 74123)
+++
trunk/extensions/DonationInterface/payflowpro_gateway/extras/recaptcha/recaptcha.body.php
2010-10-01 23:17:17 UTC (rev 74124)
@@ -50,24 +50,29 @@
* Display the submission form with the captcha injected into it
*/
public function display_captcha( &$pfp_gateway_object, &$data ) {
+ global $wgOut, $wgPayflowRecaptchaPublicKey, $wgProto;
+
+ // log that a captcha's been triggered
$this->log( $data[ 'contribution_tracking_id' ], 'Captcha
triggered' );
+
+ // check if we need to be using HTTPs to communicate with
reCaptcha
+ $use_ssl = ( $wgProto == 'https' ) ? TRUE : FALSE;
- global $wgOut;
+ // construct the HTML used to display the captcha
+ $captcha_html = Xml::openElement( 'div', array( 'id' =>
'mw-donate-captcha' ));
+ $captcha_html .= recaptcha_get_html(
$wgPayflowRecaptchaPublicKey, $this->recap_err, $use_ssl );
+ $captcha_html .= '<span class="creditcard-error-msg">' . wfMsg(
'payflowpro_gateway-error-msg-captcha-please') . '</span>';
+ $captcha_html .= Xml::closeElement( 'div' ); // close
div#mw-donate-captcha
+
+ // load up the form class
$form_class = $pfp_gateway_object->getFormClass();
$form_obj = new $form_class( $data, $pfp_gateway_object->errors
);
- $form = $form_obj->generateFormStart( $data,
$pfp_gateway_object->errors );
- $form .= Xml::openElement( 'div', array( 'id' =>
'mw-donate-captcha' ));
- // get the captcha
- global $wgPayflowRecaptchaPublicKey, $wgProto;
- $use_ssl = ( $wgProto == 'https' ) ? TRUE : FALSE;
- $form .= recaptcha_get_html( $wgPayflowRecaptchaPublicKey,
$this->recap_err, $use_ssl );
- $form .= '<span class="creditcard-error-msg">' . wfMsg(
'payflowpro_gateway-error-msg-captcha-please') . '</span>';
-
- $form .= Xml::closeElement( 'div' ); // close
div#mw-donate-captcha
- $form .= $form_obj->generateFormSubmit( $data,
$pfp_gateway_object->errors );
- $form .= $form_obj->generateFormEnd( $data,
$pfp_gateway_object->errors );
- $wgOut->addHTML( $form );
+ // set the captcha HTML to use in the form
+ $form_obj->setCaptchaHTML( $captcha_html );
+
+ // output the form
+ $wgOut->addHTML( $form_obj->getForm());
}
/**
Modified: trunk/extensions/DonationInterface/payflowpro_gateway/forms/Form.php
===================================================================
--- trunk/extensions/DonationInterface/payflowpro_gateway/forms/Form.php
2010-10-01 23:14:32 UTC (rev 74123)
+++ trunk/extensions/DonationInterface/payflowpro_gateway/forms/Form.php
2010-10-01 23:17:17 UTC (rev 74124)
@@ -32,10 +32,23 @@
*/
protected $style_path;
- abstract public function generateFormStart();
- abstract public function generateFormSubmit();
- abstract public function generateFormEnd();
+ /**
+ * A string to hold the HTML to display a cpatcha
+ * @var string
+ */
+ protected $captcha_html;
+ /**
+ * Required method for returning the full HTML for a form.
+ *
+ * Code invoking forms will expect this method to be set. Requiring
only
+ * this method allows for flexible form generation inside of child
classes
+ * while also providing a unified method for returning the full HTML
for
+ * a form.
+ * @return string The entire form HTML
+ */
+ abstract function getForm();
+
public function __construct( &$data, &$error ) {
global $wgPayflowGatewayTest, $wgOut;
@@ -44,8 +57,9 @@
$this->form_errors =& $error;
/**
- * add form-specific css - the path can be set using
$this->setStylePath,
- * which should be called before loading this constructor
+ * add form-specific css - the path can be set in child
classes
+ * using $this->setStylePath, which should be called before
+ * calling parent::__construct()
*/
if ( !strlen( $this->getStylePath())) {
$this->setStylePath();
@@ -109,6 +123,9 @@
/**
* Generate the menu select of countries
+ * @fixme It would be great if we could default the country to the
user's locale
+ * @fixme We should also do a locale-based asort on the country dropdown
+ * (see http://us.php.net/asort)
* @return string
*/
public function generateCountryDropdown() {
@@ -120,7 +137,6 @@
}
// alphabetically sort the country names
- // @fixme we should probably set locale and do a locale string
sort
asort( $countries, SORT_STRING );
// generate a dropdown option for each country
@@ -237,6 +253,12 @@
return $expiry_year_menu;
}
+ /**
+ * Generates the dropdown for states
+ * @fixme Alpha sort (ideally locale alpha sort) states in dropdown
+ * AFTER state names are translated
+ * @return string The entire HTML select element for the state dropdown
list
+ */
public function generateStateDropdown() {
require_once( dirname( __FILE__ ) .
'/../includes/stateAbbreviations.inc' );
@@ -262,6 +284,14 @@
return $state_menu;
}
+ /**
+ * Generates the dropdown list for available currencies
+ *
+ * @fixme The list of available currencies should NOT be defined here
but rather
+ * be customizable
+ * @fixme It would be great to default the currency to a locale's
currency
+ * @return string The entire HTML select for the currency dropdown
+ */
public function generateCurrencyDropdown() {
$available_currencies = array(
'USD' => 'USD: U.S. Dollar',
@@ -336,4 +366,30 @@
}
return $this->hidden_fields;
}
+
+ /**
+ * Get the HTML set to display a captcha
+ *
+ * If $this->captcha_html has no string length, an empty string is
returned.
+ * @return string The HTML to display the captcha or an empty string
+ */
+ public function getCaptchaHTML() {
+ if ( !strlen( $this->captcha_html )) {
+ return '';
+ }
+ return $this->captcha_html;
+ }
+
+ /**
+ * Set a string of HTML used to display a captcha
+ *
+ * This allows for a flexible way of inserting some kind of captcha
+ * into a form, and for a form to flexibly insert captcha HTML
+ * wherever it needs to go.
+ *
+ * @param string The HTML to display the captcha
+ */
+ public function setCaptchaHTML( $html ) {
+ $this->captcha_html = $html;
+ }
}
\ No newline at end of file
Modified:
trunk/extensions/DonationInterface/payflowpro_gateway/forms/TwoColumn.php
===================================================================
--- trunk/extensions/DonationInterface/payflowpro_gateway/forms/TwoColumn.php
2010-10-01 23:14:32 UTC (rev 74123)
+++ trunk/extensions/DonationInterface/payflowpro_gateway/forms/TwoColumn.php
2010-10-01 23:17:17 UTC (rev 74124)
@@ -13,6 +13,20 @@
'/extensions/DonationInterface/payflowpro_gateway/validate_input.js"></script>'
);
}
+ /**
+ * Required method for constructing the entire form
+ *
+ * This can of course be overloaded by a child class.
+ * @return string The entire form HTML
+ */
+ public function getForm() {
+ $form = $this->generateFormStart();
+ $form .= $this->getCaptchaHTML();
+ $form .= $this->generateFormSubmit();
+ $form .= $this->generateFormEnd();
+ return $form;
+ }
+
public function generateFormStart() {
global $wgPayflowGatewayHeader, $wgPayflwGatewayTest, $wgOut;
$form = $this->generateBannerHeader();
Modified:
trunk/extensions/DonationInterface/payflowpro_gateway/payflowpro_gateway.body.php
===================================================================
---
trunk/extensions/DonationInterface/payflowpro_gateway/payflowpro_gateway.body.php
2010-10-01 23:14:32 UTC (rev 74123)
+++
trunk/extensions/DonationInterface/payflowpro_gateway/payflowpro_gateway.body.php
2010-10-01 23:17:17 UTC (rev 74124)
@@ -168,12 +168,12 @@
}
/**
- * Build and displays form to user
+ * Build and display form to user
*
* @param $data Array: array of posted user input
* @param $error Array: array of error messages returned by
validate_form function
*
- * The message at the top of the form can be edited in the
payflow_gateway.i18.php file
+ * The message at the top of the form can be edited in the
payflow_gateway.i18n.php file
*/
public function fnPayflowDisplayForm( &$data, &$error ) {
global $wgOut;
@@ -187,10 +187,8 @@
}
$form_class = $this->getFormClass();
- $form_obj = new $form_class( $data, $error );
- $form = $form_obj->generateFormStart();
- $form .= $form_obj->generateFormSubmit();
- $form .= $form_obj->generateFormEnd();
+ $form_obj = new $form_class( $data, $error );
+ $form = $form_obj->getForm();
$wgOut->addHTML( $form );
}
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs