Another always-true-if
[civicrm-core.git] / CRM / Contact / Import / ImportJob.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class acts like a psuedo-BAO for transient import job tables.
20 */
21 class CRM_Contact_Import_ImportJob {
22
23 protected $_onDuplicate;
24 protected $_dedupe;
25 protected $_newGroupName;
26 protected $_newGroupDesc;
27 protected $_newGroupType;
28 protected $_groups;
29 protected $_allGroups;
30 protected $_newTagName;
31 protected $_newTagDesc;
32 protected $_tag;
33 protected $_allTags;
34
35 protected $_mapper;
36 protected $_mapperKeys = [];
37 protected $_mapFields;
38
39 /**
40 * @var CRM_Contact_Import_Parser_Contact
41 */
42 protected $_parser;
43
44 protected $_userJobID;
45
46 /**
47 * Has the job completed.
48 *
49 * @return bool
50 */
51 public function isComplete(): bool {
52 return $this->_parser->isComplete();
53 }
54
55 /**
56 * @param array $params
57 */
58 public function setJobParams(&$params) {
59 foreach ($params as $param => $value) {
60 $fldName = "_$param";
61 $this->$fldName = $value;
62 }
63 }
64
65 /**
66 * @param CRM_Core_Form $form
67 * @param int $timeout
68 */
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;
73 }
74
75 $this->_parser = new CRM_Contact_Import_Parser_Contact(
76 $this->_mapperKeys
77 );
78 $this->_parser->setUserJobID($this->_userJobID);
79 $this->_parser->run(
80 [],
81 CRM_Import_Parser::MODE_IMPORT,
82 $this->_statusID
83 );
84
85 $contactIds = $this->_parser->getImportedContacts();
86
87 //get the related contactIds. CRM-2926
88 $relatedContactIds = $this->_parser->getRelatedImportedContacts();
89 if ($relatedContactIds) {
90 $contactIds = array_merge($contactIds, $relatedContactIds);
91 if ($form) {
92 $form->set('relatedCount', count($relatedContactIds));
93 }
94 }
95
96 if ($this->_newGroupName || count($this->_groups)) {
97 $groupAdditions = $this->_addImportedContactsToNewGroup($contactIds,
98 $this->_newGroupName,
99 $this->_newGroupDesc,
100 $this->_newGroupType
101 );
102 if ($form) {
103 $form->set('groupAdditions', $groupAdditions);
104 }
105 }
106
107 if ($this->_newTagName || !empty($this->_tag)) {
108 $tagAdditions = $this->_tagImportedContactsWithNewTag($contactIds,
109 $this->_newTagName,
110 $this->_newTagDesc
111 );
112 if ($form) {
113 $form->set('tagAdditions', $tagAdditions);
114 }
115 }
116 }
117
118 /**
119 * @param $form
120 */
121 public function setFormVariables($form) {
122 $this->_parser->set($form, CRM_Import_Parser::MODE_IMPORT);
123 }
124
125 /**
126 * Add imported contacts.
127 *
128 * @param array $contactIds
129 * @param string $newGroupName
130 * @param string $newGroupDesc
131 * @param string $newGroupType
132 *
133 * @return array|bool
134 */
135 private function _addImportedContactsToNewGroup(
136 $contactIds,
137 $newGroupName, $newGroupDesc, $newGroupType
138 ) {
139
140 $newGroupId = NULL;
141
142 if ($newGroupName) {
143 /* Create a new group */
144 $newGroupType = $newGroupType ?? [];
145 $gParams = array(
146 'title' => $newGroupName,
147 'description' => $newGroupDesc,
148 'group_type' => $newGroupType,
149 'is_active' => TRUE,
150 );
151 $group = CRM_Contact_BAO_Group::create($gParams);
152 $this->_groups[] = $newGroupId = $group->id;
153 }
154
155 if (is_array($this->_groups)) {
156 $groupAdditions = [];
157 foreach ($this->_groups as $groupId) {
158 $addCount = CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
159 $totalCount = $addCount[1];
160 if ($groupId == $newGroupId) {
161 $name = $newGroupName;
162 $new = TRUE;
163 }
164 else {
165 $name = $this->_allGroups[$groupId];
166 $new = FALSE;
167 }
168 $groupAdditions[] = array(
169 'url' => CRM_Utils_System::url('civicrm/group/search',
170 'reset=1&force=1&context=smog&gid=' . $groupId
171 ),
172 'name' => $name,
173 'added' => $totalCount,
174 'notAdded' => $addCount[2],
175 'new' => $new,
176 );
177 }
178 return $groupAdditions;
179 }
180 return FALSE;
181 }
182
183 /**
184 * @param $contactIds
185 * @param string $newTagName
186 * @param $newTagDesc
187 *
188 * @return array|bool
189 * @throws \CRM_Core_Exception
190 */
191 private function _tagImportedContactsWithNewTag(
192 $contactIds,
193 $newTagName, $newTagDesc
194 ) {
195
196 $newTagId = NULL;
197 if ($newTagName) {
198 /* Create a new Tag */
199
200 $tagParams = array(
201 'name' => $newTagName,
202 'description' => $newTagDesc,
203 'is_selectable' => TRUE,
204 'used_for' => 'civicrm_contact',
205 );
206 $addedTag = CRM_Core_BAO_Tag::add($tagParams);
207 $this->_tag[$addedTag->id] = 1;
208 }
209 //add Tag to Import
210
211 if (is_array($this->_tag)) {
212 $tagAdditions = [];
213 foreach ($this->_tag as $tagId => $val) {
214 $addTagCount = CRM_Core_BAO_EntityTag::addEntitiesToTag($contactIds, $tagId, 'civicrm_contact', FALSE);
215 $totalTagCount = $addTagCount[1];
216 if (isset($addedTag) && $tagId == $addedTag->id) {
217 $tagName = $newTagName;
218 $new = TRUE;
219 }
220 else {
221 $tagName = $this->_allTags[$tagId];
222 $new = FALSE;
223 }
224 $tagAdditions[] = array(
225 'url' => CRM_Utils_System::url('civicrm/contact/search',
226 'reset=1&force=1&context=smog&id=' . $tagId
227 ),
228 'name' => $tagName,
229 'added' => $totalTagCount,
230 'notAdded' => $addTagCount[2],
231 'new' => $new,
232 );
233 }
234 return $tagAdditions;
235 }
236 return FALSE;
237 }
238
239 }