Import - remove import button if it will not work
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 22 Aug 2022 20:52:59 +0000 (08:52 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 23 Aug 2022 00:59:55 +0000 (12:59 +1200)
CRM/Import/Form/Preview.php
CRM/Import/Forms.php

index 5fff559d0acc47d926665c225b2e468bc6d2e9ed..693a612f46e142ea37592a5a96e1fc89ed103acc 100644 (file)
@@ -43,38 +43,7 @@ abstract class CRM_Import_Form_Preview extends CRM_Import_Forms {
    * Build the form object.
    */
   public function buildQuickForm() {
-
-    // FIXME: This is a hack...
-    // The tpl contains javascript that starts the import on form submit
-    // Since our back/cancel buttons are of html type "submit" we have to prevent a form submit event when they are clicked
-    // Hacking in some onclick js to make them act more like links instead of buttons
-    $path = CRM_Utils_System::currentPath();
-    $query = ['_qf_MapField_display' => 'true'];
-    $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String');
-    if (CRM_Utils_Rule::qfKey($qfKey)) {
-      $query['qfKey'] = $qfKey;
-    }
-    $previousURL = CRM_Utils_System::url($path, $query, FALSE, NULL, FALSE);
-    $cancelURL = CRM_Utils_System::url($path, 'reset=1', FALSE, NULL, FALSE);
-
-    $this->addButtons([
-      [
-        'type' => 'back',
-        'name' => ts('Previous'),
-        'js' => ['onclick' => "location.href='{$previousURL}'; return false;"],
-      ],
-      [
-        'type' => 'next',
-        'name' => ts('Import Now'),
-        'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
-        'isDefault' => TRUE,
-      ],
-      [
-        'type' => 'cancel',
-        'name' => ts('Cancel'),
-        'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
-      ],
-    ]);
+    $this->addButtons($this->getButtons());
   }
 
   /**
@@ -146,4 +115,47 @@ abstract class CRM_Import_Form_Preview extends CRM_Import_Forms {
     $runner->runAllViaWeb();
   }
 
+  /**
+   * Get the buttons for the form.
+   *
+   * @return array|array[]
+   * @throws \CRM_Core_Exception
+   */
+  private function getButtons(): array {
+    // FIXME: This is a hack...
+    // The tpl contains javascript that starts the import on form submit
+    // Since our back/cancel buttons are of html type "submit" we have to prevent a form submit event when they are clicked
+    // Hacking in some onclick js to make them act more like links instead of buttons
+    $path = CRM_Utils_System::currentPath();
+    $query = ['_qf_MapField_display' => 'true'];
+    $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String');
+    if (CRM_Utils_Rule::qfKey($qfKey)) {
+      $query['qfKey'] = $qfKey;
+    }
+    $previousURL = CRM_Utils_System::url($path, $query, FALSE, NULL, FALSE);
+    $cancelURL = CRM_Utils_System::url($path, 'reset=1', FALSE, NULL, FALSE);
+    $buttons = [
+      [
+        'type' => 'back',
+        'name' => ts('Previous'),
+        'js' => ['onclick' => "location.href='{$previousURL}'; return false;"],
+      ],
+    ];
+    if ($this->hasImportableRows()) {
+      $buttons[] = [
+        'type' => 'next',
+        'name' => ts('Import Now'),
+        'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
+        'isDefault' => TRUE,
+      ];
+    }
+    $buttons[] = [
+      'type' => 'cancel',
+      'name' => ts('Cancel'),
+      'js' => ['onclick' => "location.href='{$cancelURL}'; return false;"],
+    ];
+
+    return $buttons;
+  }
+
 }
index c9636b25bcff91a516c528a8832d069f97661759..a274f8ceb2490f5f5f7eafde9f8a8de3a6f2d70f 100644 (file)
@@ -683,4 +683,15 @@ class CRM_Import_Forms extends CRM_Core_Form {
     return ((int) $this->getSubmittedValue('onDuplicate')) === CRM_Import_Parser::DUPLICATE_SKIP;
   }
 
+  /**
+   * Are there valid rows to import.
+   *
+   * @return bool
+   *
+   * @throws \CRM_Core_Exception
+   */
+  protected function hasImportableRows(): bool {
+    return (bool) $this->getRowCount(['new']);
+  }
+
 }