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