From b26295b858e1fc12110d34201ba16ab5e3f65139 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 9 Jun 2013 10:08:32 -0700 Subject: [PATCH] Create parent for Import/Form/MapField CRM-11254 ---------------------------------------- * CRM-11254: Create parent class for import http://issues.civicrm.org/jira/browse/CRM-11254 --- CRM/Activity/Import/Form/MapField.php | 127 +----------------- CRM/Contact/Import/Form/MapField.php | 102 +-------------- CRM/Contribute/Import/Form/MapField.php | 124 +----------------- CRM/Event/Import/Form/MapField.php | 128 +----------------- CRM/Import/Form/MapField.php | 167 ++++++++++++++++++++++++ CRM/Member/Import/Form/MapField.php | 127 +----------------- 6 files changed, 173 insertions(+), 602 deletions(-) create mode 100644 CRM/Import/Form/MapField.php diff --git a/CRM/Activity/Import/Form/MapField.php b/CRM/Activity/Import/Form/MapField.php index 806a5ecb6e..f99c902ee3 100644 --- a/CRM/Activity/Import/Form/MapField.php +++ b/CRM/Activity/Import/Form/MapField.php @@ -36,125 +36,10 @@ /** * This class gets the name of the file to upload */ -class CRM_Activity_Import_Form_MapField extends CRM_Core_Form { +class CRM_Activity_Import_Form_MapField extends CRM_Import_Form_MapField { - /** - * cache of preview data values - * - * @var array - * @access protected - */ - protected $_dataValues; - - /** - * mapper fields - * - * @var array - * @access protected - */ - protected $_mapperFields; - - /** - * loaded mapping ID - * - * @var int - * @access protected - */ - protected $_loadedMappingId; - - /** - * number of columns in import file - * - * @var int - * @access protected - */ - protected $_columnCount; - - /** - * column headers, if we have them - * - * @var array - * @access protected - */ - protected $_columnHeaders; - - /** - * an array of booleans to keep track of whether a field has been used in - * form building already. - * - * @var array - * @access protected - */ - protected $_fieldUsed; - - /** - * Attempt to match header labels with our mapper fields - * - * @param header - * @param mapperFields - * - * @return string - * @access public - */ - public function defaultFromHeader($header, &$patterns) { - foreach ($patterns as $key => $re) { - // Skip empty key/patterns - if (!$key || !$re || strlen("$re") < 5) { - continue; - } - - // Scan through the headerPatterns defined in the schema for a match - if (preg_match($re, $header)) { - $this->_fieldUsed[$key] = TRUE; - return $key; - } - } - return ''; - } - - /** - * Guess at the field names given the data and patterns from the schema - * - * @param patterns - * @param index - * - * @return string - * @access public - */ - public function defaultFromData(&$patterns, $index) { - $best = ''; - $bestHits = 0; - $n = count($this->_dataValues); - - foreach ($patterns as $key => $re) { - // Skip empty key/patterns - if (!$key || !$re || strlen("$re") < 5) { - continue; - } - - // if ($this->_fieldUsed[$key]) - // continue; - /* Take a vote over the preview data set */ - $hits = 0; - for ($i = 0; $i < $n; $i++) { - if (preg_match($re, $this->_dataValues[$i][$index])) { - $hits++; - } - } - - if ($hits > $bestHits) { - $bestHits = $hits; - $best = $key; - } - } - - if ($best != '') { - $this->_fieldUsed[$best] = TRUE; - } - return $best; - } /** * Function to set variables up before form is built @@ -594,15 +479,5 @@ class CRM_Activity_Import_Form_MapField extends CRM_Core_Form { // add all the necessary variables to the form $parser->set($this); } - - /** - * Return a descriptive name for the page, used in wizard header - * - * @return string - * @access public - */ - public function getTitle() { - return ts('Match Fields'); - } } diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index 320ee23525..f932300677 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -36,56 +36,8 @@ /** * This class gets the name of the file to upload */ -class CRM_Contact_Import_Form_MapField extends CRM_Core_Form { +class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { - /** - * cache of preview data values - * - * @var array - * @access protected - */ - protected $_dataValues; - - /** - * mapper fields - * - * @var array - * @access protected - */ - protected $_mapperFields; - - /** - * loaded mapping ID - * - * @var int - * @access protected - */ - protected $_loadedMappingId; - - /** - * number of columns in import data - * - * @var int - * @access protected - */ - protected $_columnCount; - - /** - * column names, if we have them - * - * @var array - * @access protected - */ - protected $_columnNames; - - /** - * an array of booleans to keep track of whether a field has been used in - * form building already. - * - * @var array - * @access protected - */ - protected $_fieldUsed; /** * an array of all contact fields with @@ -107,6 +59,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Core_Form { /** * Attempt to match header labels with our mapper fields + * FIXME: This is essentially the same function as parent::defaultFromHeader * * @param header * @param mapperFields @@ -137,47 +90,6 @@ class CRM_Contact_Import_Form_MapField extends CRM_Core_Form { return ''; } - /** - * Guess at the field names given the data and patterns from the schema - * - * @param patterns - * @param index - * - * @return string - * @access public - */ - public function defaultFromData(&$patterns, $index) { - $best = ''; - $bestHits = 0; - $n = count($this->_dataValues); - - foreach ($patterns as $key => $re) { - // Skip empty key/patterns - if (!$key || !$re || strlen("$re") < 5) { - continue; - } - - /* Take a vote over the preview data set */ - - $hits = 0; - for ($i = 0; $i < $n; $i++) { - if (preg_match($re, $this->_dataValues[$i][$index])) { - $hits++; - } - } - - if ($hits > $bestHits) { - $bestHits = $hits; - $best = $key; - } - } - - if ($best != '') { - $this->_fieldUsed[$best] = TRUE; - } - return $best; - } - /** * Function to set variables up before form is built * @@ -1055,16 +967,6 @@ class CRM_Contact_Import_Form_MapField extends CRM_Core_Form { $parser->set($this); } - /** - * Return a descriptive name for the page, used in wizard header - * - * @return string - * @access public - */ - public function getTitle() { - return ts('Match Fields'); - } - /** * format custom field name. * combine group and field name to avoid conflict. diff --git a/CRM/Contribute/Import/Form/MapField.php b/CRM/Contribute/Import/Form/MapField.php index 0463eda5d8..911fcc56da 100644 --- a/CRM/Contribute/Import/Form/MapField.php +++ b/CRM/Contribute/Import/Form/MapField.php @@ -36,122 +36,10 @@ /** * This class gets the name of the file to upload */ -class CRM_Contribute_Import_Form_MapField extends CRM_Core_Form { +class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { - /** - * cache of preview data values - * - * @var array - * @access protected - */ - protected $_dataValues; - - /** - * mapper fields - * - * @var array - * @access protected - */ - protected $_mapperFields; - - /** - * loaded mapping ID - * - * @var int - * @access protected - */ - protected $_loadedMappingId; - - /** - * number of columns in import file - * - * @var int - * @access protected - */ - protected $_columnCount; - /** - * column headers, if we have them - * - * @var array - * @access protected - */ - protected $_columnHeaders; - /** - * an array of booleans to keep track of whether a field has been used in - * form building already. - * - * @var array - * @access protected - */ - protected $_fieldUsed; - - /** - * Attempt to match header labels with our mapper fields - * - * @param header - * @param mapperFields - * - * @return string - * @access public - */ - public function defaultFromHeader($header, &$patterns) { - foreach ($patterns as $key => $re) { - // Skip empty key/patterns - if (!$key || !$re || strlen("$re") < 5) { - continue; - } - - // Scan through the headerPatterns defined in the schema for a match - if (preg_match($re, $header)) { - $this->_fieldUsed[$key] = TRUE; - return $key; - } - } - return ''; - } - - /** - * Guess at the field names given the data and patterns from the schema - * - * @param patterns - * @param index - * - * @return string - * @access public - */ - public function defaultFromData(&$patterns, $index) { - $best = ''; - $bestHits = 0; - $n = count($this->_dataValues); - - foreach ($patterns as $key => $re) { - // Skip empty key/patterns - if (!$key || !$re || strlen("$re") < 5) { - continue; - } - - /* Take a vote over the preview data set */ - - $hits = 0; - for ($i = 0; $i < $n; $i++) { - if (preg_match($re, $this->_dataValues[$i][$index])) { - $hits++; - } - } - - if ($hits > $bestHits) { - $bestHits = $hits; - $best = $key; - } - } - - if ($best != '') { - $this->_fieldUsed[$best] = TRUE; - } - return $best; - } /** * Function to set variables up before form is built @@ -665,15 +553,5 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Core_Form { // add all the necessary variables to the form $parser->set($this); } - - /** - * Return a descriptive name for the page, used in wizard header - * - * @return string - * @access public - */ - public function getTitle() { - return ts('Match Fields'); - } } diff --git a/CRM/Event/Import/Form/MapField.php b/CRM/Event/Import/Form/MapField.php index c0bb4cdaa8..ff462c54c4 100644 --- a/CRM/Event/Import/Form/MapField.php +++ b/CRM/Event/Import/Form/MapField.php @@ -36,126 +36,10 @@ /** * This class gets the name of the file to upload */ -class CRM_Event_Import_Form_MapField extends CRM_Core_Form { +class CRM_Event_Import_Form_MapField extends CRM_Import_Form_MapField { - /** - * cache of preview data values - * - * @var array - * @access protected - */ - protected $_dataValues; - - /** - * mapper fields - * - * @var array - * @access protected - */ - protected $_mapperFields; - - /** - * loaded mapping ID - * - * @var int - * @access protected - */ - protected $_loadedMappingId; - - /** - * number of columns in import file - * - * @var int - * @access protected - */ - protected $_columnCount; - - /** - * column headers, if we have them - * - * @var array - * @access protected - */ - protected $_columnHeaders; - - /** - * an array of booleans to keep track of whether a field has been used in - * form building already. - * - * @var array - * @access protected - */ - protected $_fieldUsed; - - /** - * Attempt to match header labels with our mapper fields - * - * @param header - * @param mapperFields - * - * @return string - * @access public - */ - public function defaultFromHeader($header, &$patterns) { - foreach ($patterns as $key => $re) { - // Skip empty key/patterns - if (!$key || !$re || strlen("$re") < 5) { - continue; - } - - // Scan through the headerPatterns defined in the schema for a match - if (preg_match($re, $header)) { - $this->_fieldUsed[$key] = TRUE; - return $key; - } - } - return ''; - } - - /** - * Guess at the field names given the data and patterns from the schema - * - * @param patterns - * @param index - * - * @return string - * @access public - */ - public function defaultFromData(&$patterns, $index) { - $best = ''; - $bestHits = 0; - $n = count($this->_dataValues); - - foreach ($patterns as $key => $re) { - // Skip empty key/patterns - if (!$key || !$re || strlen("$re") < 5) { - continue; - } - - // if ($this->_fieldUsed[$key]) - // continue; - /* Take a vote over the preview data set */ - $hits = 0; - for ($i = 0; $i < $n; $i++) { - if (isset($this->_dataValues[$i][$index])) { - if (preg_match($re, $this->_dataValues[$i][$index])) { - $hits++; - } - } - } - if ($hits > $bestHits) { - $bestHits = $hits; - $best = $key; - } - } - - if ($best != '') { - $this->_fieldUsed[$best] = TRUE; - } - return $best; - } /** * Function to set variables up before form is built @@ -599,15 +483,5 @@ class CRM_Event_Import_Form_MapField extends CRM_Core_Form { // add all the necessary variables to the form $parser->set($this); } - - /** - * Return a descriptive name for the page, used in wizard header - * - * @return string - * @access public - */ - public function getTitle() { - return ts('Match Fields'); - } } diff --git a/CRM/Import/Form/MapField.php b/CRM/Import/Form/MapField.php new file mode 100644 index 0000000000..d4a9625458 --- /dev/null +++ b/CRM/Import/Form/MapField.php @@ -0,0 +1,167 @@ + $re) { + // Skip empty key/patterns + if (!$key || !$re || strlen("$re") < 5) { + continue; + } + + // Scan through the headerPatterns defined in the schema for a match + if (preg_match($re, $header)) { + $this->_fieldUsed[$key] = TRUE; + return $key; + } + } + return ''; + } + + /** + * Guess at the field names given the data and patterns from the schema + * + * @param patterns + * @param index + * + * @return string + * @access public + */ + public function defaultFromData(&$patterns, $index) { + $best = ''; + $bestHits = 0; + $n = count($this->_dataValues); + + foreach ($patterns as $key => $re) { + // Skip empty key/patterns + if (!$key || !$re || strlen("$re") < 5) { + continue; + } + + /* Take a vote over the preview data set */ + $hits = 0; + for ($i = 0; $i < $n; $i++) { + if (isset($this->_dataValues[$i][$index])) { + if (preg_match($re, $this->_dataValues[$i][$index])) { + $hits++; + } + } + } + if ($hits > $bestHits) { + $bestHits = $hits; + $best = $key; + } + } + + if ($best != '') { + $this->_fieldUsed[$best] = TRUE; + } + return $best; + } + +} diff --git a/CRM/Member/Import/Form/MapField.php b/CRM/Member/Import/Form/MapField.php index 1159c47907..dcb20f6753 100644 --- a/CRM/Member/Import/Form/MapField.php +++ b/CRM/Member/Import/Form/MapField.php @@ -36,56 +36,9 @@ /** * This class gets the name of the file to upload */ -class CRM_Member_Import_Form_MapField extends CRM_Core_Form { +class CRM_Member_Import_Form_MapField extends CRM_Import_Form_MapField { - /** - * cache of preview data values - * - * @var array - * @access protected - */ - protected $_dataValues; - /** - * mapper fields - * - * @var array - * @access protected - */ - protected $_mapperFields; - - /** - * loaded mapping ID - * - * @var int - * @access protected - */ - protected $_loadedMappingId; - - /** - * number of columns in import file - * - * @var int - * @access protected - */ - protected $_columnCount; - - /** - * column headers, if we have them - * - * @var array - * @access protected - */ - protected $_columnHeaders; - - /** - * an array of booleans to keep track of whether a field has been used in - * form building already. - * - * @var array - * @access protected - */ - protected $_fieldUsed; /** * to store contactType @@ -95,74 +48,6 @@ class CRM_Member_Import_Form_MapField extends CRM_Core_Form { */ static $_contactType = NULL; - /** - * Attempt to match header labels with our mapper fields - * - * @param header - * @param mapperFields - * - * @return string - * @access public - */ - public function defaultFromHeader($header, &$patterns) { - foreach ($patterns as $key => $re) { - // Skip empty key/patterns - if (!$key || !$re || strlen("$re") < 5) { - continue; - } - - // Scan through the headerPatterns defined in the schema for a match - if (preg_match($re, $header)) { - $this->_fieldUsed[$key] = TRUE; - return $key; - } - } - return ''; - } - - /** - * Guess at the field names given the data and patterns from the schema - * - * @param patterns - * @param index - * - * @return string - * @access public - */ - public function defaultFromData(&$patterns, $index) { - $best = ''; - $bestHits = 0; - $n = count($this->_dataValues); - - foreach ($patterns as $key => $re) { - // Skip empty key/patterns - if (!$key || !$re || strlen("$re") < 5) { - continue; - } - - // if ($this->_fieldUsed[$key]) - // continue; - - /* Take a vote over the preview data set */ - - $hits = 0; - for ($i = 0; $i < $n; $i++) { - if (preg_match($re, $this->_dataValues[$i][$index])) { - $hits++; - } - } - - if ($hits > $bestHits) { - $bestHits = $hits; - $best = $key; - } - } - - if ($best != '') { - $this->_fieldUsed[$best] = TRUE; - } - return $best; - } /** * Function to set variables up before form is built @@ -631,15 +516,5 @@ class CRM_Member_Import_Form_MapField extends CRM_Core_Form { // add all the necessary variables to the form $parser->set($this); } - - /** - * Return a descriptive name for the page, used in wizard header - * - * @return string - * @access public - */ - public function getTitle() { - return ts('Match Fields'); - } } -- 2.25.1