Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class previews the uploaded file and returns summary | |
38 | * statistics | |
39 | */ | |
f532671f | 40 | class CRM_Contact_Import_Form_Preview extends CRM_Import_Form_Preview { |
6a488035 TO |
41 | |
42 | /** | |
100fef9d | 43 | * Set variables up before form is built |
6a488035 TO |
44 | * |
45 | * @return void | |
6a488035 TO |
46 | */ |
47 | public function preProcess() { | |
48 | //get the data from the session | |
353ffa53 TO |
49 | $dataValues = $this->get('dataValues'); |
50 | $mapper = $this->get('mapper'); | |
51 | $invalidRowCount = $this->get('invalidRowCount'); | |
6a488035 | 52 | $conflictRowCount = $this->get('conflictRowCount'); |
353ffa53 TO |
53 | $mismatchCount = $this->get('unMatchCount'); |
54 | $columnNames = $this->get('columnNames'); | |
6a488035 TO |
55 | |
56 | //assign column names | |
57 | $this->assign('columnNames', $columnNames); | |
58 | ||
59 | //get the mapping name displayed if the mappingId is set | |
60 | $mappingId = $this->get('loadMappingId'); | |
61 | if ($mappingId) { | |
62 | $mapDAO = new CRM_Core_DAO_Mapping(); | |
63 | $mapDAO->id = $mappingId; | |
64 | $mapDAO->find(TRUE); | |
65 | $this->assign('loadedMapping', $mappingId); | |
66 | $this->assign('savedName', $mapDAO->name); | |
67 | } | |
68 | ||
69 | $this->assign('rowDisplayCount', 2); | |
70 | ||
24431f7b | 71 | $groups = CRM_Core_PseudoConstant::nestedGroup(); |
6a488035 TO |
72 | $this->set('groups', $groups); |
73 | ||
cd43c5e3 | 74 | $tag = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE)); |
6a488035 TO |
75 | if ($tag) { |
76 | $this->set('tag', $tag); | |
77 | } | |
78 | ||
79 | if ($invalidRowCount) { | |
a05662ef | 80 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contact_Import_Parser'; |
6a488035 TO |
81 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
82 | } | |
83 | ||
84 | if ($conflictRowCount) { | |
a05662ef | 85 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contact_Import_Parser'; |
6a488035 TO |
86 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
87 | } | |
88 | ||
89 | if ($mismatchCount) { | |
a05662ef | 90 | $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contact_Import_Parser'; |
6a488035 TO |
91 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
92 | } | |
93 | ||
94 | $properties = array( | |
353ffa53 TO |
95 | 'mapper', |
96 | 'locations', | |
97 | 'phones', | |
98 | 'ims', | |
99 | 'dataValues', | |
100 | 'columnCount', | |
101 | 'totalRowCount', | |
102 | 'validRowCount', | |
103 | 'invalidRowCount', | |
104 | 'conflictRowCount', | |
6a488035 TO |
105 | 'downloadErrorRecordsUrl', |
106 | 'downloadConflictRecordsUrl', | |
107 | 'downloadMismatchRecordsUrl', | |
353ffa53 TO |
108 | 'related', |
109 | 'relatedContactDetails', | |
110 | 'relatedContactLocType', | |
111 | 'relatedContactPhoneType', | |
112 | 'relatedContactImProvider', | |
113 | 'websites', | |
6a488035 TO |
114 | 'relatedContactWebsiteType', |
115 | ); | |
116 | ||
117 | foreach ($properties as $property) { | |
118 | $this->assign($property, $this->get($property)); | |
119 | } | |
120 | ||
121 | $statusID = $this->get('statusID'); | |
122 | if (!$statusID) { | |
123 | $statusID = md5(uniqid(rand(), TRUE)); | |
124 | $this->set('statusID', $statusID); | |
125 | } | |
126 | $statusUrl = CRM_Utils_System::url('civicrm/ajax/status', "id={$statusID}", FALSE, NULL, FALSE); | |
127 | $this->assign('statusUrl', $statusUrl); | |
128 | ||
129 | $showColNames = TRUE; | |
130 | if ('CRM_Import_DataSource_CSV' == $this->get('dataSource') && | |
131 | !$this->get('skipColumnHeader') | |
132 | ) { | |
133 | $showColNames = FALSE; | |
134 | } | |
135 | $this->assign('showColNames', $showColNames); | |
136 | } | |
137 | ||
138 | /** | |
c490a46a | 139 | * Build the form object |
6a488035 | 140 | * |
355ba699 | 141 | * @return void |
6a488035 TO |
142 | */ |
143 | public function buildQuickForm() { | |
144 | $this->addElement('text', 'newGroupName', ts('Name for new group')); | |
145 | $this->addElement('text', 'newGroupDesc', ts('Description of new group')); | |
146 | ||
147 | $groups = $this->get('groups'); | |
148 | ||
149 | if (!empty($groups)) { | |
353ffa53 TO |
150 | $this->addElement('select', 'groups', ts('Add imported records to existing group(s)'), $groups, array( |
151 | 'multiple' => "multiple", | |
152 | 'class' => 'crm-select2' | |
153 | )); | |
6a488035 TO |
154 | } |
155 | ||
156 | //display new tag | |
157 | $this->addElement('text', 'newTagName', ts('Tag')); | |
158 | $this->addElement('text', 'newTagDesc', ts('Description')); | |
159 | ||
160 | $tag = $this->get('tag'); | |
161 | if (!empty($tag)) { | |
162 | foreach ($tag as $tagID => $tagName) { | |
163 | $this->addElement('checkbox', "tag[$tagID]", NULL, $tagName); | |
164 | } | |
165 | } | |
166 | ||
167 | $path = "_qf_MapField_display=true"; | |
168 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form); | |
169 | if (CRM_Utils_Rule::qfKey($qfKey)) { | |
170 | $path .= "&qfKey=$qfKey"; | |
171 | } | |
172 | ||
173 | $previousURL = CRM_Utils_System::url('civicrm/import/contact', $path, FALSE, NULL, FALSE); | |
174 | $cancelURL = CRM_Utils_System::url('civicrm/import/contact', 'reset=1'); | |
175 | ||
176 | $buttons = array( | |
177 | array( | |
178 | 'type' => 'back', | |
f212d37d | 179 | 'name' => ts('Previous'), |
6a488035 TO |
180 | 'js' => array('onclick' => "location.href='{$previousURL}'; return false;"), |
181 | ), | |
182 | array( | |
183 | 'type' => 'next', | |
f212d37d | 184 | 'name' => ts('Import Now'), |
6a488035 TO |
185 | 'spacing' => ' ', |
186 | 'isDefault' => TRUE, | |
187 | 'js' => array('onclick' => "return verify( );"), | |
188 | ), | |
189 | array( | |
190 | 'type' => 'cancel', | |
191 | 'name' => ts('Cancel'), | |
192 | 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"), | |
193 | ), | |
194 | ); | |
195 | ||
196 | $this->addButtons($buttons); | |
197 | ||
719a6fec | 198 | $this->addFormRule(array('CRM_Contact_Import_Form_Preview', 'formRule'), $this); |
6a488035 TO |
199 | } |
200 | ||
201 | /** | |
100fef9d | 202 | * Global validation rules for the form |
6a488035 | 203 | * |
77c5b619 TO |
204 | * @param array $fields |
205 | * Posted values of the form. | |
6a488035 | 206 | * |
da6b46f4 EM |
207 | * @param $files |
208 | * @param $self | |
209 | * | |
a6c01b45 CW |
210 | * @return array |
211 | * list of errors to be posted back to the form | |
6a488035 | 212 | * @static |
6a488035 | 213 | */ |
00be9182 | 214 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
215 | $errors = array(); |
216 | $invalidTagName = $invalidGroupName = FALSE; | |
217 | ||
a7488080 | 218 | if (!empty($fields['newTagName'])) { |
6a488035 | 219 | if (!CRM_Utils_Rule::objectExists(trim($fields['newTagName']), |
353ffa53 TO |
220 | array('CRM_Core_DAO_Tag') |
221 | ) | |
222 | ) { | |
6a488035 TO |
223 | $errors['newTagName'] = ts('Tag \'%1\' already exists.', |
224 | array(1 => $fields['newTagName']) | |
225 | ); | |
226 | $invalidTagName = TRUE; | |
227 | } | |
228 | } | |
229 | ||
a7488080 | 230 | if (!empty($fields['newGroupName'])) { |
353ffa53 TO |
231 | $title = trim($fields['newGroupName']); |
232 | $name = CRM_Utils_String::titleToVar($title); | |
233 | $query = 'select count(*) from civicrm_group where name like %1 OR title like %2'; | |
0e6e8724 DL |
234 | $grpCnt = CRM_Core_DAO::singleValueQuery( |
235 | $query, | |
236 | array( | |
237 | 1 => array($name, 'String'), | |
6a488035 | 238 | 2 => array($title, 'String'), |
0e6e8724 DL |
239 | ) |
240 | ); | |
6a488035 TO |
241 | if ($grpCnt) { |
242 | $invalidGroupName = TRUE; | |
243 | $errors['newGroupName'] = ts('Group \'%1\' already exists.', array(1 => $fields['newGroupName'])); | |
244 | } | |
245 | } | |
246 | ||
247 | $self->assign('invalidTagName', $invalidTagName); | |
248 | $self->assign('invalidGroupName', $invalidGroupName); | |
249 | ||
250 | return empty($errors) ? TRUE : $errors; | |
251 | } | |
252 | ||
6a488035 TO |
253 | /** |
254 | * Process the mapped fields and map it into the uploaded file | |
255 | * preview the file and extract some summary statistics | |
256 | * | |
257 | * @return void | |
6a488035 TO |
258 | */ |
259 | public function postProcess() { | |
260 | ||
261 | $importJobParams = array( | |
262 | 'doGeocodeAddress' => $this->controller->exportValue('DataSource', 'doGeocodeAddress'), | |
263 | 'invalidRowCount' => $this->get('invalidRowCount'), | |
264 | 'conflictRowCount' => $this->get('conflictRowCount'), | |
265 | 'onDuplicate' => $this->get('onDuplicate'), | |
266 | 'dedupe' => $this->get('dedupe'), | |
267 | 'newGroupName' => $this->controller->exportValue($this->_name, 'newGroupName'), | |
268 | 'newGroupDesc' => $this->controller->exportValue($this->_name, 'newGroupDesc'), | |
269 | 'groups' => $this->controller->exportValue($this->_name, 'groups'), | |
270 | 'allGroups' => $this->get('groups'), | |
271 | 'newTagName' => $this->controller->exportValue($this->_name, 'newTagName'), | |
272 | 'newTagDesc' => $this->controller->exportValue($this->_name, 'newTagDesc'), | |
273 | 'tag' => $this->controller->exportValue($this->_name, 'tag'), | |
274 | 'allTags' => $this->get('tag'), | |
275 | 'mapper' => $this->controller->exportValue('MapField', 'mapper'), | |
276 | 'mapFields' => $this->get('fields'), | |
277 | 'contactType' => $this->get('contactType'), | |
278 | 'contactSubType' => $this->get('contactSubType'), | |
279 | 'primaryKeyName' => $this->get('primaryKeyName'), | |
280 | 'statusFieldName' => $this->get('statusFieldName'), | |
281 | 'statusID' => $this->get('statusID'), | |
282 | 'totalRowCount' => $this->get('totalRowCount'), | |
283 | ); | |
284 | ||
285 | $tableName = $this->get('importTableName'); | |
719a6fec | 286 | $importJob = new CRM_Contact_Import_ImportJob($tableName); |
6a488035 TO |
287 | $importJob->setJobParams($importJobParams); |
288 | ||
289 | // If ACL applies to the current user, update cache before running the import. | |
290 | if (!CRM_Core_Permission::check('view all contacts')) { | |
291 | $session = CRM_Core_Session::singleton(); | |
292 | $userID = $session->get('userID'); | |
293 | CRM_ACL_BAO_Cache::updateEntry($userID); | |
294 | } | |
295 | ||
296 | // run the import | |
297 | $importJob->runImport($this); | |
298 | ||
299 | // update cache after we done with runImport | |
300 | if (!CRM_Core_Permission::check('view all contacts')) { | |
301 | CRM_ACL_BAO_Cache::updateEntry($userID); | |
302 | } | |
303 | ||
304 | // clear all caches | |
305 | CRM_Contact_BAO_Contact_Utils::clearContactCaches(); | |
306 | ||
307 | // add all the necessary variables to the form | |
308 | $importJob->setFormVariables($this); | |
309 | ||
310 | // check if there is any error occured | |
311 | $errorStack = CRM_Core_Error::singleton(); | |
312 | $errors = $errorStack->getErrors(); | |
313 | $errorMessage = array(); | |
314 | ||
315 | if (is_array($errors)) { | |
316 | foreach ($errors as $key => $value) { | |
317 | $errorMessage[] = $value['message']; | |
318 | } | |
319 | ||
320 | // there is no fileName since this is a sql import | |
321 | // so fudge it | |
322 | $config = CRM_Core_Config::singleton(); | |
323 | $errorFile = $config->uploadDir . "sqlImport.error.log"; | |
324 | if ($fd = fopen($errorFile, 'w')) { | |
325 | fwrite($fd, implode('\n', $errorMessage)); | |
326 | } | |
327 | fclose($fd); | |
328 | ||
329 | $this->set('errorFile', $errorFile); | |
330 | ||
a05662ef | 331 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contact_Import_Parser'; |
6a488035 TO |
332 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
333 | ||
a05662ef | 334 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contact_Import_Parser'; |
6a488035 TO |
335 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
336 | ||
a05662ef | 337 | $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contact_Import_Parser'; |
6a488035 TO |
338 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
339 | } | |
340 | ||
341 | //hack to clean db | |
342 | //if job complete drop table. | |
343 | $importJob->isComplete(TRUE); | |
344 | } | |
345 | ||
346 | /** | |
347 | * Process the mapped fields and map it into the uploaded file | |
348 | * preview the file and extract some summary statistics | |
349 | * | |
350 | * @return void | |
6a488035 TO |
351 | */ |
352 | public function postProcessOld() { | |
353 | ||
354 | $doGeocodeAddress = $this->controller->exportValue('DataSource', 'doGeocodeAddress'); | |
353ffa53 | 355 | $invalidRowCount = $this->get('invalidRowCount'); |
6a488035 | 356 | $conflictRowCount = $this->get('conflictRowCount'); |
353ffa53 TO |
357 | $onDuplicate = $this->get('onDuplicate'); |
358 | $newGroupName = $this->controller->exportValue($this->_name, 'newGroupName'); | |
359 | $newGroupDesc = $this->controller->exportValue($this->_name, 'newGroupDesc'); | |
360 | $groups = $this->controller->exportValue($this->_name, 'groups'); | |
361 | $allGroups = $this->get('groups'); | |
362 | $newTagName = $this->controller->exportValue($this->_name, 'newTagName'); | |
363 | $newTagDesc = $this->controller->exportValue($this->_name, 'newTagDesc'); | |
364 | $tag = $this->controller->exportValue($this->_name, 'tag'); | |
365 | $allTags = $this->get('tag'); | |
6a488035 TO |
366 | |
367 | $mapper = $this->controller->exportValue('MapField', 'mapper'); | |
368 | ||
369 | $mapperKeys = array(); | |
370 | $mapperLocTypes = array(); | |
371 | $mapperPhoneTypes = array(); | |
372 | $mapperRelated = array(); | |
373 | $mapperRelatedContactType = array(); | |
374 | $mapperRelatedContactDetails = array(); | |
375 | $mapperRelatedContactLocType = array(); | |
376 | $mapperRelatedContactPhoneType = array(); | |
377 | ||
378 | foreach ($mapper as $key => $value) { | |
379 | $mapperKeys[$key] = $mapper[$key][0]; | |
380 | if (is_numeric($mapper[$key][1])) { | |
381 | $mapperLocTypes[$key] = $mapper[$key][1]; | |
382 | } | |
383 | else { | |
384 | $mapperLocTypes[$key] = NULL; | |
385 | } | |
386 | ||
387 | if (CRM_Utils_Array::value($key, $mapperKeys) == 'phone') { | |
388 | $mapperPhoneTypes[$key] = $mapper[$key][2]; | |
389 | } | |
390 | else { | |
391 | $mapperPhoneTypes[$key] = NULL; | |
392 | } | |
393 | ||
394 | list($id, $first, $second) = explode('_', $mapper[$key][0]); | |
395 | if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) { | |
396 | $relationType = new CRM_Contact_DAO_RelationshipType(); | |
397 | $relationType->id = $id; | |
398 | $relationType->find(TRUE); | |
0e6e8724 DL |
399 | $fieldName = "contact_type_$second"; |
400 | $mapperRelatedContactType[$key] = $relationType->$fieldName; | |
6a488035 TO |
401 | $mapperRelated[$key] = $mapper[$key][0]; |
402 | $mapperRelatedContactDetails[$key] = $mapper[$key][1]; | |
403 | $mapperRelatedContactLocType[$key] = $mapper[$key][2]; | |
404 | $mapperRelatedContactPhoneType[$key] = $mapper[$key][3]; | |
405 | } | |
406 | else { | |
407 | $mapperRelated[$key] = NULL; | |
408 | $mapperRelatedContactType[$key] = NULL; | |
409 | $mapperRelatedContactDetails[$key] = NULL; | |
410 | $mapperRelatedContactLocType[$key] = NULL; | |
411 | $mapperRelatedContactPhoneType[$key] = NULL; | |
412 | } | |
413 | } | |
414 | ||
719a6fec | 415 | $parser = new CRM_Contact_Import_Parser_Contact($mapperKeys, $mapperLocTypes, |
6a488035 TO |
416 | $mapperPhoneTypes, $mapperRelated, $mapperRelatedContactType, |
417 | $mapperRelatedContactDetails, $mapperRelatedContactLocType, | |
418 | $mapperRelatedContactPhoneType | |
419 | ); | |
420 | ||
421 | $mapFields = $this->get('fields'); | |
422 | ||
b2b0530a | 423 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
b4f964d9 | 424 | $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); |
6a488035 TO |
425 | |
426 | foreach ($mapper as $key => $value) { | |
427 | $header = array(); | |
428 | list($id, $first, $second) = explode('_', $mapper[$key][0]); | |
429 | if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) { | |
430 | $relationType = new CRM_Contact_DAO_RelationshipType(); | |
431 | $relationType->id = $id; | |
432 | $relationType->find(TRUE); | |
433 | ||
434 | $header[] = $relationType->name_a_b; | |
435 | $header[] = ucwords(str_replace("_", " ", $mapper[$key][1])); | |
436 | ||
437 | if (isset($mapper[$key][2])) { | |
438 | $header[] = $locationTypes[$mapper[$key][2]]; | |
439 | } | |
440 | if (isset($mapper[$key][3])) { | |
441 | $header[] = $phoneTypes[$mapper[$key][3]]; | |
442 | } | |
443 | } | |
444 | else { | |
445 | if (isset($mapFields[$mapper[$key][0]])) { | |
446 | $header[] = $mapFields[$mapper[$key][0]]; | |
447 | if (isset($mapper[$key][1])) { | |
448 | $header[] = $locationTypes[$mapper[$key][1]]; | |
449 | } | |
450 | if (isset($mapper[$key][2])) { | |
451 | $header[] = $phoneTypes[$mapper[$key][2]]; | |
452 | } | |
453 | } | |
454 | } | |
455 | $mapperFields[] = implode(' - ', $header); | |
456 | } | |
457 | ||
458 | $tableName = $this->get('importTableName'); | |
459 | //print "Running parser on table: $tableName<br/>"; | |
460 | $parser->run($tableName, $mapperFields, | |
a05662ef | 461 | CRM_Import_Parser::MODE_IMPORT, |
6a488035 TO |
462 | $this->get('contactType'), |
463 | $this->get('primaryKeyName'), | |
464 | $this->get('statusFieldName'), | |
465 | $onDuplicate, | |
466 | $this->get('statusID'), | |
467 | $this->get('totalRowCount'), | |
468 | $doGeocodeAddress, | |
719a6fec | 469 | CRM_Contact_Import_Parser::DEFAULT_TIMEOUT, |
6a488035 TO |
470 | $this->get('contactSubType'), |
471 | $this->get('dedupe') | |
472 | ); | |
473 | ||
474 | // add the new contacts to selected groups | |
475 | $contactIds = &$parser->getImportedContacts(); | |
476 | ||
477 | // add the new related contacts to selected groups | |
478 | $relatedContactIds = &$parser->getRelatedImportedContacts(); | |
479 | ||
480 | $this->set('relatedCount', count($relatedContactIds)); | |
481 | $newGroupId = NULL; | |
482 | ||
483 | //changed below if-statement "if ($newGroup) {" to "if ($newGroupName) {" | |
484 | if ($newGroupName) { | |
485 | /* Create a new group */ | |
486 | ||
487 | $gParams = array( | |
488 | 'name' => $newGroupName, | |
489 | 'title' => $newGroupName, | |
490 | 'description' => $newGroupDesc, | |
491 | 'is_active' => TRUE, | |
492 | ); | |
493 | $group = CRM_Contact_BAO_Group::create($gParams); | |
494 | $groups[] = $newGroupId = $group->id; | |
495 | } | |
496 | ||
497 | if (is_array($groups)) { | |
498 | $groupAdditions = array(); | |
499 | foreach ($groups as $groupId) { | |
500 | $addCount = CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId); | |
501 | if (!empty($relatedContactIds)) { | |
502 | $addRelCount = CRM_Contact_BAO_GroupContact::addContactsToGroup($relatedContactIds, $groupId); | |
503 | } | |
504 | $totalCount = $addCount[1] + $addRelCount[1]; | |
505 | if ($groupId == $newGroupId) { | |
506 | $name = $newGroupName; | |
507 | $new = TRUE; | |
508 | } | |
509 | else { | |
510 | $name = $allGroups[$groupId]; | |
511 | $new = FALSE; | |
512 | } | |
513 | $groupAdditions[] = array( | |
514 | 'url' => CRM_Utils_System::url('civicrm/group/search', | |
515 | 'reset=1&force=1&context=smog&gid=' . $groupId | |
516 | ), | |
517 | 'name' => $name, | |
518 | 'added' => $totalCount, | |
519 | 'notAdded' => $addCount[2] + $addRelCount[2], | |
520 | 'new' => $new, | |
521 | ); | |
522 | } | |
523 | $this->set('groupAdditions', $groupAdditions); | |
524 | } | |
525 | ||
526 | $newTagId = NULL; | |
527 | if ($newTagName) { | |
528 | /* Create a new Tag */ | |
529 | ||
530 | $tagParams = array( | |
531 | 'name' => $newTagName, | |
532 | 'title' => $newTagName, | |
533 | 'description' => $newTagDesc, | |
534 | 'is_active' => TRUE, | |
535 | ); | |
353ffa53 TO |
536 | $id = array(); |
537 | $addedTag = CRM_Core_BAO_Tag::add($tagParams, $id); | |
6a488035 TO |
538 | $tag[$addedTag->id] = 1; |
539 | } | |
540 | //add Tag to Import | |
541 | ||
542 | if (is_array($tag)) { | |
543 | ||
544 | $tagAdditions = array(); | |
545 | foreach ($tag as $tagId => $val) { | |
546 | $addTagCount = CRM_Core_BAO_EntityTag::addContactsToTag($contactIds, $tagId); | |
547 | if (!empty($relatedContactIds)) { | |
548 | $addRelTagCount = CRM_Core_BAO_EntityTag::addContactsToTag($relatedContactIds, $tagId); | |
549 | } | |
550 | $totalTagCount = $addTagCount[1] + $addRelTagCount[1]; | |
551 | if ($tagId == $addedTag->id) { | |
552 | $tagName = $newTagName; | |
553 | $new = TRUE; | |
554 | } | |
555 | else { | |
556 | $tagName = $allTags[$tagId]; | |
557 | $new = FALSE; | |
558 | } | |
559 | $tagAdditions[] = array( | |
560 | 'url' => CRM_Utils_System::url('civicrm/contact/search', | |
561 | 'reset=1&force=1&context=smog&id=' . $tagId | |
562 | ), | |
563 | 'name' => $tagName, | |
564 | 'added' => $totalTagCount, | |
565 | 'notAdded' => $addTagCount[2] + $addRelTagCount[2], | |
566 | 'new' => $new, | |
567 | ); | |
568 | } | |
569 | $this->set('tagAdditions', $tagAdditions); | |
570 | } | |
571 | ||
572 | // add all the necessary variables to the form | |
a05662ef | 573 | $parser->set($this, CRM_Import_Parser::MODE_IMPORT); |
6a488035 TO |
574 | |
575 | // check if there is any error occured | |
576 | ||
577 | $errorStack = CRM_Core_Error::singleton(); | |
578 | $errors = $errorStack->getErrors(); | |
579 | $errorMessage = array(); | |
580 | ||
581 | if (is_array($errors)) { | |
582 | foreach ($errors as $key => $value) { | |
583 | $errorMessage[] = $value['message']; | |
584 | } | |
585 | ||
586 | // there is no fileName since this is a sql import | |
587 | // so fudge it | |
588 | $config = CRM_Core_Config::singleton(); | |
589 | $errorFile = $config->uploadDir . "sqlImport.error.log"; | |
590 | if ($fd = fopen($errorFile, 'w')) { | |
591 | fwrite($fd, implode('\n', $errorMessage)); | |
592 | } | |
593 | fclose($fd); | |
594 | ||
595 | $this->set('errorFile', $errorFile); | |
596 | ||
a05662ef | 597 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contact_Import_Parser'; |
6a488035 TO |
598 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlparams)); |
599 | ||
a05662ef | 600 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contact_Import_Parser'; |
6a488035 TO |
601 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
602 | ||
a05662ef | 603 | $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contact_Import_Parser'; |
6a488035 TO |
604 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
605 | } | |
606 | } | |
607 | } |