version fixes
[civicrm-core.git] / CRM / Contact / Import / ImportJob.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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 2009 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 acts like a psuedo-BAO for transient import job tables
38 */
39 class CRM_Contact_Import_ImportJob {
40
41 protected $_tableName;
42 protected $_primaryKeyName;
43 protected $_statusFieldName;
44
45 protected $_doGeocodeAddress;
46 protected $_invalidRowCount;
47 protected $_conflictRowCount;
48 protected $_onDuplicate;
49 protected $_dedupe;
50 protected $_newGroupName;
51 protected $_newGroupDesc;
52 protected $_groups;
53 protected $_allGroups;
54 protected $_newTagName;
55 protected $_newTagDesc;
56 protected $_tag;
57 protected $_allTags;
58
59 protected $_mapper;
60 protected $_mapperKeys;
61 protected $_mapperLocTypes;
62 protected $_mapperPhoneTypes;
63 protected $_mapperImProviders;
64 protected $_mapperWebsiteTypes;
65 protected $_mapperRelated;
66 protected $_mapperRelatedContactType;
67 protected $_mapperRelatedContactDetails;
68 protected $_mapperRelatedContactLocType;
69 protected $_mapperRelatedContactPhoneType;
70 protected $_mapperRelatedContactImProvider;
71 protected $_mapperRelatedContactWebsiteType;
72 protected $_mapFields;
73
74 protected $_parser;
75
76 /**
77 * @param null $tableName
78 * @param null $createSql
79 * @param bool $createTable
80 *
81 * @throws Exception
82 */
83 public function __construct($tableName = NULL, $createSql = NULL, $createTable = FALSE) {
84 $dao = new CRM_Core_DAO();
85 $db = $dao->getDatabaseConnection();
86
87 if ($createTable) {
88 if (!$createSql) {
89 CRM_Core_Error::fatal('Either an existing table name or an SQL query to build one are required');
90 }
91
92 // FIXME: we should regen this table's name if it exists rather than drop it
93 if (!$tableName) {
94 $tableName = 'civicrm_import_job_' . md5(uniqid(rand(), TRUE));
95 }
96 $db->query("DROP TABLE IF EXISTS $tableName");
97 $db->query("CREATE TABLE $tableName ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci $createSql");
98 }
99
100 if (!$tableName) {
101 CRM_Core_Error::fatal('Import Table is required.');
102 }
103
104 $this->_tableName = $tableName;
105
106 //initialize the properties.
107 $properties = array(
108 'mapperKeys',
109 'mapperRelated',
110 'mapperLocTypes',
111 'mapperPhoneTypes',
112 'mapperImProviders',
113 'mapperWebsiteTypes',
114 'mapperRelatedContactType',
115 'mapperRelatedContactDetails',
116 'mapperRelatedContactLocType',
117 'mapperRelatedContactPhoneType',
118 'mapperRelatedContactImProvider',
119 'mapperRelatedContactWebsiteType',
120 );
121 foreach ($properties as $property) {
122 $this->{"_$property"} = array();
123 }
124 }
125
126 /**
127 * @return null|string
128 */
129 public function getTableName() {
130 return $this->_tableName;
131 }
132
133 /**
134 * @param bool $dropIfComplete
135 *
136 * @return bool
137 * @throws Exception
138 */
139 public function isComplete($dropIfComplete = TRUE) {
140 if (!$this->_statusFieldName) {
141 CRM_Core_Error::fatal("Could not get name of the import status field");
142 }
143 $query = "SELECT * FROM $this->_tableName
144 WHERE $this->_statusFieldName = 'NEW' LIMIT 1";
145 $result = CRM_Core_DAO::executeQuery($query);
146 if ($result->fetch()) {
147 return FALSE;
148 }
149 if ($dropIfComplete) {
150 $query = "DROP TABLE $this->_tableName";
151 CRM_Core_DAO::executeQuery($query);
152 }
153 return TRUE;
154 }
155
156 /**
157 * @param array $params
158 */
159 public function setJobParams(&$params) {
160 foreach ($params as $param => $value) {
161 $fldName = "_$param";
162 $this->$fldName = $value;
163 }
164 }
165
166 /**
167 * @param CRM_Core_Form $form
168 * @param int $timeout
169 */
170 public function runImport(&$form, $timeout = 55) {
171 $mapper = $this->_mapper;
172 $mapperFields = array();
173 $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
174 $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
175 $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id');
176 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
177
178 //initialize mapper perperty value.
179 $mapperPeroperties = array(
180 'mapperRelated' => 'mapperRelatedVal',
181 'mapperLocTypes' => 'mapperLocTypesVal',
182 'mapperPhoneTypes' => 'mapperPhoneTypesVal',
183 'mapperImProviders' => 'mapperImProvidersVal',
184 'mapperWebsiteTypes' => 'mapperWebsiteTypesVal',
185 'mapperRelatedContactType' => 'mapperRelatedContactTypeVal',
186 'mapperRelatedContactDetails' => 'mapperRelatedContactDetailsVal',
187 'mapperRelatedContactLocType' => 'mapperRelatedContactLocTypeVal',
188 'mapperRelatedContactPhoneType' => 'mapperRelatedContactPhoneTypeVal',
189 'mapperRelatedContactImProvider' => 'mapperRelatedContactImProviderVal',
190 'mapperRelatedContactWebsiteType' => 'mapperRelatedContactWebsiteTypeVal',
191 );
192
193 foreach ($mapper as $key => $value) {
194 //set respective mapper value to null.
195 foreach (array_values($mapperPeroperties) as $perpertyVal) {
196 $$perpertyVal = NULL;
197 }
198
199 $fldName = CRM_Utils_Array::value(0, $mapper[$key]);
200 $header = array($this->_mapFields[$fldName]);
201 $selOne = CRM_Utils_Array::value(1, $mapper[$key]);
202 $selTwo = CRM_Utils_Array::value(2, $mapper[$key]);
203 $selThree = CRM_Utils_Array::value(3, $mapper[$key]);
204 $this->_mapperKeys[$key] = $fldName;
205
206 //need to differentiate non location elements.
207 if ($selOne && is_numeric($selOne)) {
208 if ($fldName == 'url') {
209 $header[] = $websiteTypes[$selOne];
210 $mapperWebsiteTypesVal = $selOne;
211 }
212 else {
213 $header[] = $locationTypes[$selOne];
214 $mapperLocTypesVal = $selOne;
215 if ($selTwo && is_numeric($selTwo)) {
216 if ($fldName == 'phone') {
217 $header[] = $phoneTypes[$selTwo];
218 $mapperPhoneTypesVal = $selTwo;
219 }
220 elseif ($fldName == 'im') {
221 $header[] = $imProviders[$selTwo];
222 $mapperImProvidersVal = $selTwo;
223 }
224 }
225 }
226 }
227
228 $fldNameParts = explode('_', $fldName, 3);
229 $id = $fldNameParts[0];
230 $first = isset($fldNameParts[1]) ? $fldNameParts[1] : NULL;
231 $second = isset($fldNameParts[2]) ? $fldNameParts[2] : NULL;
232 if (($first == 'a' && $second == 'b') ||
233 ($first == 'b' && $second == 'a')
234 ) {
235
236 $header[] = ucwords(str_replace("_", " ", $selOne));
237
238 $relationType = new CRM_Contact_DAO_RelationshipType();
239 $relationType->id = $id;
240 $relationType->find(TRUE);
241 $mapperRelatedContactTypeVal = $relationType->{"contact_type_$second"};
242
243 $mapperRelatedVal = $fldName;
244 if ($selOne) {
245 $mapperRelatedContactDetailsVal = $selOne;
246 if ($selTwo) {
247 if ($selOne == 'url') {
248 $header[] = $websiteTypes[$selTwo];
249 $mapperRelatedContactWebsiteTypeVal = $selTwo;
250 }
251 else {
252 $header[] = $locationTypes[$selTwo];
253 $mapperRelatedContactLocTypeVal = $selTwo;
254 if ($selThree) {
255 if ($selOne == 'phone') {
256 $header[] = $phoneTypes[$selThree];
257 $mapperRelatedContactPhoneTypeVal = $selThree;
258 }
259 elseif ($selOne == 'im') {
260 $header[] = $imProviders[$selThree];
261 $mapperRelatedContactImProviderVal = $selThree;
262 }
263 }
264 }
265 }
266 }
267 }
268 $mapperFields[] = implode(' - ', $header);
269
270 //set the respective mapper param array values.
271 foreach ($mapperPeroperties as $mapperProKey => $mapperProVal) {
272 $this->{"_$mapperProKey"}[$key] = $$mapperProVal;
273 }
274 }
275
276 $this->_parser = new CRM_Contact_Import_Parser_Contact(
277 $this->_mapperKeys,
278 $this->_mapperLocTypes,
279 $this->_mapperPhoneTypes,
280 $this->_mapperImProviders,
281 $this->_mapperRelated,
282 $this->_mapperRelatedContactType,
283 $this->_mapperRelatedContactDetails,
284 $this->_mapperRelatedContactLocType,
285 $this->_mapperRelatedContactPhoneType,
286 $this->_mapperRelatedContactImProvider,
287 $this->_mapperWebsiteTypes,
288 $this->_mapperRelatedContactWebsiteType
289 );
290
291 $this->_parser->run($this->_tableName, $mapperFields,
292 CRM_Import_Parser::MODE_IMPORT,
293 $this->_contactType,
294 $this->_primaryKeyName,
295 $this->_statusFieldName,
296 $this->_onDuplicate,
297 $this->_statusID,
298 $this->_totalRowCount,
299 $this->_doGeocodeAddress,
300 CRM_Contact_Import_Parser::DEFAULT_TIMEOUT,
301 $this->_contactSubType,
302 $this->_dedupe
303 );
304
305 $contactIds = $this->_parser->getImportedContacts();
306
307 //get the related contactIds. CRM-2926
308 $relatedContactIds = $this->_parser->getRelatedImportedContacts();
309 if ($relatedContactIds) {
310 $contactIds = array_merge($contactIds, $relatedContactIds);
311 if ($form) {
312 $form->set('relatedCount', count($relatedContactIds));
313 }
314 }
315
316 if ($this->_newGroupName || count($this->_groups)) {
317 $groupAdditions = $this->_addImportedContactsToNewGroup($contactIds,
318 $this->_newGroupName,
319 $this->_newGroupDesc
320 );
321 if ($form) {
322 $form->set('groupAdditions', $groupAdditions);
323 }
324 }
325
326 if ($this->_newTagName || count($this->_tag)) {
327 $tagAdditions = $this->_tagImportedContactsWithNewTag($contactIds,
328 $this->_newTagName,
329 $this->_newTagDesc
330 );
331 if ($form) {
332 $form->set('tagAdditions', $tagAdditions);
333 }
334 }
335 }
336
337 /**
338 * @param $form
339 */
340 public function setFormVariables($form) {
341 $this->_parser->set($form, CRM_Import_Parser::MODE_IMPORT);
342 }
343
344 /**
345 * @param $contactIds
346 * @param string $newGroupName
347 * @param $newGroupDesc
348 *
349 * @return array|bool
350 */
351 private function _addImportedContactsToNewGroup(
352 $contactIds,
353 $newGroupName, $newGroupDesc
354 ) {
355
356 $newGroupId = NULL;
357
358 if ($newGroupName) {
359 /* Create a new group */
360
361 $gParams = array(
362 'title' => $newGroupName,
363 'description' => $newGroupDesc,
364 'is_active' => TRUE,
365 );
366 $group = CRM_Contact_BAO_Group::create($gParams);
367 $this->_groups[] = $newGroupId = $group->id;
368 }
369
370 if (is_array($this->_groups)) {
371 $groupAdditions = array();
372 foreach ($this->_groups as $groupId) {
373 $addCount = CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
374 $totalCount = $addCount[1];
375 if ($groupId == $newGroupId) {
376 $name = $newGroupName;
377 $new = TRUE;
378 }
379 else {
380 $name = $this->_allGroups[$groupId];
381 $new = FALSE;
382 }
383 $groupAdditions[] = array(
384 'url' => CRM_Utils_System::url('civicrm/group/search',
385 'reset=1&force=1&context=smog&gid=' . $groupId
386 ),
387 'name' => $name,
388 'added' => $totalCount,
389 'notAdded' => $addCount[2],
390 'new' => $new,
391 );
392 }
393 return $groupAdditions;
394 }
395 return FALSE;
396 }
397
398 /**
399 * @param $contactIds
400 * @param string $newTagName
401 * @param $newTagDesc
402 *
403 * @return array|bool
404 */
405 private function _tagImportedContactsWithNewTag(
406 $contactIds,
407 $newTagName, $newTagDesc
408 ) {
409
410 $newTagId = NULL;
411 if ($newTagName) {
412 /* Create a new Tag */
413
414 $tagParams = array(
415 'name' => $newTagName,
416 'title' => $newTagName,
417 'description' => $newTagDesc,
418 'is_selectable' => TRUE,
419 'used_for' => 'civicrm_contact',
420 );
421 $id = array();
422 $addedTag = CRM_Core_BAO_Tag::add($tagParams, $id);
423 $this->_tag[$addedTag->id] = 1;
424 }
425 //add Tag to Import
426
427 if (is_array($this->_tag)) {
428 $tagAdditions = array();
429 foreach ($this->_tag as $tagId => $val) {
430 $addTagCount = CRM_Core_BAO_EntityTag::addEntitiesToTag($contactIds, $tagId);
431 $totalTagCount = $addTagCount[1];
432 if (isset($addedTag) && $tagId == $addedTag->id) {
433 $tagName = $newTagName;
434 $new = TRUE;
435 }
436 else {
437 $tagName = $this->_allTags[$tagId];
438 $new = FALSE;
439 }
440 $tagAdditions[] = array(
441 'url' => CRM_Utils_System::url('civicrm/contact/search',
442 'reset=1&force=1&context=smog&id=' . $tagId
443 ),
444 'name' => $tagName,
445 'added' => $totalTagCount,
446 'notAdded' => $addTagCount[2],
447 'new' => $new,
448 );
449 }
450 return $tagAdditions;
451 }
452 return FALSE;
453 }
454
455 /**
456 * @return array
457 */
458 public static function getIncompleteImportTables() {
459 $dao = new CRM_Core_DAO();
460 $database = $dao->database();
461 $query = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA
462 WHERE TABLE_SCHEMA = ? AND
463 TABLE_NAME LIKE 'civicrm_import_job_%'
464 ORDER BY TABLE_NAME";
465 $result = CRM_Core_DAO::executeQuery($query, array($database));
466 $incompleteImportTables = array();
467 while ($importTable = $result->fetch()) {
468 if (!$this->isComplete($importTable)) {
469 $incompleteImportTables[] = $importTable;
470 }
471 }
472 return $incompleteImportTables;
473 }
474
475 }