3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class previews the uploaded file and returns summary statistics.
21 class CRM_Contribute_Import_Form_Preview
extends CRM_Import_Form_Preview
{
24 * Set variables up before form is built.
26 public function preProcess() {
28 //get the data from the session
29 $dataValues = $this->get('dataValues');
30 $mapper = $this->get('mapper');
31 $softCreditFields = $this->get('softCreditFields');
32 $mapperSoftCreditType = $this->get('mapperSoftCreditType');
33 $invalidRowCount = $this->get('invalidRowCount');
35 //get the mapping name displayed if the mappingId is set
36 $mappingId = $this->get('loadMappingId');
38 $mapDAO = new CRM_Core_DAO_Mapping();
39 $mapDAO->id
= $mappingId;
42 $this->assign('savedMappingName', $mappingId ?
$mapDAO->name
: NULL);
44 if ($invalidRowCount) {
45 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contribute_Import_Parser_Contribution';
46 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
52 'mapperSoftCreditType',
58 'downloadErrorRecordsUrl',
60 $this->setStatusUrl();
62 foreach ($properties as $property) {
63 $this->assign($property, $this->get($property));
68 * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics.
70 public function postProcess() {
71 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
72 $invalidRowCount = $this->get('invalidRowCount');
73 $onDuplicate = $this->get('onDuplicate');
74 $mapperSoftCreditType = $this->get('mapperSoftCreditType');
76 $mapper = $this->controller
->exportValue('MapField', 'mapper');
78 $mapperSoftCredit = [];
79 $mapperPhoneType = [];
81 foreach ($mapper as $key => $value) {
82 $mapperKeys[$key] = $mapper[$key][0];
83 if (isset($mapper[$key][0]) && $mapper[$key][0] == 'soft_credit' && isset($mapper[$key])) {
84 $mapperSoftCredit[$key] = $mapper[$key][1] ??
'';
85 $mapperSoftCreditType[$key] = $mapperSoftCreditType[$key]['value'];
88 $mapperSoftCredit[$key] = $mapperSoftCreditType[$key] = NULL;
92 $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeys, $mapperSoftCredit, $mapperPhoneType, $mapperSoftCreditType);
94 $mapFields = $this->get('fields');
96 foreach ($mapper as $key => $value) {
98 if (isset($mapFields[$mapper[$key][0]])) {
99 $header[] = $mapFields[$mapper[$key][0]];
101 $mapperFields[] = implode(' - ', $header);
104 $this->getSubmittedValue('uploadFile'),
105 $this->getSubmittedValue('fieldSeparator'),
107 $this->getSubmittedValue('skipColumnHeader'),
108 CRM_Import_Parser
::MODE_IMPORT
,
109 $this->get('contactType'),
111 $this->get('statusID'),
112 $this->get('totalRowCount')
115 // Add all the necessary variables to the form.
116 $parser->set($this, CRM_Import_Parser
::MODE_IMPORT
);
118 // Check if there is any error occurred.
120 $errorStack = CRM_Core_Error
::singleton();
121 $errors = $errorStack->getErrors();
124 if (is_array($errors)) {
125 foreach ($errors as $key => $value) {
126 $errorMessage[] = $value['message'];
129 $errorFile = $fileName['name'] . '.error.log';
131 if ($fd = fopen($errorFile, 'w')) {
132 fwrite($fd, implode('\n', $errorMessage));
136 $this->set('errorFile', $errorFile);
137 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contribute_Import_Parser_Contribution';
138 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
143 * @return \CRM_Contribute_Import_Parser_Contribution
145 protected function getParser(): CRM_Contribute_Import_Parser_Contribution
{
146 if (!$this->parser
) {
147 $this->parser
= new CRM_Contribute_Import_Parser_Contribution();
148 $this->parser
->setUserJobID($this->getUserJobID());
149 $this->parser
->init();
151 return $this->parser
;