3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class previews the uploaded file and returns summary
40 class CRM_Contribute_Import_Form_Preview
extends CRM_Import_Form_Preview
{
43 * Function to set variables up before form is built
48 public function preProcess() {
49 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
51 //get the data from the session
52 $dataValues = $this->get('dataValues');
53 $mapper = $this->get('mapper');
54 $softCreditFields = $this->get('softCreditFields');
55 $mapperSoftCreditType = $this->get('mapperSoftCreditType');
56 $invalidRowCount = $this->get('invalidRowCount');
57 $conflictRowCount = $this->get('conflictRowCount');
58 $mismatchCount = $this->get('unMatchCount');
60 //get the mapping name displayed if the mappingId is set
61 $mappingId = $this->get('loadMappingId');
63 $mapDAO = new CRM_Core_DAO_Mapping();
64 $mapDAO->id
= $mappingId;
66 $this->assign('loadedMapping', $mappingId);
67 $this->assign('savedName', $mapDAO->name
);
70 if ($skipColumnHeader) {
71 $this->assign('skipColumnHeader', $skipColumnHeader);
72 $this->assign('rowDisplayCount', 3);
75 $this->assign('rowDisplayCount', 2);
78 if ($invalidRowCount) {
79 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contribute_Import_Parser';
80 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
83 if ($conflictRowCount) {
84 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. '&parser=CRM_Contribute_Import_Parser';
85 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
89 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. '&parser=CRM_Contribute_Import_Parser';
90 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
94 'mapper', 'softCreditFields',
95 'mapperSoftCreditType',
96 'dataValues', 'columnCount',
97 'totalRowCount', 'validRowCount',
98 'invalidRowCount', 'conflictRowCount',
99 'downloadErrorRecordsUrl',
100 'downloadConflictRecordsUrl',
101 'downloadMismatchRecordsUrl',
104 foreach ($properties as $property) {
105 $this->assign($property, $this->get($property));
110 * Process the mapped fields and map it into the uploaded file
111 * preview the file and extract some summary statistics
116 public function postProcess() {
117 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
118 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
119 $invalidRowCount = $this->get('invalidRowCount');
120 $conflictRowCount = $this->get('conflictRowCount');
121 $onDuplicate = $this->get('onDuplicate');
122 $mapperSoftCreditType = $this->get('mapperSoftCreditType');
124 $config = CRM_Core_Config
::singleton();
125 $seperator = $config->fieldSeparator
;
127 $mapper = $this->controller
->exportValue('MapField', 'mapper');
128 $mapperKeys = array();
129 $mapperSoftCredit = array();
130 $mapperPhoneType = array();
132 foreach ($mapper as $key => $value) {
133 $mapperKeys[$key] = $mapper[$key][0];
134 if (isset($mapper[$key][0]) && $mapper[$key][0] == 'soft_credit' && isset($mapper[$key])) {
135 $mapperSoftCredit[$key] = isset($mapper[$key][1]) ?
$mapper[$key][1] : '';
136 $mapperSoftCreditType[$key] = $mapperSoftCreditType[$key]['value'];
139 $mapperSoftCredit[$key] = $mapperSoftCreditType[$key] = NULL;
143 $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeys, $mapperSoftCredit, $mapperPhoneType, $mapperSoftCreditType);
145 $mapFields = $this->get('fields');
147 foreach ($mapper as $key => $value) {
149 if (isset($mapFields[$mapper[$key][0]])) {
150 $header[] = $mapFields[$mapper[$key][0]];
152 $mapperFields[] = implode(' - ', $header);
154 $parser->run($fileName, $seperator,
157 CRM_Import_Parser
::MODE_IMPORT
,
158 $this->get('contactType'),
162 // add all the necessary variables to the form
163 $parser->set($this, CRM_Import_Parser
::MODE_IMPORT
);
165 // check if there is any error occured
167 $errorStack = CRM_Core_Error
::singleton();
168 $errors = $errorStack->getErrors();
169 $errorMessage = array();
171 if (is_array($errors)) {
172 foreach ($errors as $key => $value) {
173 $errorMessage[] = $value['message'];
176 $errorFile = $fileName['name'] . '.error.log';
178 if ($fd = fopen($errorFile, 'w')) {
179 fwrite($fd, implode('\n', $errorMessage));
183 $this->set('errorFile', $errorFile);
184 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contribute_Import_Parser';
185 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
186 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. '&parser=CRM_Contribute_Import_Parser';
187 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
188 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. '&parser=CRM_Contribute_Import_Parser';
189 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));