Merge pull request #23555 from civicrm/5.50
[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_Api';
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 $entity = $this->get('_entity');
22
23 //get the mapping name displayed if the mappingId is set
24 $mappingId = $this->get('loadMappingId');
25 if ($mappingId) {
26 $mapDAO = new CRM_Core_DAO_Mapping();
27 $mapDAO->id = $mappingId;
28 $mapDAO->find(TRUE);
29 }
30 $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
31
32 if ($invalidRowCount) {
33 $urlParams = 'type=' . CRM_Import_Parser::ERROR . $this->_importParserUrl;
34 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
35 }
36
37 $properties = [
38 'mapper',
39 'dataValues',
40 'columnCount',
41 'totalRowCount',
42 'validRowCount',
43 'invalidRowCount',
44 'downloadErrorRecordsUrl',
45 ];
46
47 foreach ($properties as $property) {
48 $this->assign($property, $this->get($property));
49 }
50 }
51
52 /**
53 * Process the mapped fields and map it into the uploaded file.
54 * preview the file and extract some summary statistics
55 *
56 * @return void
57 */
58 public function postProcess() {
59 $fileName = $this->getSubmittedValue('uploadFile');
60 $invalidRowCount = $this->get('invalidRowCount');
61 $onDuplicate = $this->get('onDuplicate');
62 $entity = $this->get('_entity');
63
64 $mapper = $this->controller->exportValue('MapField', 'mapper');
65 $mapperKeys = [];
66
67 foreach ($mapper as $key => $value) {
68 $mapperKeys[$key] = $mapper[$key][0];
69 }
70
71 $parser = new $this->_parser($mapperKeys);
72 $parser->setEntity($entity);
73
74 $mapFields = $this->get('fields');
75
76 foreach ($mapper as $key => $value) {
77 $header = [];
78 if (isset($mapFields[$mapper[$key][0]])) {
79 $header[] = $mapFields[$mapper[$key][0]];
80 }
81 $mapperFields[] = implode(' - ', $header);
82 }
83 $parser->run($this->getSubmittedValue('uploadFile'), $this->getSubmittedValue('fieldSeparator'),
84 $mapperFields,
85 $this->getSubmittedValue('skipColumnHeader'),
86 CRM_Import_Parser::MODE_IMPORT,
87 $this->get('contactType'),
88 $onDuplicate
89 );
90
91 // add all the necessary variables to the form
92 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
93
94 // check if there is any error occurred
95
96 $errorStack = CRM_Core_Error::singleton();
97 $errors = $errorStack->getErrors();
98 $errorMessage = [];
99
100 if (is_array($errors)) {
101 foreach ($errors as $key => $value) {
102 $errorMessage[] = $value['message'];
103 }
104
105 $errorFile = $fileName['name'] . '.error.log';
106
107 if ($fd = fopen($errorFile, 'w')) {
108 fwrite($fd, implode('\n', $errorMessage));
109 }
110 fclose($fd);
111
112 $this->set('errorFile', $errorFile);
113 $urlParams = 'type=' . CRM_Import_Parser::ERROR . $this->_importParserUrl;
114 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
115 }
116 }
117
118 }