Merge pull request #23620 from bugfolder/8_move_session_start
[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 $invalidRowCount = $this->getRowCount(CRM_Import_Parser::VALID);
29
30 $downloadURL = '';
31 if ($invalidRowCount) {
32 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser_Contribution';
33 $downloadURL = CRM_Utils_System::url('civicrm/export', $urlParams);
34 }
35
36 $this->setStatusUrl();
37 $this->assign('downloadErrorRecordsUrl', $downloadURL);
38 }
39
40 /**
41 * Get the mapped fields as an array of labels.
42 *
43 * e.g
44 * ['First Name', 'Employee Of - First Name', 'Home - Street Address']
45 *
46 * @return array
47 * @throws \API_Exception
48 * @throws \CRM_Core_Exception
49 */
50 protected function getMappedFieldLabels(): array {
51 $mapper = [];
52 $parser = $this->getParser();
53 foreach ($this->getSubmittedValue('mapper') as $columnNumber => $mappedField) {
54 $mapper[$columnNumber] = $parser->getMappedFieldLabel($parser->getMappingFieldFromMapperInput($mappedField, 0, $columnNumber));
55 }
56 return $mapper;
57 }
58
59 /**
60 * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics.
61 */
62 public function postProcess() {
63 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
64 $onDuplicate = $this->get('onDuplicate');
65 $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues());
66 $mapper = $this->controller->exportValue('MapField', 'mapper');
67
68 $parser = new CRM_Contribute_Import_Parser_Contribution();
69 $parser->setUserJobID($this->getUserJobID());
70
71 $mapFields = $this->get('fields');
72
73 foreach ($mapper as $key => $value) {
74 $header = [];
75 if (isset($mapFields[$mapper[$key][0]])) {
76 $header[] = $mapFields[$mapper[$key][0]];
77 }
78 $mapperFields[] = implode(' - ', $header);
79 }
80 $parser->run(
81 $this->getSubmittedValue('uploadFile'),
82 $this->getSubmittedValue('fieldSeparator'),
83 $mapperFields,
84 $this->getSubmittedValue('skipColumnHeader'),
85 CRM_Import_Parser::MODE_IMPORT,
86 $this->getSubmittedValue('contactType'),
87 $onDuplicate,
88 $this->get('statusID'),
89 $this->get('totalRowCount')
90 );
91
92 // Add all the necessary variables to the form.
93 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
94
95 // Check if there is any error occurred.
96
97 $errorStack = CRM_Core_Error::singleton();
98 $errors = $errorStack->getErrors();
99 $errorMessage = [];
100
101 if (is_array($errors)) {
102 foreach ($errors as $key => $value) {
103 $errorMessage[] = $value['message'];
104 }
105
106 $errorFile = $fileName['name'] . '.error.log';
107
108 if ($fd = fopen($errorFile, 'w')) {
109 fwrite($fd, implode('\n', $errorMessage));
110 }
111 fclose($fd);
112
113 $this->set('errorFile', $errorFile);
114 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contribute_Import_Parser_Contribution';
115 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
116 }
117 }
118
119 /**
120 * @return \CRM_Contribute_Import_Parser_Contribution
121 */
122 protected function getParser(): CRM_Contribute_Import_Parser_Contribution {
123 if (!$this->parser) {
124 $this->parser = new CRM_Contribute_Import_Parser_Contribution();
125 $this->parser->setUserJobID($this->getUserJobID());
126 $this->parser->init();
127 }
128 return $this->parser;
129 }
130
131 }