3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 2009. |
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 acts like a psuedo-BAO for transient import job tables
39 class CRM_Contact_Import_ImportJob
{
41 protected $_tableName;
42 protected $_primaryKeyName;
43 protected $_statusFieldName;
45 protected $_doGeocodeAddress;
46 protected $_invalidRowCount;
47 protected $_conflictRowCount;
48 protected $_onDuplicate;
50 protected $_newGroupName;
51 protected $_newGroupDesc;
53 protected $_allGroups;
54 protected $_newTagName;
55 protected $_newTagDesc;
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;
77 * @param null $tableName
78 * @param null $createSql
79 * @param bool $createTable
83 public function __construct($tableName = NULL, $createSql = NULL, $createTable = FALSE) {
84 $dao = new CRM_Core_DAO();
85 $db = $dao->getDatabaseConnection();
89 CRM_Core_Error
::fatal('Either an existing table name or an SQL query to build one are required');
92 // FIXME: we should regen this table's name if it exists rather than drop it
94 $tableName = 'civicrm_import_job_' . md5(uniqid(rand(), TRUE));
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");
101 CRM_Core_Error
::fatal('Import Table is required.');
104 $this->_tableName
= $tableName;
106 //initialize the properties.
113 'mapperWebsiteTypes',
114 'mapperRelatedContactType',
115 'mapperRelatedContactDetails',
116 'mapperRelatedContactLocType',
117 'mapperRelatedContactPhoneType',
118 'mapperRelatedContactImProvider',
119 'mapperRelatedContactWebsiteType',
121 foreach ($properties as $property) {
122 $this->{"_$property"} = array();
127 * @return null|string
129 public function getTableName() {
130 return $this->_tableName
;
134 * @param bool $dropIfComplete
139 public function isComplete($dropIfComplete = TRUE) {
140 if (!$this->_statusFieldName
) {
141 CRM_Core_Error
::fatal("Could not get name of the import status field");
143 $query = "SELECT * FROM $this->_tableName
144 WHERE $this->_statusFieldName = 'NEW' LIMIT 1";
145 $result = CRM_Core_DAO
::executeQuery($query);
146 if ($result->fetch()) {
149 if ($dropIfComplete) {
150 $query = "DROP TABLE $this->_tableName";
151 CRM_Core_DAO
::executeQuery($query);
157 * @param array $params
159 public function setJobParams(&$params) {
160 foreach ($params as $param => $value) {
161 $fldName = "_$param";
162 $this->$fldName = $value;
167 * @param CRM_Core_Form $form
168 * @param int $timeout
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');
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',
193 foreach ($mapper as $key => $value) {
194 //set respective mapper value to null.
195 foreach (array_values($mapperPeroperties) as $perpertyVal) {
196 $
$perpertyVal = NULL;
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;
206 //need to differentiate non location elements.
207 if ($selOne && is_numeric($selOne)) {
208 if ($fldName == 'url') {
209 $header[] = $websiteTypes[$selOne];
210 $mapperWebsiteTypesVal = $selOne;
213 $header[] = $locationTypes[$selOne];
214 $mapperLocTypesVal = $selOne;
215 if ($selTwo && is_numeric($selTwo)) {
216 if ($fldName == 'phone') {
217 $header[] = $phoneTypes[$selTwo];
218 $mapperPhoneTypesVal = $selTwo;
220 elseif ($fldName == 'im') {
221 $header[] = $imProviders[$selTwo];
222 $mapperImProvidersVal = $selTwo;
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')
236 $header[] = ucwords(str_replace("_", " ", $selOne));
238 $relationType = new CRM_Contact_DAO_RelationshipType();
239 $relationType->id
= $id;
240 $relationType->find(TRUE);
241 $mapperRelatedContactTypeVal = $relationType->{"contact_type_$second"};
243 $mapperRelatedVal = $fldName;
245 $mapperRelatedContactDetailsVal = $selOne;
247 if ($selOne == 'url') {
248 $header[] = $websiteTypes[$selTwo];
249 $mapperRelatedContactWebsiteTypeVal = $selTwo;
252 $header[] = $locationTypes[$selTwo];
253 $mapperRelatedContactLocTypeVal = $selTwo;
255 if ($selOne == 'phone') {
256 $header[] = $phoneTypes[$selThree];
257 $mapperRelatedContactPhoneTypeVal = $selThree;
259 elseif ($selOne == 'im') {
260 $header[] = $imProviders[$selThree];
261 $mapperRelatedContactImProviderVal = $selThree;
268 $mapperFields[] = implode(' - ', $header);
270 //set the respective mapper param array values.
271 foreach ($mapperPeroperties as $mapperProKey => $mapperProVal) {
272 $this->{"_$mapperProKey"}[$key] = $
$mapperProVal;
276 $this->_parser
= new CRM_Contact_Import_Parser_Contact(
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
291 $this->_parser
->run($this->_tableName
, $mapperFields,
292 CRM_Import_Parser
::MODE_IMPORT
,
294 $this->_primaryKeyName
,
295 $this->_statusFieldName
,
298 $this->_totalRowCount
,
299 $this->_doGeocodeAddress
,
300 CRM_Contact_Import_Parser
::DEFAULT_TIMEOUT
,
301 $this->_contactSubType
,
305 $contactIds = $this->_parser
->getImportedContacts();
307 //get the related contactIds. CRM-2926
308 $relatedContactIds = $this->_parser
->getRelatedImportedContacts();
309 if ($relatedContactIds) {
310 $contactIds = array_merge($contactIds, $relatedContactIds);
312 $form->set('relatedCount', count($relatedContactIds));
316 if ($this->_newGroupName ||
count($this->_groups
)) {
317 $groupAdditions = $this->_addImportedContactsToNewGroup($contactIds,
318 $this->_newGroupName
,
322 $form->set('groupAdditions', $groupAdditions);
326 if ($this->_newTagName ||
count($this->_tag
)) {
327 $tagAdditions = $this->_tagImportedContactsWithNewTag($contactIds,
332 $form->set('tagAdditions', $tagAdditions);
340 public function setFormVariables($form) {
341 $this->_parser
->set($form, CRM_Import_Parser
::MODE_IMPORT
);
346 * @param string $newGroupName
347 * @param $newGroupDesc
351 private function _addImportedContactsToNewGroup(
353 $newGroupName, $newGroupDesc
359 /* Create a new group */
362 'title' => $newGroupName,
363 'description' => $newGroupDesc,
366 $group = CRM_Contact_BAO_Group
::create($gParams);
367 $this->_groups
[] = $newGroupId = $group->id
;
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;
380 $name = $this->_allGroups
[$groupId];
383 $groupAdditions[] = array(
384 'url' => CRM_Utils_System
::url('civicrm/group/search',
385 'reset=1&force=1&context=smog&gid=' . $groupId
388 'added' => $totalCount,
389 'notAdded' => $addCount[2],
393 return $groupAdditions;
400 * @param string $newTagName
405 private function _tagImportedContactsWithNewTag(
407 $newTagName, $newTagDesc
412 /* Create a new Tag */
415 'name' => $newTagName,
416 'title' => $newTagName,
417 'description' => $newTagDesc,
418 'is_selectable' => TRUE,
419 'used_for' => 'civicrm_contact',
422 $addedTag = CRM_Core_BAO_Tag
::add($tagParams, $id);
423 $this->_tag
[$addedTag->id
] = 1;
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;
437 $tagName = $this->_allTags
[$tagId];
440 $tagAdditions[] = array(
441 'url' => CRM_Utils_System
::url('civicrm/contact/search',
442 'reset=1&force=1&context=smog&id=' . $tagId
445 'added' => $totalTagCount,
446 'notAdded' => $addTagCount[2],
450 return $tagAdditions;
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;
472 return $incompleteImportTables;