3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
36 * Class to parse activity csv files.
38 class CRM_Activity_Import_Parser_Activity
extends CRM_Activity_Import_Parser
{
40 protected $_mapperKeys;
42 private $_contactIdIndex;
43 private $_activityTypeIndex;
44 private $_activityLabelIndex;
45 private $_activityDateIndex;
48 * Array of successfully imported activity id's
52 protected $_newActivity;
57 * @param array $mapperKeys
58 * @param int $mapperLocType
59 * @param int $mapperPhoneType
61 public function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) {
62 parent
::__construct();
63 $this->_mapperKeys
= &$mapperKeys;
67 * Function of undocumented functionality required by the interface.
69 protected function fini() {}
72 * The initializer code, called before the processing.
74 public function init() {
75 $activityContact = CRM_Activity_BAO_ActivityContact
::import();
76 $activityTarget['target_contact_id'] = $activityContact['contact_id'];
77 $fields = array_merge(CRM_Activity_BAO_Activity
::importableFields(),
81 $fields = array_merge($fields, array(
82 'source_contact_id' => array(
83 'title' => ts('Source Contact'),
84 'headerPattern' => '/Source.Contact?/i',
86 'activity_label' => array(
87 'title' => ts('Activity Type Label'),
88 'headerPattern' => '/(activity.)?type label?/i',
92 foreach ($fields as $name => $field) {
93 $field['type'] = CRM_Utils_Array
::value('type', $field, CRM_Utils_Type
::T_INT
);
94 $field['dataPattern'] = CRM_Utils_Array
::value('dataPattern', $field, '//');
95 $field['headerPattern'] = CRM_Utils_Array
::value('headerPattern', $field, '//');
96 $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']);
99 $this->_newActivity
= array();
101 $this->setActiveFields($this->_mapperKeys
);
103 // FIXME: we should do this in one place together with Form/MapField.php
104 $this->_contactIdIndex
= -1;
105 $this->_activityTypeIndex
= -1;
106 $this->_activityLabelIndex
= -1;
107 $this->_activityDateIndex
= -1;
110 foreach ($this->_mapperKeys
as $key) {
112 case 'target_contact_id':
113 case 'external_identifier':
114 $this->_contactIdIndex
= $index;
117 case 'activity_label':
118 $this->_activityLabelIndex
= $index;
121 case 'activity_type_id':
122 $this->_activityTypeIndex
= $index;
125 case 'activity_date_time':
126 $this->_activityDateIndex
= $index;
134 * Handle the values in mapField mode.
136 * @param array $values
137 * The array of values belonging to this line.
141 public function mapField(&$values) {
142 return CRM_Import_Parser
::VALID
;
146 * Handle the values in preview mode.
148 * @param array $values
149 * The array of values belonging to this line.
152 * the result of this processing
154 public function preview(&$values) {
155 return $this->summary($values);
159 * Handle the values in summary mode.
161 * @param array $values
162 * The array of values belonging to this line.
165 * the result of this processing
167 public function summary(&$values) {
168 $erroneousField = NULL;
169 $this->setActiveFieldValues($values, $erroneousField);
172 if ($this->_activityTypeIndex
> -1 && $this->_activityLabelIndex
> -1) {
173 array_unshift($values, ts('Please select either Activity Type ID OR Activity Type Label.'));
174 return CRM_Import_Parser
::ERROR
;
176 elseif ($this->_activityLabelIndex
> -1) {
177 $index = $this->_activityLabelIndex
;
179 elseif ($this->_activityTypeIndex
> -1) {
180 $index = $this->_activityTypeIndex
;
183 if ($index < 0 or $this->_activityDateIndex
< 0) {
184 $errorRequired = TRUE;
187 $errorRequired = !CRM_Utils_Array
::value($index, $values) ||
!CRM_Utils_Array
::value($this->_activityDateIndex
, $values);
190 if ($errorRequired) {
191 array_unshift($values, ts('Missing required fields'));
192 return CRM_Import_Parser
::ERROR
;
195 $params = &$this->getActiveFieldParams();
197 $errorMessage = NULL;
200 $session = CRM_Core_Session
::singleton();
201 $dateType = $session->get('dateTypes');
202 if (!isset($params['source_contact_id'])) {
203 $params['source_contact_id'] = $session->get('userID');
205 foreach ($params as $key => $val) {
206 if ($key == 'activity_date_time') {
208 $dateValue = CRM_Utils_Date
::formatDate($val, $dateType);
210 $params[$key] = $dateValue;
213 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Activity date', $errorMessage);
217 elseif ($key == 'activity_engagement_level' && $val &&
218 !CRM_Utils_Rule
::positiveInteger($val)
220 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Activity Engagement Index', $errorMessage);
223 // Date-Format part ends.
225 // Checking error in custom data.
226 $params['contact_type'] = isset($this->_contactType
) ?
$this->_contactType
: 'Activity';
228 CRM_Contact_Import_Parser_Contact
::isErrorInCustomData($params, $errorMessage);
231 $tempMsg = "Invalid value for field(s) : $errorMessage";
232 array_unshift($values, $tempMsg);
233 $errorMessage = NULL;
234 return CRM_Import_Parser
::ERROR
;
237 return CRM_Import_Parser
::VALID
;
241 * Handle the values in import mode.
243 * @param int $onDuplicate
244 * The code for what action to take on duplicates.
245 * @param array $values
246 * The array of values belonging to this line.
249 * the result of this processing
251 public function import($onDuplicate, &$values) {
252 // First make sure this is a valid line
253 $response = $this->summary($values);
255 if ($response != CRM_Import_Parser
::VALID
) {
258 $params = &$this->getActiveFieldParams();
259 $activityLabel = array_search('activity_label', $this->_mapperKeys
);
260 if ($activityLabel) {
261 $params = array_merge($params, array('activity_label' => $values[$activityLabel]));
264 $session = CRM_Core_Session
::singleton();
265 $dateType = $session->get('dateTypes');
266 if (!isset($params['source_contact_id'])) {
267 $params['source_contact_id'] = $session->get('userID');
270 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity');
272 foreach ($params as $key => $val) {
273 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
274 if ($key == 'activity_date_time' && $val) {
275 $params[$key] = CRM_Utils_Date
::formatDate($val, $dateType);
277 elseif (!empty($customFields[$customFieldID]) && $customFields[$customFieldID]['data_type'] == 'Date') {
278 CRM_Contact_Import_Parser_Contact
::formatCustomDate($params, $params, $dateType, $key);
280 elseif (!empty($customFields[$customFieldID]) && $customFields[$customFieldID]['data_type'] == 'Boolean') {
281 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
284 elseif ($key == 'activity_date_time') {
285 $params[$key] = CRM_Utils_Date
::formatDate($val, $dateType);
287 elseif ($key == 'activity_subject') {
288 $params['subject'] = $val;
291 // Date-Format part ends.
292 require_once 'CRM/Utils/DeprecatedUtils.php';
293 $formatError = _civicrm_api3_deprecated_activity_formatted_param($params, $params, TRUE);
296 array_unshift($values, $formatError['error_message']);
297 return CRM_Import_Parser
::ERROR
;
300 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
305 if ($this->_contactIdIndex
< 0) {
307 // Retrieve contact id using contact dedupe rule.
308 // Since we are supporting only individual's activity import.
309 $params['contact_type'] = 'Individual';
310 $params['version'] = 3;
311 $error = _civicrm_api3_deprecated_duplicate_formatted_contact($params);
313 if (CRM_Core_Error
::isAPIError($error, CRM_Core_ERROR
::DUPLICATE_CONTACT
)) {
314 $matchedIDs = explode(',', $error['error_message']['params'][0]);
315 if (count($matchedIDs) > 1) {
316 array_unshift($values, 'Multiple matching contact records detected for this row. The activity was not imported');
317 return CRM_Import_Parser
::ERROR
;
320 $cid = $matchedIDs[0];
321 $params['target_contact_id'] = $cid;
322 $params['version'] = 3;
323 $newActivity = civicrm_api('activity', 'create', $params);
324 if (!empty($newActivity['is_error'])) {
325 array_unshift($values, $newActivity['error_message']);
326 return CRM_Import_Parser
::ERROR
;
329 $this->_newActivity
[] = $newActivity['id'];
330 return CRM_Import_Parser
::VALID
;
334 // Using new Dedupe rule.
336 'contact_type' => 'Individual',
337 'used' => 'Unsupervised',
339 $fieldsArray = CRM_Dedupe_BAO_Rule
::dedupeRuleFields($ruleParams);
342 foreach ($fieldsArray as $value) {
343 if (array_key_exists(trim($value), $params)) {
344 $paramValue = $params[trim($value)];
345 if (is_array($paramValue)) {
346 $disp .= $params[trim($value)][0][trim($value)] . " ";
349 $disp .= $params[trim($value)] . " ";
354 if (!empty($params['external_identifier'])) {
356 $disp .= "AND {$params['external_identifier']}";
359 $disp = $params['external_identifier'];
363 array_unshift($values, 'No matching Contact found for (' . $disp . ')');
364 return CRM_Import_Parser
::ERROR
;
368 if (!empty($params['external_identifier'])) {
369 $targetContactId = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
370 $params['external_identifier'], 'id', 'external_identifier'
373 if (!empty($params['target_contact_id']) &&
374 $params['target_contact_id'] != $targetContactId
376 array_unshift($values, 'Mismatch of External ID:' . $params['external_identifier'] . ' and Contact Id:' . $params['target_contact_id']);
377 return CRM_Import_Parser
::ERROR
;
379 elseif ($targetContactId) {
380 $params['target_contact_id'] = $targetContactId;
383 array_unshift($values, 'No Matching Contact for External ID:' . $params['external_identifier']);
384 return CRM_Import_Parser
::ERROR
;
388 $params['version'] = 3;
389 $newActivity = civicrm_api('activity', 'create', $params);
390 if (!empty($newActivity['is_error'])) {
391 array_unshift($values, $newActivity['error_message']);
392 return CRM_Import_Parser
::ERROR
;
395 $this->_newActivity
[] = $newActivity['id'];
396 return CRM_Import_Parser
::VALID
;