This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
/
teieditor.module2010-04-07
294 lines (278 loc) · 11.3 KB
/
teieditor.module2010-04-07
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/*
* @file teieditor.module
*
* Entry point of the module. All hook functions,
* are defined within this file, with the exceptions
* of hooks that are related to the modules install,
* and hooks that are related to the modules settings.
*/
/* Function:
* RenderModule
*
* Params:
* p_aForm_State - Contains information about the form.
* p_sPid - The pid for the tei page we are to edit.
*
* Description:
* Displays the module.
*/
function RenderModule($p_sPid=NULL, $p_sAction=NULL) {
require_once('utils.php'); // Only include if we are to render.
if(isValidPid($p_sPid)) {
switch($p_sAction) // Default render Page
{
// Returns a string containing a tei-document
case 'getTEI': {
echo drupal_to_js(GetFedoraObjectDatastream($p_sPid, 'TEI'));
exit;
}
break;
// Save Changes made to the page.
case 'save': {
$TEI = $_POST['TEI'];
// Save to fedora
module_load_include('php', 'Fedora_Repository', 'api/fedora_item');
module_load_include('php', 'Fedora_Repository', 'schematron/schematron_validator');
$object = new Fedora_Item($p_sPid);
$datastreams = $object->get_datastreams_list_as_array();
$bResult = FALSE;
//$nodeToReplace=null;
if($datastreams['TEI']) {
$originalTEI = $object->get_datastream_dissemination('TEI');
$orginalDoc = new DOMDocument();
//$newDoc = new DomDocument();
$newTeiElement = $orginalDoc->createDocumentFragment();
$test=$newTeiElement->appendXML($TEI);
if(!$test==true) {
$msg= 'Error appending XML to TEI!'.$TEI;
echo drupal_to_js(array('msg'=>"$msg \n"));
exit;
}
$orginalDoc->loadxml($originalTEI);
$divs=$orginalDoc->getElementsByTagName('div');
foreach ($divs as $div) {
if($div->getAttribute('type')=='page') {//replace this div with the new one from the teieditor
$parent = $div->parentNode;
$parent->removeChild($div);
$newnode= $orginalDoc->importNode($newTeiElement,true);
$parent->appendChild($newnode);
renameElement($newNode,$orginalDoc, 'persname','persName');
renameElement($newNode,$orginalDoc,'addname','addName');
renameElement($newNode,$orginalDoc,'orgname','orgName');
renameElement($newNode,$orginalDoc,'placename','placeName');
renameElement($newNode,$orginalDoc,'geogname','geogName');
renameElement($newNode,$orginalDoc,'rolename','roleName');
break;
}
}
try {
//would not validate when using $orginalDoc->.... dump into new dom and validates.
$domForValidation = new DOMDocument("1.0");
if (! $domForValidation->loadXML($orginalDoc->saveXML())) {
watchdog(t("FEDORA_REPOSITORY"), t("Error During validation could not create dom.") , NULL, WATCHDOG_ERROR);
echo drupal_to_js(array('msg'=>"Validation of this document failed. Could not create new dom\n"));
return;
}
$test = $domForValidation->relaxNGValidate(drupal_get_path('module', 'teieditor')."/tei_custom.rng");
//$test = $orginalDoc->relaxNGValidate(drupal_get_path('module', 'teieditor')."/tei_custom.rng");
}catch(exception $e ) {
watchdog(t("FEDORA_REPOSITORY"), t("Your changes were not saved.") . $e, NULL, WATCHDOG_ERROR);
echo drupal_to_js(array('msg'=>"Validation of this document failed. \n".$e->getMessage()));
return;
}
if(!$test) {
watchdog(t("FEDORA_REPOSITORY"), t("Your changes were not saved. Document was not valid TEI"), NULL, WATCHDOG_ERROR);
//echo $orginalDoc->saveXML();exit(0);
echo drupal_to_js(array('msg'=>"Failed to validate TEI document."));
return;
}
try {
// Gather Information about datastream, so we don't overwrite it
// with nil.
$dataStream = $object->soap_call('getDatastream',
array("pid" => $p_sPid,
"dsID" => 'TEI',
"asOfDateTime" => NULL
)
);
$dataStream = $dataStream->datastream; // Cleaner Looking
// Overwrite the datastream but maintain metadata.
$bResult = $object->soap_call('modifyDatastreamByValue',
array("pid" => $p_sPid,
"dsID" => 'TEI',
"altIDs" => new SoapVar($dataStream->altIDs->item, SOAP_ENC_ARRAY, 'ArrayOfString'),
"dsLabel" => $dataStream->label,
"MIMEType" => $dataStream->MIMEType,
"formatURI" => $dataStream->formatURI,
// "dsContent" => $TEI,
"dsContent" => $orginalDoc->saveXML(),
"checksumType" => $dataStream->checksumType,
"checksum" => $dataStream->checksum,
"logMessage" => "Overwrote Content of Datastream.",
"force" => FALSE,
)
);
}
catch(exception $e) {
// Report Problem.
watchdog(t("FEDORA_REPOSITORY"), t("Your changes were not saved.") . $e, NULL, WATCHDOG_ERROR);
echo drupal_to_js(array('msg'=>"Your changes were not saved. \n".$e->getMessage()));
return;
}
echo drupal_to_js(array('msg'=>"Save successful."));
exit;
}
}
break;
// Render the page
default: {
drupal_add_js(drupal_get_path('module', 'teieditor').'/jquery/js/jquery-1.3.2.min.js');
drupal_add_css(drupal_get_path('module', 'teieditor').'/jquery/css/smoothness/jquery-ui-1.7.2.custom.css');
drupal_add_js(drupal_get_path('module', 'teieditor').'/jquery/js/jquery-ui-1.7.2.custom.min.js');
drupal_add_css(drupal_get_path('module', 'teieditor').'/tei.css');
drupal_add_js(drupal_get_path('module', 'teieditor').'/tei.js');
drupal_add_css(drupal_get_path('module', 'teieditor').'/teieditor.css');
drupal_add_js(drupal_get_path('module', 'teieditor').'/teieditor.js');
// Either we have a valid page pid or we take the first page pid of our valid book pid.
$options = getPidList($p_sPid);
$startPid = (isValidPagePid($p_sPid))? $p_sPid : $options[1]['pid'];
$selections = "";
for($i = 1; $i <= sizeof($options); ++$i) {
$pid = $options[$i]['pid'];
$title = $options[$i]['title'];
$selected = ($pid==$startPid) ? ' selected="selected"' : '' ;
$selections = $selections . '<option value="'.$pid.'"'.$selected.'>'.$title.'</option>';
}
$editor = t('<div id="teieditor-module" class="ui-corner-all">
<div id="toolbar">
<button id="buttonPrevious" title="Previous Page" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-arrowthick-1-w"/></button>
<button id="buttonNext" title="Next Page" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-arrowthick-1-e"/></button>
<select id="pageSelection" title="Select a Page" class="ui-state-default ui-corner-all">'.$selections.'</select>
</div>
<div id="viewer">
<div id="map">
<iframe name="map" frameborder="0" width="100%" height="100%" src="http://137.149.66.2:8080/teieditor/teieditor_iframe.html?&pagePid='.$pid.'">Sorry Your browser does not support iframes.</iframe>
</div>
<div id="tei-editor"></div>
</div>
</div>');
return $editor;
}
}
}
else // Print error screen.
{
return t('Sorry the pid specified is not valid.');
}
}
//jquery makes all elements lowercase hack to fix this.
function renameElement($element,$doc,$oldName,$newName) {
//$existingElements = $doc->getElementsByTagName($oldName);
$xpath = new DOMXpath($doc);
$existingElements = $xpath->query("//$oldName");
foreach($existingElements as $existingElement) {
$parent = $existingElement->parentNode;
//$parent->removeChild($existingElement);
$newElement=$doc->createElement($newName);
// $newElement->nodeValue=$existingElement->nodeValue;
foreach($existingElement->attributes as $value) {
$newElement->setAttribute($value->nodeName,$value->value);
}
if($existingElement->childNodes) {
foreach($existingElement->childNodes as $child) {
if($child->nodeName=="#text")
$newElement->appendChild($doc->createTextNode($child->nodeValue));
else {
//$newElement->appendChild(renameElement($child,$doc,$oldName,$newName));
$newElement->appendChild($child->cloneNode(true));
}
}
}
$doc->importNode($newElement);
$parent->replaceChild($newElement,$existingElement);
}
}
/* Function:
* teieditor_menu
*
* Params: <none>
*
* Returns:
* An array that defines all the menu items associated with this module.
* Currently defines the settings menu, and a menu where the module can be accessed.
*/
function teieditor_menu() {
/*
// Settings
$items['admin/settings/teieditor'] = array(
'title' => 'TEI Editor settings',
'description' => 'Settings for the TEI Editor Module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('teieditor_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'teieditor.admin.inc',
);*/
// Module
$items['teieditor'] = array(
'title' => 'TEI Editor',
'description' => 'TEI Editor',
'page callback' => 'RenderModule',
//'page arguments' => array('RenderModule',1),
'access arguments' => array('access teieditor'),
'type' => MENU_CALLBACK
);
return $items;
}
/* Function:
* teieditor_theme
*
* Params: <none>
*
* Returns:
*
function teieditor_theme()
{
return array('RenderModule'=> array('arguments'=>array()));
}
/* Function:
* theme_teieditor_form
*
* Params: <none>
*
* Returns:
*
/
function theme_RenderModule($form)
{
$output = drupal_render($form);
return $output;
}
/* Function:
* teieditor_perm
*
* Params: <none>
*
* Returns:
* An array that defines all the permissions that relate to this module.
*/
function teieditor_perm() {
return array('access teieditor');
}
/* Function:
* teieditor_mail
*
* Params:
* p_sKey - ?
* p_aMessage - Referenced parameter, set to email message.
* p_aParams - User defined variables, data to be used in the message.
*
* Description:
* Notifies the admin that someone has edited an object within fedora.
*
function teieditor_mail($p_sKey, &$p_aMessage, $p_aParams)
{
$p_aMessage['subject'] = t('Notification: Datastream '.$p_aParams['datastream'].' in object '.$p_aParams['object'].' has been modified.');
$p_aMessage['body'] = t('Datastream '.$p_aParams['datastream'].' in object '.$p_aParams['object'].' has been modified by user '.$p_aParams['user'].".\n-------See new datastream below -------\n".$p_aParams['modifiedTEI']);
}*/