Merge pull request #21513 from JKingsnorth/core-2846-1-improve-start-end-date-validation
[civicrm-core.git] / CRM / Import / Form / DataSourceConfig.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 allows datasource specific fields to be added to the datasource form.
20 */
21 class CRM_Import_Form_DataSourceConfig extends CRM_Import_Forms {
22
23 /**
24 * Set variables up before form is built.
25 *
26 * @throws \CRM_Core_Exception
27 */
28 public function preProcess(): void {
29 $dataSourcePath = explode('_', $this->getDataSourceClassName());
30 $templateFile = 'CRM/Contact/Import/Form/' . $dataSourcePath[3] . '.tpl';
31 $this->assign('dataSourceFormTemplateFile', $templateFile ?? NULL);
32 if (CRM_Utils_Request::retrieveValue('user_job_id', 'Integer')) {
33 $this->setUserJobID(CRM_Utils_Request::retrieveValue('user_job_id', 'Integer'));
34 }
35 }
36
37 /**
38 * Build the form object.
39 *
40 * @throws \CRM_Core_Exception
41 */
42 public function buildQuickForm(): void {
43 $this->buildDataSourceFields();
44 }
45
46 /**
47 * Set defaults.
48 *
49 * @return array
50 *
51 * @throws \API_Exception
52 * @throws \CRM_Core_Exception
53 */
54 public function setDefaultValues() {
55 $defaults = [];
56 if ($this->userJobID) {
57 foreach ($this->getDataSourceFields() as $fieldName) {
58 $defaults[$fieldName] = $this->getSubmittedValue($fieldName);
59 }
60 }
61 return $defaults;
62 }
63
64 /**
65 * Get the submitted value, as saved in the user job.
66 *
67 * This form is not in the same flow as the DataSource but
68 * the value we want is saved to the userJob so load it from there.
69 *
70 * @param string $fieldName
71 *
72 * @return mixed|null
73 * @throws \API_Exception
74 */
75 public function getSubmittedValue(string $fieldName) {
76 $userJob = $this->getUserJob();
77 return $userJob['metadata']['submitted_values'][$fieldName];
78 }
79
80 }