Merge pull request #16541 from bhahumanists/subtype-issue-991
[civicrm-core.git] / CRM / Contact / Import / Form / DataSource.php
index 013a2d93ac4120e9e974edf9f408914d0b7bcfe6..97ffa8453d8880fedc7e704b2df34faa01a4491d 100644 (file)
@@ -1,34 +1,20 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 5                                                  |
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2019                                |
- +--------------------------------------------------------------------+
- | This file is a part of CiviCRM.                                    |
- |                                                                    |
- | CiviCRM is free software; you can copy, modify, and distribute it  |
- | under the terms of the GNU Affero General Public License           |
- | Version 3, 19 November 2009 and the CiviCRM Licensing Exception.   |
- |                                                                    |
- | CiviCRM is distributed in the hope that it will be useful, but     |
- | WITHOUT ANY WARRANTY; without even the implied warranty of         |
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
- | See the GNU Affero General Public License for more details.        |
+ | Copyright CiviCRM LLC. All rights reserved.                        |
  |                                                                    |
- | You should have received a copy of the GNU Affero General Public   |
- | License and the CiviCRM Licensing Exception along                  |
- | with this program; if not, contact CiviCRM LLC                     |
- | at info[AT]civicrm[DOT]org. If you have questions about the        |
- | GNU Affero General Public License or the licensing of CiviCRM,     |
- | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
  +--------------------------------------------------------------------+
  */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2019
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ * $Id$
+ *
  */
 
 /**
@@ -54,13 +40,15 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Core_Form {
     //Test database user privilege to create table(Temporary) CRM-4725
     $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
     $daoTestPrivilege = new CRM_Core_DAO();
-    $daoTestPrivilege->query("CREATE TEMPORARY TABLE import_job_permission_one(test int) ENGINE=InnoDB");
-    $daoTestPrivilege->query("CREATE TEMPORARY TABLE import_job_permission_two(test int) ENGINE=InnoDB");
-    $daoTestPrivilege->query("DROP TEMPORARY TABLE IF EXISTS import_job_permission_one, import_job_permission_two");
+    $tempTable1 = CRM_Utils_SQL_TempTable::build()->getName();
+    $tempTable2 = CRM_Utils_SQL_TempTable::build()->getName();
+    $daoTestPrivilege->query("CREATE TEMPORARY TABLE {$tempTable1} (test int) ENGINE=InnoDB");
+    $daoTestPrivilege->query("CREATE TEMPORARY TABLE {$tempTable2} (test int) ENGINE=InnoDB");
+    $daoTestPrivilege->query("DROP TEMPORARY TABLE IF EXISTS {$tempTable1}, {$tempTable2}");
     unset($errorScope);
 
     if ($daoTestPrivilege->_lastError) {
-      CRM_Core_Error::fatal(ts('Database Configuration Error: Insufficient permissions. Import requires that the CiviCRM database user has permission to create temporary tables. Contact your site administrator for assistance.'));
+      $this->invalidConfig(ts('Database Configuration Error: Insufficient permissions. Import requires that the CiviCRM database user has permission to create temporary tables. Contact your site administrator for assistance.'));
     }
 
     $results = [];
@@ -69,7 +57,7 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Core_Form {
     $errorFiles = ['sqlImport.errors', 'sqlImport.conflicts', 'sqlImport.duplicates', 'sqlImport.mismatch'];
 
     // check for post max size avoid when called twice
-    $snippet = CRM_Utils_Array::value('snippet', $_GET, 0);
+    $snippet = $_GET['snippet'] ?? 0;
     if (empty($snippet)) {
       CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE);
     }
@@ -83,7 +71,7 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Core_Form {
     }
     closedir($handler);
     if (!empty($results)) {
-      CRM_Core_Error::fatal(ts('<b>%1</b> file(s) in %2 directory are not writable. Listed file(s) might be used during the import to log the errors occurred during Import process. Contact your site administrator for assistance.', [
+      $this->invalidConfig(ts('<b>%1</b> file(s) in %2 directory are not writable. Listed file(s) might be used during the import to log the errors occurred during Import process. Contact your site administrator for assistance.', [
         1 => implode(', ', $results),
         2 => $config->uploadDir,
       ]));
@@ -122,7 +110,7 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Core_Form {
       $this->assign('dataSourceFormTemplateFile', $templateFile);
     }
     elseif ($this->_dataSource) {
-      throw new \CRM_Core_Exception("Invalid data source");
+      $this->invalidConfig('Invalid data source');
     }
   }
 
@@ -270,10 +258,10 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Core_Form {
     $dataSourceDir = $civicrm_root . DIRECTORY_SEPARATOR . 'CRM' . DIRECTORY_SEPARATOR . 'Import' . DIRECTORY_SEPARATOR . 'DataSource' . DIRECTORY_SEPARATOR;
     $dataSources = [];
     if (!is_dir($dataSourceDir)) {
-      CRM_Core_Error::fatal("Import DataSource directory $dataSourceDir does not exist");
+      $this->invalidConfig("Import DataSource directory $dataSourceDir does not exist");
     }
     if (!$dataSourceHandle = opendir($dataSourceDir)) {
-      CRM_Core_Error::fatal("Unable to access DataSource directory $dataSourceDir");
+      $this->invalidConfig("Unable to access DataSource directory $dataSourceDir");
     }
 
     while (($dataSourceFile = readdir($dataSourceHandle)) !== FALSE) {
@@ -363,7 +351,7 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Core_Form {
       $parser->set($this);
     }
     else {
-      CRM_Core_Error::fatal("Invalid DataSource on form post. This shouldn't happen!");
+      $this->invalidConfig("Invalid DataSource on form post. This shouldn't happen!");
     }
   }
 
@@ -406,6 +394,21 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Core_Form {
     return ['status' => $statusFieldName, 'pk' => $primaryKeyName];
   }
 
+  /**
+   * General function for handling invalid configuration.
+   *
+   * I was going to statusBounce them all but when I tested I was 'bouncing' to weird places
+   * whereas throwing an exception gave no behaviour change. So, I decided to centralise
+   * and we can 'flip the switch' later.
+   *
+   * @param $message
+   *
+   * @throws \CRM_Core_Exception
+   */
+  protected function invalidConfig($message) {
+    throw new CRM_Core_Exception($message);
+  }
+
   /**
    * Return a descriptive name for the page, used in wizard header
    *