forked from DBC-as/Vopros-opensearch-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosa_addi.module
181 lines (161 loc) · 7.37 KB
/
osa_addi.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
<?php
/**
* Implements hook_menu().
*/
function osa_addi_menu() {
return array(
'admin/config/services/osa-addi' => array(
'title' => 'Open Search Admin',
'description' => 'Configure Open Search Admin.',
'page callback' => 'drupal_get_form',
'page arguments' => array('osa_addi_settings_form'),
'access arguments' => array('administer site configuration'),
),
);
}
/**
* Settings form
*/
function osa_addi_settings_form($form, &$form_state) {
$form['osa_addi_source'] = array(
'#type' => 'textfield',
'#title' => t('Source'),
'#description' => t('Enter the source (name) used when posting to data well.'),
'#default_value' => variable_get('osa_addi_source', ''),
);
$form['osa_addi_soap_url'] = array(
'#type' => 'textfield',
'#title' => t('URL'),
'#description' => t('Enter the URL used for SOAP requests.'),
'#default_value' => variable_get('osa_addi_soap_url', 'http://opensearchadmin.addi.dk/0.5/'),
);
return system_settings_form($form);
}
/**
* Create a new object or update existing in external DB.
*/
function osa_addi_create_object($object, $uuid, $settings = array(), $action = 'createObjectRequest') {
// Set content type and send the SOAP action as a header.
$xml_headers = array(
'Content-Type: text/xml',
'SOAPAction: ns1:' . $action,
);
$xml_body = osa_addi_create_object_request_xml($object, $uuid, $settings, $action);
$url = variable_get('osa_addi_soap_url', 'http://opensearchadmin.addi.dk/0.5/');
$client = new NanoSOAPClient($url);
$reponse_xml = $client->curlRequest($url, 'POST', $xml_body, $xml_headers);
$doc = DOMDocument::loadXML($reponse_xml);
if (!$doc) {
return array('status' => 'error');
}
if ($doc->getElementsByTagName('error')->item(0)) {
return array('status' => $doc->getElementsByTagName('error')->item(0)->nodeValue);
}
$result = array(
'status' => $doc->getElementsByTagName('status')->item(0)->nodeValue,
'id' => $doc->getElementsByTagName('objectIdentifier')->item(0)->nodeValue,
);
return $result;
}
/**
* Create the XML string used for the create object request.
*/
function osa_addi_create_object_request_xml($object, $uuid, $settings = array(), $action = 'createObjectRequest') {
$default_settings = array(
'source' => variable_get('osa_addi_source', ''),
'group_id_aut' => variable_get('ting_agency', ''),
);
$settings += $default_settings;
// Create XML document.
$url = variable_get('osa_addi_soap_url', 'http://opensearchadmin.addi.dk/0.5/');
$namespaces = _osa_addi_soap_create_object_ns();
$client = new NanoSOAPClient($url, array('namespaces' => $namespaces));
$doc = DOMDocument::loadXML($client->generateSOAPenvelope());
$body = $doc->getElementsByTagName('Body')->item(0);
// Create the create object action
$create_object = $doc->createElement('ns1:' . $action);
// Create authentication element.
$elem_authentication = $doc->createElement('ns1:authentication');
$elem_authentication->appendChild($doc->createElement('ns1:groupIdAut', $settings['group_id_aut']));
$create_object->appendChild($elem_authentication);
// Create local identifier object.
$identifier = ($action == 'createObjectRequest' ? 'localIdentifier' : 'objectIdentifier');
$local_identifier = $doc->createElement('ns1:' . $identifier, $settings['group_id_aut'] . ':' . $uuid);
$create_object->appendChild($local_identifier);
// If we want to delete the object - skip here.
if ($action == 'deleteObjectRequest') {
$body->appendChild($create_object);
// Return the XML.
return $doc->saveXML();
}
// Create the object we want to insert.
$ns_object = $doc->createElement('ns1:object');
// Create the record.
$dkabm_record = $doc->createElement('dkabm:record');
// Add values to the record.
if ($action == 'updateObjectRequest') {
$dkabm_record->appendChild($doc->createElement('ac:identifier', $uuid . '|' . $settings['group_id_aut']));
}
$dkabm_record->appendChild($doc->createElement('ac:source', $settings['source']));
$dkabm_record->appendChild($doc->createElement('dc:title', str_replace('&', '&', $object->title)));
foreach ($object->subject as $subject) {
$dkabm_record->appendChild($doc->createElement('dc:subject', str_replace('&', '&', $subject)));
}
$dkabm_record->appendChild($doc->createElement('dc:description', str_replace('&', '&', $object->description)));
$dkabm_record->appendChild($doc->createElement('dc:date', date('Y', $object->created)));
$dc_type = $doc->createElement('dc:type', 'Spørgsmål og svar');
$dc_type->setAttribute('xsi:type', 'dkdcplus:BibDK-Type');
$dkabm_record->appendChild($dc_type);
$dkabm_record->appendChild($doc->createElement('dc:language', 'Dansk'));
$dkabm_record->appendChild($doc->createElement('dcterms:created', date('c', $object->created)));
$dkabm_record->appendChild($doc->createElement('dcterms:modified', date('c', $object->changed)));
// Add record to the ns1:object.
$ns_object->appendChild($dkabm_record);
// Create the docbook:article
$docbook_article = $doc->createElement('docbook:article');
$docbook_article->appendChild($doc->createElement('docbook:title', str_replace('&', '&', $object->title)));
$docbook_info = $doc->createElement('docbook:info');
$docbook_publisher = $doc->createElement('docbook:publisher');
$docbook_publisher->appendChild($doc->createElement('docbook:publishername', $settings['source']));
$docbook_info->appendChild($docbook_publisher);
$docbook_article->appendChild($docbook_info);
// Create the section which holds the creations and answer set.
$docbook_section = $doc->createElement('docbook:section');
$docbook_qandaset = $doc->createElement('docbook:qandaset');
$docbook_qandadiv = $doc->createElement('docbook:qandadiv');
$docbook_qandaentry = $doc->createElement('docbook:qandaentry');
$docbook_question = $doc->createElement('docbook:question');
$docbook_question->appendChild($doc->createElement('docbook:para', str_replace('&', '&', $object->question_content)));
$docbook_qandaentry->appendChild($docbook_question);
$docbook_answer = $doc->createElement('docbook:answer');
$docbook_answer->appendChild($doc->createElement('docbook:para', str_replace('&', '&', $object->answer_content)));
$docbook_qandaentry->appendChild($docbook_answer);
$docbook_qandadiv->appendChild($docbook_qandaentry);
$docbook_qandaset->appendChild($docbook_qandadiv);
$docbook_section->appendChild($docbook_qandaset);
$docbook_article->appendChild($docbook_section);
// Add docbook:article to the ns1:object.
$ns_object->appendChild($docbook_article);
// Add ns1:object to the create object action.
$create_object->appendChild($ns_object);
// Add out soap element to the body.
$body->appendChild($create_object);
// Return the XML.
return $doc->saveXML();
}
/**
* Util function, get the namespaces for the create object request
*/
function _osa_addi_soap_create_object_ns() {
return array(
'xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'dc' => 'http://purl.org/dc/elements/1.1/',
'dkabm' => 'http://biblstandard.dk/abm/namespace/dkabm/',
'dcmitype' => 'http://purl.org/dc/dcmitype/',
'dcterms' => 'http://purl.org/dc/terms/',
'ac' => 'http://biblstandard.dk/ac/namespace/',
'dkdcplus' => 'http://biblstandard.dk/abm/namespace/dkdcplus/',
'docbook' => 'http://docbook.org/ns/docbook',
'ns1' => 'http://oss.dbc.dk/ns/opensearchadmin'
);
}