3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
37 * This class previews the uploaded file and returns summary
40 class CRM_Contact_Import_Form_Preview
extends CRM_Import_Form_Preview
{
43 * Whether USPS validation should be disabled during import.
47 protected $_disableUSPS;
50 * Set variables up before form is built.
54 public function preProcess() {
55 //get the data from the session
56 $dataValues = $this->get('dataValues');
57 $mapper = $this->get('mapper');
58 $invalidRowCount = $this->get('invalidRowCount');
59 $conflictRowCount = $this->get('conflictRowCount');
60 $mismatchCount = $this->get('unMatchCount');
61 $columnNames = $this->get('columnNames');
62 $this->_disableUSPS
= $this->get('disableUSPS');
65 $this->assign('columnNames', $columnNames);
67 //get the mapping name displayed if the mappingId is set
68 $mappingId = $this->get('loadMappingId');
70 $mapDAO = new CRM_Core_DAO_Mapping();
71 $mapDAO->id
= $mappingId;
73 $this->assign('loadedMapping', $mappingId);
74 $this->assign('savedName', $mapDAO->name
);
77 $this->assign('rowDisplayCount', 2);
79 $groups = CRM_Core_PseudoConstant
::nestedGroup();
80 $this->set('groups', $groups);
82 $tag = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
84 $this->set('tag', $tag);
87 if ($invalidRowCount) {
88 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contact_Import_Parser';
89 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
92 if ($conflictRowCount) {
93 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. '&parser=CRM_Contact_Import_Parser';
94 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
98 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. '&parser=CRM_Contact_Import_Parser';
99 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
113 'downloadErrorRecordsUrl',
114 'downloadConflictRecordsUrl',
115 'downloadMismatchRecordsUrl',
117 'relatedContactDetails',
118 'relatedContactLocType',
119 'relatedContactPhoneType',
120 'relatedContactImProvider',
122 'relatedContactWebsiteType',
125 foreach ($properties as $property) {
126 $this->assign($property, $this->get($property));
129 $statusID = $this->get('statusID');
131 $statusID = md5(uniqid(rand(), TRUE));
132 $this->set('statusID', $statusID);
134 $statusUrl = CRM_Utils_System
::url('civicrm/ajax/status', "id={$statusID}", FALSE, NULL, FALSE);
135 $this->assign('statusUrl', $statusUrl);
137 $showColNames = TRUE;
138 if ('CRM_Import_DataSource_CSV' == $this->get('dataSource') &&
139 !$this->get('skipColumnHeader')
141 $showColNames = FALSE;
143 $this->assign('showColNames', $showColNames);
147 * Build the form object.
151 public function buildQuickForm() {
152 $this->addElement('text', 'newGroupName', ts('Name for new group'));
153 $this->addElement('text', 'newGroupDesc', ts('Description of new group'));
154 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
155 if (!empty($groupTypes)) {
156 $this->addCheckBox('newGroupType',
159 NULL, NULL, NULL, NULL, ' '
163 $groups = $this->get('groups');
165 if (!empty($groups)) {
166 $this->addElement('select', 'groups', ts('Add imported records to existing group(s)'), $groups, array(
167 'multiple' => "multiple",
168 'class' => 'crm-select2',
173 $this->addElement('text', 'newTagName', ts('Tag'));
174 $this->addElement('text', 'newTagDesc', ts('Description'));
176 $tag = $this->get('tag');
178 foreach ($tag as $tagID => $tagName) {
179 $this->addElement('checkbox', "tag[$tagID]", NULL, $tagName);
183 $path = "_qf_MapField_display=true";
184 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $form);
185 if (CRM_Utils_Rule
::qfKey($qfKey)) {
186 $path .= "&qfKey=$qfKey";
189 $previousURL = CRM_Utils_System
::url('civicrm/import/contact', $path, FALSE, NULL, FALSE);
190 $cancelURL = CRM_Utils_System
::url('civicrm/import/contact', 'reset=1');
195 'name' => ts('Previous'),
196 'js' => array('onclick' => "location.href='{$previousURL}'; return false;"),
200 'name' => ts('Import Now'),
201 'spacing' => ' ',
203 'js' => array('onclick' => "return verify( );"),
207 'name' => ts('Cancel'),
208 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"),
212 $this->addButtons($buttons);
214 $this->addFormRule(array('CRM_Contact_Import_Form_Preview', 'formRule'), $this);
218 * Global validation rules for the form.
220 * @param array $fields
221 * Posted values of the form.
227 * list of errors to be posted back to the form
229 public static function formRule($fields, $files, $self) {
231 $invalidTagName = $invalidGroupName = FALSE;
233 if (!empty($fields['newTagName'])) {
234 if (!CRM_Utils_Rule
::objectExists(trim($fields['newTagName']),
235 array('CRM_Core_DAO_Tag')
238 $errors['newTagName'] = ts('Tag \'%1\' already exists.',
239 array(1 => $fields['newTagName'])
241 $invalidTagName = TRUE;
245 if (!empty($fields['newGroupName'])) {
246 $title = trim($fields['newGroupName']);
247 $name = CRM_Utils_String
::titleToVar($title);
248 $query = 'select count(*) from civicrm_group where name like %1 OR title like %2';
249 $grpCnt = CRM_Core_DAO
::singleValueQuery(
252 1 => array($name, 'String'),
253 2 => array($title, 'String'),
257 $invalidGroupName = TRUE;
258 $errors['newGroupName'] = ts('Group \'%1\' already exists.', array(1 => $fields['newGroupName']));
262 $self->assign('invalidTagName', $invalidTagName);
263 $self->assign('invalidGroupName', $invalidGroupName);
265 return empty($errors) ?
TRUE : $errors;
269 * Process the mapped fields and map it into the uploaded file
270 * preview the file and extract some summary statistics
274 public function postProcess() {
276 $importJobParams = array(
277 'doGeocodeAddress' => $this->controller
->exportValue('DataSource', 'doGeocodeAddress'),
278 'invalidRowCount' => $this->get('invalidRowCount'),
279 'conflictRowCount' => $this->get('conflictRowCount'),
280 'onDuplicate' => $this->get('onDuplicate'),
281 'dedupe' => $this->get('dedupe'),
282 'newGroupName' => $this->controller
->exportValue($this->_name
, 'newGroupName'),
283 'newGroupDesc' => $this->controller
->exportValue($this->_name
, 'newGroupDesc'),
284 'newGroupType' => $this->controller
->exportValue($this->_name
, 'newGroupType'),
285 'groups' => $this->controller
->exportValue($this->_name
, 'groups'),
286 'allGroups' => $this->get('groups'),
287 'newTagName' => $this->controller
->exportValue($this->_name
, 'newTagName'),
288 'newTagDesc' => $this->controller
->exportValue($this->_name
, 'newTagDesc'),
289 'tag' => $this->controller
->exportValue($this->_name
, 'tag'),
290 'allTags' => $this->get('tag'),
291 'mapper' => $this->controller
->exportValue('MapField', 'mapper'),
292 'mapFields' => $this->get('fields'),
293 'contactType' => $this->get('contactType'),
294 'contactSubType' => $this->get('contactSubType'),
295 'primaryKeyName' => $this->get('primaryKeyName'),
296 'statusFieldName' => $this->get('statusFieldName'),
297 'statusID' => $this->get('statusID'),
298 'totalRowCount' => $this->get('totalRowCount'),
301 $tableName = $this->get('importTableName');
302 $importJob = new CRM_Contact_Import_ImportJob($tableName);
303 $importJob->setJobParams($importJobParams);
305 // If ACL applies to the current user, update cache before running the import.
306 if (!CRM_Core_Permission
::check('view all contacts')) {
307 $session = CRM_Core_Session
::singleton();
308 $userID = $session->get('userID');
309 CRM_ACL_BAO_Cache
::updateEntry($userID);
312 CRM_Utils_Address_USPS
::disable($this->_disableUSPS
);
315 $importJob->runImport($this);
317 // update cache after we done with runImport
318 if (!CRM_Core_Permission
::check('view all contacts')) {
319 CRM_ACL_BAO_Cache
::updateEntry($userID);
323 CRM_Contact_BAO_Contact_Utils
::clearContactCaches();
325 // add all the necessary variables to the form
326 $importJob->setFormVariables($this);
328 // check if there is any error occured
329 $errorStack = CRM_Core_Error
::singleton();
330 $errors = $errorStack->getErrors();
331 $errorMessage = array();
333 if (is_array($errors)) {
334 foreach ($errors as $key => $value) {
335 $errorMessage[] = $value['message'];
338 // there is no fileName since this is a sql import
340 $config = CRM_Core_Config
::singleton();
341 $errorFile = $config->uploadDir
. "sqlImport.error.log";
342 if ($fd = fopen($errorFile, 'w')) {
343 fwrite($fd, implode('\n', $errorMessage));
347 $this->set('errorFile', $errorFile);
349 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contact_Import_Parser';
350 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
352 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. '&parser=CRM_Contact_Import_Parser';
353 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
355 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. '&parser=CRM_Contact_Import_Parser';
356 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
360 //if job complete drop table.
361 $importJob->isComplete(TRUE);
365 * Process the mapped fields and map it into the uploaded file
366 * preview the file and extract some summary statistics
370 public function postProcessOld() {
372 $doGeocodeAddress = $this->controller
->exportValue('DataSource', 'doGeocodeAddress');
373 $invalidRowCount = $this->get('invalidRowCount');
374 $conflictRowCount = $this->get('conflictRowCount');
375 $onDuplicate = $this->get('onDuplicate');
376 $newGroupName = $this->controller
->exportValue($this->_name
, 'newGroupName');
377 $newGroupDesc = $this->controller
->exportValue($this->_name
, 'newGroupDesc');
378 $newGroupType = $this->controller
->exportValue($this->_name
, 'newGroupType');
379 $groups = $this->controller
->exportValue($this->_name
, 'groups');
380 $allGroups = $this->get('groups');
381 $newTagName = $this->controller
->exportValue($this->_name
, 'newTagName');
382 $newTagDesc = $this->controller
->exportValue($this->_name
, 'newTagDesc');
383 $tag = $this->controller
->exportValue($this->_name
, 'tag');
384 $allTags = $this->get('tag');
386 $mapper = $this->controller
->exportValue('MapField', 'mapper');
388 $mapperKeys = array();
389 $mapperLocTypes = array();
390 $mapperPhoneTypes = array();
391 $mapperRelated = array();
392 $mapperRelatedContactType = array();
393 $mapperRelatedContactDetails = array();
394 $mapperRelatedContactLocType = array();
395 $mapperRelatedContactPhoneType = array();
397 foreach ($mapper as $key => $value) {
398 $mapperKeys[$key] = $mapper[$key][0];
399 if (is_numeric($mapper[$key][1])) {
400 $mapperLocTypes[$key] = $mapper[$key][1];
403 $mapperLocTypes[$key] = NULL;
406 if (CRM_Utils_Array
::value($key, $mapperKeys) == 'phone') {
407 $mapperPhoneTypes[$key] = $mapper[$key][2];
410 $mapperPhoneTypes[$key] = NULL;
413 list($id, $first, $second) = explode('_', $mapper[$key][0]);
414 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
415 $relationType = new CRM_Contact_DAO_RelationshipType();
416 $relationType->id
= $id;
417 $relationType->find(TRUE);
418 $fieldName = "contact_type_$second";
419 $mapperRelatedContactType[$key] = $relationType->$fieldName;
420 $mapperRelated[$key] = $mapper[$key][0];
421 $mapperRelatedContactDetails[$key] = $mapper[$key][1];
422 $mapperRelatedContactLocType[$key] = $mapper[$key][2];
423 $mapperRelatedContactPhoneType[$key] = $mapper[$key][3];
426 $mapperRelated[$key] = NULL;
427 $mapperRelatedContactType[$key] = NULL;
428 $mapperRelatedContactDetails[$key] = NULL;
429 $mapperRelatedContactLocType[$key] = NULL;
430 $mapperRelatedContactPhoneType[$key] = NULL;
434 $parser = new CRM_Contact_Import_Parser_Contact($mapperKeys, $mapperLocTypes,
435 $mapperPhoneTypes, $mapperRelated, $mapperRelatedContactType,
436 $mapperRelatedContactDetails, $mapperRelatedContactLocType,
437 $mapperRelatedContactPhoneType
440 $mapFields = $this->get('fields');
442 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
443 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
445 foreach ($mapper as $key => $value) {
447 list($id, $first, $second) = explode('_', $mapper[$key][0]);
448 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
449 $relationType = new CRM_Contact_DAO_RelationshipType();
450 $relationType->id
= $id;
451 $relationType->find(TRUE);
453 $header[] = $relationType->name_a_b
;
454 $header[] = ucwords(str_replace("_", " ", $mapper[$key][1]));
456 if (isset($mapper[$key][2])) {
457 $header[] = $locationTypes[$mapper[$key][2]];
459 if (isset($mapper[$key][3])) {
460 $header[] = $phoneTypes[$mapper[$key][3]];
464 if (isset($mapFields[$mapper[$key][0]])) {
465 $header[] = $mapFields[$mapper[$key][0]];
466 if (isset($mapper[$key][1])) {
467 $header[] = $locationTypes[$mapper[$key][1]];
469 if (isset($mapper[$key][2])) {
470 $header[] = $phoneTypes[$mapper[$key][2]];
474 $mapperFields[] = implode(' - ', $header);
477 $tableName = $this->get('importTableName');
478 //print "Running parser on table: $tableName<br/>";
479 $parser->run($tableName, $mapperFields,
480 CRM_Import_Parser
::MODE_IMPORT
,
481 $this->get('contactType'),
482 $this->get('primaryKeyName'),
483 $this->get('statusFieldName'),
485 $this->get('statusID'),
486 $this->get('totalRowCount'),
488 CRM_Contact_Import_Parser
::DEFAULT_TIMEOUT
,
489 $this->get('contactSubType'),
493 // add the new contacts to selected groups
494 $contactIds = &$parser->getImportedContacts();
496 // add the new related contacts to selected groups
497 $relatedContactIds = &$parser->getRelatedImportedContacts();
499 $this->set('relatedCount', count($relatedContactIds));
502 //changed below if-statement "if ($newGroup) {" to "if ($newGroupName) {"
504 /* Create a new group */
507 'name' => $newGroupName,
508 'title' => $newGroupName,
509 'description' => $newGroupDesc,
510 'group_type' => $newGroupType,
513 $group = CRM_Contact_BAO_Group
::create($gParams);
514 $groups[] = $newGroupId = $group->id
;
517 if (is_array($groups)) {
518 $groupAdditions = array();
519 foreach ($groups as $groupId) {
520 $addCount = CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIds, $groupId);
521 if (!empty($relatedContactIds)) {
522 $addRelCount = CRM_Contact_BAO_GroupContact
::addContactsToGroup($relatedContactIds, $groupId);
524 $totalCount = $addCount[1] +
$addRelCount[1];
525 if ($groupId == $newGroupId) {
526 $name = $newGroupName;
530 $name = $allGroups[$groupId];
533 $groupAdditions[] = array(
534 'url' => CRM_Utils_System
::url('civicrm/group/search',
535 'reset=1&force=1&context=smog&gid=' . $groupId
538 'added' => $totalCount,
539 'notAdded' => $addCount[2] +
$addRelCount[2],
543 $this->set('groupAdditions', $groupAdditions);
548 /* Create a new Tag */
551 'name' => $newTagName,
552 'title' => $newTagName,
553 'description' => $newTagDesc,
557 $addedTag = CRM_Core_BAO_Tag
::add($tagParams, $id);
558 $tag[$addedTag->id
] = 1;
562 if (is_array($tag)) {
564 $tagAdditions = array();
565 foreach ($tag as $tagId => $val) {
566 $addTagCount = CRM_Core_BAO_EntityTag
::addContactsToTag($contactIds, $tagId);
567 if (!empty($relatedContactIds)) {
568 $addRelTagCount = CRM_Core_BAO_EntityTag
::addContactsToTag($relatedContactIds, $tagId);
570 $totalTagCount = $addTagCount[1] +
$addRelTagCount[1];
571 if ($tagId == $addedTag->id
) {
572 $tagName = $newTagName;
576 $tagName = $allTags[$tagId];
579 $tagAdditions[] = array(
580 'url' => CRM_Utils_System
::url('civicrm/contact/search',
581 'reset=1&force=1&context=smog&id=' . $tagId
584 'added' => $totalTagCount,
585 'notAdded' => $addTagCount[2] +
$addRelTagCount[2],
589 $this->set('tagAdditions', $tagAdditions);
592 // add all the necessary variables to the form
593 $parser->set($this, CRM_Import_Parser
::MODE_IMPORT
);
595 // check if there is any error occured
597 $errorStack = CRM_Core_Error
::singleton();
598 $errors = $errorStack->getErrors();
599 $errorMessage = array();
601 if (is_array($errors)) {
602 foreach ($errors as $key => $value) {
603 $errorMessage[] = $value['message'];
606 // there is no fileName since this is a sql import
608 $config = CRM_Core_Config
::singleton();
609 $errorFile = $config->uploadDir
. "sqlImport.error.log";
610 if ($fd = fopen($errorFile, 'w')) {
611 fwrite($fd, implode('\n', $errorMessage));
615 $this->set('errorFile', $errorFile);
617 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contact_Import_Parser';
618 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlparams));
620 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. '&parser=CRM_Contact_Import_Parser';
621 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
623 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. '&parser=CRM_Contact_Import_Parser';
624 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));