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) { $this->{"_$property"} = array();
126 * @return null|string
128 public function getTableName() {
129 return $this->_tableName
;
133 * @param bool $dropIfComplete
138 public function isComplete($dropIfComplete = TRUE) {
139 if (!$this->_statusFieldName
) {
140 CRM_Core_Error
::fatal("Could not get name of the import status field");
142 $query = "SELECT * FROM $this->_tableName
143 WHERE $this->_statusFieldName = 'NEW' LIMIT 1";
144 $result = CRM_Core_DAO
::executeQuery($query);
145 if ($result->fetch()) {
148 if ($dropIfComplete) {
149 $query = "DROP TABLE $this->_tableName";
150 CRM_Core_DAO
::executeQuery($query);
156 * @param array $params
158 public function setJobParams(&$params) {
159 foreach ($params as $param => $value) {
160 $fldName = "_$param";
161 $this->$fldName = $value;
166 * @param CRM_Core_Form $form
167 * @param int $timeout
169 public function runImport(&$form, $timeout = 55) {
170 $mapper = $this->_mapper
;
171 $mapperFields = array();
172 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
173 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
174 $websiteTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id');
175 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
177 //initialize mapper perperty value.
178 $mapperPeroperties = array(
179 'mapperRelated' => 'mapperRelatedVal',
180 'mapperLocTypes' => 'mapperLocTypesVal',
181 'mapperPhoneTypes' => 'mapperPhoneTypesVal',
182 'mapperImProviders' => 'mapperImProvidersVal',
183 'mapperWebsiteTypes' => 'mapperWebsiteTypesVal',
184 'mapperRelatedContactType' => 'mapperRelatedContactTypeVal',
185 'mapperRelatedContactDetails' => 'mapperRelatedContactDetailsVal',
186 'mapperRelatedContactLocType' => 'mapperRelatedContactLocTypeVal',
187 'mapperRelatedContactPhoneType' => 'mapperRelatedContactPhoneTypeVal',
188 'mapperRelatedContactImProvider' => 'mapperRelatedContactImProviderVal',
189 'mapperRelatedContactWebsiteType' => 'mapperRelatedContactWebsiteTypeVal',
192 foreach ($mapper as $key => $value) {
193 //set respective mapper value to null.
194 foreach (array_values($mapperPeroperties) as $perpertyVal) { $
$perpertyVal = NULL;
197 $fldName = CRM_Utils_Array
::value(0, $mapper[$key]);
198 $header = array($this->_mapFields
[$fldName]);
199 $selOne = CRM_Utils_Array
::value(1, $mapper[$key]);
200 $selTwo = CRM_Utils_Array
::value(2, $mapper[$key]);
201 $selThree = CRM_Utils_Array
::value(3, $mapper[$key]);
202 $this->_mapperKeys
[$key] = $fldName;
204 //need to differentiate non location elements.
205 if ($selOne && is_numeric($selOne)) {
206 if ($fldName == 'url') {
207 $header[] = $websiteTypes[$selOne];
208 $mapperWebsiteTypesVal = $selOne;
211 $header[] = $locationTypes[$selOne];
212 $mapperLocTypesVal = $selOne;
213 if ($selTwo && is_numeric($selTwo)) {
214 if ($fldName == 'phone') {
215 $header[] = $phoneTypes[$selTwo];
216 $mapperPhoneTypesVal = $selTwo;
218 elseif ($fldName == 'im') {
219 $header[] = $imProviders[$selTwo];
220 $mapperImProvidersVal = $selTwo;
226 $fldNameParts = explode('_', $fldName, 3);
227 $id = $fldNameParts[0];
228 $first = isset($fldNameParts[1]) ?
$fldNameParts[1] : NULL;
229 $second = isset($fldNameParts[2]) ?
$fldNameParts[2] : NULL;
230 if (($first == 'a' && $second == 'b') ||
231 ($first == 'b' && $second == 'a')
234 $header[] = ucwords(str_replace("_", " ", $selOne));
236 $relationType = new CRM_Contact_DAO_RelationshipType();
237 $relationType->id
= $id;
238 $relationType->find(TRUE);
239 $mapperRelatedContactTypeVal = $relationType->{"contact_type_$second"};
241 $mapperRelatedVal = $fldName;
243 $mapperRelatedContactDetailsVal = $selOne;
245 if ($selOne == 'url') {
246 $header[] = $websiteTypes[$selTwo];
247 $mapperRelatedContactWebsiteTypeVal = $selTwo;
250 $header[] = $locationTypes[$selTwo];
251 $mapperRelatedContactLocTypeVal = $selTwo;
253 if ($selOne == 'phone') {
254 $header[] = $phoneTypes[$selThree];
255 $mapperRelatedContactPhoneTypeVal = $selThree;
257 elseif ($selOne == 'im') {
258 $header[] = $imProviders[$selThree];
259 $mapperRelatedContactImProviderVal = $selThree;
266 $mapperFields[] = implode(' - ', $header);
268 //set the respective mapper param array values.
269 foreach ($mapperPeroperties as $mapperProKey => $mapperProVal) {
270 $this->{"_$mapperProKey"}[$key] = $
$mapperProVal;
274 $this->_parser
= new CRM_Contact_Import_Parser_Contact(
276 $this->_mapperLocTypes
,
277 $this->_mapperPhoneTypes
,
278 $this->_mapperImProviders
,
279 $this->_mapperRelated
,
280 $this->_mapperRelatedContactType
,
281 $this->_mapperRelatedContactDetails
,
282 $this->_mapperRelatedContactLocType
,
283 $this->_mapperRelatedContactPhoneType
,
284 $this->_mapperRelatedContactImProvider
,
285 $this->_mapperWebsiteTypes
,
286 $this->_mapperRelatedContactWebsiteType
289 $this->_parser
->run($this->_tableName
, $mapperFields,
290 CRM_Import_Parser
::MODE_IMPORT
,
292 $this->_primaryKeyName
,
293 $this->_statusFieldName
,
296 $this->_totalRowCount
,
297 $this->_doGeocodeAddress
,
298 CRM_Contact_Import_Parser
::DEFAULT_TIMEOUT
,
299 $this->_contactSubType
,
303 $contactIds = $this->_parser
->getImportedContacts();
305 //get the related contactIds. CRM-2926
306 $relatedContactIds = $this->_parser
->getRelatedImportedContacts();
307 if ($relatedContactIds) {
308 $contactIds = array_merge($contactIds, $relatedContactIds);
310 $form->set('relatedCount', count($relatedContactIds));
314 if ($this->_newGroupName ||
count($this->_groups
)) {
315 $groupAdditions = $this->_addImportedContactsToNewGroup($contactIds,
316 $this->_newGroupName
,
320 $form->set('groupAdditions', $groupAdditions);
324 if ($this->_newTagName ||
count($this->_tag
)) {
325 $tagAdditions = $this->_tagImportedContactsWithNewTag($contactIds,
330 $form->set('tagAdditions', $tagAdditions);
338 public function setFormVariables($form) {
339 $this->_parser
->set($form, CRM_Import_Parser
::MODE_IMPORT
);
344 * @param string $newGroupName
345 * @param $newGroupDesc
349 private function _addImportedContactsToNewGroup($contactIds,
350 $newGroupName, $newGroupDesc
356 /* Create a new group */
359 'title' => $newGroupName,
360 'description' => $newGroupDesc,
363 $group = CRM_Contact_BAO_Group
::create($gParams);
364 $this->_groups
[] = $newGroupId = $group->id
;
367 if (is_array($this->_groups
)) {
368 $groupAdditions = array();
369 foreach ($this->_groups
as $groupId) {
370 $addCount = CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIds, $groupId);
371 $totalCount = $addCount[1];
372 if ($groupId == $newGroupId) {
373 $name = $newGroupName;
377 $name = $this->_allGroups
[$groupId];
380 $groupAdditions[] = array(
381 'url' => CRM_Utils_System
::url('civicrm/group/search',
382 'reset=1&force=1&context=smog&gid=' . $groupId
385 'added' => $totalCount,
386 'notAdded' => $addCount[2],
390 return $groupAdditions;
397 * @param string $newTagName
402 private function _tagImportedContactsWithNewTag($contactIds,
403 $newTagName, $newTagDesc
408 /* Create a new Tag */
411 'name' => $newTagName,
412 'title' => $newTagName,
413 'description' => $newTagDesc,
414 'is_selectable' => TRUE,
415 'used_for' => 'civicrm_contact',
418 $addedTag = CRM_Core_BAO_Tag
::add($tagParams, $id);
419 $this->_tag
[$addedTag->id
] = 1;
423 if (is_array($this->_tag
)) {
424 $tagAdditions = array();
425 foreach ($this->_tag
as $tagId => $val) {
426 $addTagCount = CRM_Core_BAO_EntityTag
::addEntitiesToTag($contactIds, $tagId);
427 $totalTagCount = $addTagCount[1];
428 if (isset($addedTag) && $tagId == $addedTag->id
) {
429 $tagName = $newTagName;
433 $tagName = $this->_allTags
[$tagId];
436 $tagAdditions[] = array(
437 'url' => CRM_Utils_System
::url('civicrm/contact/search',
438 'reset=1&force=1&context=smog&id=' . $tagId
441 'added' => $totalTagCount,
442 'notAdded' => $addTagCount[2],
446 return $tagAdditions;
454 public static function getIncompleteImportTables() {
455 $dao = new CRM_Core_DAO();
456 $database = $dao->database();
457 $query = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA
458 WHERE TABLE_SCHEMA = ? AND
459 TABLE_NAME LIKE 'civicrm_import_job_%'
460 ORDER BY TABLE_NAME";
461 $result = CRM_Core_DAO
::executeQuery($query, array($database));
462 $incompleteImportTables = array();
463 while ($importTable = $result->fetch()) {
464 if (!$this->isComplete($importTable)) {
465 $incompleteImportTables[] = $importTable;
468 return $incompleteImportTables;