Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | require_once 'api/api.php'; | |
37 | ||
38 | /** | |
39 | * class to parse activity csv files | |
40 | */ | |
41 | class CRM_Activity_Import_Parser_Activity extends CRM_Activity_Import_Parser { | |
42 | ||
43 | protected $_mapperKeys; | |
44 | ||
45 | private $_contactIdIndex; | |
46 | private $_activityTypeIndex; | |
47 | private $_activityLabelIndex; | |
48 | private $_activityDateIndex; | |
49 | ||
50 | /** | |
ceb10dc7 | 51 | * Array of successfully imported activity id's |
6a488035 TO |
52 | * |
53 | * @array | |
54 | */ | |
55 | protected $_newActivity; | |
56 | ||
57 | /** | |
58 | * class constructor | |
59 | */ | |
60 | function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) { | |
61 | parent::__construct(); | |
62 | $this->_mapperKeys = &$mapperKeys; | |
63 | } | |
64 | ||
65 | /** | |
66 | * the initializer code, called before the processing | |
67 | * | |
68 | * @return void | |
69 | * @access public | |
70 | */ | |
71 | function init() { | |
cc6f3942 | 72 | $activityContact = CRM_Activity_BAO_ActivityContact::import(); |
73 | $activityTarget['target_contact_id'] = $activityContact['contact_id']; | |
6a488035 | 74 | $fields = array_merge(CRM_Activity_BAO_Activity::importableFields(), |
cc6f3942 | 75 | $activityTarget |
6a488035 TO |
76 | ); |
77 | ||
cc6f3942 | 78 | $fields = array_merge($fields, array( |
79 | 'activity_label' => array( | |
80 | 'title' => ts('Activity Type Label'), | |
81 | 'headerPattern' => '/(activity.)?type label?/i', | |
82 | ) | |
83 | )); | |
6a488035 TO |
84 | |
85 | foreach ($fields as $name => $field) { | |
86 | $field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT); | |
87 | $field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//'); | |
88 | $field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//'); | |
89 | $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']); | |
90 | } | |
91 | ||
92 | $this->_newActivity = array(); | |
93 | ||
94 | $this->setActiveFields($this->_mapperKeys); | |
95 | ||
96 | // FIXME: we should do this in one place together with Form/MapField.php | |
97 | $this->_contactIdIndex = -1; | |
98 | $this->_activityTypeIndex = -1; | |
99 | $this->_activityLabelIndex = -1; | |
100 | $this->_activityDateIndex = -1; | |
101 | ||
102 | $index = 0; | |
103 | foreach ($this->_mapperKeys as $key) { | |
104 | switch ($key) { | |
105 | case 'target_contact_id': | |
106 | case 'external_identifier': | |
107 | $this->_contactIdIndex = $index; | |
108 | break; | |
109 | ||
110 | case 'activity_label': | |
111 | $this->_activityLabelIndex = $index; | |
112 | break; | |
113 | ||
114 | case 'activity_type_id': | |
115 | $this->_activityTypeIndex = $index; | |
116 | break; | |
117 | ||
118 | case 'activity_date_time': | |
119 | $this->_activityDateIndex = $index; | |
120 | break; | |
121 | } | |
122 | $index++; | |
123 | } | |
124 | } | |
125 | ||
126 | /** | |
127 | * handle the values in mapField mode | |
128 | * | |
129 | * @param array $values the array of values belonging to this line | |
130 | * | |
131 | * @return boolean | |
132 | * @access public | |
133 | */ | |
134 | function mapField(&$values) { | |
a05662ef | 135 | return CRM_Import_Parser::VALID; |
6a488035 TO |
136 | } |
137 | ||
138 | /** | |
139 | * handle the values in preview mode | |
140 | * | |
141 | * @param array $values the array of values belonging to this line | |
142 | * | |
143 | * @return boolean the result of this processing | |
144 | * @access public | |
145 | */ | |
146 | function preview(&$values) { | |
147 | return $this->summary($values); | |
148 | } | |
149 | ||
150 | /** | |
151 | * handle the values in summary mode | |
152 | * | |
153 | * @param array $values the array of values belonging to this line | |
154 | * | |
155 | * @return boolean the result of this processing | |
156 | * @access public | |
157 | */ | |
158 | function summary(&$values) { | |
159 | $erroneousField = NULL; | |
160 | $response = $this->setActiveFieldValues($values, $erroneousField); | |
161 | $index = -1; | |
162 | $errorRequired = FALSE; | |
163 | ||
164 | if ($this->_activityTypeIndex > -1 && $this->_activityLabelIndex > -1) { | |
165 | array_unshift($values, ts('Please select either Activity Type ID OR Activity Type Label.')); | |
a05662ef | 166 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
167 | } |
168 | elseif ($this->_activityLabelIndex > -1) { | |
169 | $index = $this->_activityLabelIndex; | |
170 | } | |
171 | elseif ($this->_activityTypeIndex > -1) { | |
172 | $index = $this->_activityTypeIndex; | |
173 | } | |
174 | ||
175 | if ($index < 0 or $this->_activityDateIndex < 0) { | |
176 | $errorRequired = TRUE; | |
177 | } | |
178 | else { | |
179 | $errorRequired = !CRM_Utils_Array::value($index, $values) || !CRM_Utils_Array::value($this->_activityDateIndex, $values); | |
180 | } | |
181 | ||
182 | if ($errorRequired) { | |
183 | array_unshift($values, ts('Missing required fields')); | |
a05662ef | 184 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
185 | } |
186 | ||
187 | $params = &$this->getActiveFieldParams(); | |
188 | ||
189 | $errorMessage = NULL; | |
190 | ||
191 | //for date-Formats | |
192 | $session = CRM_Core_Session::singleton(); | |
193 | $dateType = $session->get('dateTypes'); | |
194 | if (!isset($params['source_contact_id'])) { | |
195 | $params['source_contact_id'] = $session->get('userID'); | |
196 | } | |
197 | foreach ($params as $key => $val) { | |
198 | if ($key == 'activity_date_time') { | |
199 | if ($val) { | |
200 | $dateValue = CRM_Utils_Date::formatDate($val, $dateType); | |
201 | if ($dateValue) { | |
202 | $params[$key] = $dateValue; | |
203 | } | |
204 | else { | |
719a6fec | 205 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Activity date', $errorMessage); |
6a488035 TO |
206 | } |
207 | } | |
208 | } | |
209 | elseif ($key == 'activity_engagement_level' && $val && | |
210 | !CRM_Utils_Rule::positiveInteger($val) | |
211 | ) { | |
719a6fec | 212 | CRM_Contact_Import_Parser_Contact::addToErrorMsg('Activity Engagement Index', $errorMessage); |
6a488035 TO |
213 | } |
214 | } | |
215 | //date-Format part ends | |
216 | ||
217 | //checking error in custom data | |
218 | $params['contact_type'] = isset($this->_contactType) ? $this->_contactType : 'Activity'; | |
219 | ||
719a6fec | 220 | CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); |
6a488035 TO |
221 | |
222 | if ($errorMessage) { | |
223 | $tempMsg = "Invalid value for field(s) : $errorMessage"; | |
224 | array_unshift($values, $tempMsg); | |
225 | $errorMessage = NULL; | |
a05662ef | 226 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
227 | } |
228 | ||
a05662ef | 229 | return CRM_Import_Parser::VALID; |
6a488035 TO |
230 | } |
231 | ||
232 | /** | |
233 | * handle the values in import mode | |
234 | * | |
235 | * @param int $onDuplicate the code for what action to take on duplicates | |
236 | * @param array $values the array of values belonging to this line | |
237 | * | |
238 | * @return boolean the result of this processing | |
239 | * @access public | |
240 | */ | |
241 | function import($onDuplicate, &$values) { | |
242 | // first make sure this is a valid line | |
243 | $response = $this->summary($values); | |
244 | ||
a05662ef | 245 | if ($response != CRM_Import_Parser::VALID) { |
6a488035 TO |
246 | return $response; |
247 | } | |
248 | $params = &$this->getActiveFieldParams(); | |
249 | $activityLabel = array_search('activity_label', $this->_mapperKeys); | |
250 | if ($activityLabel) { | |
251 | $params = array_merge($params, array('activity_label' => $values[$activityLabel])); | |
252 | } | |
253 | //for date-Formats | |
254 | $session = CRM_Core_Session::singleton(); | |
255 | $dateType = $session->get('dateTypes'); | |
256 | if (!isset($params['source_contact_id'])) { | |
257 | $params['source_contact_id'] = $session->get('userID'); | |
258 | } | |
259 | $formatted = array(); | |
260 | $customFields = CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $params)); | |
261 | ||
262 | foreach ($params as $key => $val) { | |
263 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
2272a067 BS |
264 | if ($key == 'activity_date_time' && $val) { |
265 | $params[$key] = CRM_Utils_Date::formatDate($val, $dateType); | |
266 | } | |
267 | elseif ($customFields[$customFieldID]['data_type'] == 'Date') { | |
719a6fec | 268 | CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $params, $dateType, $key); |
6a488035 TO |
269 | } |
270 | elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') { | |
271 | $params[$key] = CRM_Utils_String::strtoboolstr($val); | |
272 | } | |
273 | } | |
274 | elseif ($key == 'activity_subject') { | |
275 | $params['subject'] = $val; | |
276 | } | |
277 | } | |
278 | //date-Format part ends | |
279 | require_once 'CRM/Utils/DeprecatedUtils.php'; | |
280 | $formatError = _civicrm_api3_deprecated_activity_formatted_param($params, $params, TRUE); | |
281 | ||
282 | if ($formatError) { | |
283 | array_unshift($values, $formatError['error_message']); | |
a05662ef | 284 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
285 | } |
286 | ||
287 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
288 | CRM_Core_DAO::$_nullObject, | |
289 | NULL, | |
290 | 'Activity' | |
291 | ); | |
292 | ||
293 | if ($this->_contactIdIndex < 0) { | |
294 | ||
295 | //retrieve contact id using contact dedupe rule. | |
296 | //since we are support only individual's activity import. | |
297 | $params['contact_type'] = 'Individual'; | |
298 | $params['version'] = 3; | |
299 | $error = _civicrm_api3_deprecated_duplicate_formatted_contact($params); | |
300 | ||
301 | if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { | |
302 | $matchedIDs = explode(',', $error['error_message']['params'][0]); | |
303 | if (count($matchedIDs) > 1) { | |
304 | array_unshift($values, 'Multiple matching contact records detected for this row. The activity was not imported'); | |
a05662ef | 305 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
306 | } |
307 | else { | |
308 | $cid = $matchedIDs[0]; | |
309 | $params['target_contact_id'] = $cid; | |
310 | $params['version'] = 3; | |
311 | $newActivity = civicrm_api('activity', 'create', $params); | |
a7488080 | 312 | if (!empty($newActivity['is_error'])) { |
6a488035 | 313 | array_unshift($values, $newActivity['error_message']); |
a05662ef | 314 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
315 | } |
316 | ||
317 | $this->_newActivity[] = $newActivity['id']; | |
a05662ef | 318 | return CRM_Import_Parser::VALID; |
6a488035 TO |
319 | } |
320 | } | |
321 | else { | |
322 | // Using new Dedupe rule. | |
323 | $ruleParams = array( | |
324 | 'contact_type' => 'Individual', | |
325 | 'used' => 'Unsupervised', | |
326 | ); | |
327 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); | |
328 | ||
329 | $disp = NULL; | |
330 | foreach ($fieldsArray as $value) { | |
331 | if (array_key_exists(trim($value), $params)) { | |
332 | $paramValue = $params[trim($value)]; | |
333 | if (is_array($paramValue)) { | |
334 | $disp .= $params[trim($value)][0][trim($value)] . " "; | |
335 | } | |
336 | else { | |
337 | $disp .= $params[trim($value)] . " "; | |
338 | } | |
339 | } | |
340 | } | |
341 | ||
a7488080 | 342 | if (!empty($params['external_identifier'])) { |
6a488035 TO |
343 | if ($disp) { |
344 | $disp .= "AND {$params['external_identifier']}"; | |
345 | } | |
346 | else { | |
347 | $disp = $params['external_identifier']; | |
348 | } | |
349 | } | |
350 | ||
351 | array_unshift($values, 'No matching Contact found for (' . $disp . ')'); | |
a05662ef | 352 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
353 | } |
354 | } | |
355 | else { | |
a7488080 | 356 | if (!empty($params['external_identifier'])) { |
6a488035 TO |
357 | $targetContactId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
358 | $params['external_identifier'], 'id', 'external_identifier' | |
359 | ); | |
360 | ||
a7488080 | 361 | if (!empty($params['target_contact_id']) && |
6a488035 TO |
362 | $params['target_contact_id'] != $targetContactId |
363 | ) { | |
364 | array_unshift($values, 'Mismatch of External identifier :' . $params['external_identifier'] . ' and Contact Id:' . $params['target_contact_id']); | |
a05662ef | 365 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
366 | } |
367 | elseif ($targetContactId) { | |
368 | $params['target_contact_id'] = $targetContactId; | |
369 | } | |
370 | else { | |
371 | array_unshift($values, 'No Matching Contact for External identifier :' . $params['external_identifier']); | |
a05662ef | 372 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
373 | } |
374 | } | |
375 | ||
376 | $params['version'] = 3; | |
377 | $newActivity = civicrm_api('activity', 'create', $params); | |
a7488080 | 378 | if (!empty($newActivity['is_error'])) { |
6a488035 | 379 | array_unshift($values, $newActivity['error_message']); |
a05662ef | 380 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
381 | } |
382 | ||
383 | $this->_newActivity[] = $newActivity['id']; | |
a05662ef | 384 | return CRM_Import_Parser::VALID; |
6a488035 TO |
385 | } |
386 | } | |
387 | ||
388 | /** | |
389 | * the initializer code, called before the processing | |
390 | * | |
391 | * @return void | |
392 | * @access public | |
393 | */ | |
394 | function fini() {} | |
395 | ||
396 | } | |
397 |