Merge pull request #23520 from eileenmcnaughton/old_code
[civicrm-core.git] / CRM / Member / Import / Form / Preview.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * This class previews the uploaded file and returns summary
20 * statistics
21 */
f532671f 22class CRM_Member_Import_Form_Preview extends CRM_Import_Form_Preview {
6a488035
TO
23
24 /**
fe482240 25 * Set variables up before form is built.
6a488035
TO
26 *
27 * @return void
6a488035
TO
28 */
29 public function preProcess() {
9d7974eb 30 parent::preProcess();
6a488035 31 //get the data from the session
353ffa53
TO
32 $dataValues = $this->get('dataValues');
33 $mapper = $this->get('mapper');
34 $invalidRowCount = $this->get('invalidRowCount');
6a488035
TO
35
36 //get the mapping name displayed if the mappingId is set
37 $mappingId = $this->get('loadMappingId');
38 if ($mappingId) {
39 $mapDAO = new CRM_Core_DAO_Mapping();
40 $mapDAO->id = $mappingId;
41 $mapDAO->find(TRUE);
6a488035 42 }
262b7f26 43 $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
6a488035 44
6a488035 45 if ($invalidRowCount) {
ca4caf13 46 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Member_Import_Parser_Membership';
6a488035
TO
47 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
48 }
49
be2fb01f 50 $properties = [
6a488035 51 'mapper',
353ffa53
TO
52 'dataValues',
53 'columnCount',
54 'totalRowCount',
55 'validRowCount',
56 'invalidRowCount',
6a488035 57 'downloadErrorRecordsUrl',
be2fb01f 58 ];
8cebffb2 59 $this->setStatusUrl();
6a488035
TO
60
61 foreach ($properties as $property) {
62 $this->assign($property, $this->get($property));
63 }
64 }
65
6a488035
TO
66 /**
67 * Process the mapped fields and map it into the uploaded file
68 * preview the file and extract some summary statistics
69 *
70 * @return void
6a488035
TO
71 */
72 public function postProcess() {
5e8faabc 73 $fileName = $this->getSubmittedValue('uploadFile');
353ffa53 74 $invalidRowCount = $this->get('invalidRowCount');
353ffa53 75 $onDuplicate = $this->get('onDuplicate');
6a488035 76
6a488035 77 $mapper = $this->controller->exportValue('MapField', 'mapper');
be2fb01f 78 $mapperKeys = [];
6a488035
TO
79 // Note: we keep the multi-dimension array (even thought it's not
80 // needed in the case of memberships import) so that we can merge
81 // the common code with contacts import later and subclass contact
82 // and membership imports from there
83 foreach ($mapper as $key => $value) {
84 $mapperKeys[$key] = $mapper[$key][0];
6a488035
TO
85 }
86
9b78b60c
EM
87 $parser = new CRM_Member_Import_Parser_Membership($mapperKeys);
88 $parser->setUserJobID($this->getUserJobID());
6a488035
TO
89
90 $mapFields = $this->get('fields');
91
92 foreach ($mapper as $key => $value) {
be2fb01f 93 $header = [];
6a488035
TO
94 if (isset($mapFields[$mapper[$key][0]])) {
95 $header[] = $mapFields[$mapper[$key][0]];
96 }
97 $mapperFields[] = implode(' - ', $header);
98 }
5e8faabc 99 $parser->run($this->getSubmittedValue('uploadFile'), $this->getSubmittedValue('fieldSeparator'),
6a488035 100 $mapperFields,
9d7974eb 101 $this->getSubmittedValue('skipColumnHeader'),
a05662ef 102 CRM_Import_Parser::MODE_IMPORT,
6a488035 103 $this->get('contactType'),
8cebffb2
JP
104 $onDuplicate,
105 $this->get('statusID'),
106 $this->get('totalRowCount')
6a488035
TO
107 );
108
109 // add all the necessary variables to the form
a05662ef 110 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
6a488035 111
b44e3f84 112 // check if there is any error occurred
6a488035
TO
113 $errorStack = CRM_Core_Error::singleton();
114 $errors = $errorStack->getErrors();
be2fb01f 115 $errorMessage = [];
6a488035
TO
116
117 if (is_array($errors)) {
118 foreach ($errors as $key => $value) {
119 $errorMessage[] = $value['message'];
120 }
121
122 $errorFile = $fileName['name'] . '.error.log';
123
124 if ($fd = fopen($errorFile, 'w')) {
125 fwrite($fd, implode('\n', $errorMessage));
126 }
127 fclose($fd);
128
129 $this->set('errorFile', $errorFile);
ca4caf13 130 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Member_Import_Parser_Membership';
6a488035 131 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
6a488035
TO
132 }
133 }
96025800 134
83a1c234
EM
135 /**
136 * @return \CRM_Member_Import_Parser_Membership
137 */
138 protected function getParser(): CRM_Member_Import_Parser_Membership {
139 if (!$this->parser) {
140 $this->parser = new CRM_Member_Import_Parser_Membership();
141 $this->parser->setUserJobID($this->getUserJobID());
142 $this->parser->init();
143 }
144 return $this->parser;
145 }
146
6a488035 147}