Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-12-22-22-01-44
[civicrm-core.git] / CRM / Activity / Import / Form / Preview.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class previews the uploaded file and returns summary
38 * statistics
39 */
f532671f 40class CRM_Activity_Import_Form_Preview extends CRM_Import_Form_Preview {
6a488035
TO
41
42 /**
100fef9d 43 * Set variables up before form is built
6a488035
TO
44 *
45 * @return void
46 * @access public
47 */
48 public function preProcess() {
de7b9b56 49 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
6a488035
TO
50
51 //get the data from the session
52 $dataValues = $this->get('dataValues');
53 $mapper = $this->get('mapper');
54 $invalidRowCount = $this->get('invalidRowCount');
55 $conflictRowCount = $this->get('conflictRowCount');
56 $mismatchCount = $this->get('unMatchCount');
57
58 //get the mapping name displayed if the mappingId is set
59 $mappingId = $this->get('loadMappingId');
60 if ($mappingId) {
61 $mapDAO = new CRM_Core_DAO_Mapping();
62 $mapDAO->id = $mappingId;
63 $mapDAO->find(TRUE);
64 $this->assign('loadedMapping', $mappingId);
65 $this->assign('savedName', $mapDAO->name);
66 }
67
68 if ($skipColumnHeader) {
69 $this->assign('skipColumnHeader', $skipColumnHeader);
70 $this->assign('rowDisplayCount', 3);
71 }
72 else {
73 $this->assign('rowDisplayCount', 2);
74 }
75
76 if ($invalidRowCount) {
a05662ef 77 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Activity_Import_Parser';
6a488035
TO
78 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
79 }
80
81 if ($conflictRowCount) {
a05662ef 82 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Activity_Import_Parser';
6a488035
TO
83 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
84 }
85
86 if ($mismatchCount) {
a05662ef 87 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Activity_Import_Parser';
6a488035
TO
88 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
89 }
90
91 $properties = array(
92 'mapper',
93 'dataValues', 'columnCount',
94 'totalRowCount', 'validRowCount',
95 'invalidRowCount', 'conflictRowCount',
96 'downloadErrorRecordsUrl',
97 'downloadConflictRecordsUrl',
98 'downloadMismatchRecordsUrl',
99 );
100
101 foreach ($properties as $property) {
102 $this->assign($property, $this->get($property));
103 }
104 }
105
6a488035
TO
106 /**
107 * Process the mapped fields and map it into the uploaded file
108 * preview the file and extract some summary statistics
109 *
110 * @return void
111 * @access public
112 */
113 public function postProcess() {
de7b9b56
CW
114 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
115 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
6a488035
TO
116 $invalidRowCount = $this->get('invalidRowCount');
117 $conflictRowCount = $this->get('conflictRowCount');
118 $onDuplicate = $this->get('onDuplicate');
119
120 $config = CRM_Core_Config::singleton();
121 $seperator = $config->fieldSeparator;
122
123 $mapper = $this->controller->exportValue('MapField', 'mapper');
124 $mapperKeys = array();
125 $mapperLocType = array();
126 $mapperPhoneType = array();
127
128 foreach ($mapper as $key => $value) {
129 $mapperKeys[$key] = $mapper[$key][0];
130
a7488080 131 if (!empty($mapper[$key][1]) && is_numeric($mapper[$key][1])) {
6a488035
TO
132 $mapperLocType[$key] = $mapper[$key][1];
133 }
134 else {
135 $mapperLocType[$key] = NULL;
136 }
137
a7488080 138 if (!empty($mapper[$key][2]) && (!is_numeric($mapper[$key][2]))) {
6a488035
TO
139 $mapperPhoneType[$key] = $mapper[$key][2];
140 }
141 else {
142 $mapperPhoneType[$key] = NULL;
143 }
144 }
145
146 $parser = new CRM_Activity_Import_Parser_Activity($mapperKeys, $mapperLocType, $mapperPhoneType);
147
148 $mapFields = $this->get('fields');
149
150 foreach ($mapper as $key => $value) {
151 $header = array();
152 if (isset($mapFields[$mapper[$key][0]])) {
153 $header[] = $mapFields[$mapper[$key][0]];
154 }
155 $mapperFields[] = implode(' - ', $header);
156 }
157 $parser->run($fileName, $seperator,
158 $mapperFields,
159 $skipColumnHeader,
a05662ef 160 CRM_Import_Parser::MODE_IMPORT,
6a488035
TO
161 $onDuplicate
162 );
163
164 // add all the necessary variables to the form
a05662ef 165 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
6a488035
TO
166
167 // check if there is any error occured
168
169 $errorStack = CRM_Core_Error::singleton();
170 $errors = $errorStack->getErrors();
171 $errorMessage = array();
172
173 if (is_array($errors)) {
174 foreach ($errors as $key => $value) {
175 $errorMessage[] = $value['message'];
176 }
177
178 $errorFile = $fileName['name'] . '.error.log';
179
180 if ($fd = fopen($errorFile, 'w')) {
181 fwrite($fd, implode('\n', $errorMessage));
182 }
183 fclose($fd);
184
185 $this->set('errorFile', $errorFile);
a05662ef 186 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Activity_Import_Parser';
6a488035 187 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
a05662ef 188 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Activity_Import_Parser';
6a488035 189 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
a05662ef 190 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Activity_Import_Parser';
6a488035
TO
191 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
192 }
193 }
194}
195