Merge pull request #6431 from JMAConsulting/CRM-16526
[civicrm-core.git] / CRM / Import / Form / MapField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * This class gets the name of the file to upload
38 * TODO: CRM-11254 - There's still a lot of duplicate code in the 5 child classes that should be moved here
39 */
40 abstract class CRM_Import_Form_MapField extends CRM_Core_Form {
41
42 /**
43 * Cache of preview data values
44 *
45 * @var array
46 */
47 protected $_dataValues;
48
49 /**
50 * Mapper fields
51 *
52 * @var array
53 */
54 protected $_mapperFields;
55
56 /**
57 * Loaded mapping ID
58 *
59 * @var int
60 */
61 protected $_loadedMappingId;
62
63 /**
64 * Number of columns in import file
65 *
66 * @var int
67 */
68 protected $_columnCount;
69
70 /**
71 * Column headers, if we have them
72 *
73 * @var array
74 */
75 protected $_columnHeaders;
76
77 /**
78 * An array of booleans to keep track of whether a field has been used in
79 * form building already.
80 *
81 * @var array
82 */
83 protected $_fieldUsed;
84
85 /**
86 * Return a descriptive name for the page, used in wizard header
87 *
88 * @return string
89 */
90 public function getTitle() {
91 return ts('Match Fields');
92 }
93
94 /**
95 * Attempt to match header labels with our mapper fields.
96 *
97 * @param string $header
98 * @param array $patterns
99 *
100 * @return string
101 */
102 public function defaultFromHeader($header, &$patterns) {
103 foreach ($patterns as $key => $re) {
104 // Skip empty key/patterns
105 if (!$key || !$re || strlen("$re") < 5) {
106 continue;
107 }
108
109 // Scan through the headerPatterns defined in the schema for a match
110 if (preg_match($re, $header)) {
111 $this->_fieldUsed[$key] = TRUE;
112 return $key;
113 }
114 }
115 return '';
116 }
117
118 /**
119 * Guess at the field names given the data and patterns from the schema.
120 *
121 * @param array $patterns
122 * @param string $index
123 *
124 * @return string
125 */
126 public function defaultFromData(&$patterns, $index) {
127 $best = '';
128 $bestHits = 0;
129 $n = count($this->_dataValues);
130
131 foreach ($patterns as $key => $re) {
132 // Skip empty key/patterns
133 if (!$key || !$re || strlen("$re") < 5) {
134 continue;
135 }
136
137 /* Take a vote over the preview data set */
138 $hits = 0;
139 for ($i = 0; $i < $n; $i++) {
140 if (isset($this->_dataValues[$i][$index])) {
141 if (preg_match($re, $this->_dataValues[$i][$index])) {
142 $hits++;
143 }
144 }
145 }
146 if ($hits > $bestHits) {
147 $bestHits = $hits;
148 $best = $key;
149 }
150 }
151
152 if ($best != '') {
153 $this->_fieldUsed[$best] = TRUE;
154 }
155 return $best;
156 }
157
158 }