Merge pull request #23559 from eileenmcnaughton/import_yay
[civicrm-core.git] / CRM / Contribute / 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_Contribute_Import_Form_Preview extends CRM_Import_Form_Preview {
22
23 /**
24 * Set variables up before form is built.
25 */
26 public function preProcess() {
27 parent::preProcess();
28 //get the data from the session
29 $dataValues = $this->get('dataValues');
30 $mapper = $this->get('mapper');
31 $softCreditFields = $this->get('softCreditFields');
32 $mapperSoftCreditType = $this->get('mapperSoftCreditType');
33 $invalidRowCount = $this->get('invalidRowCount');
34
35 //get the mapping name displayed if the mappingId is set
36 $mappingId = $this->get('loadMappingId');
37 if ($mappingId) {
38 $mapDAO = new CRM_Core_DAO_Mapping();
39 $mapDAO->id = $mappingId;
40 $mapDAO->find(TRUE);
41 }
42 $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
43
44 if ($invalidRowCount) {
45 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser_Contribution';
46 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
47 }
48
49 $properties = [
50 'mapper',
51 'softCreditFields',
52 'mapperSoftCreditType',
53 'dataValues',
54 'columnCount',
55 'totalRowCount',
56 'validRowCount',
57 'invalidRowCount',
58 'downloadErrorRecordsUrl',
59 ];
60 $this->setStatusUrl();
61
62 foreach ($properties as $property) {
63 $this->assign($property, $this->get($property));
64 }
65 }
66
67 /**
68 * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics.
69 */
70 public function postProcess() {
71 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
72 $invalidRowCount = $this->get('invalidRowCount');
73 $onDuplicate = $this->get('onDuplicate');
74 $mapperSoftCreditType = $this->get('mapperSoftCreditType');
75
76 $mapper = $this->controller->exportValue('MapField', 'mapper');
77 $mapperKeys = [];
78 $mapperSoftCredit = [];
79 $mapperPhoneType = [];
80
81 foreach ($mapper as $key => $value) {
82 $mapperKeys[$key] = $mapper[$key][0];
83 if (isset($mapper[$key][0]) && $mapper[$key][0] == 'soft_credit' && isset($mapper[$key])) {
84 $mapperSoftCredit[$key] = $mapper[$key][1] ?? '';
85 $mapperSoftCreditType[$key] = $mapperSoftCreditType[$key]['value'];
86 }
87 else {
88 $mapperSoftCredit[$key] = $mapperSoftCreditType[$key] = NULL;
89 }
90 }
91
92 $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeys, $mapperSoftCredit, $mapperPhoneType, $mapperSoftCreditType);
93
94 $mapFields = $this->get('fields');
95
96 foreach ($mapper as $key => $value) {
97 $header = [];
98 if (isset($mapFields[$mapper[$key][0]])) {
99 $header[] = $mapFields[$mapper[$key][0]];
100 }
101 $mapperFields[] = implode(' - ', $header);
102 }
103 $parser->run(
104 $this->getSubmittedValue('uploadFile'),
105 $this->getSubmittedValue('fieldSeparator'),
106 $mapperFields,
107 $this->getSubmittedValue('skipColumnHeader'),
108 CRM_Import_Parser::MODE_IMPORT,
109 $this->get('contactType'),
110 $onDuplicate,
111 $this->get('statusID'),
112 $this->get('totalRowCount')
113 );
114
115 // Add all the necessary variables to the form.
116 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
117
118 // Check if there is any error occurred.
119
120 $errorStack = CRM_Core_Error::singleton();
121 $errors = $errorStack->getErrors();
122 $errorMessage = [];
123
124 if (is_array($errors)) {
125 foreach ($errors as $key => $value) {
126 $errorMessage[] = $value['message'];
127 }
128
129 $errorFile = $fileName['name'] . '.error.log';
130
131 if ($fd = fopen($errorFile, 'w')) {
132 fwrite($fd, implode('\n', $errorMessage));
133 }
134 fclose($fd);
135
136 $this->set('errorFile', $errorFile);
137 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser_Contribution';
138 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
139 }
140 }
141
142 }