forked from omeka/plugin-ExhibitBuilder
/
Exhibit.php
497 lines (442 loc) · 12.6 KB
/
Exhibit.php
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
/**
* @copyright Roy Rosenzweig Center for History and New Media, 2007-2012
* @license http://www.gnu.org/licenses/gpl-3.0.txt
* @package ExhibitBuilder
*/
/**
* Exhibit model.
*
* @package ExhibitBuilder
*/
class Exhibit extends Omeka_Record_AbstractRecord implements Zend_Acl_Resource_Interface
{
/**
* Exhibit title.
*
* @var string
*/
public $title;
/**
* Exhibit description (in HTML).
*
* @var string
*/
public $description;
/**
* Exhibit credits.
*
* @var string
*/
public $credits;
/**
* Whether the exhibit is featured.
*
* @var integer
*/
public $featured = 0;
/**
* Whether the exhibit is public.
*
* @var integer
*/
public $public = 0;
/**
* Public theme to use for this exhibit.
*
* @var string
*/
public $theme;
/**
* Options for this exhibit's theme, serialized.
*
* @var string
*/
public $theme_options;
/**
* URL slug for the exhibit.
*
* @var string
*/
public $slug;
/**
* Date the exhibit was created, as a MySQL-formatted date string.
*
* @var string
*/
public $added;
/**
* Date the exhibit was last modified, as a MySQL-formatted date string.
*
* @var string
*/
public $modified;
/**
* User ID of the user who created the exhibit.
*
* @var integer
*/
public $owner_id;
/**
* Whether the summary page will be used.
*
* @var integer
*/
public $use_summary_page = 1;
/**
* The File ID of the File used to represent this Exhibit.
*
* @var integer
*/
public $cover_image_file_id;
/**
* Quick-access mappings for related records.
*
* @var array
*/
protected $_related = array(
'Pages' => 'getPages',
'PagesById' => 'getPagesById',
'PagesByParent' => 'getPagesByParent',
'TopPages' => 'getTopPages',
'Tags' => 'getTags'
);
/**
* Set up mixins for shared behaviors.
*/
public function _initializeMixins()
{
$this->_mixins[] = new Mixin_Tag($this);
$this->_mixins[] = new Mixin_Owner($this);
$this->_mixins[] = new Mixin_Slug($this, array(
'slugEmptyErrorMessage' => __('Exhibits must be given a valid slug.'),
'slugLengthErrorMessage' => __('A slug must be 30 characters or less.'),
'slugUniqueErrorMessage' => __('Your URL slug is already in use by another exhibit. Please choose another.')));
$this->_mixins[] = new Mixin_Timestamp($this);
$this->_mixins[] = new Mixin_PublicFeatured($this);
$this->_mixins[] = new Mixin_Search($this);
}
/**
* Validation callback.
*/
protected function _validate()
{
if (!strlen((string)$this->title)) {
$this->addError('title', __('An exhibit must be given a title.'));
}
if (strlen((string)$this->title) > 255) {
$this->addError('title', __('The title for an exhibit must be 255 characters or less.'));
}
if (strlen((string)$this->theme) > 30) {
$this->addError('theme', __('The name of your theme must be 30 characters or less.'));
}
}
/**
* Delete callback.
*
* Delete all assigned pages when the exhibit is deleted.
*/
protected function _delete()
{
//get all the pages and delete them
$pages = $this->getTable('ExhibitPage')->findBy(array('exhibit'=>$this->id));
foreach($pages as $page) {
$page->setFixChildrenOnDelete(false);
$page->delete();
}
$this->deleteTaggings();
}
protected function beforeSave($args)
{
if($args['post']) {
$post = $args['post'];
if(empty($post['cover_image_file_id']))
unset($this->cover_image_file_id);
}
}
/**
* After-save callback.
*
* Updates search text and page data for the exhibit.
*
* @param array $args
*/
protected function afterSave($args)
{
if (!$this->public) {
$this->setSearchTextPrivate();
}
$this->setSearchTextTitle($this->title);
$this->addSearchText($this->title);
$this->addSearchText($this->description);
if ($args['post']) {
//Add the tags after the form has been saved
$post = $args['post'];
$this->applyTagString($post['tags']);
if (isset($post['pages-hidden'])) {
parse_str($post['pages-hidden'], $pageData);
$this->_savePages($pageData['page']);
}
if (isset($post['pages-delete-hidden'])) {
$pagesToDelete = explode(',', $post['pages-delete-hidden']);
foreach ($pagesToDelete as $id) {
$page = $this->getTable('ExhibitPage')->find($id);
if ($page) {
$page->delete();
}
}
}
}
}
/**
* Save the order and parent data for the existing pages.
*
* @param array Page parent data array
*/
protected function _savePages($pageData)
{
$orders = array();
$ordersByParent = array();
foreach ($pageData as $pageId => $parentId) {
if ($parentId == 'null') {
$pageData[$pageId] = null;
}
if (!isset($ordersByParent[$parentId])) {
$order = $ordersByParent[$parentId] = 0;
} else {
$order = ++$ordersByParent[$parentId];
}
$orders[$pageId] = $order;
}
$pages = $this->getPages();
foreach ($pages as $page) {
$id = $page->id;
if (array_key_exists($id, $pageData)) {
$page->parent_id = $pageData[$id];
$page->order = $orders[$id];
$page->save();
}
}
}
/**
* Get all the pages for this Exhibit.
*
* @return Exhibit[]
*/
public function getPages()
{
return $this->getTable('ExhibitPage')->findBy(array('exhibit' => $this->id, 'sort_field' => 'order'));
}
/**
* Get all pages for this Exhibit, indexed by page ID.
*
* @return Exhibit[]
*/
public function getPagesById()
{
$pages = $this->Pages;
$pagesById = array();
foreach ($pages as $page) {
$pagesById[$page->id] = $page;
}
return $pagesById;
}
/**
* Get all pages for this Exhibit, indexed by parent ID.
*
* @return array
*/
public function getPagesByParent()
{
$pages = $this->Pages;
$pagesByParent = array();
foreach ($pages as $page) {
$parent_id = $page->parent_id ? (int) $page->parent_id : 0;
$pagesByParent[$parent_id][] = $page;
}
return $pagesByParent;
}
/**
* Get all the pages for this exhibit with no parent (top-level pages).
*
* @return ExhibitPage[]
*/
public function getTopPages()
{
if (!$this->exists()) {
return array();
}
$pages = $this->PagesByParent;
return isset($pages[0]) ? $pages[0] : array();
}
/**
* Get the first page with no parent.
*
* @return ExhibitPage|null
*/
public function getFirstTopPage()
{
$topPages = $this->getTopPages();
$topPage = null;
if ($topPages) {
$topPage = current($topPages);
}
return $topPage;
}
/**
* Get the number of pages for the exhibit. Optionally, restrict the count
* to only top-level pages.
*
* @param boolean $topOnly Whether to count only top pages
* @return ExhibitPage[]
*/
public function countPages($topOnly = false)
{
return $this->getTable('ExhibitPage')->count(array(
'exhibit' => $this->id, 'topOnly' => $topOnly));
}
/**
* Alias for countPages, for compatibility purposes.
*
* @deprecated
* @see countPages()
* @param boolean $topOnly Whether to count only top pages
* @return ExhibitPage[]
*/
public function getPagesCount($topOnly = false)
{
return $this->countPages($topOnly);
}
/**
* Determine whether an exhibit uses a particular item on any of its pages.
*
* @param Item $item
* @return boolean
*/
public function hasItem(Item $item)
{
if (!$item->exists()) {
throw new InvalidArgumentException("Item does not exist (is not persisted).");
}
if (!$this->exists()) {
throw new RuntimeException("Cannot call hasItem() on a new (non-persisted) exhibit.");
}
return $this->getTable()->exhibitHasItem($this->id, $item->id);
}
/**
* Set options and optionally the theme name.
*
* @param array $themeOptions
* @param string|null $themeName
*/
public function setThemeOptions($themeOptions, $themeName = null)
{
if ($themeName === null) {
$themeName = $this->theme;
}
if ($themeName !== null && $themeName != '') {
$themeOptionsArray = unserialize($this->theme_options);
$themeOptionsArray[$themeName] = $themeOptions;
}
$this->theme_options = serialize($themeOptionsArray);
}
/**
* Get the options for the exhibit's theme.
*
* @param string|null $themeName If specified, get options for this theme
* instead of the exhibit's theme
* @return array
*/
public function getThemeOptions($themeName = null)
{
if ($themeName === null) {
$themeName = $this->theme;
}
$themeName = (string)$themeName;
if ($themeName == '' || empty($this->theme_options)) {
return array();
}
$themeOptionsArray = unserialize($this->theme_options);
return @$themeOptionsArray[$themeName];
}
/**
* Get a URL to this exhibit with the specified action.
*
* @param string $action Action to link to
* @return string
*/
public function getRecordUrl($action = 'show')
{
if ('show' == $action) {
return exhibit_builder_exhibit_uri($this);
}
$urlHelper = new Omeka_View_Helper_Url;
$params = array('action' => $action, 'id' => $this->id);
return $urlHelper->url($params, 'exhibitStandard');
}
/**
* Get the specified cover image File for this exhibit. If none exists,
* return null.
*
* @return File|null
*/
public function getCoverImage()
{
$db = $this->getDb();
$fileTable = $this->getDb()->getTable('File');
$file = $fileTable->find($this->cover_image_file_id);
return ($file) ? $file : null;
}
/**
* Get a representative File for this Exhibit.
*
* The representative File is the File with the id specified in
* cover_image_file_id. If that field is empty, the first File in the
* Exhibit is returned. If the Exhibit has no associated Files, null is
* returned.
*
* @return File|null
*/
public function getFile()
{
$file = null;
if (isset($this->cover_image_file_id)) {
$file = $this->getCoverImage();
} elseif ($this->exists()) {
$db = $this->getDb();
$fileTable = $this->getDb()->getTable('File');
$select =
$fileTable->getSelect()
->joinInner(
array('eba' => $db->ExhibitBlockAttachment),
'eba.file_id = files.id',
array()
)
->joinInner(
array('epb' => $db->ExhibitPageBlock),
'epb.id = eba.block_id',
array()
)
->joinInner(
array('ep' => $db->ExhibitPage),
'ep.id = epb.page_id',
array()
)
->where('ep.exhibit_id = ?', $this->id)
->where('files.has_derivative_image = 1')
->order(array('ep.order', 'ep.parent_id', 'epb.order', 'eba.order'))
->limit(1);
$file = $fileTable->fetchObject($select);
}
return $file;
}
/**
* Required by Zend_Acl_Resource_Interface.
*
* @return string
*/
public function getResourceId()
{
return 'ExhibitBuilder_Exhibits';
}
}