3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class acts like a psuedo-BAO for transient import job tables.
21 class CRM_Contact_Import_ImportJob
{
23 protected $_onDuplicate;
25 protected $_newGroupName;
26 protected $_newGroupDesc;
27 protected $_newGroupType;
29 protected $_allGroups;
30 protected $_newTagName;
31 protected $_newTagDesc;
36 protected $_mapperKeys = [];
37 protected $_mapFields;
40 * @var CRM_Contact_Import_Parser_Contact
44 protected $_userJobID;
47 * Has the job completed.
51 public function isComplete(): bool {
52 return $this->_parser
->isComplete();
56 * @param array $params
58 public function setJobParams(&$params) {
59 foreach ($params as $param => $value) {
61 $this->$fldName = $value;
66 * @param CRM_Core_Form $form
69 public function runImport(&$form, $timeout = 55) {
70 $mapper = $this->_mapper
;
71 foreach ($mapper as $key => $value) {
72 $this->_mapperKeys
[$key] = $mapper[$key][0] ??
NULL;
75 $this->_parser
= new CRM_Contact_Import_Parser_Contact(
78 $this->_parser
->setUserJobID($this->_userJobID
);
81 CRM_Import_Parser
::MODE_IMPORT
,
85 $contactIds = $this->_parser
->getImportedContacts();
87 //get the related contactIds. CRM-2926
88 $relatedContactIds = $this->_parser
->getRelatedImportedContacts();
89 if ($relatedContactIds) {
90 $contactIds = array_merge($contactIds, $relatedContactIds);
93 if ($this->_newGroupName ||
count($this->_groups
)) {
94 $groupAdditions = $this->_addImportedContactsToNewGroup($contactIds,
100 $form->set('groupAdditions', $groupAdditions);
104 if ($this->_newTagName ||
!empty($this->_tag
)) {
105 $tagAdditions = $this->_tagImportedContactsWithNewTag($contactIds,
110 $form->set('tagAdditions', $tagAdditions);
118 public function setFormVariables($form) {
119 $this->_parser
->set($form, CRM_Import_Parser
::MODE_IMPORT
);
123 * Add imported contacts.
125 * @param array $contactIds
126 * @param string $newGroupName
127 * @param string $newGroupDesc
128 * @param string $newGroupType
132 private function _addImportedContactsToNewGroup(
134 $newGroupName, $newGroupDesc, $newGroupType
140 /* Create a new group */
141 $newGroupType = $newGroupType ??
[];
143 'title' => $newGroupName,
144 'description' => $newGroupDesc,
145 'group_type' => $newGroupType,
148 $group = CRM_Contact_BAO_Group
::create($gParams);
149 $this->_groups
[] = $newGroupId = $group->id
;
152 if (is_array($this->_groups
)) {
153 $groupAdditions = [];
154 foreach ($this->_groups
as $groupId) {
155 $addCount = CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIds, $groupId);
156 $totalCount = $addCount[1];
157 if ($groupId == $newGroupId) {
158 $name = $newGroupName;
162 $name = $this->_allGroups
[$groupId];
165 $groupAdditions[] = array(
166 'url' => CRM_Utils_System
::url('civicrm/group/search',
167 'reset=1&force=1&context=smog&gid=' . $groupId
170 'added' => $totalCount,
171 'notAdded' => $addCount[2],
175 return $groupAdditions;
182 * @param string $newTagName
186 * @throws \CRM_Core_Exception
188 private function _tagImportedContactsWithNewTag(
190 $newTagName, $newTagDesc
195 /* Create a new Tag */
198 'name' => $newTagName,
199 'description' => $newTagDesc,
200 'is_selectable' => TRUE,
201 'used_for' => 'civicrm_contact',
203 $addedTag = CRM_Core_BAO_Tag
::add($tagParams);
204 $this->_tag
[$addedTag->id
] = 1;
208 if (is_array($this->_tag
)) {
210 foreach ($this->_tag
as $tagId => $val) {
211 $addTagCount = CRM_Core_BAO_EntityTag
::addEntitiesToTag($contactIds, $tagId, 'civicrm_contact', FALSE);
212 $totalTagCount = $addTagCount[1];
213 if (isset($addedTag) && $tagId == $addedTag->id
) {
214 $tagName = $newTagName;
218 $tagName = $this->_allTags
[$tagId];
221 $tagAdditions[] = array(
222 'url' => CRM_Utils_System
::url('civicrm/contact/search',
223 'reset=1&force=1&context=smog&id=' . $tagId
226 'added' => $totalTagCount,
227 'notAdded' => $addTagCount[2],
231 return $tagAdditions;