3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
36 require_once 'api/api.php';
39 * class to parse activity csv files
41 class CRM_Activity_Import_Parser_Activity
extends CRM_Activity_Import_Parser
{
43 protected $_mapperKeys;
45 private $_contactIdIndex;
46 private $_activityTypeIndex;
47 private $_activityLabelIndex;
48 private $_activityDateIndex;
51 * Array of successfully imported activity id's
55 protected $_newActivity;
60 function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) {
61 parent
::__construct();
62 $this->_mapperKeys
= &$mapperKeys;
66 * the initializer code, called before the processing
72 $activityContact = CRM_Activity_BAO_ActivityContact
::import();
73 $activityTarget['target_contact_id'] = $activityContact['contact_id'];
74 $fields = array_merge(CRM_Activity_BAO_Activity
::importableFields(),
78 $fields = array_merge($fields, array(
79 'activity_label' => array(
80 'title' => ts('Activity Type Label'),
81 'headerPattern' => '/(activity.)?type label?/i',
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']);
92 $this->_newActivity
= array();
94 $this->setActiveFields($this->_mapperKeys
);
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;
103 foreach ($this->_mapperKeys
as $key) {
105 case 'target_contact_id':
106 case 'external_identifier':
107 $this->_contactIdIndex
= $index;
110 case 'activity_label':
111 $this->_activityLabelIndex
= $index;
114 case 'activity_type_id':
115 $this->_activityTypeIndex
= $index;
118 case 'activity_date_time':
119 $this->_activityDateIndex
= $index;
127 * handle the values in mapField mode
129 * @param array $values the array of values belonging to this line
134 function mapField(&$values) {
135 return CRM_Import_Parser
::VALID
;
139 * handle the values in preview mode
141 * @param array $values the array of values belonging to this line
143 * @return boolean the result of this processing
146 function preview(&$values) {
147 return $this->summary($values);
151 * handle the values in summary mode
153 * @param array $values the array of values belonging to this line
155 * @return boolean the result of this processing
158 function summary(&$values) {
159 $erroneousField = NULL;
160 $response = $this->setActiveFieldValues($values, $erroneousField);
162 $errorRequired = FALSE;
164 if ($this->_activityTypeIndex
> -1 && $this->_activityLabelIndex
> -1) {
165 array_unshift($values, ts('Please select either Activity Type ID OR Activity Type Label.'));
166 return CRM_Import_Parser
::ERROR
;
168 elseif ($this->_activityLabelIndex
> -1) {
169 $index = $this->_activityLabelIndex
;
171 elseif ($this->_activityTypeIndex
> -1) {
172 $index = $this->_activityTypeIndex
;
175 if ($index < 0 or $this->_activityDateIndex
< 0) {
176 $errorRequired = TRUE;
179 $errorRequired = !CRM_Utils_Array
::value($index, $values) ||
!CRM_Utils_Array
::value($this->_activityDateIndex
, $values);
182 if ($errorRequired) {
183 array_unshift($values, ts('Missing required fields'));
184 return CRM_Import_Parser
::ERROR
;
187 $params = &$this->getActiveFieldParams();
189 $errorMessage = NULL;
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');
197 foreach ($params as $key => $val) {
198 if ($key == 'activity_date_time') {
200 $dateValue = CRM_Utils_Date
::formatDate($val, $dateType);
202 $params[$key] = $dateValue;
205 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Activity date', $errorMessage);
209 elseif ($key == 'activity_engagement_level' && $val &&
210 !CRM_Utils_Rule
::positiveInteger($val)
212 CRM_Contact_Import_Parser_Contact
::addToErrorMsg('Activity Engagement Index', $errorMessage);
215 //date-Format part ends
217 //checking error in custom data
218 $params['contact_type'] = isset($this->_contactType
) ?
$this->_contactType
: 'Activity';
220 CRM_Contact_Import_Parser_Contact
::isErrorInCustomData($params, $errorMessage);
223 $tempMsg = "Invalid value for field(s) : $errorMessage";
224 array_unshift($values, $tempMsg);
225 $errorMessage = NULL;
226 return CRM_Import_Parser
::ERROR
;
229 return CRM_Import_Parser
::VALID
;
233 * handle the values in import mode
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
238 * @return boolean the result of this processing
241 function import($onDuplicate, &$values) {
242 // first make sure this is a valid line
243 $response = $this->summary($values);
245 if ($response != CRM_Import_Parser
::VALID
) {
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]));
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');
259 $formatted = array();
260 $customFields = CRM_Core_BAO_CustomField
::getFields(CRM_Utils_Array
::value('contact_type', $params));
262 foreach ($params as $key => $val) {
263 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
264 if ($key == 'activity_date_time' && $val) {
265 $params[$key] = CRM_Utils_Date
::formatDate($val, $dateType);
267 elseif ($customFields[$customFieldID]['data_type'] == 'Date') {
268 CRM_Contact_Import_Parser_Contact
::formatCustomDate($params, $params, $dateType, $key);
270 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
271 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
274 elseif ($key == 'activity_subject') {
275 $params['subject'] = $val;
278 //date-Format part ends
279 require_once 'CRM/Utils/DeprecatedUtils.php';
280 $formatError = _civicrm_api3_deprecated_activity_formatted_param($params, $params, TRUE);
283 array_unshift($values, $formatError['error_message']);
284 return CRM_Import_Parser
::ERROR
;
287 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
288 CRM_Core_DAO
::$_nullObject,
293 if ($this->_contactIdIndex
< 0) {
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);
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');
305 return CRM_Import_Parser
::ERROR
;
308 $cid = $matchedIDs[0];
309 $params['target_contact_id'] = $cid;
310 $params['version'] = 3;
311 $newActivity = civicrm_api('activity', 'create', $params);
312 if (CRM_Utils_Array
::value('is_error', $newActivity)) {
313 array_unshift($values, $newActivity['error_message']);
314 return CRM_Import_Parser
::ERROR
;
317 $this->_newActivity
[] = $newActivity['id'];
318 return CRM_Import_Parser
::VALID
;
322 // Using new Dedupe rule.
324 'contact_type' => 'Individual',
325 'used' => 'Unsupervised',
327 $fieldsArray = CRM_Dedupe_BAO_Rule
::dedupeRuleFields($ruleParams);
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)] . " ";
337 $disp .= $params[trim($value)] . " ";
342 if (CRM_Utils_Array
::value('external_identifier', $params)) {
344 $disp .= "AND {$params['external_identifier']}";
347 $disp = $params['external_identifier'];
351 array_unshift($values, 'No matching Contact found for (' . $disp . ')');
352 return CRM_Import_Parser
::ERROR
;
356 if (CRM_Utils_Array
::value('external_identifier', $params)) {
357 $targetContactId = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
358 $params['external_identifier'], 'id', 'external_identifier'
361 if (CRM_Utils_Array
::value('target_contact_id', $params) &&
362 $params['target_contact_id'] != $targetContactId
364 array_unshift($values, 'Mismatch of External identifier :' . $params['external_identifier'] . ' and Contact Id:' . $params['target_contact_id']);
365 return CRM_Import_Parser
::ERROR
;
367 elseif ($targetContactId) {
368 $params['target_contact_id'] = $targetContactId;
371 array_unshift($values, 'No Matching Contact for External identifier :' . $params['external_identifier']);
372 return CRM_Import_Parser
::ERROR
;
376 $params['version'] = 3;
377 $newActivity = civicrm_api('activity', 'create', $params);
378 if (CRM_Utils_Array
::value('is_error', $newActivity)) {
379 array_unshift($values, $newActivity['error_message']);
380 return CRM_Import_Parser
::ERROR
;
383 $this->_newActivity
[] = $newActivity['id'];
384 return CRM_Import_Parser
::VALID
;
389 * the initializer code, called before the processing