2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
2012-06-26 15:03:11 +02:00
|
|
|
* Allows input of credit card numbers via four separate form fields,
|
|
|
|
* including generic validation of its numeric values.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-06-26 15:03:11 +02:00
|
|
|
* @todo Validate
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class CreditCardField extends TextField {
|
2013-10-03 16:26:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add default attributes for use on all inputs.
|
|
|
|
*
|
|
|
|
* @return array List of attributes
|
|
|
|
*/
|
|
|
|
public function getAttributes() {
|
|
|
|
return array_merge(
|
|
|
|
parent::getAttributes(),
|
|
|
|
array(
|
|
|
|
'autocomplete' => 'off',
|
|
|
|
'maxlength' => 4,
|
|
|
|
'size' => 4
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2012-03-12 04:27:41 +01:00
|
|
|
$parts = $this->value;
|
2012-03-12 21:47:30 +01:00
|
|
|
if(!is_array($parts)) $parts = explode("\n", chunk_split($parts,4,"\n"));
|
2007-07-19 12:40:28 +02:00
|
|
|
$parts = array_pad($parts, 4, "");
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2013-09-30 17:48:15 +02:00
|
|
|
$properties['ValueOne'] = $parts[0];
|
|
|
|
$properties['ValueTwo'] = $parts[1];
|
|
|
|
$properties['ValueThree'] = $parts[2];
|
|
|
|
$properties['ValueFour'] = $parts[3];
|
|
|
|
|
|
|
|
return parent::Field($properties);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-03-07 17:45:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get tabindex HTML string
|
|
|
|
*
|
|
|
|
* @param int $increment Increase current tabindex by this value
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-10-03 16:26:31 +02:00
|
|
|
public function getTabIndexHTML($increment = 0) {
|
2013-09-20 01:13:10 +02:00
|
|
|
// we can't add a tabindex if there hasn't been one set yet.
|
|
|
|
if($this->getAttribute('tabindex') === null) return false;
|
|
|
|
|
2012-05-08 14:26:22 +02:00
|
|
|
$tabIndex = (int)$this->getAttribute('tabindex') + (int)$increment;
|
2012-03-07 17:45:14 +01:00
|
|
|
return (is_numeric($tabIndex)) ? ' tabindex = "' . $tabIndex . '"' : '';
|
|
|
|
}
|
2013-10-03 16:26:31 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function dataValue() {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(is_array($this->value)) return implode("", $this->value);
|
|
|
|
else return $this->value;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-11-12 03:19:12 +01:00
|
|
|
public function validate(Validator $validator){
|
2015-01-30 23:15:52 +01:00
|
|
|
if(!$this->value || !trim(implode("", $this->value))) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$i=0;
|
2015-01-30 23:15:52 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->value) foreach($this->value as $part){
|
2012-02-27 22:14:02 +01:00
|
|
|
if(!$part || !(strlen($part) == 4) || !preg_match("/([0-9]{4})/", $part)){
|
2007-07-19 12:40:28 +02:00
|
|
|
switch($i){
|
2012-12-08 12:20:20 +01:00
|
|
|
case 0: $number = _t('CreditCardField.FIRST', 'first'); break;
|
2008-01-10 04:28:13 +01:00
|
|
|
case 1: $number = _t('CreditCardField.SECOND', 'second'); break;
|
|
|
|
case 2: $number = _t('CreditCardField.THIRD', 'third'); break;
|
|
|
|
case 3: $number = _t('CreditCardField.FOURTH', 'fourth'); break;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-10-25 04:47:45 +02:00
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
2012-05-01 21:44:54 +02:00
|
|
|
_t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Form.VALIDATIONCREDITNUMBER',
|
2012-06-01 03:13:06 +02:00
|
|
|
"Please ensure you have entered the {number} credit card number correctly",
|
2012-05-01 21:44:54 +02:00
|
|
|
array('number' => $number)
|
2007-10-25 04:47:45 +02:00
|
|
|
),
|
|
|
|
"validation",
|
|
|
|
false
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|