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 | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
f12c6f7d | 19 | * This class previews the uploaded file and returns summary statistics. |
6a488035 | 20 | */ |
f532671f | 21 | class CRM_Contact_Import_Form_Preview extends CRM_Import_Form_Preview { |
6a488035 | 22 | |
fd830836 DRJ |
23 | /** |
24 | * Whether USPS validation should be disabled during import. | |
25 | * | |
26 | * @var bool | |
27 | */ | |
28 | protected $_disableUSPS; | |
29 | ||
6a488035 | 30 | /** |
fe482240 | 31 | * Set variables up before form is built. |
99e3c5f7 EM |
32 | * |
33 | * @throws \API_Exception | |
34 | * @throws \CRM_Core_Exception | |
6a488035 TO |
35 | */ |
36 | public function preProcess() { | |
4d9f4d69 | 37 | parent::preProcess(); |
feab3e2e | 38 | $this->_disableUSPS = $this->getSubmittedValue('disableUSPS'); |
6a488035 | 39 | |
24431f7b | 40 | $groups = CRM_Core_PseudoConstant::nestedGroup(); |
6a488035 TO |
41 | $this->set('groups', $groups); |
42 | ||
cd43c5e3 | 43 | $tag = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE)); |
6a488035 TO |
44 | if ($tag) { |
45 | $this->set('tag', $tag); | |
46 | } | |
47 | ||
8cebffb2 | 48 | $this->setStatusUrl(); |
6a488035 TO |
49 | } |
50 | ||
51 | /** | |
fe482240 | 52 | * Build the form object. |
6a488035 TO |
53 | */ |
54 | public function buildQuickForm() { | |
c7162176 | 55 | $this->addElement('text', 'newGroupName', ts('Name for new group'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title')); |
6a488035 | 56 | $this->addElement('text', 'newGroupDesc', ts('Description of new group')); |
5a552b87 SL |
57 | $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE); |
58 | if (!empty($groupTypes)) { | |
59 | $this->addCheckBox('newGroupType', | |
60 | ts('Group Type'), | |
61 | $groupTypes, | |
62 | NULL, NULL, NULL, NULL, ' ' | |
63 | ); | |
64 | } | |
6a488035 TO |
65 | |
66 | $groups = $this->get('groups'); | |
67 | ||
68 | if (!empty($groups)) { | |
353ffa53 | 69 | $this->addElement('select', 'groups', ts('Add imported records to existing group(s)'), $groups, array( |
69078420 SL |
70 | 'multiple' => "multiple", |
71 | 'class' => 'crm-select2', | |
72 | )); | |
6a488035 TO |
73 | } |
74 | ||
75 | //display new tag | |
c7162176 | 76 | $this->addElement('text', 'newTagName', ts('Tag'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'name')); |
77 | $this->addElement('text', 'newTagDesc', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'description')); | |
6a488035 TO |
78 | |
79 | $tag = $this->get('tag'); | |
80 | if (!empty($tag)) { | |
81 | foreach ($tag as $tagID => $tagName) { | |
82 | $this->addElement('checkbox', "tag[$tagID]", NULL, $tagName); | |
83 | } | |
84 | } | |
85 | ||
719a6fec | 86 | $this->addFormRule(array('CRM_Contact_Import_Form_Preview', 'formRule'), $this); |
4c7e1926 CW |
87 | |
88 | parent::buildQuickForm(); | |
6a488035 TO |
89 | } |
90 | ||
91 | /** | |
fe482240 | 92 | * Global validation rules for the form. |
6a488035 | 93 | * |
77c5b619 TO |
94 | * @param array $fields |
95 | * Posted values of the form. | |
6a488035 | 96 | * |
da6b46f4 | 97 | * @param $files |
e8cf95b4 | 98 | * @param self $self |
da6b46f4 | 99 | * |
a6c01b45 CW |
100 | * @return array |
101 | * list of errors to be posted back to the form | |
6a488035 | 102 | */ |
00be9182 | 103 | public static function formRule($fields, $files, $self) { |
affcc9d2 | 104 | $errors = []; |
6a488035 TO |
105 | $invalidTagName = $invalidGroupName = FALSE; |
106 | ||
a7488080 | 107 | if (!empty($fields['newTagName'])) { |
6a488035 | 108 | if (!CRM_Utils_Rule::objectExists(trim($fields['newTagName']), |
353ffa53 TO |
109 | array('CRM_Core_DAO_Tag') |
110 | ) | |
111 | ) { | |
6a488035 TO |
112 | $errors['newTagName'] = ts('Tag \'%1\' already exists.', |
113 | array(1 => $fields['newTagName']) | |
114 | ); | |
115 | $invalidTagName = TRUE; | |
116 | } | |
117 | } | |
118 | ||
a7488080 | 119 | if (!empty($fields['newGroupName'])) { |
353ffa53 TO |
120 | $title = trim($fields['newGroupName']); |
121 | $name = CRM_Utils_String::titleToVar($title); | |
122 | $query = 'select count(*) from civicrm_group where name like %1 OR title like %2'; | |
0e6e8724 DL |
123 | $grpCnt = CRM_Core_DAO::singleValueQuery( |
124 | $query, | |
125 | array( | |
126 | 1 => array($name, 'String'), | |
6a488035 | 127 | 2 => array($title, 'String'), |
0e6e8724 DL |
128 | ) |
129 | ); | |
6a488035 TO |
130 | if ($grpCnt) { |
131 | $invalidGroupName = TRUE; | |
132 | $errors['newGroupName'] = ts('Group \'%1\' already exists.', array(1 => $fields['newGroupName'])); | |
133 | } | |
134 | } | |
135 | ||
136 | $self->assign('invalidTagName', $invalidTagName); | |
137 | $self->assign('invalidGroupName', $invalidGroupName); | |
138 | ||
139 | return empty($errors) ? TRUE : $errors; | |
140 | } | |
141 | ||
6a488035 | 142 | /** |
f12c6f7d | 143 | * Process the mapped fields and map it into the uploaded file. |
52bd01f5 EM |
144 | * |
145 | * @throws \API_Exception | |
6a488035 TO |
146 | */ |
147 | public function postProcess() { | |
148 | ||
149 | $importJobParams = array( | |
69c39cd4 EM |
150 | 'doGeocodeAddress' => $this->getSubmittedValue('doGeocodeAddress'), |
151 | 'invalidRowCount' => $this->getRowCount(CRM_Import_Parser::ERROR), | |
152 | 'onDuplicate' => $this->getSubmittedValue('onDuplicate'), | |
dfa2f16c | 153 | 'dedupe' => $this->getSubmittedValue('dedupe_rule_id'), |
6a488035 TO |
154 | 'newGroupName' => $this->controller->exportValue($this->_name, 'newGroupName'), |
155 | 'newGroupDesc' => $this->controller->exportValue($this->_name, 'newGroupDesc'), | |
5a552b87 | 156 | 'newGroupType' => $this->controller->exportValue($this->_name, 'newGroupType'), |
6a488035 TO |
157 | 'groups' => $this->controller->exportValue($this->_name, 'groups'), |
158 | 'allGroups' => $this->get('groups'), | |
159 | 'newTagName' => $this->controller->exportValue($this->_name, 'newTagName'), | |
160 | 'newTagDesc' => $this->controller->exportValue($this->_name, 'newTagDesc'), | |
161 | 'tag' => $this->controller->exportValue($this->_name, 'tag'), | |
162 | 'allTags' => $this->get('tag'), | |
163 | 'mapper' => $this->controller->exportValue('MapField', 'mapper'), | |
52bd01f5 | 164 | 'mapFields' => $this->getAvailableFields(), |
c4f66023 | 165 | 'contactType' => $this->getContactType(), |
80cb71bb | 166 | 'contactSubType' => $this->getSubmittedValue('contactSubType'), |
8d88fae0 EM |
167 | 'primaryKeyName' => '_id', |
168 | 'statusFieldName' => '_status', | |
6a488035 | 169 | 'statusID' => $this->get('statusID'), |
69c39cd4 | 170 | 'totalRowCount' => $this->getRowCount([]), |
10716b26 | 171 | 'userJobID' => $this->getUserJobID(), |
6a488035 TO |
172 | ); |
173 | ||
3377d521 | 174 | $importJob = new CRM_Contact_Import_ImportJob(); |
6a488035 TO |
175 | $importJob->setJobParams($importJobParams); |
176 | ||
177 | // If ACL applies to the current user, update cache before running the import. | |
178 | if (!CRM_Core_Permission::check('view all contacts')) { | |
a92e7856 SL |
179 | $userID = CRM_Core_Session::getLoggedInContactID(); |
180 | CRM_ACL_BAO_Cache::deleteEntry($userID); | |
181 | CRM_ACL_BAO_Cache::deleteContactCacheEntry($userID); | |
6a488035 TO |
182 | } |
183 | ||
fd830836 DRJ |
184 | CRM_Utils_Address_USPS::disable($this->_disableUSPS); |
185 | ||
6a488035 TO |
186 | // run the import |
187 | $importJob->runImport($this); | |
188 | ||
16d2a5df | 189 | // Clear all caches, forcing any searches to recheck the ACLs or group membership as the import |
190 | // may have changed it. | |
0626851e | 191 | CRM_Contact_BAO_Contact_Utils::clearContactCaches(TRUE); |
6a488035 TO |
192 | |
193 | // add all the necessary variables to the form | |
194 | $importJob->setFormVariables($this); | |
195 | ||
b44e3f84 | 196 | // check if there is any error occurred |
6a488035 TO |
197 | $errorStack = CRM_Core_Error::singleton(); |
198 | $errors = $errorStack->getErrors(); | |
affcc9d2 | 199 | $errorMessage = []; |
6a488035 TO |
200 | |
201 | if (is_array($errors)) { | |
202 | foreach ($errors as $key => $value) { | |
203 | $errorMessage[] = $value['message']; | |
204 | } | |
205 | ||
206 | // there is no fileName since this is a sql import | |
207 | // so fudge it | |
208 | $config = CRM_Core_Config::singleton(); | |
209 | $errorFile = $config->uploadDir . "sqlImport.error.log"; | |
210 | if ($fd = fopen($errorFile, 'w')) { | |
211 | fwrite($fd, implode('\n', $errorMessage)); | |
212 | } | |
213 | fclose($fd); | |
214 | ||
215 | $this->set('errorFile', $errorFile); | |
6a488035 TO |
216 | } |
217 | ||
218 | //hack to clean db | |
219 | //if job complete drop table. | |
91971b3c | 220 | $importJob->isComplete(); |
6a488035 TO |
221 | } |
222 | ||
73edfc10 EM |
223 | /** |
224 | * @return \CRM_Contact_Import_Parser_Contact | |
225 | */ | |
226 | protected function getParser(): CRM_Contact_Import_Parser_Contact { | |
d00da884 EM |
227 | if (!$this->parser) { |
228 | $this->parser = new CRM_Contact_Import_Parser_Contact(); | |
229 | $this->parser->setUserJobID($this->getUserJobID()); | |
230 | } | |
231 | return $this->parser; | |
73edfc10 EM |
232 | } |
233 | ||
6a488035 | 234 | } |