Merge pull request #14977 from eileenmcnaughton/map_field
[civicrm-core.git] / CRM / Import / Form / MapField.php
CommitLineData
b26295b8
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
b26295b8 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
b26295b8
CW
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
b26295b8
CW
27
28/**
b26295b8 29 * @package CRM
6b83d5bd 30 * @copyright CiviCRM LLC (c) 2004-2019
b26295b8
CW
31 */
32
33/**
2b4bc760 34 * This class gets the name of the file to upload.
35 *
b26295b8
CW
36 * TODO: CRM-11254 - There's still a lot of duplicate code in the 5 child classes that should be moved here
37 */
38abstract class CRM_Import_Form_MapField extends CRM_Core_Form {
39
40 /**
100fef9d 41 * Cache of preview data values
b26295b8
CW
42 *
43 * @var array
b26295b8
CW
44 */
45 protected $_dataValues;
46
47 /**
100fef9d 48 * Mapper fields
b26295b8
CW
49 *
50 * @var array
b26295b8
CW
51 */
52 protected $_mapperFields;
53
54 /**
100fef9d 55 * Loaded mapping ID
b26295b8
CW
56 *
57 * @var int
b26295b8
CW
58 */
59 protected $_loadedMappingId;
60
61 /**
100fef9d 62 * Number of columns in import file
b26295b8
CW
63 *
64 * @var int
b26295b8
CW
65 */
66 protected $_columnCount;
67
68 /**
100fef9d 69 * Column headers, if we have them
b26295b8
CW
70 *
71 * @var array
b26295b8
CW
72 */
73 protected $_columnHeaders;
74
75 /**
100fef9d 76 * An array of booleans to keep track of whether a field has been used in
b26295b8
CW
77 * form building already.
78 *
79 * @var array
b26295b8
CW
80 */
81 protected $_fieldUsed;
82
83 /**
2b4bc760 84 * Return a descriptive name for the page, used in wizard header.
b26295b8
CW
85 *
86 * @return string
b26295b8
CW
87 */
88 public function getTitle() {
89 return ts('Match Fields');
90 }
91
92 /**
fe482240 93 * Attempt to match header labels with our mapper fields.
b26295b8 94 *
79d7553f 95 * @param string $header
96 * @param array $patterns
b26295b8
CW
97 *
98 * @return string
b26295b8
CW
99 */
100 public function defaultFromHeader($header, &$patterns) {
101 foreach ($patterns as $key => $re) {
102 // Skip empty key/patterns
103 if (!$key || !$re || strlen("$re") < 5) {
104 continue;
105 }
106
107 // Scan through the headerPatterns defined in the schema for a match
108 if (preg_match($re, $header)) {
109 $this->_fieldUsed[$key] = TRUE;
110 return $key;
111 }
112 }
113 return '';
114 }
115
116 /**
fe482240 117 * Guess at the field names given the data and patterns from the schema.
b26295b8 118 *
79d7553f 119 * @param array $patterns
120 * @param string $index
b26295b8
CW
121 *
122 * @return string
b26295b8
CW
123 */
124 public function defaultFromData(&$patterns, $index) {
353ffa53 125 $best = '';
b26295b8 126 $bestHits = 0;
353ffa53 127 $n = count($this->_dataValues);
b26295b8
CW
128
129 foreach ($patterns as $key => $re) {
130 // Skip empty key/patterns
131 if (!$key || !$re || strlen("$re") < 5) {
132 continue;
133 }
134
135 /* Take a vote over the preview data set */
136 $hits = 0;
137 for ($i = 0; $i < $n; $i++) {
138 if (isset($this->_dataValues[$i][$index])) {
139 if (preg_match($re, $this->_dataValues[$i][$index])) {
140 $hits++;
141 }
142 }
143 }
144 if ($hits > $bestHits) {
145 $bestHits = $hits;
146 $best = $key;
147 }
148 }
149
150 if ($best != '') {
151 $this->_fieldUsed[$best] = TRUE;
152 }
153 return $best;
154 }
155
156}