Merge pull request #22805 from braders/permission_denied_wordpress_improvement-altern...
[civicrm-core.git] / CRM / Custom / Import / Form / Preview.php
CommitLineData
9ff5f6c0 1<?php
4c6ce474
EM
2
3/**
4 * Class CRM_Custom_Import_Form_Preview
5 */
9ff5f6c0
N
6class CRM_Custom_Import_Form_Preview extends CRM_Import_Form_Preview {
7 public $_parser = 'CRM_Custom_Import_Parser_Api';
f999a5f6 8 protected $_importParserUrl = '&parser=CRM_Custom_Import_Parser_Api';
353ffa53 9
9ff5f6c0 10 /**
fe482240 11 * Set variables up before form is built.
9ff5f6c0
N
12 *
13 * @return void
9ff5f6c0
N
14 */
15 public function preProcess() {
9d7974eb 16 parent::preProcess();
9ff5f6c0 17 //get the data from the session
353ffa53
TO
18 $dataValues = $this->get('dataValues');
19 $mapper = $this->get('mapper');
20 $invalidRowCount = $this->get('invalidRowCount');
9ff5f6c0 21 $conflictRowCount = $this->get('conflictRowCount');
353ffa53
TO
22 $mismatchCount = $this->get('unMatchCount');
23 $entity = $this->get('_entity');
9ff5f6c0
N
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);
9ff5f6c0 31 }
262b7f26 32 $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
9ff5f6c0 33
9ff5f6c0
N
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
be2fb01f 49 $properties = [
9ff5f6c0 50 'mapper',
353ffa53
TO
51 'dataValues',
52 'columnCount',
53 'totalRowCount',
54 'validRowCount',
55 'invalidRowCount',
56 'conflictRowCount',
9ff5f6c0
N
57 'downloadErrorRecordsUrl',
58 'downloadConflictRecordsUrl',
59 'downloadMismatchRecordsUrl',
be2fb01f 60 ];
9ff5f6c0
N
61
62 foreach ($properties as $property) {
63 $this->assign($property, $this->get($property));
64 }
65 }
66
67 /**
fe482240 68 * Process the mapped fields and map it into the uploaded file.
9ff5f6c0
N
69 * preview the file and extract some summary statistics
70 *
71 * @return void
9ff5f6c0
N
72 */
73 public function postProcess() {
5e8faabc 74 $fileName = $this->getSubmittedValue('uploadFile');
353ffa53 75 $invalidRowCount = $this->get('invalidRowCount');
9ff5f6c0 76 $conflictRowCount = $this->get('conflictRowCount');
353ffa53
TO
77 $onDuplicate = $this->get('onDuplicate');
78 $entity = $this->get('_entity');
9ff5f6c0 79
9ff5f6c0 80 $mapper = $this->controller->exportValue('MapField', 'mapper');
be2fb01f 81 $mapperKeys = [];
9ff5f6c0
N
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) {
be2fb01f 93 $header = [];
9ff5f6c0
N
94 if (isset($mapFields[$mapper[$key][0]])) {
95 $header[] = $mapFields[$mapper[$key][0]];
96 }
97 $mapperFields[] = implode(' - ', $header);
98 }
5e8faabc 99 $parser->run($this->getSubmittedValue('uploadFile'), $this->getSubmittedValue('fieldSeparator'),
9ff5f6c0 100 $mapperFields,
9d7974eb 101 $this->getSubmittedValue('skipColumnHeader'),
9ff5f6c0
N
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
b44e3f84 110 // check if there is any error occurred
9ff5f6c0
N
111
112 $errorStack = CRM_Core_Error::singleton();
113 $errors = $errorStack->getErrors();
be2fb01f 114 $errorMessage = [];
9ff5f6c0
N
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 }
96025800 137
4c6ce474 138}