3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This class previews the uploaded file and returns summary
40 class CRM_Contact_Import_Form_Preview
extends CRM_Import_Form_Preview
{
43 * Function to set variables up before form is built
48 public function preProcess() {
49 //get the data from the session
50 $dataValues = $this->get('dataValues');
51 $mapper = $this->get('mapper');
52 $invalidRowCount = $this->get('invalidRowCount');
53 $conflictRowCount = $this->get('conflictRowCount');
54 $mismatchCount = $this->get('unMatchCount');
55 $columnNames = $this->get('columnNames');
58 $this->assign('columnNames', $columnNames);
60 //get the mapping name displayed if the mappingId is set
61 $mappingId = $this->get('loadMappingId');
63 $mapDAO = new CRM_Core_DAO_Mapping();
64 $mapDAO->id
= $mappingId;
66 $this->assign('loadedMapping', $mappingId);
67 $this->assign('savedName', $mapDAO->name
);
70 $this->assign('rowDisplayCount', 2);
72 $groups = CRM_Core_PseudoConstant
::nestedGroup();
73 $this->set('groups', $groups);
75 $tag = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
77 $this->set('tag', $tag);
80 if ($invalidRowCount) {
81 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contact_Import_Parser';
82 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
85 if ($conflictRowCount) {
86 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. '&parser=CRM_Contact_Import_Parser';
87 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
91 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. '&parser=CRM_Contact_Import_Parser';
92 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
96 'mapper', 'locations', 'phones', 'ims',
97 'dataValues', 'columnCount',
98 'totalRowCount', 'validRowCount',
99 'invalidRowCount', 'conflictRowCount',
100 'downloadErrorRecordsUrl',
101 'downloadConflictRecordsUrl',
102 'downloadMismatchRecordsUrl',
103 'related', 'relatedContactDetails', 'relatedContactLocType',
104 'relatedContactPhoneType', 'relatedContactImProvider', 'websites',
105 'relatedContactWebsiteType',
108 foreach ($properties as $property) {
109 $this->assign($property, $this->get($property));
112 $statusID = $this->get('statusID');
114 $statusID = md5(uniqid(rand(), TRUE));
115 $this->set('statusID', $statusID);
117 $statusUrl = CRM_Utils_System
::url('civicrm/ajax/status', "id={$statusID}", FALSE, NULL, FALSE);
118 $this->assign('statusUrl', $statusUrl);
120 $showColNames = TRUE;
121 if ('CRM_Import_DataSource_CSV' == $this->get('dataSource') &&
122 !$this->get('skipColumnHeader')
124 $showColNames = FALSE;
126 $this->assign('showColNames', $showColNames);
130 * Function to actually build the form
135 public function buildQuickForm() {
136 $this->addElement('text', 'newGroupName', ts('Name for new group'));
137 $this->addElement('text', 'newGroupDesc', ts('Description of new group'));
139 $groups = $this->get('groups');
141 if (!empty($groups)) {
142 $this->addElement('select', 'groups', ts('Add imported records to existing group(s)'), $groups, array('multiple' => "multiple", 'class' => 'crm-select2'));
146 $this->addElement('text', 'newTagName', ts('Tag'));
147 $this->addElement('text', 'newTagDesc', ts('Description'));
149 $tag = $this->get('tag');
151 foreach ($tag as $tagID => $tagName) {
152 $this->addElement('checkbox', "tag[$tagID]", NULL, $tagName);
156 $path = "_qf_MapField_display=true";
157 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $form);
158 if (CRM_Utils_Rule
::qfKey($qfKey)) {
159 $path .= "&qfKey=$qfKey";
162 $previousURL = CRM_Utils_System
::url('civicrm/import/contact', $path, FALSE, NULL, FALSE);
163 $cancelURL = CRM_Utils_System
::url('civicrm/import/contact', 'reset=1');
168 'name' => ts('<< Previous'),
169 'js' => array('onclick' => "location.href='{$previousURL}'; return false;"),
173 'name' => ts('Import Now >>'),
174 'spacing' => ' ',
176 'js' => array('onclick' => "return verify( );"),
180 'name' => ts('Cancel'),
181 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"),
185 $this->addButtons($buttons);
187 $this->addFormRule(array('CRM_Contact_Import_Form_Preview', 'formRule'), $this);
191 * global validation rules for the form
193 * @param array $fields posted values of the form
198 * @return array list of errors to be posted back to the form
202 static function formRule($fields, $files, $self) {
204 $invalidTagName = $invalidGroupName = FALSE;
206 if (!empty($fields['newTagName'])) {
207 if (!CRM_Utils_Rule
::objectExists(trim($fields['newTagName']),
208 array('CRM_Core_DAO_Tag')
210 $errors['newTagName'] = ts('Tag \'%1\' already exists.',
211 array(1 => $fields['newTagName'])
213 $invalidTagName = TRUE;
217 if (!empty($fields['newGroupName'])) {
218 $title = trim($fields['newGroupName']);
219 $name = CRM_Utils_String
::titleToVar($title);
220 $query = 'select count(*) from civicrm_group where name like %1 OR title like %2';
221 $grpCnt = CRM_Core_DAO
::singleValueQuery(
224 1 => array($name, 'String'),
225 2 => array($title, 'String'),
229 $invalidGroupName = TRUE;
230 $errors['newGroupName'] = ts('Group \'%1\' already exists.', array(1 => $fields['newGroupName']));
234 $self->assign('invalidTagName', $invalidTagName);
235 $self->assign('invalidGroupName', $invalidGroupName);
237 return empty($errors) ?
TRUE : $errors;
241 * Process the mapped fields and map it into the uploaded file
242 * preview the file and extract some summary statistics
247 public function postProcess() {
249 $importJobParams = array(
250 'doGeocodeAddress' => $this->controller
->exportValue('DataSource', 'doGeocodeAddress'),
251 'invalidRowCount' => $this->get('invalidRowCount'),
252 'conflictRowCount' => $this->get('conflictRowCount'),
253 'onDuplicate' => $this->get('onDuplicate'),
254 'dedupe' => $this->get('dedupe'),
255 'newGroupName' => $this->controller
->exportValue($this->_name
, 'newGroupName'),
256 'newGroupDesc' => $this->controller
->exportValue($this->_name
, 'newGroupDesc'),
257 'groups' => $this->controller
->exportValue($this->_name
, 'groups'),
258 'allGroups' => $this->get('groups'),
259 'newTagName' => $this->controller
->exportValue($this->_name
, 'newTagName'),
260 'newTagDesc' => $this->controller
->exportValue($this->_name
, 'newTagDesc'),
261 'tag' => $this->controller
->exportValue($this->_name
, 'tag'),
262 'allTags' => $this->get('tag'),
263 'mapper' => $this->controller
->exportValue('MapField', 'mapper'),
264 'mapFields' => $this->get('fields'),
265 'contactType' => $this->get('contactType'),
266 'contactSubType' => $this->get('contactSubType'),
267 'primaryKeyName' => $this->get('primaryKeyName'),
268 'statusFieldName' => $this->get('statusFieldName'),
269 'statusID' => $this->get('statusID'),
270 'totalRowCount' => $this->get('totalRowCount'),
273 $tableName = $this->get('importTableName');
274 $importJob = new CRM_Contact_Import_ImportJob($tableName);
275 $importJob->setJobParams($importJobParams);
277 // If ACL applies to the current user, update cache before running the import.
278 if (!CRM_Core_Permission
::check('view all contacts')) {
279 $session = CRM_Core_Session
::singleton();
280 $userID = $session->get('userID');
281 CRM_ACL_BAO_Cache
::updateEntry($userID);
285 $importJob->runImport($this);
287 // update cache after we done with runImport
288 if (!CRM_Core_Permission
::check('view all contacts')) {
289 CRM_ACL_BAO_Cache
::updateEntry($userID);
293 CRM_Contact_BAO_Contact_Utils
::clearContactCaches();
295 // add all the necessary variables to the form
296 $importJob->setFormVariables($this);
298 // check if there is any error occured
299 $errorStack = CRM_Core_Error
::singleton();
300 $errors = $errorStack->getErrors();
301 $errorMessage = array();
303 if (is_array($errors)) {
304 foreach ($errors as $key => $value) {
305 $errorMessage[] = $value['message'];
308 // there is no fileName since this is a sql import
310 $config = CRM_Core_Config
::singleton();
311 $errorFile = $config->uploadDir
. "sqlImport.error.log";
312 if ($fd = fopen($errorFile, 'w')) {
313 fwrite($fd, implode('\n', $errorMessage));
317 $this->set('errorFile', $errorFile);
319 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contact_Import_Parser';
320 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
322 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. '&parser=CRM_Contact_Import_Parser';
323 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
325 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. '&parser=CRM_Contact_Import_Parser';
326 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
330 //if job complete drop table.
331 $importJob->isComplete(TRUE);
335 * Process the mapped fields and map it into the uploaded file
336 * preview the file and extract some summary statistics
341 public function postProcessOld() {
343 $doGeocodeAddress = $this->controller
->exportValue('DataSource', 'doGeocodeAddress');
344 $invalidRowCount = $this->get('invalidRowCount');
345 $conflictRowCount = $this->get('conflictRowCount');
346 $onDuplicate = $this->get('onDuplicate');
347 $newGroupName = $this->controller
->exportValue($this->_name
, 'newGroupName');
348 $newGroupDesc = $this->controller
->exportValue($this->_name
, 'newGroupDesc');
349 $groups = $this->controller
->exportValue($this->_name
, 'groups');
350 $allGroups = $this->get('groups');
351 $newTagName = $this->controller
->exportValue($this->_name
, 'newTagName');
352 $newTagDesc = $this->controller
->exportValue($this->_name
, 'newTagDesc');
353 $tag = $this->controller
->exportValue($this->_name
, 'tag');
354 $allTags = $this->get('tag');
356 $mapper = $this->controller
->exportValue('MapField', 'mapper');
358 $mapperKeys = array();
359 $mapperLocTypes = array();
360 $mapperPhoneTypes = array();
361 $mapperRelated = array();
362 $mapperRelatedContactType = array();
363 $mapperRelatedContactDetails = array();
364 $mapperRelatedContactLocType = array();
365 $mapperRelatedContactPhoneType = array();
367 foreach ($mapper as $key => $value) {
368 $mapperKeys[$key] = $mapper[$key][0];
369 if (is_numeric($mapper[$key][1])) {
370 $mapperLocTypes[$key] = $mapper[$key][1];
373 $mapperLocTypes[$key] = NULL;
376 if (CRM_Utils_Array
::value($key, $mapperKeys) == 'phone') {
377 $mapperPhoneTypes[$key] = $mapper[$key][2];
380 $mapperPhoneTypes[$key] = NULL;
383 list($id, $first, $second) = explode('_', $mapper[$key][0]);
384 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
385 $relationType = new CRM_Contact_DAO_RelationshipType();
386 $relationType->id
= $id;
387 $relationType->find(TRUE);
388 $fieldName = "contact_type_$second";
389 $mapperRelatedContactType[$key] = $relationType->$fieldName;
390 $mapperRelated[$key] = $mapper[$key][0];
391 $mapperRelatedContactDetails[$key] = $mapper[$key][1];
392 $mapperRelatedContactLocType[$key] = $mapper[$key][2];
393 $mapperRelatedContactPhoneType[$key] = $mapper[$key][3];
396 $mapperRelated[$key] = NULL;
397 $mapperRelatedContactType[$key] = NULL;
398 $mapperRelatedContactDetails[$key] = NULL;
399 $mapperRelatedContactLocType[$key] = NULL;
400 $mapperRelatedContactPhoneType[$key] = NULL;
404 $parser = new CRM_Contact_Import_Parser_Contact($mapperKeys, $mapperLocTypes,
405 $mapperPhoneTypes, $mapperRelated, $mapperRelatedContactType,
406 $mapperRelatedContactDetails, $mapperRelatedContactLocType,
407 $mapperRelatedContactPhoneType
410 $mapFields = $this->get('fields');
412 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
413 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
415 foreach ($mapper as $key => $value) {
417 list($id, $first, $second) = explode('_', $mapper[$key][0]);
418 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
419 $relationType = new CRM_Contact_DAO_RelationshipType();
420 $relationType->id
= $id;
421 $relationType->find(TRUE);
423 $header[] = $relationType->name_a_b
;
424 $header[] = ucwords(str_replace("_", " ", $mapper[$key][1]));
426 if (isset($mapper[$key][2])) {
427 $header[] = $locationTypes[$mapper[$key][2]];
429 if (isset($mapper[$key][3])) {
430 $header[] = $phoneTypes[$mapper[$key][3]];
434 if (isset($mapFields[$mapper[$key][0]])) {
435 $header[] = $mapFields[$mapper[$key][0]];
436 if (isset($mapper[$key][1])) {
437 $header[] = $locationTypes[$mapper[$key][1]];
439 if (isset($mapper[$key][2])) {
440 $header[] = $phoneTypes[$mapper[$key][2]];
444 $mapperFields[] = implode(' - ', $header);
447 $tableName = $this->get('importTableName');
448 //print "Running parser on table: $tableName<br/>";
449 $parser->run($tableName, $mapperFields,
450 CRM_Import_Parser
::MODE_IMPORT
,
451 $this->get('contactType'),
452 $this->get('primaryKeyName'),
453 $this->get('statusFieldName'),
455 $this->get('statusID'),
456 $this->get('totalRowCount'),
458 CRM_Contact_Import_Parser
::DEFAULT_TIMEOUT
,
459 $this->get('contactSubType'),
463 // add the new contacts to selected groups
464 $contactIds = &$parser->getImportedContacts();
466 // add the new related contacts to selected groups
467 $relatedContactIds = &$parser->getRelatedImportedContacts();
469 $this->set('relatedCount', count($relatedContactIds));
472 //changed below if-statement "if ($newGroup) {" to "if ($newGroupName) {"
474 /* Create a new group */
477 'name' => $newGroupName,
478 'title' => $newGroupName,
479 'description' => $newGroupDesc,
482 $group = CRM_Contact_BAO_Group
::create($gParams);
483 $groups[] = $newGroupId = $group->id
;
486 if (is_array($groups)) {
487 $groupAdditions = array();
488 foreach ($groups as $groupId) {
489 $addCount = CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIds, $groupId);
490 if (!empty($relatedContactIds)) {
491 $addRelCount = CRM_Contact_BAO_GroupContact
::addContactsToGroup($relatedContactIds, $groupId);
493 $totalCount = $addCount[1] +
$addRelCount[1];
494 if ($groupId == $newGroupId) {
495 $name = $newGroupName;
499 $name = $allGroups[$groupId];
502 $groupAdditions[] = array(
503 'url' => CRM_Utils_System
::url('civicrm/group/search',
504 'reset=1&force=1&context=smog&gid=' . $groupId
507 'added' => $totalCount,
508 'notAdded' => $addCount[2] +
$addRelCount[2],
512 $this->set('groupAdditions', $groupAdditions);
517 /* Create a new Tag */
520 'name' => $newTagName,
521 'title' => $newTagName,
522 'description' => $newTagDesc,
526 $addedTag = CRM_Core_BAO_Tag
::add($tagParams, $id);
527 $tag[$addedTag->id
] = 1;
531 if (is_array($tag)) {
533 $tagAdditions = array();
534 foreach ($tag as $tagId => $val) {
535 $addTagCount = CRM_Core_BAO_EntityTag
::addContactsToTag($contactIds, $tagId);
536 if (!empty($relatedContactIds)) {
537 $addRelTagCount = CRM_Core_BAO_EntityTag
::addContactsToTag($relatedContactIds, $tagId);
539 $totalTagCount = $addTagCount[1] +
$addRelTagCount[1];
540 if ($tagId == $addedTag->id
) {
541 $tagName = $newTagName;
545 $tagName = $allTags[$tagId];
548 $tagAdditions[] = array(
549 'url' => CRM_Utils_System
::url('civicrm/contact/search',
550 'reset=1&force=1&context=smog&id=' . $tagId
553 'added' => $totalTagCount,
554 'notAdded' => $addTagCount[2] +
$addRelTagCount[2],
558 $this->set('tagAdditions', $tagAdditions);
561 // add all the necessary variables to the form
562 $parser->set($this, CRM_Import_Parser
::MODE_IMPORT
);
564 // check if there is any error occured
566 $errorStack = CRM_Core_Error
::singleton();
567 $errors = $errorStack->getErrors();
568 $errorMessage = array();
570 if (is_array($errors)) {
571 foreach ($errors as $key => $value) {
572 $errorMessage[] = $value['message'];
575 // there is no fileName since this is a sql import
577 $config = CRM_Core_Config
::singleton();
578 $errorFile = $config->uploadDir
. "sqlImport.error.log";
579 if ($fd = fopen($errorFile, 'w')) {
580 fwrite($fd, implode('\n', $errorMessage));
584 $this->set('errorFile', $errorFile);
586 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contact_Import_Parser';
587 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlparams));
589 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. '&parser=CRM_Contact_Import_Parser';
590 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
592 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. '&parser=CRM_Contact_Import_Parser';
593 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));