Merge pull request #15916 from civicrm/5.20
[civicrm-core.git] / CRM / Activity / 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 statistics.
20 */
21 class CRM_Activity_Import_Form_Preview extends CRM_Import_Form_Preview {
22
23 /**
24 * Set variables up before form is built.
25 */
26 public function preProcess() {
27 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
28
29 // Get the data from the session.
30 $dataValues = $this->get('dataValues');
31 $mapper = $this->get('mapper');
32 $invalidRowCount = $this->get('invalidRowCount');
33 $conflictRowCount = $this->get('conflictRowCount');
34 $mismatchCount = $this->get('unMatchCount');
35
36 // Get the mapping name displayed if the mappingId is set.
37 $mappingId = $this->get('loadMappingId');
38 if ($mappingId) {
39 $mapDAO = new CRM_Core_DAO_Mapping();
40 $mapDAO->id = $mappingId;
41 $mapDAO->find(TRUE);
42 $this->assign('loadedMapping', $mappingId);
43 $this->assign('savedName', $mapDAO->name);
44 }
45
46 if ($skipColumnHeader) {
47 $this->assign('skipColumnHeader', $skipColumnHeader);
48 $this->assign('rowDisplayCount', 3);
49 }
50 else {
51 $this->assign('rowDisplayCount', 2);
52 }
53
54 if ($invalidRowCount) {
55 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Activity_Import_Parser';
56 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
57 }
58
59 if ($conflictRowCount) {
60 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Activity_Import_Parser';
61 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
62 }
63
64 if ($mismatchCount) {
65 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Activity_Import_Parser';
66 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
67 }
68
69 $properties = [
70 'mapper',
71 'dataValues',
72 'columnCount',
73 'totalRowCount',
74 'validRowCount',
75 'invalidRowCount',
76 'conflictRowCount',
77 'downloadErrorRecordsUrl',
78 'downloadConflictRecordsUrl',
79 'downloadMismatchRecordsUrl',
80 ];
81 $this->setStatusUrl();
82
83 foreach ($properties as $property) {
84 $this->assign($property, $this->get($property));
85 }
86 }
87
88 /**
89 * Process the mapped fields and map it into the uploaded file.
90 *
91 * Preview the file and extract some summary statistics
92 */
93 public function postProcess() {
94 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
95 $seperator = $this->controller->exportValue('DataSource', 'fieldSeparator');
96 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
97 $invalidRowCount = $this->get('invalidRowCount');
98 $conflictRowCount = $this->get('conflictRowCount');
99 $onDuplicate = $this->get('onDuplicate');
100
101 $mapper = $this->controller->exportValue('MapField', 'mapper');
102 $mapperKeys = [];
103 $mapperLocType = [];
104 $mapperPhoneType = [];
105
106 foreach ($mapper as $key => $value) {
107 $mapperKeys[$key] = $mapper[$key][0];
108
109 if (!empty($mapper[$key][1]) && is_numeric($mapper[$key][1])) {
110 $mapperLocType[$key] = $mapper[$key][1];
111 }
112 else {
113 $mapperLocType[$key] = NULL;
114 }
115
116 if (!empty($mapper[$key][2]) && (!is_numeric($mapper[$key][2]))) {
117 $mapperPhoneType[$key] = $mapper[$key][2];
118 }
119 else {
120 $mapperPhoneType[$key] = NULL;
121 }
122 }
123
124 $parser = new CRM_Activity_Import_Parser_Activity($mapperKeys, $mapperLocType, $mapperPhoneType);
125
126 $mapFields = $this->get('fields');
127
128 foreach ($mapper as $key => $value) {
129 $header = [];
130 if (isset($mapFields[$mapper[$key][0]])) {
131 $header[] = $mapFields[$mapper[$key][0]];
132 }
133 $mapperFields[] = implode(' - ', $header);
134 }
135 $parser->run($fileName, $seperator,
136 $mapperFields,
137 $skipColumnHeader,
138 CRM_Import_Parser::MODE_IMPORT,
139 $onDuplicate,
140 $this->get('statusID'),
141 $this->get('totalRowCount')
142 );
143
144 // add all the necessary variables to the form
145 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
146
147 // check if there is any error occurred
148
149 $errorStack = CRM_Core_Error::singleton();
150 $errors = $errorStack->getErrors();
151 $errorMessage = [];
152
153 if (is_array($errors)) {
154 foreach ($errors as $key => $value) {
155 $errorMessage[] = $value['message'];
156 }
157
158 $errorFile = $fileName['name'] . '.error.log';
159
160 if ($fd = fopen($errorFile, 'w')) {
161 fwrite($fd, implode('\n', $errorMessage));
162 }
163 fclose($fd);
164
165 $this->set('errorFile', $errorFile);
166 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Activity_Import_Parser';
167 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
168 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Activity_Import_Parser';
169 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
170 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Activity_Import_Parser';
171 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
172 }
173 }
174
175 }