forked from omeka/plugin-ExhibitBuilder
/
ExhibitBlockAttachment.php
154 lines (136 loc) · 3.28 KB
/
ExhibitBlockAttachment.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
<?php
/**
* @copyright Roy Rosenzweig Center for History and New Media, 2007-2012
* @license http://www.gnu.org/licenses/gpl-3.0.txt
* @package ExhibitBuilder
*/
/**
* ExhibitBlockAttachment model.
*
* @package ExhibitBuilder
*/
class ExhibitBlockAttachment extends Omeka_Record_AbstractRecord
{
/**
* ID of the block this attachment belongs to.
*
* @var integer
*/
public $block_id;
/**
* ID of the attached item.
*
* @var integer
*/
public $item_id;
/**
* ID of the attached file, if any.
*
* @var integer
*/
public $file_id;
/**
* User-specified HTML caption for the attachment.
*
* @var string
*/
public $caption;
/**
* Order of this attachment within the block.
*
* @var integer
*/
public $order;
/**
* Related records mappings.
*
* @var array
*/
protected $_related = array(
'Item' => 'getItem',
'File' => 'getFile'
);
/**
* Get the attached item
*
* @return Item|null
*/
public function getItem()
{
if ($this->item_id) {
return $this->getTable('Item')->find($this->item_id);
} else {
return null;
}
}
/**
* Get the attached file.
*
* @return File|null
*/
public function getFile()
{
$file = null;
if ($this->file_id) {
$file = $this->getTable('File')->find($this->file_id);
}
// Fallback if specified file missing or no file specified.
if (!$file && ($item = $this->getItem()) && ($files = $item->Files)) {
$file = $files[0];
}
return $file;
}
/**
* Validate the attachment data.
*
* A valid attachment has a numeric block ID, a numeric order, and a
* numeric item ID.
*/
protected function _validate()
{
if (empty($this->block_id) || !is_numeric($this->block_id)) {
$this->addError('page_id', "Must be associated with an exhibit block.");
}
if ($this->order === null || !is_numeric($this->order)) {
$this->addError('order', "Must be ordered within the block.");
}
if (empty($this->item_id) || !is_numeric($this->item_id)) {
$this->addError(null, 'item_id field must be a valid foreign key');
}
}
/**
* Get the owning block.
*
* @return ExhibitPageBlock
*/
protected function getBlock()
{
return $this->getTable('ExhibitPage')->find($this->block_id);
}
/**
* Set this attachment's data by key-value array.
*
* @param array $data
*/
public function setData($data)
{
if (!empty($data['item_id'])) {
$this->item_id = (int) $data['item_id'];
} else {
$this->item_id = null;
}
if (!empty($data['file_id'])) {
$this->file_id = (int) $data['file_id'];
} else {
$this->file_id = null;
}
if (!empty($data['caption'])) {
$this->caption = $data['caption'];
} else {
$this->caption = null;
}
if (!empty($data['order'])) {
$this->order = $data['order'];
}
}
}