Merge pull request #23675 from demeritcowboy/test-sequential
[civicrm-core.git] / CRM / Member / Import / Form / Preview.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class previews the uploaded file and returns summary
20 * statistics
21 */
22 class CRM_Member_Import_Form_Preview extends CRM_Import_Form_Preview {
23
24 /**
25 * Set variables up before form is built.
26 *
27 * @return void
28 */
29 public function preProcess() {
30 parent::preProcess();
31 //get the data from the session
32 $dataValues = $this->get('dataValues');
33 $mapper = $this->get('mapper');
34 $invalidRowCount = $this->get('invalidRowCount');
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);
42 }
43 $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
44
45 if ($invalidRowCount) {
46 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Member_Import_Parser_Membership';
47 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
48 }
49
50 $properties = [
51 'mapper',
52 'dataValues',
53 'columnCount',
54 'totalRowCount',
55 'validRowCount',
56 'invalidRowCount',
57 'downloadErrorRecordsUrl',
58 ];
59 $this->setStatusUrl();
60
61 foreach ($properties as $property) {
62 $this->assign($property, $this->get($property));
63 }
64 }
65
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
71 */
72 public function postProcess() {
73 $fileName = $this->getSubmittedValue('uploadFile');
74 $invalidRowCount = $this->get('invalidRowCount');
75 $onDuplicate = $this->get('onDuplicate');
76
77 $mapper = $this->controller->exportValue('MapField', 'mapper');
78 $mapperKeys = [];
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];
85 }
86
87 $parser = new CRM_Member_Import_Parser_Membership($mapperKeys);
88 $parser->setUserJobID($this->getUserJobID());
89
90 $mapFields = $this->get('fields');
91
92 foreach ($mapper as $key => $value) {
93 $header = [];
94 if (isset($mapFields[$mapper[$key][0]])) {
95 $header[] = $mapFields[$mapper[$key][0]];
96 }
97 $mapperFields[] = implode(' - ', $header);
98 }
99 $parser->run($this->getSubmittedValue('uploadFile'), $this->getSubmittedValue('fieldSeparator'),
100 $mapperFields,
101 $this->getSubmittedValue('skipColumnHeader'),
102 CRM_Import_Parser::MODE_IMPORT,
103 $this->get('contactType'),
104 $onDuplicate,
105 $this->get('statusID'),
106 $this->get('totalRowCount')
107 );
108
109 // add all the necessary variables to the form
110 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
111
112 // check if there is any error occurred
113 $errorStack = CRM_Core_Error::singleton();
114 $errors = $errorStack->getErrors();
115 $errorMessage = [];
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);
130 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Member_Import_Parser_Membership';
131 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
132 }
133 }
134
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
147 }