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