-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos2web_edoc_file_import.module
executable file
·250 lines (217 loc) · 9.42 KB
/
os2web_edoc_file_import.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
use Drupal\Core\File\FileSystemInterface;
use Drupal\node\Entity\Node;
use Drupal\file\Entity\File;
/**
* Implements hook_cron().
*/
function os2web_edoc_file_import_cron() {
// Get the import path from configuration.
$config = \Drupal::config('os2web_edoc_file_import.settings');
$import_path = $config->get('import_path');
if (!file_exists($import_path)) {
\Drupal::logger('os2web_edoc_file_import')->error('The import path does not exist. (PATH: /var/www/edoc_filer)');
return;
}
$xml_files = \Drupal::service('file_system')->scanDirectory($import_path, "/\.xml$/i", ['recurse' => FALSE]);
foreach ($xml_files as $file) {
os2web_edoc_file_import_process_file($file);
}
}
/**
* Checks if a case with the given CaseNumber exists.
*
* @param string $caseNumber
* The CaseNumber to check.
*
* @return \Drupal\node\Entity\Node|null
* The case node if found, or NULL if not.
*/
function os2web_edoc_case_exists($caseNumber) {
$cases = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['type' => 'esdh_case', 'field_case_id' => $caseNumber]);
return !empty($cases) ? reset($cases) : NULL;
}
/**
* Creates or updates a case based on XML data.
*/
function os2web_edoc_file_import_create_or_update_case($xml) {
$trans = \Drupal::transliteration(); // Transliteration service
$caseNumber = (string) $xml->CaseNumber;
$case = os2web_edoc_case_exists($caseNumber);
if (!$case) {
// If case does not exist, create a new one.
$case = Node::create([
'type' => 'esdh_case',
'title' => $trans->transliterate((string) $xml->CaseName),
'field_case_id' => $caseNumber,
'field_case_name' => $trans->transliterate((string) $xml->CaseName),
]);
\Drupal::logger('os2web_edoc_file_import')->info('Created new case: ' . $caseNumber);
} else {
// If case exists, update it.
$case->setTitle($trans->transliterate((string) $xml->CaseName));
$case->set('field_case_name', $trans->transliterate((string) $xml->CaseName));
\Drupal::logger('os2web_edoc_file_import')->info('Updated case: ' . $caseNumber);
}
$case->save();
\Drupal::logger('os2web_edoc_file_import')->info('Saved case with ID: ' . $case->id());
return $case;
}
/**
* Creates or updates a document based on XML data.
*/
function os2web_edoc_file_import_create_or_update_document($xml, $case = NULL, $update = FALSE) {
// Using transliteration to replace Danish chars.
$trans = \Drupal::transliteration();
$document = NULL;
if ($update) {
$documents = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['type' => 'esdh_document', 'field_document_id' => (string) $xml->FileID]);
$document = reset($documents);
\Drupal::logger('os2web_edoc_file_import')->info('Loaded existing document for update: ' . (string) $xml->FileID);
}
if (!$document) {
$document = Node::create([
'type' => 'esdh_document',
'title' => $trans->transliterate((string)$xml->FileName),
'field_document_id' => (string) $xml->FileID,
]);
}
else {
$document->setTitle($trans->transliterate((string) $xml->FileName));
\Drupal::logger('os2web_edoc_file_import')->info('Updated document: ' . (string) $xml->FileID);
}
$document->set('field_document_title', $trans->transliterate((string) $xml->FileName));
$document->set('field_case_id', (string) $xml->CaseNumber);
// Set case.
if ($case) {
$document->set('field_case_reference', ['target_id' => $case->id(), 'target_type' => 'node']);
\Drupal::logger('os2web_edoc_file_import')->info('Linked document to case: ' . $case->id());
}
// Attach the file to the node's file field
$config = \Drupal::config('os2web_edoc_file_import.settings');
$import_path = $config->get('import_path');
$fileName = (string) $xml->FileName;
$fileId = (string) $xml->FileID;
// Check if the file exists with the original name
$filepath = "$import_path/Files/$fileId" . '_' . $fileName;
if (!file_exists($filepath)) {
// Try adding .pdf if the original file doesn't exist
$filepath .= '.pdf';
}
// Ensure only PDF files are processed
$ext = pathinfo($filepath, PATHINFO_EXTENSION);
if (strtolower($ext) !== 'pdf') {
\Drupal::logger('os2web_edoc_file_import')->warning('Skipped non-PDF file: ' . $filepath);
return;
}
if (file_exists($filepath)) {
$edocPublicDirectory = 'public://edoc_files';
// At this point we know that we have PDF file. Remove all other extensions, keep only .pdf
$filenameNaked = pathinfo($fileName, PATHINFO_FILENAME); // returns ma
$destinationFilename = $filenameNaked . '.pdf';
$destinationUri = $edocPublicDirectory . '/' . $destinationFilename;
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$file_system->prepareDirectory($edocPublicDirectory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$file_system->copy($filepath, $destinationUri, FileSystemInterface::EXISTS_REPLACE);
$file = File::create([
'filename' => $destinationFilename,
'uri' => $destinationUri,
'status' => 1,
'uid' => 1,
]);
$file->save();
\Drupal::logger('os2web_edoc_file_import')->info('Created file entity for document: ' . (string) $xml->FileID);
} else {
\Drupal::logger('os2web_edoc_file_import')->error('File not found: ' . $filepath);
return;
}
if ($file) {
// Replacing file information.
$document->setTitle($file->getFilename());
$document->set('field_document_title', $trans->transliterate($file->getFilename()));
$document->set('field_case_file', [
'target_id' => $file->id(),
'display' => 1,
'description' => $file->getFilename(),
]);
\Drupal::logger('os2web_edoc_file_import')->info('Assigned file to document: ' . (string) $xml->FileID);
}
\Drupal::logger('os2web_edoc_file_import')->info('Updated document: ' . (string) $xml->FileID);
$document->save();
if ($case) {
// Update case to reference this document
$case_documents = $case->get('field_case_documents')->getValue();
$case_documents[] = ['target_id' => $document->id()];
$case->set('field_case_documents', $case_documents);
$case->save();
\Drupal::logger('os2web_edoc_file_import')->info('Updated case to include document: ' . (string) $xml->FileID);
}
return $document;
}
/**
* Deletes a document based on XML data.
*/
function os2web_edoc_file_import_delete_document($xml) {
$documents = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['field_document_id' => (string) $xml->FileID]);
$document = reset($documents);
if ($document) {
$document->delete();
\Drupal::logger('os2web_edoc_file_import')->info('Deleted document: ' . (string) $xml->FileID);
} else {
\Drupal::logger('os2web_edoc_file_import')->warning('No document found to delete with FileID: ' . (string) $xml->FileID);
}
}
/**
* Processes a single XML file.
*/
function os2web_edoc_file_import_process_file($file) {
$xml = simplexml_load_file($file->uri);
\Drupal::logger('os2web_edoc_file_import')->info('Loaded XML file: ' . $file->uri);
// Debugging: Log the XML content
\Drupal::logger('os2web_edoc_file_import')->debug('XML content: ' . print_r($xml, TRUE));
$fileName = (string) $xml->FileName;
// Handle create, update, delete based on XML content.
if (!empty($xml->Created)) {
// Create a new case.
if (!empty($xml->CaseNumber)) {
\Drupal::logger('os2web_edoc_file_import')->info('Creating or updating case with CaseNumber: ' . (string) $xml->CaseNumber);
$case = os2web_edoc_file_import_create_or_update_case($xml);
}
// Create a new document.
if (!empty($xml->FileID)) {
\Drupal::logger('os2web_edoc_file_import')->info('Creating or updating document with FileID: ' . (string) $xml->FileID);
os2web_edoc_file_import_create_or_update_document($xml, isset($case) ? $case : NULL);
}
} elseif (!empty($xml->Updated)) {
// Update existing case or document.
if (!empty($xml->CaseNumber)) {
\Drupal::logger('os2web_edoc_file_import')->info('Updating case with CaseNumber: ' . (string) $xml->CaseNumber . ' and FileID: ' . (string) $xml->FileID);
$case = os2web_edoc_file_import_create_or_update_case($xml, TRUE);
}
if (!empty($xml->FileID)) {
\Drupal::logger('os2web_edoc_file_import')->info('Updating document with FileID: ' . (string) $xml->FileID);
os2web_edoc_file_import_create_or_update_document($xml, isset($case) ? $case : NULL, TRUE);
}
} elseif (!empty($xml->Deleted)) {
// Delete existing document.
if (!empty($xml->FileID)) {
\Drupal::logger('os2web_edoc_file_import')->info('Deleting document with FileID: ' . (string) $xml->FileID);
os2web_edoc_file_import_delete_document($xml);
}
}
// Moving XML after processed
$processed_path = dirname($file->uri) . '/processed';
if (!file_exists($processed_path)) {
mkdir($processed_path, 0755, TRUE);
\Drupal::logger('os2web_edoc_file_import')->info('Created processed directory: ' . $processed_path);
}
$new_path = $processed_path . '/' . basename($file->uri);
rename($file->uri, $new_path);
\Drupal::logger('os2web_edoc_file_import')->info('Moved file to processed directory: ' . $new_path);
// Delete the original file from the main folder (import folder)
if (file_exists($file->uri)) {
unlink($file->uri);
\Drupal::logger('os2web_edoc_file_import')->info('Deleted original file from main folder: ' . $file->uri);
}
}