CRM/Case add automatically generated comments
[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 * Function to set variables up before form is built
11 *
12 * @return void
13 * @access public
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', 'columnCount',
62 'totalRowCount', 'validRowCount',
63 'invalidRowCount', 'conflictRowCount',
64 'downloadErrorRecordsUrl',
65 'downloadConflictRecordsUrl',
66 'downloadMismatchRecordsUrl',
67 );
68
69 foreach ($properties as $property) {
70 $this->assign($property, $this->get($property));
71 }
72 }
73
74 /**
75 * Process the mapped fields and map it into the uploaded file
76 * preview the file and extract some summary statistics
77 *
78 * @return void
79 * @access public
80 */
81 public function postProcess() {
82 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
83 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
84 $invalidRowCount = $this->get('invalidRowCount');
85 $conflictRowCount = $this->get('conflictRowCount');
86 $onDuplicate = $this->get('onDuplicate');
87 $entity = $this->get('_entity');
88
89 $config = CRM_Core_Config::singleton();
90 $separator = $config->fieldSeparator;
91
92 $mapper = $this->controller->exportValue('MapField', 'mapper');
93 $mapperKeys = array();
94
95 foreach ($mapper as $key => $value) {
96 $mapperKeys[$key] = $mapper[$key][0];
97 }
98
99 $parser = new $this->_parser($mapperKeys);
100 $parser->setEntity($entity);
101
102 $mapFields = $this->get('fields');
103
104 foreach ($mapper as $key => $value) {
105 $header = array();
106 if (isset($mapFields[$mapper[$key][0]])) {
107 $header[] = $mapFields[$mapper[$key][0]];
108 }
109 $mapperFields[] = implode(' - ', $header);
110 }
111 $parser->run($fileName, $separator,
112 $mapperFields,
113 $skipColumnHeader,
114 CRM_Import_Parser::MODE_IMPORT,
115 $this->get('contactType'),
116 $onDuplicate
117 );
118
119 // add all the necessary variables to the form
120 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
121
122 // check if there is any error occured
123
124 $errorStack = CRM_Core_Error::singleton();
125 $errors = $errorStack->getErrors();
126 $errorMessage = array();
127
128 if (is_array($errors)) {
129 foreach ($errors as $key => $value) {
130 $errorMessage[] = $value['message'];
131 }
132
133 $errorFile = $fileName['name'] . '.error.log';
134
135 if ($fd = fopen($errorFile, 'w')) {
136 fwrite($fd, implode('\n', $errorMessage));
137 }
138 fclose($fd);
139
140 $this->set('errorFile', $errorFile);
141 $urlParams = 'type=' . CRM_Import_Parser::ERROR . $this->_importParserUrl;
142 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
143 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . $this->_importParserUrl;
144 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
145 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . $this->_importParserUrl;
146 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
147 }
148 }
149 }