-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathartwork.controller.inc
192 lines (160 loc) · 5.25 KB
/
artwork.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
<?php
/**
* Controller for loading, creating, and saving artworks.
*
* The default loader, which we extend, handles load() already. We only
* need to add saving and creating.
*/
class ArtworkController extends DrupalDefaultEntityController {
public function save($artwork) {
$transaction = db_transaction();
try {
global $user;
// Determine if we will be inserting a new artwork.
$artwork->is_new = empty($artwork->aid);
// Set the timestamp fields.
if (empty($artwork->created)) {
$artwork->created = REQUEST_TIME;
}
$artwork->changed = REQUEST_TIME;
$artwork->revision_timestamp = REQUEST_TIME;
$update_artwork = TRUE;
// Give modules the opportunity to prepare field data for saving.
field_attach_presave('artwork', $artwork);
// When saving a new artwork revision, unset any existing $artwork->vid
// to ensure a new revision will actually be created and store the old
// revision ID in a separate property for artwork hook implementations.
if (!$artwork->is_new && !empty($artwork->revision) && $artwork->vid) {
$artwork->old_vid = $artwork->vid;
unset($artwork->vid);
}
// If this is a new artwork...
if ($artwork->is_new) {
// Save the new artwork.
drupal_write_record('artwork', $artwork);
// Save the initial revision.
$this->saveRevision($artwork, $user->uid);
$op = 'insert';
}
else {
// Save the updated artwork.
drupal_write_record('artwork', $artwork, 'aid');
// If a new artwork revision was requested, save a new record for that;
// otherwise, update the artwork revision record that matches the value
// of $artwork->vid.
if (!empty($artwork->revision)) {
$this->saveRevision($artwork, $user->uid);
}
else {
$this->saveRevision($artwork, $user->uid, TRUE);
$update_artwork = FALSE;
}
$op = 'update';
}
// If the revision ID is new or updated, save it to the artwork.
if ($update_artwork) {
db_update('artwork')
->fields(array('vid' => $artwork->vid))
->condition('aid', $artwork->aid)
->execute();
}
// Save fields.
$function = 'field_attach_' . $op;
$function('artwork', $artwork);
module_invoke_all('entity_' . $op, $artwork, 'artwork');
// Clear internal properties.
unset($artwork->is_new);
// Ignore slave server temporarily to give time for the saved order to be
// propagated to the slave.
db_ignore_slave();
return $artwork;
}
catch (Exception $e) {
$transaction->rollback();
watchdog_exception('artwork', $e, NULL, WATCHDOG_ERROR);
return FALSE;
}
}
/**
* Saves an artwork revision with the uid of the current user.
*
* @param $artwork
* The fully loaded artwork object.
* @param $uid
* The user's uid for the current revision.
* @param $update
* TRUE or FALSE indicating whether or not the existing revision should be
* updated instead of a new one created.
*/
function saveRevision($artwork, $uid, $update = FALSE) {
// Hold on to the artwork's original creator_uid but swap in the revision's
// creator_uid for the momentary write.
$temp_uid = $artwork->uid;
$artwork->uid = $uid;
// Update the existing revision if specified.
if ($update) {
drupal_write_record('artwork_revision', $artwork, 'vid');
}
else {
// Otherwise insert a new revision. This will automatically update $artwork
// to include the vid.
drupal_write_record('artwork_revision', $artwork);
}
// Reset the order's creator_uid to the original value.
$artwork->uid = $temp_uid;
}
/**
* Deletes multiple artworks by ID.
*
* @param $aids
* An array of artwork IDs to delete.
* @return
* TRUE on success, FALSE otherwise.
*/
public function delete($aids) {
if (!empty($aids)) {
$artworks = $this->load($aids, array());
$transaction = db_transaction();
try {
db_delete('artwork')
->condition('aid', $aids, 'IN')
->execute();
db_delete('artwork_revision')
->condition('aid', $aids, 'IN')
->execute();
foreach ($artworks as $artwork_id => $artwork) {
field_attach_delete('artwork', $artwork);
}
// Ignore slave server temporarily to give time for the
// saved artwork to be propagated to the slave.
db_ignore_slave();
}
catch (Exception $e) {
$transaction->rollback();
watchdog_exception('artwork', $e, NULL, WATCHDOG_ERROR);
return FALSE;
}
module_invoke_all('entity_delete', $artwork, 'artwork');
// Clear the page and block and artwork caches.
cache_clear_all();
$this->resetCache();
}
return TRUE;
}
/**
* Create a default artwork.
*
* @param $type
* The machine-readable type of the artwork.
*
* @return
* An artwork object with all default fields initialized.
*/
public function create($type = '') {
return (object) array(
'aid' => '',
'type' => $type,
'title' => '',
);
}
}