e39652bbf21ff14b164c2a637b278629539f2a50
[civicrm-core.git] / CRM / Event / Import / Form / Preview.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 previews the uploaded file and returns summary
20 * statistics
21 */
22 class CRM_Event_Import_Form_Preview extends CRM_Import_Form_Preview {
23
24 /**
25 * Set variables up before form is built.
26 *
27 * @return void
28 */
29 public function preProcess() {
30 parent::preProcess();
31
32 //get the data from the session
33 $dataValues = $this->get('dataValues');
34 $mapper = $this->get('mapper');
35 $invalidRowCount = $this->get('invalidRowCount');
36 $conflictRowCount = $this->get('conflictRowCount');
37 $mismatchCount = $this->get('unMatchCount');
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);
45 }
46 $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
47
48 if ($invalidRowCount) {
49 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser';
50 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
51 }
52
53 if ($conflictRowCount) {
54 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Event_Import_Parser';
55 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
56 }
57
58 if ($mismatchCount) {
59 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Event_Import_Parser';
60 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
61 }
62
63 $properties = [
64 'mapper',
65 'dataValues',
66 'columnCount',
67 'totalRowCount',
68 'validRowCount',
69 'invalidRowCount',
70 'conflictRowCount',
71 'downloadErrorRecordsUrl',
72 'downloadConflictRecordsUrl',
73 'downloadMismatchRecordsUrl',
74 ];
75
76 foreach ($properties as $property) {
77 $this->assign($property, $this->get($property));
78 }
79 }
80
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
86 */
87 public function postProcess() {
88 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
89 $separator = $this->controller->exportValue('DataSource', 'fieldSeparator');
90 $invalidRowCount = $this->get('invalidRowCount');
91 $conflictRowCount = $this->get('conflictRowCount');
92 $onDuplicate = $this->get('onDuplicate');
93
94 $mapper = $this->controller->exportValue('MapField', 'mapper');
95 $mapperKeys = [];
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) {
106 $header = [];
107 if (isset($mapFields[$mapper[$key][0]])) {
108 $header[] = $mapFields[$mapper[$key][0]];
109 }
110 $mapperFields[] = implode(' - ', $header);
111 }
112 $parser->run($fileName, $separator,
113 $mapperFields,
114 $this->getSubmittedValue('skipColumnHeader'),
115 CRM_Import_Parser::MODE_IMPORT,
116 $this->get('contactType'),
117 $onDuplicate
118 );
119
120 // add all the necessary variables to the form
121 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
122
123 // check if there is any error occurred
124
125 $errorStack = CRM_Core_Error::singleton();
126 $errors = $errorStack->getErrors();
127 $errorMessage = [];
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);
142 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser';
143 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
144 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Event_Import_Parser';
145 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
146 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Event_Import_Parser';
147 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
148 }
149 }
150
151 }