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 previews the uploaded file and returns summary statistics.
21 class CRM_Contact_Import_Form_Preview
extends CRM_Import_Form_Preview
{
24 * Whether USPS validation should be disabled during import.
28 protected $_disableUSPS;
31 * Set variables up before form is built.
33 public function preProcess() {
34 $mapper = $this->get('mapper');
35 $invalidRowCount = $this->get('invalidRowCount');
36 $conflictRowCount = $this->get('conflictRowCount');
37 $mismatchCount = $this->get('unMatchCount');
38 $columnNames = $this->get('columnNames');
39 $this->_disableUSPS
= $this->get('disableUSPS');
42 $this->assign('columnNames', $columnNames);
44 //get the mapping name displayed if the mappingId is set
45 $mappingId = $this->get('loadMappingId');
47 $mapDAO = new CRM_Core_DAO_Mapping();
48 $mapDAO->id
= $mappingId;
51 $this->assign('savedMappingName', $mappingId ?
$mapDAO->name
: NULL);
53 $this->assign('rowDisplayCount', 2);
55 $groups = CRM_Core_PseudoConstant
::nestedGroup();
56 $this->set('groups', $groups);
58 $tag = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
60 $this->set('tag', $tag);
63 if ($invalidRowCount) {
64 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contact_Import_Parser_Contact';
65 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
68 if ($conflictRowCount) {
69 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. '&parser=CRM_Contact_Import_Parser_Contact';
70 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
74 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. '&parser=CRM_Contact_Import_Parser_Contact';
75 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
88 'downloadErrorRecordsUrl',
89 'downloadConflictRecordsUrl',
90 'downloadMismatchRecordsUrl',
92 'relatedContactDetails',
93 'relatedContactLocType',
94 'relatedContactPhoneType',
95 'relatedContactImProvider',
97 'relatedContactWebsiteType',
100 foreach ($properties as $property) {
101 $this->assign($property, $this->get($property));
103 $this->assign('dataValues', $this->getDataRows(2));
105 $this->setStatusUrl();
107 $showColNames = TRUE;
108 if ('CRM_Import_DataSource_CSV' == $this->get('dataSource') &&
109 !$this->getSubmittedValue('skipColumnHeader')
111 $showColNames = FALSE;
113 $this->assign('showColNames', $showColNames);
117 * Build the form object.
119 public function buildQuickForm() {
120 $this->addElement('text', 'newGroupName', ts('Name for new group'), CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Group', 'title'));
121 $this->addElement('text', 'newGroupDesc', ts('Description of new group'));
122 $groupTypes = CRM_Core_OptionGroup
::values('group_type', TRUE);
123 if (!empty($groupTypes)) {
124 $this->addCheckBox('newGroupType',
127 NULL, NULL, NULL, NULL, ' '
131 $groups = $this->get('groups');
133 if (!empty($groups)) {
134 $this->addElement('select', 'groups', ts('Add imported records to existing group(s)'), $groups, array(
135 'multiple' => "multiple",
136 'class' => 'crm-select2',
141 $this->addElement('text', 'newTagName', ts('Tag'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Tag', 'name'));
142 $this->addElement('text', 'newTagDesc', ts('Description'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Tag', 'description'));
144 $tag = $this->get('tag');
146 foreach ($tag as $tagID => $tagName) {
147 $this->addElement('checkbox', "tag[$tagID]", NULL, $tagName);
151 $this->addFormRule(array('CRM_Contact_Import_Form_Preview', 'formRule'), $this);
153 parent
::buildQuickForm();
157 * Global validation rules for the form.
159 * @param array $fields
160 * Posted values of the form.
166 * list of errors to be posted back to the form
168 public static function formRule($fields, $files, $self) {
170 $invalidTagName = $invalidGroupName = FALSE;
172 if (!empty($fields['newTagName'])) {
173 if (!CRM_Utils_Rule
::objectExists(trim($fields['newTagName']),
174 array('CRM_Core_DAO_Tag')
177 $errors['newTagName'] = ts('Tag \'%1\' already exists.',
178 array(1 => $fields['newTagName'])
180 $invalidTagName = TRUE;
184 if (!empty($fields['newGroupName'])) {
185 $title = trim($fields['newGroupName']);
186 $name = CRM_Utils_String
::titleToVar($title);
187 $query = 'select count(*) from civicrm_group where name like %1 OR title like %2';
188 $grpCnt = CRM_Core_DAO
::singleValueQuery(
191 1 => array($name, 'String'),
192 2 => array($title, 'String'),
196 $invalidGroupName = TRUE;
197 $errors['newGroupName'] = ts('Group \'%1\' already exists.', array(1 => $fields['newGroupName']));
201 $self->assign('invalidTagName', $invalidTagName);
202 $self->assign('invalidGroupName', $invalidGroupName);
204 return empty($errors) ?
TRUE : $errors;
208 * Process the mapped fields and map it into the uploaded file.
210 * @throws \API_Exception
212 public function postProcess() {
214 $importJobParams = array(
215 'doGeocodeAddress' => $this->controller
->exportValue('DataSource', 'doGeocodeAddress'),
216 'invalidRowCount' => $this->get('invalidRowCount'),
217 'conflictRowCount' => $this->get('conflictRowCount'),
218 'onDuplicate' => $this->get('onDuplicate'),
219 'dedupe' => $this->getSubmittedValue('dedupe_rule_id'),
220 'newGroupName' => $this->controller
->exportValue($this->_name
, 'newGroupName'),
221 'newGroupDesc' => $this->controller
->exportValue($this->_name
, 'newGroupDesc'),
222 'newGroupType' => $this->controller
->exportValue($this->_name
, 'newGroupType'),
223 'groups' => $this->controller
->exportValue($this->_name
, 'groups'),
224 'allGroups' => $this->get('groups'),
225 'newTagName' => $this->controller
->exportValue($this->_name
, 'newTagName'),
226 'newTagDesc' => $this->controller
->exportValue($this->_name
, 'newTagDesc'),
227 'tag' => $this->controller
->exportValue($this->_name
, 'tag'),
228 'allTags' => $this->get('tag'),
229 'mapper' => $this->controller
->exportValue('MapField', 'mapper'),
230 'mapFields' => $this->getAvailableFields(),
231 'contactType' => $this->get('contactType'),
232 'contactSubType' => $this->getSubmittedValue('contactSubType'),
233 'primaryKeyName' => $this->get('primaryKeyName'),
234 'statusFieldName' => $this->get('statusFieldName'),
235 'statusID' => $this->get('statusID'),
236 'totalRowCount' => $this->get('totalRowCount'),
237 'userJobID' => $this->getUserJobID(),
240 $tableName = $this->get('importTableName');
241 $importJob = new CRM_Contact_Import_ImportJob($tableName);
242 $importJob->setJobParams($importJobParams);
244 // If ACL applies to the current user, update cache before running the import.
245 if (!CRM_Core_Permission
::check('view all contacts')) {
246 $userID = CRM_Core_Session
::getLoggedInContactID();
247 CRM_ACL_BAO_Cache
::deleteEntry($userID);
248 CRM_ACL_BAO_Cache
::deleteContactCacheEntry($userID);
251 CRM_Utils_Address_USPS
::disable($this->_disableUSPS
);
254 $importJob->runImport($this);
256 // Clear all caches, forcing any searches to recheck the ACLs or group membership as the import
257 // may have changed it.
258 CRM_Contact_BAO_Contact_Utils
::clearContactCaches(TRUE);
260 // add all the necessary variables to the form
261 $importJob->setFormVariables($this);
263 // check if there is any error occurred
264 $errorStack = CRM_Core_Error
::singleton();
265 $errors = $errorStack->getErrors();
268 if (is_array($errors)) {
269 foreach ($errors as $key => $value) {
270 $errorMessage[] = $value['message'];
273 // there is no fileName since this is a sql import
275 $config = CRM_Core_Config
::singleton();
276 $errorFile = $config->uploadDir
. "sqlImport.error.log";
277 if ($fd = fopen($errorFile, 'w')) {
278 fwrite($fd, implode('\n', $errorMessage));
282 $this->set('errorFile', $errorFile);
284 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contact_Import_Parser_Contact';
285 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
287 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. '&parser=CRM_Contact_Import_Parser_Contact';
288 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
290 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. '&parser=CRM_Contact_Import_Parser_Contact';
291 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
295 //if job complete drop table.
296 $importJob->isComplete();