Merge pull request #23419 from chrisgaraffa/contactheader-regions
[civicrm-core.git] / CRM / Import / Form / Preview.php
CommitLineData
f532671f
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
f532671f 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 |
f532671f 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
f532671f
CW
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
f532671f
CW
16 */
17
18/**
2b4bc760 19 * This class previews the uploaded file and returns summary statistics.
20 *
f532671f
CW
21 * TODO: CRM-11254 - if preProcess and postProcess functions can be reconciled between the 5 child classes,
22 * those classes can be removed entirely and this class will not need to be abstract
23 */
9d7974eb 24abstract class CRM_Import_Form_Preview extends CRM_Import_Forms {
971e129b 25
f532671f 26 /**
2b4bc760 27 * Return a descriptive name for the page, used in wizard header.
f532671f
CW
28 *
29 * @return string
f532671f
CW
30 */
31 public function getTitle() {
32 return ts('Preview');
33 }
34
9d7974eb
EM
35 /**
36 * Assign common values to the template.
37 */
38 public function preProcess() {
4d9f4d69 39 $this->assignPreviewVariables();
9d7974eb
EM
40 }
41
f532671f 42 /**
fe482240 43 * Build the form object.
f532671f
CW
44 */
45 public function buildQuickForm() {
4c7e1926
CW
46
47 // FIXME: This is a hack...
48 // The tpl contains javascript that starts the import on form submit
49 // Since our back/cancel buttons are of html type "submit" we have to prevent a form submit event when they are clicked
50 // Hacking in some onclick js to make them act more like links instead of buttons
51 $path = CRM_Utils_System::currentPath();
52 $query = ['_qf_MapField_display' => 'true'];
53 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String');
54 if (CRM_Utils_Rule::qfKey($qfKey)) {
55 $query['qfKey'] = $qfKey;
56 }
57 $previousURL = CRM_Utils_System::url($path, $query, FALSE, NULL, FALSE);
58 $cancelURL = CRM_Utils_System::url($path, 'reset=1', FALSE, NULL, FALSE);
59
be2fb01f 60 $this->addButtons([
4c7e1926
CW
61 [
62 'type' => 'back',
63 'name' => ts('Previous'),
64 'js' => ['onclick' => "location.href='{$previousURL}'; return false;"],
65 ],
66 [
67 'type' => 'next',
68 'name' => ts('Import Now'),
69 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
70 'isDefault' => TRUE,
71 ],
72 [
73 'type' => 'cancel',
74 'name' => ts('Cancel'),
75 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
76 ],
971e129b 77 ]);
f532671f
CW
78 }
79
8cebffb2
JP
80 /**
81 * Set status url for ajax.
82 */
83 public function setStatusUrl() {
84 $statusID = $this->get('statusID');
85 if (!$statusID) {
86 $statusID = md5(uniqid(rand(), TRUE));
87 $this->set('statusID', $statusID);
88 }
89 $statusUrl = CRM_Utils_System::url('civicrm/ajax/status', "id={$statusID}", FALSE, NULL, FALSE);
90 $this->assign('statusUrl', $statusUrl);
91 }
92
4d9f4d69
EM
93 /**
94 * Assign smarty variables for the preview screen.
95 *
96 * @throws \API_Exception
97 * @throws \CRM_Core_Exception
98 */
99 protected function assignPreviewVariables(): void {
100 $this->assign('downloadErrorRecordsUrl', $this->getDownloadURL(CRM_Import_Parser::ERROR));
101 $this->assign('invalidRowCount', $this->getRowCount(CRM_Import_Parser::ERROR));
102 $this->assign('validRowCount', $this->getRowCount(CRM_Import_Parser::VALID));
103 $this->assign('totalRowCount', $this->getRowCount([]));
104 $this->assign('mapper', $this->getMappedFieldLabels());
105 $this->assign('dataValues', $this->getDataRows([], 2));
106 $this->assign('columnNames', $this->getColumnHeaders());
107 //get the mapping name displayed if the mappingId is set
108 $mappingId = $this->get('loadMappingId');
109 if ($mappingId) {
110 $mapDAO = new CRM_Core_DAO_Mapping();
111 $mapDAO->id = $mappingId;
112 $mapDAO->find(TRUE);
113 }
114 $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
115 $this->assign('skipColumnHeader', $this->getSubmittedValue('skipColumnHeader'));
fb8d5738 116 $this->assign('showColumnNames', $this->getSubmittedValue('skipColumnHeader'));
4d9f4d69
EM
117 // rowDisplayCount is deprecated - it used to be used with {section} but we have nearly gotten rid of it.
118 $this->assign('rowDisplayCount', $this->getSubmittedValue('skipColumnHeader') ? 3 : 2);
119 }
120
992a3d9e
EM
121 /**
122 * Process the mapped fields and map it into the uploaded file
123 * preview the file and extract some summary statistics
124 *
125 * @return void
126 */
127 public function postProcess() {
4ff86743
EM
128 $this->runTheImport();
129 }
130
131 /**
132 * Run the import.
133 */
134 protected function runTheImport(): void {
135 $parser = $this->getParser();
136 $parser->queue();
137 $queue = Civi::queue('user_job_' . $this->getUserJobID());
138 $runner = new CRM_Queue_Runner([
139 'queue' => $queue,
140 'errorMode' => CRM_Queue_Runner::ERROR_ABORT,
141 'onEndUrl' => CRM_Utils_System::url('civicrm/import/contact/summary', [
142 'user_job_id' => $this->getUserJobID(),
143 'reset' => 1,
144 ]),
145 ]);
146 $runner->runAllViaWeb();
992a3d9e
EM
147 }
148
f532671f 149}