Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
6a488035 TO |
18 | |
19 | /** | |
b6c94f42 | 20 | * Class to parse activity csv files. |
6a488035 TO |
21 | */ |
22 | class CRM_Activity_Import_Parser_Activity extends CRM_Activity_Import_Parser { | |
23 | ||
24 | protected $_mapperKeys; | |
25 | ||
26 | private $_contactIdIndex; | |
6a488035 TO |
27 | |
28 | /** | |
ceb10dc7 | 29 | * Array of successfully imported activity id's |
6a488035 | 30 | * |
62d3ee27 | 31 | * @var array |
6a488035 TO |
32 | */ |
33 | protected $_newActivity; | |
34 | ||
35 | /** | |
fe482240 | 36 | * Class constructor. |
b6c94f42 | 37 | * |
38 | * @param array $mapperKeys | |
6a488035 | 39 | */ |
15e60eaf | 40 | public function __construct($mapperKeys) { |
6a488035 | 41 | parent::__construct(); |
15e60eaf | 42 | $this->_mapperKeys = $mapperKeys; |
6a488035 TO |
43 | } |
44 | ||
1ae8fc42 | 45 | /** |
46 | * Function of undocumented functionality required by the interface. | |
47 | */ | |
48 | protected function fini() {} | |
49 | ||
6a488035 | 50 | /** |
b6c94f42 | 51 | * The initializer code, called before the processing. |
6a488035 | 52 | */ |
00be9182 | 53 | public function init() { |
cc6f3942 | 54 | $activityContact = CRM_Activity_BAO_ActivityContact::import(); |
55 | $activityTarget['target_contact_id'] = $activityContact['contact_id']; | |
6a488035 | 56 | $fields = array_merge(CRM_Activity_BAO_Activity::importableFields(), |
cc6f3942 | 57 | $activityTarget |
6a488035 TO |
58 | ); |
59 | ||
be2fb01f CW |
60 | $fields = array_merge($fields, [ |
61 | 'source_contact_id' => [ | |
4039effc JP |
62 | 'title' => ts('Source Contact'), |
63 | 'headerPattern' => '/Source.Contact?/i', | |
be2fb01f CW |
64 | ], |
65 | 'activity_label' => [ | |
cc6f3942 | 66 | 'title' => ts('Activity Type Label'), |
67 | 'headerPattern' => '/(activity.)?type label?/i', | |
be2fb01f CW |
68 | ], |
69 | ]); | |
6a488035 TO |
70 | |
71 | foreach ($fields as $name => $field) { | |
72 | $field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT); | |
73 | $field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//'); | |
74 | $field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//'); | |
75 | $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']); | |
76 | } | |
77 | ||
be2fb01f | 78 | $this->_newActivity = []; |
6a488035 TO |
79 | |
80 | $this->setActiveFields($this->_mapperKeys); | |
81 | ||
82 | // FIXME: we should do this in one place together with Form/MapField.php | |
83 | $this->_contactIdIndex = -1; | |
6a488035 TO |
84 | |
85 | $index = 0; | |
86 | foreach ($this->_mapperKeys as $key) { | |
87 | switch ($key) { | |
88 | case 'target_contact_id': | |
89 | case 'external_identifier': | |
90 | $this->_contactIdIndex = $index; | |
91 | break; | |
6a488035 TO |
92 | } |
93 | $index++; | |
94 | } | |
95 | } | |
96 | ||
97 | /** | |
fe482240 | 98 | * Handle the values in mapField mode. |
6a488035 | 99 | * |
041ab3d1 TO |
100 | * @param array $values |
101 | * The array of values belonging to this line. | |
6a488035 | 102 | * |
408b79bf | 103 | * @return bool |
6a488035 | 104 | */ |
00be9182 | 105 | public function mapField(&$values) { |
a05662ef | 106 | return CRM_Import_Parser::VALID; |
6a488035 TO |
107 | } |
108 | ||
109 | /** | |
fe482240 | 110 | * Handle the values in preview mode. |
6a488035 | 111 | * |
041ab3d1 TO |
112 | * @param array $values |
113 | * The array of values belonging to this line. | |
6a488035 | 114 | * |
408b79bf | 115 | * @return bool |
a6c01b45 | 116 | * the result of this processing |
6a488035 | 117 | */ |
00be9182 | 118 | public function preview(&$values) { |
6a488035 TO |
119 | return $this->summary($values); |
120 | } | |
121 | ||
122 | /** | |
fe482240 | 123 | * Handle the values in summary mode. |
6a488035 | 124 | * |
041ab3d1 TO |
125 | * @param array $values |
126 | * The array of values belonging to this line. | |
6a488035 | 127 | * |
408b79bf | 128 | * @return bool |
a6c01b45 | 129 | * the result of this processing |
6a488035 | 130 | */ |
00be9182 | 131 | public function summary(&$values) { |
c5a8afa5 | 132 | try { |
7541b9e8 | 133 | // Check required fields if this is not an update. |
134 | if (!$this->getFieldValue($values, 'activity_id')) { | |
135 | if (!$this->getFieldValue($values, 'activity_label') | |
136 | && !$this->getFieldValue($values, 'activity_type_id')) { | |
137 | throw new CRM_Core_Exception(ts('Missing required fields: Activity type label or Activity type ID')); | |
138 | } | |
139 | if (!$this->getFieldValue($values, 'activity_date_time')) { | |
140 | throw new CRM_Core_Exception(ts('Missing required fields')); | |
141 | } | |
142 | } | |
143 | ||
c5a8afa5 | 144 | $this->validateActivityTypeIDAndLabel($values); |
7541b9e8 | 145 | if ($this->getFieldValue($values, 'activity_date_time') |
146 | && !$this->isValidDate($this->getFieldValue($values, 'activity_date_time'))) { | |
147 | throw new CRM_Core_Exception(ts('Invalid Activity Date')); | |
c5a8afa5 | 148 | } |
3f4d714a | 149 | |
150 | if ($this->getFieldValue($values, 'activity_engagement_level') | |
151 | && !CRM_Utils_Rule::positiveInteger($this->getFieldValue($values, 'activity_engagement_level'))) { | |
152 | throw new CRM_Core_Exception(ts('Activity Engagement Index')); | |
153 | } | |
06f0289b | 154 | $this->validateCustomFields($values); |
6a488035 | 155 | } |
c5a8afa5 | 156 | catch (CRM_Core_Exception $e) { |
157 | return $this->addError($values, [$e->getMessage()]); | |
6a488035 TO |
158 | } |
159 | ||
a05662ef | 160 | return CRM_Import_Parser::VALID; |
6a488035 TO |
161 | } |
162 | ||
163 | /** | |
fe482240 | 164 | * Handle the values in import mode. |
6a488035 | 165 | * |
041ab3d1 TO |
166 | * @param int $onDuplicate |
167 | * The code for what action to take on duplicates. | |
168 | * @param array $values | |
169 | * The array of values belonging to this line. | |
6a488035 | 170 | * |
408b79bf | 171 | * @return bool |
a6c01b45 | 172 | * the result of this processing |
ead76331 | 173 | * @throws \CRM_Core_Exception |
6a488035 | 174 | */ |
00be9182 | 175 | public function import($onDuplicate, &$values) { |
7808aae6 | 176 | // First make sure this is a valid line |
6a488035 TO |
177 | $response = $this->summary($values); |
178 | ||
a05662ef | 179 | if ($response != CRM_Import_Parser::VALID) { |
6a488035 TO |
180 | return $response; |
181 | } | |
ead76331 | 182 | $params = $this->getActiveFieldParams(); |
6a488035 TO |
183 | $activityLabel = array_search('activity_label', $this->_mapperKeys); |
184 | if ($activityLabel) { | |
be2fb01f | 185 | $params = array_merge($params, ['activity_label' => $values[$activityLabel]]); |
6a488035 | 186 | } |
7808aae6 | 187 | // For date-Formats. |
6a488035 TO |
188 | $session = CRM_Core_Session::singleton(); |
189 | $dateType = $session->get('dateTypes'); | |
190 | if (!isset($params['source_contact_id'])) { | |
191 | $params['source_contact_id'] = $session->get('userID'); | |
192 | } | |
1ae8fc42 | 193 | |
127f50ce | 194 | $customFields = CRM_Core_BAO_CustomField::getFields('Activity'); |
6a488035 TO |
195 | |
196 | foreach ($params as $key => $val) { | |
197 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
3c85c6e7 | 198 | if (!empty($customFields[$customFieldID]) && $customFields[$customFieldID]['data_type'] == 'Date') { |
719a6fec | 199 | CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $params, $dateType, $key); |
6a488035 | 200 | } |
8bc84e3c | 201 | elseif (!empty($customFields[$customFieldID]) && $customFields[$customFieldID]['data_type'] == 'Boolean') { |
6a488035 TO |
202 | $params[$key] = CRM_Utils_String::strtoboolstr($val); |
203 | } | |
204 | } | |
ead76331 | 205 | elseif ($key === 'activity_date_time') { |
7cad3e54 C |
206 | $params[$key] = CRM_Utils_Date::formatDate($val, $dateType); |
207 | } | |
ead76331 | 208 | elseif ($key === 'activity_subject') { |
6a488035 TO |
209 | $params['subject'] = $val; |
210 | } | |
211 | } | |
7808aae6 | 212 | // Date-Format part ends. |
4a50b17f | 213 | $formatError = $this->deprecated_activity_formatted_param($params, $params, TRUE); |
6a488035 TO |
214 | |
215 | if ($formatError) { | |
216 | array_unshift($values, $formatError['error_message']); | |
a05662ef | 217 | return CRM_Import_Parser::ERROR; |
6a488035 TO |
218 | } |
219 | ||
6a488035 TO |
220 | if ($this->_contactIdIndex < 0) { |
221 | ||
7808aae6 SB |
222 | // Retrieve contact id using contact dedupe rule. |
223 | // Since we are supporting only individual's activity import. | |
6a488035 TO |
224 | $params['contact_type'] = 'Individual'; |
225 | $params['version'] = 3; | |
226 | $error = _civicrm_api3_deprecated_duplicate_formatted_contact($params); | |
227 | ||
228 | if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { | |
229 | $matchedIDs = explode(',', $error['error_message']['params'][0]); | |
230 | if (count($matchedIDs) > 1) { | |
231 | array_unshift($values, 'Multiple matching contact records detected for this row. The activity was not imported'); | |
a05662ef | 232 | return CRM_Import_Parser::ERROR; |
6a488035 | 233 | } |
cb8bc3b6 | 234 | $cid = $matchedIDs[0]; |
235 | $params['target_contact_id'] = $cid; | |
236 | $params['version'] = 3; | |
237 | $newActivity = civicrm_api('activity', 'create', $params); | |
238 | if (!empty($newActivity['is_error'])) { | |
239 | array_unshift($values, $newActivity['error_message']); | |
240 | return CRM_Import_Parser::ERROR; | |
6a488035 TO |
241 | } |
242 | ||
cb8bc3b6 | 243 | $this->_newActivity[] = $newActivity['id']; |
244 | return CRM_Import_Parser::VALID; | |
245 | ||
246 | } | |
247 | // Using new Dedupe rule. | |
248 | $ruleParams = [ | |
249 | 'contact_type' => 'Individual', | |
250 | 'used' => 'Unsupervised', | |
251 | ]; | |
252 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); | |
253 | ||
254 | $disp = NULL; | |
255 | foreach ($fieldsArray as $value) { | |
256 | if (array_key_exists(trim($value), $params)) { | |
257 | $paramValue = $params[trim($value)]; | |
258 | if (is_array($paramValue)) { | |
259 | $disp .= $params[trim($value)][0][trim($value)] . " "; | |
6a488035 TO |
260 | } |
261 | else { | |
cb8bc3b6 | 262 | $disp .= $params[trim($value)] . " "; |
6a488035 TO |
263 | } |
264 | } | |
6a488035 | 265 | } |
cb8bc3b6 | 266 | |
a7488080 | 267 | if (!empty($params['external_identifier'])) { |
cb8bc3b6 | 268 | if ($disp) { |
269 | $disp .= "AND {$params['external_identifier']}"; | |
6a488035 TO |
270 | } |
271 | else { | |
cb8bc3b6 | 272 | $disp = $params['external_identifier']; |
6a488035 TO |
273 | } |
274 | } | |
275 | ||
cb8bc3b6 | 276 | array_unshift($values, 'No matching Contact found for (' . $disp . ')'); |
277 | return CRM_Import_Parser::ERROR; | |
278 | } | |
279 | if (!empty($params['external_identifier'])) { | |
280 | $targetContactId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
281 | $params['external_identifier'], 'id', 'external_identifier' | |
282 | ); | |
283 | ||
284 | if (!empty($params['target_contact_id']) && | |
285 | $params['target_contact_id'] != $targetContactId | |
286 | ) { | |
287 | array_unshift($values, 'Mismatch of External ID:' . $params['external_identifier'] . ' and Contact Id:' . $params['target_contact_id']); | |
a05662ef | 288 | return CRM_Import_Parser::ERROR; |
6a488035 | 289 | } |
cb8bc3b6 | 290 | if ($targetContactId) { |
291 | $params['target_contact_id'] = $targetContactId; | |
292 | } | |
293 | else { | |
294 | array_unshift($values, 'No Matching Contact for External ID:' . $params['external_identifier']); | |
295 | return CRM_Import_Parser::ERROR; | |
296 | } | |
297 | } | |
6a488035 | 298 | |
cb8bc3b6 | 299 | $params['version'] = 3; |
300 | $newActivity = civicrm_api('activity', 'create', $params); | |
301 | if (!empty($newActivity['is_error'])) { | |
302 | array_unshift($values, $newActivity['error_message']); | |
303 | return CRM_Import_Parser::ERROR; | |
6a488035 | 304 | } |
cb8bc3b6 | 305 | |
306 | $this->_newActivity[] = $newActivity['id']; | |
307 | return CRM_Import_Parser::VALID; | |
6a488035 TO |
308 | } |
309 | ||
4a50b17f | 310 | /** |
311 | * take the input parameter list as specified in the data model and | |
312 | * convert it into the same format that we use in QF and BAO object | |
313 | * | |
314 | * @param array $params | |
315 | * Associative array of property name/value. | |
316 | * pairs to insert in new contact. | |
317 | * @param array $values | |
318 | * The reformatted properties that we can use internally. | |
319 | * | |
320 | * @param array|bool $create Is the formatted Values array going to | |
321 | * be used for CRM_Activity_BAO_Activity::create() | |
322 | * | |
323 | * @return array|CRM_Error | |
324 | */ | |
325 | protected function deprecated_activity_formatted_param(&$params, &$values, $create = FALSE) { | |
326 | // copy all the activity fields as is | |
327 | $fields = CRM_Activity_DAO_Activity::fields(); | |
328 | _civicrm_api3_store_values($fields, $params, $values); | |
329 | ||
4a50b17f | 330 | foreach ($params as $key => $value) { |
331 | // ignore empty values or empty arrays etc | |
332 | if (CRM_Utils_System::isNull($value)) { | |
333 | continue; | |
334 | } | |
335 | ||
4a50b17f | 336 | if ($key == 'target_contact_id') { |
337 | if (!CRM_Utils_Rule::integer($value)) { | |
338 | return civicrm_api3_create_error("contact_id not valid: $value"); | |
339 | } | |
340 | $contactID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value"); | |
341 | if (!$contactID) { | |
342 | return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); | |
343 | } | |
344 | } | |
345 | } | |
346 | return NULL; | |
347 | } | |
348 | ||
c5a8afa5 | 349 | /** |
350 | * | |
351 | * Get the value for the given field from the row of values. | |
352 | * | |
353 | * @param array $row | |
354 | * @param string $fieldName | |
355 | * | |
356 | * @return null|string | |
357 | */ | |
358 | protected function getFieldValue(array $row, string $fieldName) { | |
359 | if (!is_numeric($this->getFieldIndex($fieldName))) { | |
360 | return NULL; | |
361 | } | |
362 | return $row[$this->getFieldIndex($fieldName)] ?? NULL; | |
363 | } | |
364 | ||
365 | /** | |
366 | * Get the index for the given field. | |
367 | * | |
368 | * @param string $fieldName | |
369 | * | |
370 | * @return false|int | |
371 | */ | |
372 | protected function getFieldIndex(string $fieldName) { | |
373 | return array_search($fieldName, $this->_mapperKeys, TRUE); | |
374 | ||
375 | } | |
376 | ||
377 | /** | |
378 | * Add an error to the values. | |
379 | * | |
380 | * @param array $values | |
381 | * @param array $error | |
382 | * | |
383 | * @return int | |
384 | */ | |
385 | protected function addError(array &$values, array $error): int { | |
386 | array_unshift($values, implode(';', $error)); | |
387 | return CRM_Import_Parser::ERROR; | |
388 | } | |
389 | ||
390 | /** | |
391 | * Validate that the activity type id does not conflict with the label. | |
392 | * | |
393 | * @param array $values | |
394 | * | |
395 | * @return void | |
396 | * @throws \CRM_Core_Exception | |
397 | */ | |
398 | protected function validateActivityTypeIDAndLabel(array $values): void { | |
399 | $activityLabel = $this->getFieldValue($values, 'activity_label'); | |
400 | $activityTypeID = $this->getFieldValue($values, 'activity_type_id'); | |
401 | if ($activityLabel && $activityTypeID | |
402 | && $activityLabel !== CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $activityTypeID)) { | |
403 | throw new CRM_Core_Exception(ts('Activity type label and Activity type ID are in conflict')); | |
404 | } | |
7541b9e8 | 405 | } |
406 | ||
407 | /** | |
408 | * Is the supplied date field valid based on selected date format. | |
409 | * | |
410 | * @param string $value | |
411 | * | |
412 | * @return bool | |
413 | */ | |
414 | protected function isValidDate(string $value): bool { | |
415 | return (bool) CRM_Utils_Date::formatDate($value, CRM_Core_Session::singleton()->get('dateTypes')); | |
c5a8afa5 | 416 | } |
417 | ||
06f0289b | 418 | /** |
419 | * Validate custom fields. | |
420 | * | |
421 | * @param array $values | |
422 | * | |
423 | * @throws \CRM_Core_Exception | |
424 | */ | |
425 | protected function validateCustomFields($values):void { | |
426 | $this->setActiveFieldValues($values); | |
427 | $params = $this->getActiveFieldParams(); | |
428 | $errorMessage = NULL; | |
429 | // Checking error in custom data. | |
430 | $params['contact_type'] = 'Activity'; | |
431 | CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); | |
432 | if ($errorMessage) { | |
433 | throw new CRM_Core_Exception('Invalid value for field(s) : ' . $errorMessage); | |
434 | } | |
435 | } | |
436 | ||
6a488035 | 437 | } |