Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
7b057b66 EM |
18 | use Civi\Api4\UserJob; |
19 | ||
6a488035 TO |
20 | /** |
21 | * This class defines the DataSource interface but must be subclassed to be | |
22 | * useful. | |
23 | */ | |
24 | abstract class CRM_Import_DataSource { | |
25 | ||
7b057b66 EM |
26 | /** |
27 | * Class constructor. | |
28 | * | |
29 | * @param int|null $userJobID | |
30 | */ | |
31 | public function __construct(int $userJobID = NULL) { | |
32 | if ($userJobID) { | |
33 | $this->setUserJobID($userJobID); | |
34 | } | |
35 | } | |
36 | ||
37 | /** | |
38 | * Form fields declared for this datasource. | |
39 | * | |
40 | * @var string[] | |
41 | */ | |
42 | protected $submittableFields = []; | |
43 | ||
44 | /** | |
45 | * User job id. | |
46 | * | |
47 | * This is the primary key of the civicrm_user_job table which is used to | |
48 | * track the import. | |
49 | * | |
50 | * @var int | |
51 | */ | |
52 | protected $userJobID; | |
53 | ||
54 | /** | |
55 | * @return int|null | |
56 | */ | |
57 | public function getUserJobID(): ?int { | |
58 | return $this->userJobID; | |
59 | } | |
60 | ||
61 | /** | |
62 | * Set user job ID. | |
63 | * | |
64 | * @param int $userJobID | |
65 | */ | |
66 | public function setUserJobID(int $userJobID): void { | |
67 | $this->userJobID = $userJobID; | |
68 | } | |
69 | ||
70 | /** | |
71 | * User job details. | |
72 | * | |
73 | * This is the relevant row from civicrm_user_job. | |
74 | * | |
75 | * @var array | |
76 | */ | |
77 | protected $userJob; | |
78 | ||
79 | /** | |
80 | * Get User Job. | |
81 | * | |
82 | * API call to retrieve the userJob row. | |
83 | * | |
84 | * @return array | |
85 | * | |
86 | * @throws \API_Exception | |
87 | */ | |
88 | protected function getUserJob(): array { | |
89 | if (!$this->userJob) { | |
90 | $this->userJob = UserJob::get() | |
91 | ->addWhere('id', '=', $this->getUserJobID()) | |
92 | ->execute() | |
93 | ->first(); | |
94 | } | |
95 | return $this->userJob; | |
96 | } | |
97 | ||
98 | /** | |
99 | * Generated metadata relating to the the datasource. | |
100 | * | |
101 | * This is values that are computed within the DataSource class and | |
102 | * which are stored in the userJob metadata in the DataSource key - eg. | |
103 | * | |
104 | * ['table_name' => $] | |
105 | * | |
106 | * Will be in the user_job.metadata field encoded into the json like | |
107 | * | |
108 | * `{'DataSource' : ['table_name' => $], 'submitted_values' : .....}` | |
109 | * | |
110 | * @var array | |
111 | */ | |
112 | protected $dataSourceMetadata = []; | |
113 | ||
114 | /** | |
115 | * @return array | |
116 | */ | |
117 | public function getDataSourceMetadata(): array { | |
118 | return $this->dataSourceMetadata; | |
119 | } | |
120 | ||
121 | /** | |
122 | * Get the fields declared for this datasource. | |
123 | * | |
124 | * @return string[] | |
125 | */ | |
126 | public function getSubmittableFields(): array { | |
127 | return $this->submittableFields; | |
128 | } | |
129 | ||
6a488035 | 130 | /** |
fe482240 | 131 | * Provides information about the data source. |
6a488035 | 132 | * |
a6c01b45 | 133 | * @return array |
11749569 TO |
134 | * Description of this data source, including: |
135 | * - title: string, translated, required | |
136 | * - permissions: array, optional | |
137 | * | |
6a488035 TO |
138 | */ |
139 | abstract public function getInfo(); | |
140 | ||
141 | /** | |
fe482240 | 142 | * Set variables up before form is built. |
54957108 | 143 | * |
144 | * @param CRM_Core_Form $form | |
6a488035 TO |
145 | */ |
146 | abstract public function preProcess(&$form); | |
147 | ||
148 | /** | |
54957108 | 149 | * This is function is called by the form object to get the DataSource's form snippet. |
6a488035 | 150 | * |
54957108 | 151 | * It should add all fields necessary to get the data uploaded to the temporary table in the DB. |
6c8f6e67 | 152 | * |
54957108 | 153 | * @param CRM_Core_Form $form |
6a488035 TO |
154 | */ |
155 | abstract public function buildQuickForm(&$form); | |
156 | ||
157 | /** | |
fe482240 | 158 | * Process the form submission. |
54957108 | 159 | * |
160 | * @param array $params | |
161 | * @param string $db | |
162 | * @param CRM_Core_Form $form | |
6a488035 TO |
163 | */ |
164 | abstract public function postProcess(&$params, &$db, &$form); | |
96025800 | 165 | |
11749569 TO |
166 | /** |
167 | * Determine if the current user has access to this data source. | |
168 | * | |
169 | * @return bool | |
170 | */ | |
171 | public function checkPermission() { | |
172 | $info = $this->getInfo(); | |
173 | return empty($info['permissions']) || CRM_Core_Permission::check($info['permissions']); | |
174 | } | |
175 | ||
6a488035 | 176 | } |