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