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