[Import] Simplify and limit dataSource retrieval
[civicrm-core.git] / CRM / Import / Forms.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 helps the forms within the import flow access submitted & parsed values.
20 */
21 class CRM_Import_Forms extends CRM_Core_Form {
22
23 /**
24 * Get the submitted value, accessing it from whatever form in the flow it is submitted on.
25 * @param string $fieldName
26 *
27 * @return mixed|null
28 */
29 public function getSubmittedValue(string $fieldName) {
30 $mappedValues = [
31 'skipColumnHeader' => 'DataSource',
32 'fieldSeparator' => 'DataSource',
33 'uploadFile' => 'DataSource',
34 'contactType' => 'DataSource',
35 'dateFormats' => 'DataSource',
36 'savedMapping' => 'DataSource',
37 ];
38 if (array_key_exists($fieldName, $mappedValues)) {
39 return $this->controller->exportValue($mappedValues[$fieldName], $fieldName);
40 }
41 return parent::getSubmittedValue($fieldName);
42
43 }
44
45 /**
46 * Get the available datasource.
47 *
48 * Permission dependent, this will look like
49 * [
50 * 'CRM_Import_DataSource_CSV' => 'Comma-Separated Values (CSV)',
51 * 'CRM_Import_DataSource_SQL' => 'SQL Query',
52 * ]
53 *
54 * The label is translated.
55 *
56 * @return array
57 */
58 protected function getDataSources(): array {
59 $dataSources = [];
60 foreach (['CRM_Import_DataSource_SQL', 'CRM_Import_DataSource_CSV'] as $dataSourceClass) {
61 $object = new $dataSourceClass();
62 if ($object->checkPermission()) {
63 $dataSources[$dataSourceClass] = $object->getInfo()['title'];
64 }
65 }
66 return $dataSources;
67 }
68
69 }