Merge pull request #23135 from eileenmcnaughton/memm
[civicrm-core.git] / CRM / Event / Import / Form / Preview.php
CommitLineData
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/**
19 * This class previews the uploaded file and returns summary
20 * statistics
21 */
f532671f 22class CRM_Event_Import_Form_Preview extends CRM_Import_Form_Preview {
6a488035
TO
23
24 /**
fe482240 25 * Set variables up before form is built.
6a488035
TO
26 *
27 * @return void
6a488035
TO
28 */
29 public function preProcess() {
9d7974eb 30 parent::preProcess();
6a488035
TO
31
32 //get the data from the session
353ffa53
TO
33 $dataValues = $this->get('dataValues');
34 $mapper = $this->get('mapper');
35 $invalidRowCount = $this->get('invalidRowCount');
6a488035 36 $conflictRowCount = $this->get('conflictRowCount');
353ffa53 37 $mismatchCount = $this->get('unMatchCount');
6a488035
TO
38
39 //get the mapping name displayed if the mappingId is set
40 $mappingId = $this->get('loadMappingId');
41 if ($mappingId) {
42 $mapDAO = new CRM_Core_DAO_Mapping();
43 $mapDAO->id = $mappingId;
44 $mapDAO->find(TRUE);
6a488035 45 }
262b7f26 46 $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
6a488035 47
6a488035 48 if ($invalidRowCount) {
a05662ef 49 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser';
6a488035
TO
50 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
51 }
52
53 if ($conflictRowCount) {
a05662ef 54 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Event_Import_Parser';
6a488035
TO
55 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
56 }
57
58 if ($mismatchCount) {
a05662ef 59 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Event_Import_Parser';
6a488035
TO
60 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
61 }
62
be2fb01f 63 $properties = [
6a488035 64 'mapper',
353ffa53
TO
65 'dataValues',
66 'columnCount',
67 'totalRowCount',
68 'validRowCount',
69 'invalidRowCount',
70 'conflictRowCount',
6a488035
TO
71 'downloadErrorRecordsUrl',
72 'downloadConflictRecordsUrl',
73 'downloadMismatchRecordsUrl',
be2fb01f 74 ];
6a488035
TO
75
76 foreach ($properties as $property) {
77 $this->assign($property, $this->get($property));
78 }
79 }
80
6a488035
TO
81 /**
82 * Process the mapped fields and map it into the uploaded file
83 * preview the file and extract some summary statistics
84 *
85 * @return void
6a488035
TO
86 */
87 public function postProcess() {
353ffa53 88 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
f0fed404 89 $separator = $this->controller->exportValue('DataSource', 'fieldSeparator');
353ffa53 90 $invalidRowCount = $this->get('invalidRowCount');
6a488035 91 $conflictRowCount = $this->get('conflictRowCount');
353ffa53 92 $onDuplicate = $this->get('onDuplicate');
6a488035 93
063338e5 94 $mapper = $this->controller->exportValue('MapField', 'mapper');
be2fb01f 95 $mapperKeys = [];
6a488035
TO
96
97 foreach ($mapper as $key => $value) {
98 $mapperKeys[$key] = $mapper[$key][0];
99 }
100
101 $parser = new CRM_Event_Import_Parser_Participant($mapperKeys);
102
103 $mapFields = $this->get('fields');
104
105 foreach ($mapper as $key => $value) {
be2fb01f 106 $header = [];
6a488035
TO
107 if (isset($mapFields[$mapper[$key][0]])) {
108 $header[] = $mapFields[$mapper[$key][0]];
109 }
110 $mapperFields[] = implode(' - ', $header);
111 }
f0fed404 112 $parser->run($fileName, $separator,
6a488035 113 $mapperFields,
9d7974eb 114 $this->getSubmittedValue('skipColumnHeader'),
a05662ef 115 CRM_Import_Parser::MODE_IMPORT,
6a488035
TO
116 $this->get('contactType'),
117 $onDuplicate
118 );
119
120 // add all the necessary variables to the form
a05662ef 121 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
6a488035 122
b44e3f84 123 // check if there is any error occurred
6a488035
TO
124
125 $errorStack = CRM_Core_Error::singleton();
126 $errors = $errorStack->getErrors();
be2fb01f 127 $errorMessage = [];
6a488035
TO
128
129 if (is_array($errors)) {
130 foreach ($errors as $key => $value) {
131 $errorMessage[] = $value['message'];
132 }
133
134 $errorFile = $fileName['name'] . '.error.log';
135
136 if ($fd = fopen($errorFile, 'w')) {
137 fwrite($fd, implode('\n', $errorMessage));
138 }
139 fclose($fd);
140
141 $this->set('errorFile', $errorFile);
a05662ef 142 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser';
6a488035 143 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
a05662ef 144 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Event_Import_Parser';
6a488035 145 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
a05662ef 146 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Event_Import_Parser';
6a488035
TO
147 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
148 }
149 }
96025800 150
6a488035 151}