Merge pull request #14919 from eileenmcnaughton/mem_review
[civicrm-core.git] / CRM / Import / Form / MapField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2019
31 */
32
33 /**
34 * This class gets the name of the file to upload.
35 *
36 * TODO: CRM-11254 - There's still a lot of duplicate code in the 5 child classes that should be moved here
37 */
38 abstract class CRM_Import_Form_MapField extends CRM_Core_Form {
39
40 /**
41 * Cache of preview data values
42 *
43 * @var array
44 */
45 protected $_dataValues;
46
47 /**
48 * Mapper fields
49 *
50 * @var array
51 */
52 protected $_mapperFields;
53
54 /**
55 * Loaded mapping ID
56 *
57 * @var int
58 */
59 protected $_loadedMappingId;
60
61 /**
62 * Number of columns in import file
63 *
64 * @var int
65 */
66 protected $_columnCount;
67
68 /**
69 * Column headers, if we have them
70 *
71 * @var array
72 */
73 protected $_columnHeaders;
74
75 /**
76 * An array of booleans to keep track of whether a field has been used in
77 * form building already.
78 *
79 * @var array
80 */
81 protected $_fieldUsed;
82
83 /**
84 * Return a descriptive name for the page, used in wizard header.
85 *
86 * @return string
87 */
88 public function getTitle() {
89 return ts('Match Fields');
90 }
91
92 /**
93 * Attempt to match header labels with our mapper fields.
94 *
95 * @param string $header
96 * @param array $patterns
97 *
98 * @return string
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 /**
117 * Guess at the field names given the data and patterns from the schema.
118 *
119 * @param array $patterns
120 * @param string $index
121 *
122 * @return string
123 */
124 public function defaultFromData($patterns, $index) {
125 $best = '';
126 $bestHits = 0;
127 $n = count($this->_dataValues);
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 /**
157 * Add the saved mapping fields to the form.
158 *
159 * @param int|null $savedMappingID
160 *
161 * @throws \CiviCRM_API3_Exception
162 */
163 protected function buildSavedMappingFields($savedMappingID) {
164 //to save the current mappings
165 if (!$savedMappingID) {
166 $saveDetailsName = ts('Save this field mapping');
167 $this->applyFilter('saveMappingName', 'trim');
168 $this->add('text', 'saveMappingName', ts('Name'));
169 $this->add('text', 'saveMappingDesc', ts('Description'));
170 }
171 else {
172 $savedMapping = $this->get('savedMapping');
173
174 $mappingName = (string) civicrm_api3('Mapping', 'getvalue', ['id' => $savedMappingID, 'return' => 'name']);
175 $this->set('loadedMapping', $savedMapping);
176 $this->assign('loadedMapping', $mappingName);
177 $this->assign('savedName', $mappingName);
178 $this->add('hidden', 'mappingId', $savedMappingID);
179
180 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
181 $saveDetailsName = ts('Save as a new field mapping');
182 $this->add('text', 'saveMappingName', ts('Name'));
183 $this->add('text', 'saveMappingDesc', ts('Description'));
184 }
185
186 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, ['onclick' => "showSaveDetails(this)"]);
187 }
188
189 }