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 statistics.
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
24 abstract class CRM_Import_Form_Preview
extends CRM_Import_Forms
{
27 * Return a descriptive name for the page, used in wizard header.
31 public function getTitle() {
36 * Assign common values to the template.
38 public function preProcess() {
39 $this->assignPreviewVariables();
43 * Build the form object.
45 public function buildQuickForm() {
46 $this->addButtons($this->getButtons());
50 * Set status url for ajax.
52 public function setStatusUrl() {
53 $statusID = $this->get('statusID');
55 $statusID = md5(uniqid(rand(), TRUE));
56 $this->set('statusID', $statusID);
58 $statusUrl = CRM_Utils_System
::url('civicrm/ajax/status', "id={$statusID}", FALSE, NULL, FALSE);
59 $this->assign('statusUrl', $statusUrl);
63 * Assign smarty variables for the preview screen.
65 * @throws \API_Exception
66 * @throws \CRM_Core_Exception
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');
79 $mapDAO = new CRM_Core_DAO_Mapping();
80 $mapDAO->id
= $mappingId;
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);
91 * Process the mapped fields and map it into the uploaded file
92 * preview the file and extract some summary statistics
96 public function postProcess() {
97 $this->runTheImport();
103 protected function runTheImport(): void
{
104 $parser = $this->getParser();
106 $queue = Civi
::queue('user_job_' . $this->getUserJobID());
107 $runner = new CRM_Queue_Runner([
109 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
110 'onEndUrl' => CRM_Utils_System
::url('civicrm/import/contact/summary', [
111 'user_job_id' => $this->getUserJobID(),
113 ], FALSE, NULL, FALSE),
115 $runner->runAllViaWeb();
119 * Get the buttons for the form.
121 * @return array|array[]
122 * @throws \CRM_Core_Exception
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;
135 $previousURL = CRM_Utils_System
::url($path, $query, FALSE, NULL, FALSE);
136 $cancelURL = CRM_Utils_System
::url($path, 'reset=1', FALSE, NULL, FALSE);
140 'name' => ts('Previous'),
141 'js' => ['onclick' => "location.href='{$previousURL}'; return false;"],
144 if ($this->hasImportableRows()) {
147 'name' => ts('Import Now'),
148 'spacing' => ' ',
154 'name' => ts('Cancel'),
155 'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],