Import - remove import button if it will not work
[civicrm-core.git] / CRM / 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 statistics.
20 *
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 */
24 abstract class CRM_Import_Form_Preview extends CRM_Import_Forms {
25
26 /**
27 * Return a descriptive name for the page, used in wizard header.
28 *
29 * @return string
30 */
31 public function getTitle() {
32 return ts('Preview');
33 }
34
35 /**
36 * Assign common values to the template.
37 */
38 public function preProcess() {
39 $this->assignPreviewVariables();
40 }
41
42 /**
43 * Build the form object.
44 */
45 public function buildQuickForm() {
46 $this->addButtons($this->getButtons());
47 }
48
49 /**
50 * Set status url for ajax.
51 */
52 public function setStatusUrl() {
53 $statusID = $this->get('statusID');
54 if (!$statusID) {
55 $statusID = md5(uniqid(rand(), TRUE));
56 $this->set('statusID', $statusID);
57 }
58 $statusUrl = CRM_Utils_System::url('civicrm/ajax/status', "id={$statusID}", FALSE, NULL, FALSE);
59 $this->assign('statusUrl', $statusUrl);
60 }
61
62 /**
63 * Assign smarty variables for the preview screen.
64 *
65 * @throws \API_Exception
66 * @throws \CRM_Core_Exception
67 */
68 protected function assignPreviewVariables(): void {
69 $this->assign('downloadErrorRecordsUrl', $this->getDownloadURL(CRM_Import_Parser::ERROR));
70 $this->assign('invalidRowCount', $this->getRowCount(CRM_Import_Parser::ERROR));
71 $this->assign('validRowCount', $this->getRowCount(CRM_Import_Parser::VALID));
72 $this->assign('totalRowCount', $this->getRowCount([]));
73 $this->assign('mapper', $this->getMappedFieldLabels());
74 $this->assign('dataValues', $this->getDataRows([], 2));
75 $this->assign('columnNames', $this->getColumnHeaders());
76 //get the mapping name displayed if the mappingId is set
77 $mappingId = $this->get('loadMappingId');
78 if ($mappingId) {
79 $mapDAO = new CRM_Core_DAO_Mapping();
80 $mapDAO->id = $mappingId;
81 $mapDAO->find(TRUE);
82 }
83 $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
84 $this->assign('skipColumnHeader', $this->getSubmittedValue('skipColumnHeader'));
85 $this->assign('showColumnNames', $this->getSubmittedValue('skipColumnHeader'));
86 // rowDisplayCount is deprecated - it used to be used with {section} but we have nearly gotten rid of it.
87 $this->assign('rowDisplayCount', $this->getSubmittedValue('skipColumnHeader') ? 3 : 2);
88 }
89
90 /**
91 * Process the mapped fields and map it into the uploaded file
92 * preview the file and extract some summary statistics
93 *
94 * @return void
95 */
96 public function postProcess() {
97 $this->runTheImport();
98 }
99
100 /**
101 * Run the import.
102 */
103 protected function runTheImport(): void {
104 $parser = $this->getParser();
105 $parser->queue();
106 $queue = Civi::queue('user_job_' . $this->getUserJobID());
107 $runner = new CRM_Queue_Runner([
108 'queue' => $queue,
109 'errorMode' => CRM_Queue_Runner::ERROR_ABORT,
110 'onEndUrl' => CRM_Utils_System::url('civicrm/import/contact/summary', [
111 'user_job_id' => $this->getUserJobID(),
112 'reset' => 1,
113 ], FALSE, NULL, FALSE),
114 ]);
115 $runner->runAllViaWeb();
116 }
117
118 /**
119 * Get the buttons for the form.
120 *
121 * @return array|array[]
122 * @throws \CRM_Core_Exception
123 */
124 private function getButtons(): array {
125 // FIXME: This is a hack...
126 // The tpl contains javascript that starts the import on form submit
127 // Since our back/cancel buttons are of html type "submit" we have to prevent a form submit event when they are clicked
128 // Hacking in some onclick js to make them act more like links instead of buttons
129 $path = CRM_Utils_System::currentPath();
130 $query = ['_qf_MapField_display' => 'true'];
131 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String');
132 if (CRM_Utils_Rule::qfKey($qfKey)) {
133 $query['qfKey'] = $qfKey;
134 }
135 $previousURL = CRM_Utils_System::url($path, $query, FALSE, NULL, FALSE);
136 $cancelURL = CRM_Utils_System::url($path, 'reset=1', FALSE, NULL, FALSE);
137 $buttons = [
138 [
139 'type' => 'back',
140 'name' => ts('Previous'),
141 'js' => ['onclick' => "location.href='{$previousURL}'; return false;"],
142 ],
143 ];
144 if ($this->hasImportableRows()) {
145 $buttons[] = [
146 'type' => 'next',
147 'name' => ts('Import Now'),
148 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
149 'isDefault' => TRUE,
150 ];
151 }
152 $buttons[] = [
153 'type' => 'cancel',
154 'name' => ts('Cancel'),
155 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
156 ];
157
158 return $buttons;
159 }
160
161 }