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
22 class CRM_Member_Import_Form_Preview
extends CRM_Import_Form_Preview
{
25 * Set variables up before form is built.
29 public function preProcess() {
31 //get the data from the session
32 $dataValues = $this->get('dataValues');
33 $mapper = $this->get('mapper');
34 $invalidRowCount = $this->get('invalidRowCount');
36 //get the mapping name displayed if the mappingId is set
37 $mappingId = $this->get('loadMappingId');
39 $mapDAO = new CRM_Core_DAO_Mapping();
40 $mapDAO->id
= $mappingId;
43 $this->assign('savedMappingName', $mappingId ?
$mapDAO->name
: NULL);
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));
57 'downloadErrorRecordsUrl',
59 $this->setStatusUrl();
61 foreach ($properties as $property) {
62 $this->assign($property, $this->get($property));
67 * Process the mapped fields and map it into the uploaded file
68 * preview the file and extract some summary statistics
72 public function postProcess() {
73 $fileName = $this->getSubmittedValue('uploadFile');
74 $invalidRowCount = $this->get('invalidRowCount');
75 $onDuplicate = $this->get('onDuplicate');
77 $mapper = $this->controller
->exportValue('MapField', 'mapper');
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];
87 $parser = new CRM_Member_Import_Parser_Membership($mapperKeys);
88 $parser->setUserJobID($this->getUserJobID());
90 $mapFields = $this->get('fields');
92 foreach ($mapper as $key => $value) {
94 if (isset($mapFields[$mapper[$key][0]])) {
95 $header[] = $mapFields[$mapper[$key][0]];
97 $mapperFields[] = implode(' - ', $header);
99 $parser->run($this->getSubmittedValue('uploadFile'), $this->getSubmittedValue('fieldSeparator'),
101 $this->getSubmittedValue('skipColumnHeader'),
102 CRM_Import_Parser
::MODE_IMPORT
,
103 $this->get('contactType'),
105 $this->get('statusID'),
106 $this->get('totalRowCount')
109 // add all the necessary variables to the form
110 $parser->set($this, CRM_Import_Parser
::MODE_IMPORT
);
112 // check if there is any error occurred
113 $errorStack = CRM_Core_Error
::singleton();
114 $errors = $errorStack->getErrors();
117 if (is_array($errors)) {
118 foreach ($errors as $key => $value) {
119 $errorMessage[] = $value['message'];
122 $errorFile = $fileName['name'] . '.error.log';
124 if ($fd = fopen($errorFile, 'w')) {
125 fwrite($fd, implode('\n', $errorMessage));
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));
136 * @return \CRM_Member_Import_Parser_Membership
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();
144 return $this->parser
;