-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathartwork.module
377 lines (353 loc) · 9.42 KB
/
artwork.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
/**
* Implements hook_entity_info().
*/
function artwork_entity_info() {
$return['artwork'] = array(
'label' => t('Artwork'),
'controller class' => 'ArtworkController',
'base table' => 'artwork',
'revision table' => 'artwork_revision',
'uri callback' => 'artwork_uri',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'aid',
'revision' => 'vid',
'bundle' => 'type',
'label' => 'title',
),
'bundle keys' => array(
'bundle' => 'type',
),
'static cache' => TRUE,
'bundles' => array(),
'view modes' => array(
'full' => array(
'label' => t('Full content'),
'custom settings' => FALSE,
),
'teaser' => array(
'label' => t('Teaser'),
'custom settings' => FALSE,
),
),
);
foreach (artwork_types() as $type => $info) {
$return['artwork']['bundles'][$type] = array(
'label' => $info->name,
'admin' => array(
'path' => 'admin/structure/artworks/manage/%artwork_type',
'real path' => 'admin/structure/artworks/manage/' . str_replace('_', '-', $type),
'bundle argument' => 4,
'access arguments' => array('administer artworks'),
),
);
}
return $return;
}
/**
* Entity uri callback.
*/
function artwork_uri($artwork) {
return array(
'path' => 'artwork/' . $artwork->aid,
);
}
/**
* Implements hook_fied_extra_fields().
*/
function artwork_field_extra_fields() {
$extra = array();
foreach (artwork_types() as $type) {
$extra['artwork'][$type->type] = array(
'form' => array(
'title' => array(
'label' => t('Title'),
'description' => t('The name of the artwork'),
'weight' => -5,
),
),
'display' => array(
'title' => array(
'label' => t('Title'),
'description' => t('The name of the artwork'),
'weight' => -5,
),
),
);
}
return $extra;
}
/**
* Implements hook_permission().
*/
function artwork_permission() {
return array(
'administer artworks' => array(
'title' => t('Administer artworks'),
),
'create artworks' => array(
'title' => t('Create artworks'),
),
'update artworks' => array(
'title' => t('Update artworks'),
),
'view artworks' => array(
'title' => t('View artworks'),
),
'delete artworks' => array(
'title' => t('Delete artworks'),
),
);
}
/**
* Implements hook_menu().
*/
function artwork_menu() {
$items['admin/structure/artworks'] = array(
'title' => 'Manage artworks',
'description' => 'Manage artworks.',
'page callback' => 'artwork_overview_types',
'access arguments' => array('administer artworks'),
'file' => 'artwork.admin.inc',
);
$items['admin/structure/artworks/manage/%artwork_type'] = array(
'title' => 'View artwork type',
'title callback' => 'artwork_type_page_title',
'title arguments' => array(4),
'page callback' => 'artwork_information',
'page arguments' => array(4),
'access arguments' => array('administer artworks'),
'file' => 'artwork.admin.inc',
);
$items['admin/structure/artworks/manage/%artwork_type/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['artwork/add'] = array(
'title' => 'Add new artwork',
'page callback' => 'artwork_add_page',
'access arguments' => array('create artworks'),
'weight' => 1,
'menu_name' => 'management',
'file' => 'artwork.pages.inc',
);
foreach (artwork_types() as $type) {
$type_url_str = str_replace('_', '-', $type->type);
$items['artwork/add/' . $type_url_str] = array(
'title' => $type->name,
'title callback' => 'check_plain',
'page callback' => 'artwork_add',
'page arguments' => array(2),
'access arguments' => array('create artwork'),
'description' => $type->description,
'file' => 'artwork.pages.inc',
);
}
$items['artwork/%artwork'] = array(
'title callback' => 'artwork_page_title',
'title arguments' => array(1),
'page callback' => 'artwork_page_view',
'page arguments' => array(1),
'access arguments' => array('view artworks'),
'type' => MENU_CALLBACK,
'file' => 'artwork.pages.inc',
);
$items['artwork/%artwork/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['artwork/%artwork/edit'] = array(
'title' => 'Edit',
'page callback' => 'artwork_page_edit',
'page arguments' => array(1),
'access arguments' => array('update artworks'),
'weight' => 0,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'artwork.pages.inc',
);
$items['artwork/%artwork/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('artwork_delete_confirm', 1),
'access arguments' => array('delete artworks'),
'weight' => 1,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'file' => 'artwork.pages.inc',
);
$items['artwork/recent'] = array(
'title' => 'Recently added artworks',
'page callback' => 'artwork_page_list_recent',
'access arguments' => array('view artworks'),
'file' => 'artwork.pages.inc',
);
return $items;
}
/**
* Sets the page title based on the specified artwork.
*
* @param $artwork
* The artwork object.
*/
function artwork_page_title($artwork) {
return $artwork->title;
}
/**
* Implements hook_forms().
*
* All artwork forms share the same form handler.
*/
function artwork_forms() {
$forms = array();
if ($types = artwork_types()) {
foreach (array_keys($types) as $type) {
$forms[$type . '_artwork_form']['callback'] = 'artwork_form';
}
}
return $forms;
}
/**
* Get a list of all artwork types available.
*
* Usually this would be more full-featured.
*
* @return array
* An array of artwork type objects. Note that the "type" key matches what
* was defined as the main key in hook_entity_info().
*/
function artwork_types() {
$types = &drupal_static(__FUNCTION__);
if (empty($types)) {
$types['painting'] = (object)array(
'type' => 'painting',
'name' => t('Painting'),
'description' => t('A picture made with paint.')
);
$types['sculpture'] = (object)array(
'type' => 'sculpture',
'name' => t('Sculpture'),
'description' => t('A carving made out of stone or wood.')
);
$types['ancient_book'] = (object)array(
'type' => 'ancient_book',
'name' => t('Ancient book'),
'description' => t('A very old tome of knowledge.')
);
}
return $types;
}
/**
* Menu title callback.
*
* @param $type
* The artwork type.
* @return string
* The human-friendly name of the artwork type.
*/
function artwork_type_page_title($type) {
return t('Manage @type', array('@type' => $type->name));
}
/**
* Get an individual artwork type definition object.
*
* @param $type
* The key of the artwork type we want.
* @return object
* The specified artwork type.
*/
function artwork_type_load($type) {
$types = artwork_types();
$type = str_replace('-', '_', $type);
return isset($types[$type]) ? $types[$type] : FALSE;
}
/**
* Load artwork entities from the database.
*
* This function should be used whenever you need to load more than one artwork
* from the database. Artworks are loaded into memory and will not require
* database access if loaded again during the same page request.
*
* @see entity_load()
*
* @param $aids
* An array of artwork IDs.
* @param $conditions
* An array of conditions on the {artwork} table in the form 'field' => $value.
* @param $reset
* Whether to reset the internal entity_load cache.
*
* @return
* An array of node objects indexed by nid.
*/
function artwork_load_multiple($aids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('artwork', $aids, $conditions, $reset);
}
/**
* Load an artwork object from the database.
*
* @param $aid
* The artwork ID.
* @param $vid
* The revision ID.
* @param $reset
* Whether to reset the artwork_load_multiple cache.
*
* @return
* A fully-populated node object.
*/
function artwork_load($aid = NULL, $vid = NULL, $reset = FALSE) {
$aids = (isset($aid) ? array($aid) : array());
$conditions = (isset($vid) ? array('vid' => $vid) : array());
$artwork = artwork_load_multiple($aids, $conditions, $reset);
return $artwork ? reset($artwork) : FALSE;
}
/**
* Save an artwork.
*
* @param $artwork
* The artwork to be saved.
* @return
* The saved artwork, now with an aid if necessary.
*/
function artwork_save($artwork) {
return entity_get_controller('artwork')->save($artwork);
}
/**
* Returns an initialized artwork object.
*
* @param $type
* The machine-readable type of the artwork.
*
* @return
* An artwork object with all default fields initialized.
*/
function artwork_new($type = '') {
return entity_get_controller('artwork')->create($type);
}
/**
* Deletes an artwork by ID.
*
* @param $aid
* The ID of the product to delete.
*
* @return
* TRUE on success, FALSE otherwise.
*/
function artwork_delete($aid) {
return artwork_delete_multiple(array($aid));
}
/**
* Deletes multiple artworks by ID.
*
* @param $aids
* An array of artwork IDs to delete.
*
* @return
* TRUE on success, FALSE otherwise.
*/
function artwork_delete_multiple($aids) {
return entity_get_controller('artwork')->delete($aids);
}