r100953 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100952‎ | r100953 | r100954 >
Date:07:11, 27 October 2011
Author:jpostlethwaite
Status:ok (Comments)
Tags:fundraising 
Comment:
Updated TwoStepAmount form to use ResourceLoader. Using jquery.validation.js. Prepping form for dropdowns and Direct Debit.
Modified paths:
  • /trunk/extensions/DonationInterface/globalcollect_gateway/forms/TwoStepAmount.php (modified) (history)

Diff [purge]

Index: trunk/extensions/DonationInterface/globalcollect_gateway/forms/TwoStepAmount.php
@@ -23,103 +23,48 @@
2424 */
2525 class Gateway_Form_TwoStepAmount extends Gateway_Form {
2626
27 - public function __construct( &$gateway, &$form_errors ) {
28 - parent::__construct( $gateway, $form_errors );
 27+ /**
 28+ * Initialize the form
 29+ *
 30+ */
 31+ protected function init() {
2932
3033 //TODO: This is pretty odd to do here. However, as this form is only
3134 //being used for testing purposes, it's getting the update that goes
3235 //along with yet another change in Form Class construction.
3336 $this->form_data['payment_method'] = empty($this->form_data['payment_method']) ? 'bt' : $this->form_data['payment_method'];
3437 $this->form_data['payment_submethod'] = empty($this->form_data['payment_submethod']) ? 'bt' : $this->form_data['payment_submethod'];
35 -
36 - //$this->form_data['payment_method'] = 'rtbt';
37 - //$this->form_data['payment_submethod'] = 'rtbt_nordea_sweeden';
38 -
39 - //$this->form_data['payment_method'] = 'rtbt';
40 - //$this->form_data['payment_submethod'] = 'rtbt_ideal';
41 - //$this->form_data['issuerids'] = array();
4238
 39+ $this->setPaymentMethod( $this->form_data['payment_method'] );
 40+ $this->setPaymentSubmethod( $this->form_data['payment_submethod'] );
 41+
4342 $this->form_data['process'] = 'other';
4443
45 - // we only want to load this JS if the form is being rendered
46 - $this->loadValidateJs(); // validation JS
47 -
48 - $this->loadPlaceholders();
 44+ $this->loadResources();
4945 }
5046
51 - public function loadPlaceholders() {
52 - global $wgOut;
53 - // form placeholder values
54 - $first = wfMsg( 'donate_interface-donor-fname' );
55 - $last = wfMsg( 'donate_interface-donor-lname' );
56 - $js = <<<EOT
57 -<script type="text/javascript">
58 -function loadPlaceholders() {
59 - var fname = document.getElementById('fname');
60 - var lname = document.getElementById('lname');
61 - var amountOther = document.getElementById('amountOther');
62 - if (fname.value == '') {
63 - fname.style.color = '#999999';
64 - fname.value = '$first';
 47+ /**
 48+ * Load form resources
 49+ */
 50+ protected function loadResources() {
 51+
 52+ $this->loadValidateJs();
6553 }
66 - if (lname.value == '') {
67 - lname.style.color = '#999999';
68 - lname.value = '$last';
69 - }
70 -}
71 -addEvent( window, 'load', loadPlaceholders );
7254
73 -function formCheck( ccform ) {
74 - var msg = [ 'EmailAdd', 'Fname', 'Lname', 'Street', 'City', 'Zip' ];
75 -
76 - var fields = ["emailAdd","fname","lname","street","city","zip" ],
77 - numFields = fields.length,
78 - i,
79 - output = '',
80 - currField = '';
81 -
82 - for( i = 0; i < numFields; i++ ) {
83 - if( document.getElementById( fields[i] ).value == '' ) {
84 - currField = window['payflowproGatewayErrorMsg'+ msg[i]];
85 - output += payflowproGatewayErrorMsgJs + ' ' + currField + '.\\r\\n';
86 - }
87 - }
88 -
89 - if (document.getElementById('fname').value == '$first') {
90 - output += payflowproGatewayErrorMsgJs + ' first name.\\r\\n';
91 - }
92 - if (document.getElementById('lname').value == '$last') {
93 - output += payflowproGatewayErrorMsgJs + ' last name.\\r\\n';
94 - }
95 - var countryField = document.getElementById( 'country' );
96 - if( countryField.options[countryField.selectedIndex].value == '' ) {
97 - output += payflowproGatewayErrorMsgJs + ' ' + window['payflowproGatewayErrorMsgCountry'] + '.\\r\\n';
98 - }
99 -
100 - // validate email address
101 - var apos = document.payment.emailAdd.value.indexOf("@");
102 - var dotpos = document.payment.emailAdd.value.lastIndexOf(".");
103 -
104 - if( apos < 1 || dotpos-apos < 2 ) {
105 - output += payflowproGatewayErrorMsgEmail;
106 - }
107 -
108 - if( output ) {
109 - alert( output );
110 - return false;
111 - } else {
112 - return true;
113 - }
114 -}
115 -</script>
116 -EOT;
 55+ protected function loadValidateJs() {
 56+ global $wgOut;
 57+ $wgOut->addModules( 'gc.form.core.validate' );
 58+ $wgOut->addHeadItem( 'donationinterface_validate', "\n" . '<script type="text/javascript" src="/extensions/DonationInterface/globalcollect_gateway/modules/js/validate.js"></script>' );
 59+ $wgOut->addHeadItem( 'donationinterface_js_validate', "\n" . '<script type="text/javascript" src="/extensions/DonationInterface/globalcollect_gateway/modules/js/jquery.validate.js"></script>' );
 60+ $wgOut->addHeadItem( 'donationinterface_js_validate_additional', "\n" . '<script type="text/javascript" src="/extensions/DonationInterface/globalcollect_gateway/modules/js/jquery.validate.additional-methods.js"></script>' );
 61+
 62+ $js = "\n" . '<script type="text/javascript">' . "validateForm( { validate: { address: true, amount: true, creditCard: false, email: true, name: true, }, payment_method: '" . $this->getPaymentMethod() . "', payment_submethod: '" . $this->getPaymentSubmethod() . "', formId: '" . $this->getFormId() . "' } );" . '</script>' . "\n";
11763 $wgOut->addHeadItem( 'placeholders', $js );
11864 }
11965
12066 /**
121 - * Required method for constructing the entire form
 67+ * Required method for returning the full HTML for a form.
12268 *
123 - * This can of course be overloaded by a child class.
12469 * @return string The entire form HTML
12570 */
12671 public function getForm() {
@@ -129,14 +74,20 @@
13075 return $form;
13176 }
13277
 78+ /**
 79+ * Generate the first part of the form
 80+ */
13381 public function generateFormStart() {
134 - $form = $this->generateBannerHeader();
 82+
 83+ $form = '';
 84+
 85+ //$form .= $this->generateBannerHeader();
13586
13687 $form .= Xml::openElement( 'div', array( 'id' => 'mw-creditcard' ) );
13788
13889 // provide a place at the top of the form for displaying general messages
13990 if ( $this->form_errors['general'] ) {
140 - $form .= Xml::openElement( 'div', array( 'id' => 'mw-payflow-general-error' ) );
 91+ $form .= Xml::openElement( 'div', array( 'id' => 'mw-payment-general-error' ) );
14192 if ( is_array( $this->form_errors['general'] ) ) {
14293 foreach ( $this->form_errors['general'] as $this->form_errors_msg ) {
14394 $form .= Xml::tags( 'p', array( 'class' => 'creditcard-error-msg' ), $this->form_errors_msg );
@@ -155,30 +106,26 @@
156107
157108 // Xml::element seems to convert html to htmlentities
158109 $form .= "<p class='creditcard-error-msg'>" . $this->form_errors['retryMsg'] . "</p>";
159 - $form .= Xml::openElement( 'form', array( 'name' => 'payment', 'method' => 'post', 'action' => $this->getNoCacheAction(), 'onsubmit' => 'return formCheck(this)', 'autocomplete' => 'off' ) );
 110+ $form .= Xml::openElement( 'form', array( 'id' => $this->getFormId(), 'name' => $this->getFormName(), 'method' => 'post', 'action' => $this->getNoCacheAction(), 'onsubmit' => '', 'autocomplete' => 'off' ) );
160111
161 - $form .= Xml::openElement( 'div', array( 'id' => 'left-column', 'class' => 'payflow-cc-form-section' ) );
 112+ $form .= Xml::openElement( 'div', array( 'id' => 'left-column', 'class' => 'payment-cc-form-section' ) );
162113 $form .= $this->generatePersonalContainer();
163114 $form .= $this->generatePaymentContainer();
164115 $form .= $this->generateFormSubmit();
165116 $form .= Xml::closeElement( 'div' ); // close div#left-column
166117
167 - //$form .= Xml::openElement( 'div', array( 'id' => 'right-column', 'class' => 'payflow-cc-form-section' ) );
 118+ //$form .= Xml::openElement( 'div', array( 'id' => 'right-column', 'class' => 'payment-cc-form-section' ) );
168119
169120 return $form;
170121 }
171122
172123 public function generateFormSubmit() {
173124 // submit button
174 - $form = Xml::openElement( 'div', array( 'id' => 'payflowpro_gateway-form-submit' ) );
 125+ $form = Xml::openElement( 'div', array( 'id' => 'payment_gateway-form-submit' ) );
175126 $form .= Xml::openElement( 'div', array( 'id' => 'mw-donate-submit-button' ) );
176 - // $form .= Xml::submitButton( wfMsg( 'donate_interface-submit-button' ));
177 - $form .= Xml::element( 'input', array( 'class' => 'button-plain', 'value' => wfMsg( 'donate_interface-cc-button' ), 'type' => 'submit' ) );
 127+ $form .= Xml::element( 'input', array( 'class' => 'button-plain', 'value' => wfMsg( 'donate_interface-submit-button' ), 'type' => 'submit' ) );
178128 $form .= Xml::closeElement( 'div' ); // close div#mw-donate-submit-button
179 - $form .= Xml::openElement( 'div', array( 'class' => 'mw-donate-submessage', 'id' => 'payflowpro_gateway-donate-submessage' ) ) .
180 - wfMsg( 'donate_interface-donate-click' );
181 - $form .= Xml::closeElement( 'div' ); // close div#payflowpro_gateway-donate-submessage
182 - $form .= Xml::closeElement( 'div' ); // close div#payflowpro_gateway-form-submit
 129+ $form .= Xml::closeElement( 'div' ); // close div#payment_gateway-form-submit
183130 return $form;
184131 }
185132
@@ -190,9 +137,6 @@
191138 $form .= Html::hidden( $field, $value );
192139 }
193140
194 - $value = 'BANK_TRANSFER';
195 - //$form .= Html::hidden( $field, $value );
196 - //$form .= Xml::closeElement( 'div' ); // close div#right-column
197141 $form .= Xml::closeElement( 'form' );
198142 $form .= Xml::closeElement( 'div' ); // close div#mw-creditcard-form
199143 $form .= $this->generateDonationFooter();
@@ -202,14 +146,14 @@
203147
204148 protected function generatePersonalContainer() {
205149 $form = '';
206 - $form .= Xml::openElement( 'div', array( 'id' => 'payflowpro_gateway-personal-info' ) ); ;
207 - //$form .= Xml::tags( 'h3', array( 'class' => 'payflow-cc-form-header', 'id' => 'payflow-cc-form-header-personal' ), wfMsg( 'donate_interface-cc-form-header-personal' ) );
208 - $form .= Xml::openElement( 'table', array( 'id' => 'payflow-table-donor' ) );
 150+ $form .= Xml::openElement( 'div', array( 'id' => 'payment_gateway-personal-info' ) ); ;
 151+ //$form .= Xml::tags( 'h3', array( 'class' => 'payment-cc-form-header', 'id' => 'payment-cc-form-header-personal' ), wfMsg( 'donate_interface-cc-form-header-personal' ) );
 152+ $form .= Xml::openElement( 'table', array( 'id' => 'payment-table-donor' ) );
209153
210154 $form .= $this->generatePersonalFields();
211155
212 - $form .= Xml::closeElement( 'table' ); // close table#payflow-table-donor
213 - $form .= Xml::closeElement( 'div' ); // close div#payflowpro_gateway-personal-info
 156+ $form .= Xml::closeElement( 'table' ); // close table#payment-table-donor
 157+ $form .= Xml::closeElement( 'div' ); // close div#payment_gateway-personal-info
214158
215159 return $form;
216160 }
@@ -243,14 +187,14 @@
244188 protected function generatePaymentContainer() {
245189 $form = '';
246190 // credit card info
247 - $form .= Xml::openElement( 'div', array( 'id' => 'payflowpro_gateway-payment-info' ) );
248 - //$form .= Xml::tags( 'h3', array( 'class' => 'payflow-cc-form-header', 'id' => 'payflow-cc-form-header-payment' ), wfMsg( 'donate_interface-cc-form-header-payment' ) );
249 - $form .= Xml::openElement( 'table', array( 'id' => 'payflow-table-cc' ) );
 191+ $form .= Xml::openElement( 'div', array( 'id' => 'donation-payment-info' ) );
 192+ //$form .= Xml::tags( 'h3', array( 'class' => 'payment-cc-form-header', 'id' => 'payment-cc-form-header-payment' ), wfMsg( 'donate_interface-cc-form-header-payment' ) );
 193+ $form .= Xml::openElement( 'table', array( 'id' => 'donation-table-cc' ) );
250194
251195 $form .= $this->generatePaymentFields();
252196
253 - $form .= Xml::closeElement( 'table' ); // close table#payflow-table-cc
254 - $form .= Xml::closeElement( 'div' ); // close div#payflowpro_gateway-payment-info
 197+ $form .= Xml::closeElement( 'table' ); // close table#payment-table-cc
 198+ $form .= Xml::closeElement( 'div' ); // close div#payment_gateway-payment-info
255199
256200 return $form;
257201 }
@@ -258,12 +202,12 @@
259203 protected function generatePaymentFields() {
260204 // amount
261205 $form = '<tr>';
262 - $form .= '<td colspan="2"><span class="creditcard-error-msg">' . $this->form_errors['invalidamount'] . '</span></td>';
 206+ $form .= '<td colspan="2"><span class="donation-error-msg">' . $this->form_errors['invalidamount'] . '</span></td>';
263207 $form .= '</tr>';
264208 $form .= '<tr>';
265209 $form .= '<td class="label">' . Xml::label( wfMsg( 'donate_interface-donor-amount' ), 'amount' ) . '</td>';
266 - $form .= '<td>' . Xml::input( 'amount', '7', $this->form_data['amount'], array( 'type' => 'text', 'maxlength' => '10', 'id' => 'amount' ) ) .
267 - ' ' . $this->generateCurrencyDropdown() . '</td>';
 210+ $form .= '<td>' . Xml::input( 'amount', '7', $this->form_data['amount'], array( 'class' => 'required', 'type' => 'text', 'maxlength' => '10', 'id' => 'amount' ) ) .
 211+ ' ' . $this->generateCurrencyDropdown( array( 'showCardsOnCurrencyChange' => false, ) ) . '</td>';
268212 $form .= '</tr>';
269213
270214 return $form;

Comments

#Comment by Awjrichards (talk | contribs)   22:26, 4 November 2011
+		$this->setPaymentMethod( $this->form_data['payment_method'] );
+		$this->setPaymentSubmethod( $this->form_data['payment_submethod'] );

$this->form_data['whatever'] should now be accessed from the adapter. This is not a blocker, but should be updated.

Also, consider taking a look at the HTML class rather than the XML class for generating HTML output. It's more likely to properly sanitize output and is generally easier to write/read. http://svn.wikimedia.org/doc/classHtml.html

Status & tagging log