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 $invalidRowCount = $this->getRowCount(CRM_Import_Parser
::VALID
);
31 if ($invalidRowCount) {
32 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contribute_Import_Parser_Contribution';
33 $downloadURL = CRM_Utils_System
::url('civicrm/export', $urlParams);
36 $this->setStatusUrl();
37 $this->assign('downloadErrorRecordsUrl', $downloadURL);
41 * Get the mapped fields as an array of labels.
44 * ['First Name', 'Employee Of - First Name', 'Home - Street Address']
47 * @throws \API_Exception
48 * @throws \CRM_Core_Exception
50 protected function getMappedFieldLabels(): array {
52 $parser = $this->getParser();
53 foreach ($this->getSubmittedValue('mapper') as $columnNumber => $mappedField) {
54 $mapper[$columnNumber] = $parser->getMappedFieldLabel($parser->getMappingFieldFromMapperInput($mappedField, 0, $columnNumber));
60 * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics.
62 public function postProcess() {
63 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
64 $onDuplicate = $this->get('onDuplicate');
65 $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues());
66 $mapper = $this->controller
->exportValue('MapField', 'mapper');
68 $parser = new CRM_Contribute_Import_Parser_Contribution();
69 $parser->setUserJobID($this->getUserJobID());
71 $mapFields = $this->get('fields');
73 foreach ($mapper as $key => $value) {
75 if (isset($mapFields[$mapper[$key][0]])) {
76 $header[] = $mapFields[$mapper[$key][0]];
78 $mapperFields[] = implode(' - ', $header);
81 $this->getSubmittedValue('uploadFile'),
82 $this->getSubmittedValue('fieldSeparator'),
84 $this->getSubmittedValue('skipColumnHeader'),
85 CRM_Import_Parser
::MODE_IMPORT
,
86 $this->getSubmittedValue('contactType'),
88 $this->get('statusID'),
89 $this->get('totalRowCount')
92 // Add all the necessary variables to the form.
93 $parser->set($this, CRM_Import_Parser
::MODE_IMPORT
);
95 // Check if there is any error occurred.
97 $errorStack = CRM_Core_Error
::singleton();
98 $errors = $errorStack->getErrors();
101 if (is_array($errors)) {
102 foreach ($errors as $key => $value) {
103 $errorMessage[] = $value['message'];
106 $errorFile = $fileName['name'] . '.error.log';
108 if ($fd = fopen($errorFile, 'w')) {
109 fwrite($fd, implode('\n', $errorMessage));
113 $this->set('errorFile', $errorFile);
114 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. '&parser=CRM_Contribute_Import_Parser_Contribution';
115 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
120 * @return \CRM_Contribute_Import_Parser_Contribution
122 protected function getParser(): CRM_Contribute_Import_Parser_Contribution
{
123 if (!$this->parser
) {
124 $this->parser
= new CRM_Contribute_Import_Parser_Contribution();
125 $this->parser
->setUserJobID($this->getUserJobID());
126 $this->parser
->init();
128 return $this->parser
;