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 $invalidRowCount = $this->get('invalidRowCount');
32 //get the mapping name displayed if the mappingId is set
33 $mappingId = $this->get('loadMappingId');
35 $mapDAO = new CRM_Core_DAO_Mapping();
36 $mapDAO->id
= $mappingId;
39 $this->assign('savedMappingName', $mappingId ?
$mapDAO->name
: NULL);
41 if ($invalidRowCount) {
42 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contribute_Import_Parser_Contribution';
43 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
52 'downloadErrorRecordsUrl',
54 $this->setStatusUrl();
55 $this->assign('mapper', $this->getMappedFieldLabels());
57 foreach ($properties as $property) {
58 $this->assign($property, $this->get($property));
63 * Get the mapped fields as an array of labels.
66 * ['First Name', 'Employee Of - First Name', 'Home - Street Address']
69 * @throws \API_Exception
70 * @throws \CRM_Core_Exception
72 protected function getMappedFieldLabels(): array {
74 $parser = $this->getParser();
75 foreach ($this->getSubmittedValue('mapper') as $columnNumber => $mappedField) {
76 $mapper[$columnNumber] = $parser->getMappedFieldLabel($parser->getMappingFieldFromMapperInput($mappedField, 0, $columnNumber));
82 * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics.
84 public function postProcess() {
85 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
86 $onDuplicate = $this->get('onDuplicate');
87 $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues());
88 $mapper = $this->controller
->exportValue('MapField', 'mapper');
90 $parser = new CRM_Contribute_Import_Parser_Contribution();
91 $parser->setUserJobID($this->getUserJobID());
93 $mapFields = $this->get('fields');
95 foreach ($mapper as $key => $value) {
97 if (isset($mapFields[$mapper[$key][0]])) {
98 $header[] = $mapFields[$mapper[$key][0]];
100 $mapperFields[] = implode(' - ', $header);
103 $this->getSubmittedValue('uploadFile'),
104 $this->getSubmittedValue('fieldSeparator'),
106 $this->getSubmittedValue('skipColumnHeader'),
107 CRM_Import_Parser
::MODE_IMPORT
,
108 $this->get('contactType'),
110 $this->get('statusID'),
111 $this->get('totalRowCount')
114 // Add all the necessary variables to the form.
115 $parser->set($this, CRM_Import_Parser
::MODE_IMPORT
);
117 // Check if there is any error occurred.
119 $errorStack = CRM_Core_Error
::singleton();
120 $errors = $errorStack->getErrors();
123 if (is_array($errors)) {
124 foreach ($errors as $key => $value) {
125 $errorMessage[] = $value['message'];
128 $errorFile = $fileName['name'] . '.error.log';
130 if ($fd = fopen($errorFile, 'w')) {
131 fwrite($fd, implode('\n', $errorMessage));
135 $this->set('errorFile', $errorFile);
136 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contribute_Import_Parser_Contribution';
137 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
142 * @return \CRM_Contribute_Import_Parser_Contribution
144 protected function getParser(): CRM_Contribute_Import_Parser_Contribution
{
145 if (!$this->parser
) {
146 $this->parser
= new CRM_Contribute_Import_Parser_Contribution();
147 $this->parser
->setUserJobID($this->getUserJobID());
148 $this->parser
->init();
150 return $this->parser
;