3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * This class gets the name of the file to upload.
20 * TODO: CRM-11254 - There's still a lot of duplicate code in the 5 child classes that should be moved here
22 abstract class CRM_Import_Form_MapField
extends CRM_Core_Form
{
25 * Cache of preview data values
29 protected $_dataValues;
36 protected $_mapperFields;
43 protected $_loadedMappingId;
46 * Number of columns in import file
50 protected $_columnCount;
53 * Column headers, if we have them
57 protected $_columnHeaders;
60 * An array of booleans to keep track of whether a field has been used in
61 * form building already.
65 protected $_fieldUsed;
68 * Return a descriptive name for the page, used in wizard header.
72 public function getTitle() {
73 return ts('Match Fields');
77 * Attempt to match header labels with our mapper fields.
79 * @param string $header
80 * @param array $patterns
84 public function defaultFromHeader($header, &$patterns) {
85 foreach ($patterns as $key => $re) {
86 // Skip empty key/patterns
87 if (!$key ||
!$re ||
strlen("$re") < 5) {
91 // Scan through the headerPatterns defined in the schema for a match
92 if (preg_match($re, $header)) {
93 $this->_fieldUsed
[$key] = TRUE;
101 * Guess at the field names given the data and patterns from the schema.
103 * @param array $patterns
104 * @param string $index
108 public function defaultFromData($patterns, $index) {
111 $n = count($this->_dataValues
);
113 foreach ($patterns as $key => $re) {
114 // Skip empty key/patterns
115 if (!$key ||
!$re ||
strlen("$re") < 5) {
119 /* Take a vote over the preview data set */
121 for ($i = 0; $i < $n; $i++
) {
122 if (isset($this->_dataValues
[$i][$index])) {
123 if (preg_match($re, $this->_dataValues
[$i][$index])) {
128 if ($hits > $bestHits) {
135 $this->_fieldUsed
[$best] = TRUE;
141 * Add the saved mapping fields to the form.
143 * @param int|null $savedMappingID
145 * @throws \CiviCRM_API3_Exception
147 protected function buildSavedMappingFields($savedMappingID) {
148 //to save the current mappings
149 if (!$savedMappingID) {
150 $saveDetailsName = ts('Save this field mapping');
151 $this->applyFilter('saveMappingName', 'trim');
152 $this->add('text', 'saveMappingName', ts('Name'));
153 $this->add('text', 'saveMappingDesc', ts('Description'));
156 $savedMapping = $this->get('savedMapping');
158 $mappingName = (string) civicrm_api3('Mapping', 'getvalue', ['id' => $savedMappingID, 'return' => 'name']);
159 $this->set('loadedMapping', $savedMapping);
160 $this->assign('loadedMapping', $mappingName);
161 $this->assign('savedName', $mappingName);
162 $this->add('hidden', 'mappingId', $savedMappingID);
164 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
165 $saveDetailsName = ts('Save as a new field mapping');
166 $this->add('text', 'saveMappingName', ts('Name'));
167 $this->add('text', 'saveMappingDesc', ts('Description'));
170 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, ['onclick' => "showSaveDetails(this)"]);