commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Contact / Import / Form / Preview.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * This class previews the uploaded file and returns summary
38 * statistics
39 */
40 class CRM_Contact_Import_Form_Preview extends CRM_Import_Form_Preview {
41
42 /**
43 * Whether USPS validation should be disabled during import.
44 *
45 * @var bool
46 */
47 protected $_disableUSPS;
48
49 /**
50 * Set variables up before form is built.
51 *
52 * @return void
53 */
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');
63
64 //assign column names
65 $this->assign('columnNames', $columnNames);
66
67 //get the mapping name displayed if the mappingId is set
68 $mappingId = $this->get('loadMappingId');
69 if ($mappingId) {
70 $mapDAO = new CRM_Core_DAO_Mapping();
71 $mapDAO->id = $mappingId;
72 $mapDAO->find(TRUE);
73 $this->assign('loadedMapping', $mappingId);
74 $this->assign('savedName', $mapDAO->name);
75 }
76
77 $this->assign('rowDisplayCount', 2);
78
79 $groups = CRM_Core_PseudoConstant::nestedGroup();
80 $this->set('groups', $groups);
81
82 $tag = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
83 if ($tag) {
84 $this->set('tag', $tag);
85 }
86
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));
90 }
91
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));
95 }
96
97 if ($mismatchCount) {
98 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contact_Import_Parser';
99 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
100 }
101
102 $properties = array(
103 'mapper',
104 'locations',
105 'phones',
106 'ims',
107 'dataValues',
108 'columnCount',
109 'totalRowCount',
110 'validRowCount',
111 'invalidRowCount',
112 'conflictRowCount',
113 'downloadErrorRecordsUrl',
114 'downloadConflictRecordsUrl',
115 'downloadMismatchRecordsUrl',
116 'related',
117 'relatedContactDetails',
118 'relatedContactLocType',
119 'relatedContactPhoneType',
120 'relatedContactImProvider',
121 'websites',
122 'relatedContactWebsiteType',
123 );
124
125 foreach ($properties as $property) {
126 $this->assign($property, $this->get($property));
127 }
128
129 $statusID = $this->get('statusID');
130 if (!$statusID) {
131 $statusID = md5(uniqid(rand(), TRUE));
132 $this->set('statusID', $statusID);
133 }
134 $statusUrl = CRM_Utils_System::url('civicrm/ajax/status', "id={$statusID}", FALSE, NULL, FALSE);
135 $this->assign('statusUrl', $statusUrl);
136
137 $showColNames = TRUE;
138 if ('CRM_Import_DataSource_CSV' == $this->get('dataSource') &&
139 !$this->get('skipColumnHeader')
140 ) {
141 $showColNames = FALSE;
142 }
143 $this->assign('showColNames', $showColNames);
144 }
145
146 /**
147 * Build the form object.
148 *
149 * @return void
150 */
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',
157 ts('Group Type'),
158 $groupTypes,
159 NULL, NULL, NULL, NULL, '&nbsp;&nbsp;&nbsp;'
160 );
161 }
162
163 $groups = $this->get('groups');
164
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',
169 ));
170 }
171
172 //display new tag
173 $this->addElement('text', 'newTagName', ts('Tag'));
174 $this->addElement('text', 'newTagDesc', ts('Description'));
175
176 $tag = $this->get('tag');
177 if (!empty($tag)) {
178 foreach ($tag as $tagID => $tagName) {
179 $this->addElement('checkbox', "tag[$tagID]", NULL, $tagName);
180 }
181 }
182
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";
187 }
188
189 $previousURL = CRM_Utils_System::url('civicrm/import/contact', $path, FALSE, NULL, FALSE);
190 $cancelURL = CRM_Utils_System::url('civicrm/import/contact', 'reset=1');
191
192 $buttons = array(
193 array(
194 'type' => 'back',
195 'name' => ts('Previous'),
196 'js' => array('onclick' => "location.href='{$previousURL}'; return false;"),
197 ),
198 array(
199 'type' => 'next',
200 'name' => ts('Import Now'),
201 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
202 'isDefault' => TRUE,
203 'js' => array('onclick' => "return verify( );"),
204 ),
205 array(
206 'type' => 'cancel',
207 'name' => ts('Cancel'),
208 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"),
209 ),
210 );
211
212 $this->addButtons($buttons);
213
214 $this->addFormRule(array('CRM_Contact_Import_Form_Preview', 'formRule'), $this);
215 }
216
217 /**
218 * Global validation rules for the form.
219 *
220 * @param array $fields
221 * Posted values of the form.
222 *
223 * @param $files
224 * @param $self
225 *
226 * @return array
227 * list of errors to be posted back to the form
228 */
229 public static function formRule($fields, $files, $self) {
230 $errors = array();
231 $invalidTagName = $invalidGroupName = FALSE;
232
233 if (!empty($fields['newTagName'])) {
234 if (!CRM_Utils_Rule::objectExists(trim($fields['newTagName']),
235 array('CRM_Core_DAO_Tag')
236 )
237 ) {
238 $errors['newTagName'] = ts('Tag \'%1\' already exists.',
239 array(1 => $fields['newTagName'])
240 );
241 $invalidTagName = TRUE;
242 }
243 }
244
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(
250 $query,
251 array(
252 1 => array($name, 'String'),
253 2 => array($title, 'String'),
254 )
255 );
256 if ($grpCnt) {
257 $invalidGroupName = TRUE;
258 $errors['newGroupName'] = ts('Group \'%1\' already exists.', array(1 => $fields['newGroupName']));
259 }
260 }
261
262 $self->assign('invalidTagName', $invalidTagName);
263 $self->assign('invalidGroupName', $invalidGroupName);
264
265 return empty($errors) ? TRUE : $errors;
266 }
267
268 /**
269 * Process the mapped fields and map it into the uploaded file
270 * preview the file and extract some summary statistics
271 *
272 * @return void
273 */
274 public function postProcess() {
275
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'),
299 );
300
301 $tableName = $this->get('importTableName');
302 $importJob = new CRM_Contact_Import_ImportJob($tableName);
303 $importJob->setJobParams($importJobParams);
304
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);
310 }
311
312 CRM_Utils_Address_USPS::disable($this->_disableUSPS);
313
314 // run the import
315 $importJob->runImport($this);
316
317 // update cache after we done with runImport
318 if (!CRM_Core_Permission::check('view all contacts')) {
319 CRM_ACL_BAO_Cache::updateEntry($userID);
320 }
321
322 // clear all caches
323 CRM_Contact_BAO_Contact_Utils::clearContactCaches();
324
325 // add all the necessary variables to the form
326 $importJob->setFormVariables($this);
327
328 // check if there is any error occured
329 $errorStack = CRM_Core_Error::singleton();
330 $errors = $errorStack->getErrors();
331 $errorMessage = array();
332
333 if (is_array($errors)) {
334 foreach ($errors as $key => $value) {
335 $errorMessage[] = $value['message'];
336 }
337
338 // there is no fileName since this is a sql import
339 // so fudge it
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));
344 }
345 fclose($fd);
346
347 $this->set('errorFile', $errorFile);
348
349 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contact_Import_Parser';
350 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
351
352 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contact_Import_Parser';
353 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
354
355 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contact_Import_Parser';
356 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
357 }
358
359 //hack to clean db
360 //if job complete drop table.
361 $importJob->isComplete(TRUE);
362 }
363
364 /**
365 * Process the mapped fields and map it into the uploaded file
366 * preview the file and extract some summary statistics
367 *
368 * @return void
369 */
370 public function postProcessOld() {
371
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');
385
386 $mapper = $this->controller->exportValue('MapField', 'mapper');
387
388 $mapperKeys = array();
389 $mapperLocTypes = array();
390 $mapperPhoneTypes = array();
391 $mapperRelated = array();
392 $mapperRelatedContactType = array();
393 $mapperRelatedContactDetails = array();
394 $mapperRelatedContactLocType = array();
395 $mapperRelatedContactPhoneType = array();
396
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];
401 }
402 else {
403 $mapperLocTypes[$key] = NULL;
404 }
405
406 if (CRM_Utils_Array::value($key, $mapperKeys) == 'phone') {
407 $mapperPhoneTypes[$key] = $mapper[$key][2];
408 }
409 else {
410 $mapperPhoneTypes[$key] = NULL;
411 }
412
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];
424 }
425 else {
426 $mapperRelated[$key] = NULL;
427 $mapperRelatedContactType[$key] = NULL;
428 $mapperRelatedContactDetails[$key] = NULL;
429 $mapperRelatedContactLocType[$key] = NULL;
430 $mapperRelatedContactPhoneType[$key] = NULL;
431 }
432 }
433
434 $parser = new CRM_Contact_Import_Parser_Contact($mapperKeys, $mapperLocTypes,
435 $mapperPhoneTypes, $mapperRelated, $mapperRelatedContactType,
436 $mapperRelatedContactDetails, $mapperRelatedContactLocType,
437 $mapperRelatedContactPhoneType
438 );
439
440 $mapFields = $this->get('fields');
441
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');
444
445 foreach ($mapper as $key => $value) {
446 $header = array();
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);
452
453 $header[] = $relationType->name_a_b;
454 $header[] = ucwords(str_replace("_", " ", $mapper[$key][1]));
455
456 if (isset($mapper[$key][2])) {
457 $header[] = $locationTypes[$mapper[$key][2]];
458 }
459 if (isset($mapper[$key][3])) {
460 $header[] = $phoneTypes[$mapper[$key][3]];
461 }
462 }
463 else {
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]];
468 }
469 if (isset($mapper[$key][2])) {
470 $header[] = $phoneTypes[$mapper[$key][2]];
471 }
472 }
473 }
474 $mapperFields[] = implode(' - ', $header);
475 }
476
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'),
484 $onDuplicate,
485 $this->get('statusID'),
486 $this->get('totalRowCount'),
487 $doGeocodeAddress,
488 CRM_Contact_Import_Parser::DEFAULT_TIMEOUT,
489 $this->get('contactSubType'),
490 $this->get('dedupe')
491 );
492
493 // add the new contacts to selected groups
494 $contactIds = &$parser->getImportedContacts();
495
496 // add the new related contacts to selected groups
497 $relatedContactIds = &$parser->getRelatedImportedContacts();
498
499 $this->set('relatedCount', count($relatedContactIds));
500 $newGroupId = NULL;
501
502 //changed below if-statement "if ($newGroup) {" to "if ($newGroupName) {"
503 if ($newGroupName) {
504 /* Create a new group */
505
506 $gParams = array(
507 'name' => $newGroupName,
508 'title' => $newGroupName,
509 'description' => $newGroupDesc,
510 'group_type' => $newGroupType,
511 'is_active' => TRUE,
512 );
513 $group = CRM_Contact_BAO_Group::create($gParams);
514 $groups[] = $newGroupId = $group->id;
515 }
516
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);
523 }
524 $totalCount = $addCount[1] + $addRelCount[1];
525 if ($groupId == $newGroupId) {
526 $name = $newGroupName;
527 $new = TRUE;
528 }
529 else {
530 $name = $allGroups[$groupId];
531 $new = FALSE;
532 }
533 $groupAdditions[] = array(
534 'url' => CRM_Utils_System::url('civicrm/group/search',
535 'reset=1&force=1&context=smog&gid=' . $groupId
536 ),
537 'name' => $name,
538 'added' => $totalCount,
539 'notAdded' => $addCount[2] + $addRelCount[2],
540 'new' => $new,
541 );
542 }
543 $this->set('groupAdditions', $groupAdditions);
544 }
545
546 $newTagId = NULL;
547 if ($newTagName) {
548 /* Create a new Tag */
549
550 $tagParams = array(
551 'name' => $newTagName,
552 'title' => $newTagName,
553 'description' => $newTagDesc,
554 'is_active' => TRUE,
555 );
556 $id = array();
557 $addedTag = CRM_Core_BAO_Tag::add($tagParams, $id);
558 $tag[$addedTag->id] = 1;
559 }
560 //add Tag to Import
561
562 if (is_array($tag)) {
563
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);
569 }
570 $totalTagCount = $addTagCount[1] + $addRelTagCount[1];
571 if ($tagId == $addedTag->id) {
572 $tagName = $newTagName;
573 $new = TRUE;
574 }
575 else {
576 $tagName = $allTags[$tagId];
577 $new = FALSE;
578 }
579 $tagAdditions[] = array(
580 'url' => CRM_Utils_System::url('civicrm/contact/search',
581 'reset=1&force=1&context=smog&id=' . $tagId
582 ),
583 'name' => $tagName,
584 'added' => $totalTagCount,
585 'notAdded' => $addTagCount[2] + $addRelTagCount[2],
586 'new' => $new,
587 );
588 }
589 $this->set('tagAdditions', $tagAdditions);
590 }
591
592 // add all the necessary variables to the form
593 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
594
595 // check if there is any error occured
596
597 $errorStack = CRM_Core_Error::singleton();
598 $errors = $errorStack->getErrors();
599 $errorMessage = array();
600
601 if (is_array($errors)) {
602 foreach ($errors as $key => $value) {
603 $errorMessage[] = $value['message'];
604 }
605
606 // there is no fileName since this is a sql import
607 // so fudge it
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));
612 }
613 fclose($fd);
614
615 $this->set('errorFile', $errorFile);
616
617 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contact_Import_Parser';
618 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlparams));
619
620 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contact_Import_Parser';
621 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
622
623 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contact_Import_Parser';
624 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
625 }
626 }
627
628 }