INFRA-132 comments to end with full stops
[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 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
17
18 //get the data from the session
19 $dataValues = $this->get('dataValues');
20 $mapper = $this->get('mapper');
21 $invalidRowCount = $this->get('invalidRowCount');
22 $conflictRowCount = $this->get('conflictRowCount');
23 $mismatchCount = $this->get('unMatchCount');
24 $entity = $this->get('_entity');
25
26 //get the mapping name displayed if the mappingId is set
27 $mappingId = $this->get('loadMappingId');
28 if ($mappingId) {
29 $mapDAO = new CRM_Core_DAO_Mapping();
30 $mapDAO->id = $mappingId;
31 $mapDAO->find(TRUE);
32 $this->assign('loadedMapping', $mappingId);
33 $this->assign('savedName', $mapDAO->name);
34 }
35
36 if ($skipColumnHeader) {
37 $this->assign('skipColumnHeader', $skipColumnHeader);
38 $this->assign('rowDisplayCount', 3);
39 }
40 else {
41 $this->assign('rowDisplayCount', 2);
42 }
43
44 if ($invalidRowCount) {
45 $urlParams = 'type=' . CRM_Import_Parser::ERROR . $this->_importParserUrl;
46 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
47 }
48
49 if ($conflictRowCount) {
50 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . $this->_importParserUrl;
51 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
52 }
53
54 if ($mismatchCount) {
55 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . $this->_importParserUrl;
56 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
57 }
58
59 $properties = array(
60 'mapper',
61 'dataValues',
62 'columnCount',
63 'totalRowCount',
64 'validRowCount',
65 'invalidRowCount',
66 'conflictRowCount',
67 'downloadErrorRecordsUrl',
68 'downloadConflictRecordsUrl',
69 'downloadMismatchRecordsUrl',
70 );
71
72 foreach ($properties as $property) {
73 $this->assign($property, $this->get($property));
74 }
75 }
76
77 /**
78 * Process the mapped fields and map it into the uploaded file.
79 * preview the file and extract some summary statistics
80 *
81 * @return void
82 */
83 public function postProcess() {
84 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
85 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
86 $invalidRowCount = $this->get('invalidRowCount');
87 $conflictRowCount = $this->get('conflictRowCount');
88 $onDuplicate = $this->get('onDuplicate');
89 $entity = $this->get('_entity');
90
91 $config = CRM_Core_Config::singleton();
92 $separator = $config->fieldSeparator;
93
94 $mapper = $this->controller->exportValue('MapField', 'mapper');
95 $mapperKeys = array();
96
97 foreach ($mapper as $key => $value) {
98 $mapperKeys[$key] = $mapper[$key][0];
99 }
100
101 $parser = new $this->_parser($mapperKeys);
102 $parser->setEntity($entity);
103
104 $mapFields = $this->get('fields');
105
106 foreach ($mapper as $key => $value) {
107 $header = array();
108 if (isset($mapFields[$mapper[$key][0]])) {
109 $header[] = $mapFields[$mapper[$key][0]];
110 }
111 $mapperFields[] = implode(' - ', $header);
112 }
113 $parser->run($fileName, $separator,
114 $mapperFields,
115 $skipColumnHeader,
116 CRM_Import_Parser::MODE_IMPORT,
117 $this->get('contactType'),
118 $onDuplicate
119 );
120
121 // add all the necessary variables to the form
122 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
123
124 // check if there is any error occured
125
126 $errorStack = CRM_Core_Error::singleton();
127 $errors = $errorStack->getErrors();
128 $errorMessage = array();
129
130 if (is_array($errors)) {
131 foreach ($errors as $key => $value) {
132 $errorMessage[] = $value['message'];
133 }
134
135 $errorFile = $fileName['name'] . '.error.log';
136
137 if ($fd = fopen($errorFile, 'w')) {
138 fwrite($fd, implode('\n', $errorMessage));
139 }
140 fclose($fd);
141
142 $this->set('errorFile', $errorFile);
143 $urlParams = 'type=' . CRM_Import_Parser::ERROR . $this->_importParserUrl;
144 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
145 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . $this->_importParserUrl;
146 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
147 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . $this->_importParserUrl;
148 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
149 }
150 }
151
152 }