-
Notifications
You must be signed in to change notification settings - Fork 0
/
fedoraobject.controller.inc
196 lines (163 loc) · 5.86 KB
/
fedoraobject.controller.inc
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
<?php
/**
* @file
* Controller implementation for Fedora Commons repository objects.
*/
class FedoraObjectController extends DrupalDefaultEntityController {
public function create($type = '', $pid = '', $title = '') {
return (object) array(
'foid' => '',
'pid' => $pid,
'type' => $type,
'title' => $title,
);
}
public function delete($foids) {
if (!empty($foids)) {
$fedoraobjects = $this->load($foids, array());
$transaction = db_transaction();
try {
db_delete('fedoraobject')
->condition('foid', $foids, 'IN')
->execute();
foreach($fedoraobjects as $fedoraobject_foid => $fedoraobject) {
field_attach_delete('fedoraobject', $fedoraobject);
}
db_ignore_slave();
}
catch(Excaption $e) {
$transaction->rollback();
watchdog_exception('fedoraobject', $e, NULL, WATCHDOG_ERROR);
return FALSE;
}
module_invoke_all('entity_delete', $fedoraobject, 'fedoraobject');
// Clear the page, block and fedoraobject caches.
cache_clear_all();
$this->resetCache();
}
return TRUE;
}
public function load($ids = array(), $conditions = array()) {
module_load_include('module', 'fedora_api');
// Get the foids for any $ids that are PID strings.
$pids = array();
$foids = array();
$ids_to_remove = array();
$new_fedoraobjects = array();
for ($i = 0; $i < count($ids); $i++) {
if (!is_numeric($ids[$i]) && fedora_api_valid_pid($ids[$i])) {
$pids[] = $ids[$i];
$ids_to_remove[] = $i;
}
}
foreach($ids_to_remove as $id) {
unset($ids[$id]);
}
if (!empty($pids)) {
$found_pids = array();
// Get the objects by PID
$select = db_select('fedoraobject', 'e')
->fields('e', array('foid', 'pid'))
->condition('pid', $pids, 'IN')
->execute();
while ($res = $select->fetchAssoc()) {
$foids[] = $res['foid'];
$found_pids[] = $res['pid'];
}
$remaining_pids = array_diff($pids, $found_pids);
$new_foids = array();
foreach ($remaining_pids as $remaining_pid) {
$new_fedoraobjects[] = $this->createFedoraObjectForPid($remaining_pid);
}
$ids = array_merge($ids, $foids);
}
return array_merge(parent::load($ids, $conditions), $new_fedoraobjects);
}
public function save($fedoraobject) {
$transaction = db_transaction();
try {
global $user;
// Determine if we are inserting a new fedora object.
$fedoraobject->is_new = empty($fedoraobject->foid);
// Set the timestamp fields
if (empty($fedoraobject->created)) {
$fedoraobject->created = REQUEST_TIME;
}
$fedoraobject->changed = REQUEST_TIME;
$fedoraobject->revision_timestamp = REQUEST_TIME;
$update_fedoraobject = TRUE;
// Give modules the opportunity to prepare field data for saving.
field_attach_presave('fedoraobject', $fedoraobject);
if (!$fedoraobject->is_new && !empty($fedoraobject->revision) && $fedoraobject->vid) {
$fedoraobject->old_vid = $fedoraobject->vid;
unset($fedoraobject->vid);
}
// If this is a new fedora object
if ($fedoraobject->is_new) {
// Save the new fedora object
drupal_write_record('fedoraobject', $fedoraobject);
// Save the initial revision
$this->saveRevision($fedoraobject, $user->uid);
$op = 'insert';
}
else {
// Save the updated fedora object.
drupal_write_record('fedoraobject', $fedoraobject, 'foid');
if (!empty($fedoraobject->revision)) {
$this->saveRevision($fedoraobject, $user->uid);
}
else {
$this->saveRevision($fedoraobject, $user->uid, TRUE);
$update_fedoraobject = TRUE;
}
$op = 'update';
}
// If the revision id is new or updated, save it to the fedora object.
if ($update_fedoraobject) {
db_update('fedoraobject')
->fields(array('vid' => $fedoraobject->vid))
->condition('foid', $fedoraobject->foid)
->execute();
}
// Save fields.
$function = 'field_attach_' . $op;
$function('fedoraobject', $fedoraobject);
module_invoke_all('entity_' . $op, $fedoraobject, 'fedoraobject');
// Clear internal properties
unset($fedoraobject->is_new);
// Ignore slave server temporarily to give time for the save order to be propagated to the slave.
db_ignore_slave();
return $fedoraobject;
}
catch(Exception $e) {
$transaction->rollback();
watchdog_exception('fedoraobject', $e, NULL, WATCHDOG_ERROR);
return FALSE;
}
}
function saveRevision($fedoraobject, $uid, $update = FALSE) {
// Hold on to the original fedora object's creator_uid but swap
// but swap in the revision's creator_uid for the momentary update
$temp_uid = $fedoraobject->uid;
$fedoraobject->uid = $uid;
if ($update) {
drupal_write_record('fedoraobject_revision', $fedoraobject, 'vid');
}
else {
drupal_write_record('fedoraobject_revision', $fedoraobject);
}
// Reset the fedora object's creator_uid to the original value.
$fedoraobject->uid = $temp_uid;
}
protected function createFedoraObjectForPid($pid) {
module_load_include('inc', 'fedora_api', 'fedora_api.client');
$fedora_client = new FedoraClient();
$results = $fedora_client->findObjects(NULL, "pid=$pid");
$new_fedoraobject = NULL;
if (isset($results[$pid])) {
$result = $results[$pid];
$new_fedoraobject = $this->create('generic', $pid, $result['title']);
}
return $new_fedoraobject;
}
}