INFRA-132 - Civi - PHPStorm cleanup
[civicrm-core.git] / CRM / Import / Form / MapField.php
CommitLineData
b26295b8
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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
b26295b8
CW
46 */
47 protected $_dataValues;
48
49 /**
100fef9d 50 * Mapper fields
b26295b8
CW
51 *
52 * @var array
b26295b8
CW
53 */
54 protected $_mapperFields;
55
56 /**
100fef9d 57 * Loaded mapping ID
b26295b8
CW
58 *
59 * @var int
b26295b8
CW
60 */
61 protected $_loadedMappingId;
62
63 /**
100fef9d 64 * Number of columns in import file
b26295b8
CW
65 *
66 * @var int
b26295b8
CW
67 */
68 protected $_columnCount;
69
70 /**
100fef9d 71 * Column headers, if we have them
b26295b8
CW
72 *
73 * @var array
b26295b8
CW
74 */
75 protected $_columnHeaders;
76
77 /**
100fef9d 78 * An array of booleans to keep track of whether a field has been used in
b26295b8
CW
79 * form building already.
80 *
81 * @var array
b26295b8
CW
82 */
83 protected $_fieldUsed;
84
85 /**
86 * Return a descriptive name for the page, used in wizard header
87 *
88 * @return string
b26295b8
CW
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 header
98 * @param mapperFields
99 *
100 * @return string
b26295b8
CW
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 patterns
122 * @param index
123 *
124 * @return string
b26295b8
CW
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}