Merge pull request #23209 from civicrm/5.49
[civicrm-core.git] / CRM / Custom / Import / Form / Preview.php
1 <?php
2
3 /**
4 * Class CRM_Custom_Import_Form_Preview
5 */
6 class CRM_Custom_Import_Form_Preview extends CRM_Import_Form_Preview {
7 public $_parser = 'CRM_Custom_Import_Parser_Api';
8 protected $_importParserUrl = '&parser=CRM_Custom_Import_Parser';
9
10 /**
11 * Set variables up before form is built.
12 *
13 * @return void
14 */
15 public function preProcess() {
16 parent::preProcess();
17 //get the data from the session
18 $dataValues = $this->get('dataValues');
19 $mapper = $this->get('mapper');
20 $invalidRowCount = $this->get('invalidRowCount');
21 $conflictRowCount = $this->get('conflictRowCount');
22 $mismatchCount = $this->get('unMatchCount');
23 $entity = $this->get('_entity');
24
25 //get the mapping name displayed if the mappingId is set
26 $mappingId = $this->get('loadMappingId');
27 if ($mappingId) {
28 $mapDAO = new CRM_Core_DAO_Mapping();
29 $mapDAO->id = $mappingId;
30 $mapDAO->find(TRUE);
31 }
32 $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
33
34 if ($invalidRowCount) {
35 $urlParams = 'type=' . CRM_Import_Parser::ERROR . $this->_importParserUrl;
36 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
37 }
38
39 if ($conflictRowCount) {
40 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . $this->_importParserUrl;
41 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
42 }
43
44 if ($mismatchCount) {
45 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . $this->_importParserUrl;
46 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
47 }
48
49 $properties = [
50 'mapper',
51 'dataValues',
52 'columnCount',
53 'totalRowCount',
54 'validRowCount',
55 'invalidRowCount',
56 'conflictRowCount',
57 'downloadErrorRecordsUrl',
58 'downloadConflictRecordsUrl',
59 'downloadMismatchRecordsUrl',
60 ];
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.
69 * preview the file and extract some summary statistics
70 *
71 * @return void
72 */
73 public function postProcess() {
74 $fileName = $this->getSubmittedValue('uploadFile');
75 $invalidRowCount = $this->get('invalidRowCount');
76 $conflictRowCount = $this->get('conflictRowCount');
77 $onDuplicate = $this->get('onDuplicate');
78 $entity = $this->get('_entity');
79
80 $mapper = $this->controller->exportValue('MapField', 'mapper');
81 $mapperKeys = [];
82
83 foreach ($mapper as $key => $value) {
84 $mapperKeys[$key] = $mapper[$key][0];
85 }
86
87 $parser = new $this->_parser($mapperKeys);
88 $parser->setEntity($entity);
89
90 $mapFields = $this->get('fields');
91
92 foreach ($mapper as $key => $value) {
93 $header = [];
94 if (isset($mapFields[$mapper[$key][0]])) {
95 $header[] = $mapFields[$mapper[$key][0]];
96 }
97 $mapperFields[] = implode(' - ', $header);
98 }
99 $parser->run($this->getSubmittedValue('uploadFile'), $this->getSubmittedValue('fieldSeparator'),
100 $mapperFields,
101 $this->getSubmittedValue('skipColumnHeader'),
102 CRM_Import_Parser::MODE_IMPORT,
103 $this->get('contactType'),
104 $onDuplicate
105 );
106
107 // add all the necessary variables to the form
108 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
109
110 // check if there is any error occurred
111
112 $errorStack = CRM_Core_Error::singleton();
113 $errors = $errorStack->getErrors();
114 $errorMessage = [];
115
116 if (is_array($errors)) {
117 foreach ($errors as $key => $value) {
118 $errorMessage[] = $value['message'];
119 }
120
121 $errorFile = $fileName['name'] . '.error.log';
122
123 if ($fd = fopen($errorFile, 'w')) {
124 fwrite($fd, implode('\n', $errorMessage));
125 }
126 fclose($fd);
127
128 $this->set('errorFile', $errorFile);
129 $urlParams = 'type=' . CRM_Import_Parser::ERROR . $this->_importParserUrl;
130 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
131 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . $this->_importParserUrl;
132 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
133 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . $this->_importParserUrl;
134 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
135 }
136 }
137
138 }