More buttonrama fallout
[civicrm-core.git] / CRM / Import / Form / MapField.php
CommitLineData
b26295b8
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
b26295b8 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
b26295b8 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
b26295b8
CW
11
12/**
b26295b8 13 * @package CRM
ca5cec67 14 * @copyright CiviCRM LLC https://civicrm.org/licensing
b26295b8
CW
15 */
16
17/**
2b4bc760 18 * This class gets the name of the file to upload.
19 *
b26295b8
CW
20 * TODO: CRM-11254 - There's still a lot of duplicate code in the 5 child classes that should be moved here
21 */
22abstract class CRM_Import_Form_MapField extends CRM_Core_Form {
23
24 /**
100fef9d 25 * Cache of preview data values
b26295b8
CW
26 *
27 * @var array
b26295b8
CW
28 */
29 protected $_dataValues;
30
31 /**
100fef9d 32 * Mapper fields
b26295b8
CW
33 *
34 * @var array
b26295b8
CW
35 */
36 protected $_mapperFields;
37
38 /**
100fef9d 39 * Loaded mapping ID
b26295b8
CW
40 *
41 * @var int
b26295b8
CW
42 */
43 protected $_loadedMappingId;
44
45 /**
100fef9d 46 * Number of columns in import file
b26295b8
CW
47 *
48 * @var int
b26295b8
CW
49 */
50 protected $_columnCount;
51
52 /**
100fef9d 53 * Column headers, if we have them
b26295b8
CW
54 *
55 * @var array
b26295b8
CW
56 */
57 protected $_columnHeaders;
58
59 /**
100fef9d 60 * An array of booleans to keep track of whether a field has been used in
b26295b8
CW
61 * form building already.
62 *
63 * @var array
b26295b8
CW
64 */
65 protected $_fieldUsed;
66
67 /**
2b4bc760 68 * Return a descriptive name for the page, used in wizard header.
b26295b8
CW
69 *
70 * @return string
b26295b8
CW
71 */
72 public function getTitle() {
73 return ts('Match Fields');
74 }
75
76 /**
fe482240 77 * Attempt to match header labels with our mapper fields.
b26295b8 78 *
79d7553f 79 * @param string $header
80 * @param array $patterns
b26295b8
CW
81 *
82 * @return string
b26295b8
CW
83 */
84 public function defaultFromHeader($header, &$patterns) {
85 foreach ($patterns as $key => $re) {
86 // Skip empty key/patterns
87 if (!$key || !$re || strlen("$re") < 5) {
88 continue;
89 }
90
91 // Scan through the headerPatterns defined in the schema for a match
92 if (preg_match($re, $header)) {
93 $this->_fieldUsed[$key] = TRUE;
94 return $key;
95 }
96 }
97 return '';
98 }
99
100 /**
fe482240 101 * Guess at the field names given the data and patterns from the schema.
b26295b8 102 *
79d7553f 103 * @param array $patterns
104 * @param string $index
b26295b8
CW
105 *
106 * @return string
b26295b8 107 */
56dd62a0 108 public function defaultFromData($patterns, $index) {
353ffa53 109 $best = '';
b26295b8 110 $bestHits = 0;
353ffa53 111 $n = count($this->_dataValues);
b26295b8
CW
112
113 foreach ($patterns as $key => $re) {
114 // Skip empty key/patterns
115 if (!$key || !$re || strlen("$re") < 5) {
116 continue;
117 }
118
119 /* Take a vote over the preview data set */
120 $hits = 0;
121 for ($i = 0; $i < $n; $i++) {
122 if (isset($this->_dataValues[$i][$index])) {
123 if (preg_match($re, $this->_dataValues[$i][$index])) {
124 $hits++;
125 }
126 }
127 }
128 if ($hits > $bestHits) {
129 $bestHits = $hits;
130 $best = $key;
131 }
132 }
133
134 if ($best != '') {
135 $this->_fieldUsed[$best] = TRUE;
136 }
137 return $best;
138 }
139
ad05d047 140 /**
141 * Add the saved mapping fields to the form.
142 *
143 * @param int|null $savedMappingID
144 *
145 * @throws \CiviCRM_API3_Exception
146 */
147 protected function buildSavedMappingFields($savedMappingID) {
148 //to save the current mappings
149 if (!$savedMappingID) {
150 $saveDetailsName = ts('Save this field mapping');
151 $this->applyFilter('saveMappingName', 'trim');
152 $this->add('text', 'saveMappingName', ts('Name'));
153 $this->add('text', 'saveMappingDesc', ts('Description'));
154 }
155 else {
156 $savedMapping = $this->get('savedMapping');
157
158 $mappingName = (string) civicrm_api3('Mapping', 'getvalue', ['id' => $savedMappingID, 'return' => 'name']);
159 $this->set('loadedMapping', $savedMapping);
160 $this->assign('loadedMapping', $mappingName);
161 $this->assign('savedName', $mappingName);
162 $this->add('hidden', 'mappingId', $savedMappingID);
163
164 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
165 $saveDetailsName = ts('Save as a new field mapping');
166 $this->add('text', 'saveMappingName', ts('Name'));
167 $this->add('text', 'saveMappingDesc', ts('Description'));
168 }
169
170 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, ['onclick' => "showSaveDetails(this)"]);
171 }
172
b26295b8 173}