forked from omeka/plugin-ExhibitBuilder
/
ExhibitPageBlock.php
199 lines (178 loc) · 4.51 KB
/
ExhibitPageBlock.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
<?php
/**
* @copyright Roy Rosenzweig Center for History and New Media, 2007-2012
* @license http://www.gnu.org/licenses/gpl-3.0.txt
* @package ExhibitBuilder
*/
/**
* ExhibitPageBlock model.
*
* @package ExhibitBuilder
*/
class ExhibitPageBlock extends Omeka_Record_AbstractRecord
{
/**
* ID of the page that owns this block.
*
* @var integer
*/
public $page_id;
/**
* Identifier of the layout being used by this block.
*
* @var string
*/
public $layout;
/**
* JSON-encoded set of options for the layout.
*
* @var string
*/
public $options;
/**
* User-input text for this block, in HTML.
*
* @var string
*/
public $text;
/**
* Order of this block on the page.
*
* @var integer
*/
public $order;
/**
* Related record mappings.
*
* @var array
*/
protected $_related = array('ExhibitBlockAttachment' => 'getAttachments');
/**
* Delete all attachments when deleting the block.
*/
protected function _delete()
{
if ($this->ExhibitBlockAttachment) {
foreach ($this->ExhibitBlockAttachment as $attachment) {
$attachment->delete();
}
}
}
/**
* Get the page that owns the block.
*
* @return ExhibitPage
*/
public function getPage()
{
return $this->getTable('ExhibitPage')->find($this->page_id);
}
/**
* Set the data for this block from an array.
*
* @param array $data Data to set
*/
public function setData($data)
{
if (!empty($data['layout'])) {
$this->layout = $data['layout'];
}
if (!empty($data['options'])) {
$this->setOptions($data['options']);
}
if (!empty($data['text'])) {
$this->text = $data['text'];
} else {
$this->text = null;
}
if (!empty($data['attachments'])) {
$this->setAttachments($data['attachments']);
} else {
$this->setAttachments(array());
}
if (!empty($data['order'])) {
$this->order = $data['order'];
}
}
/**
* Get a PHP array from the JSON-serialized layout options.
*
* @return array
*/
public function getOptions()
{
if (!empty($this->options)) {
return json_decode($this->options, true);
} else {
return array();
}
}
/**
* Set an key-value array of options to be JSON-encoded.
*
* @param array $options
*/
public function setOptions($options)
{
$this->options = json_encode($options);
}
/**
* Get the attachments for this block.
*
* @return ExhibitBlockAttachment[]
*/
public function getAttachments()
{
return $this->getTable('ExhibitBlockAttachment')->findByBlock($this);
}
/**
* Set attachment data for this block by array.
*
* @param array $attachmentsData Array of key-value arrays of data for each
* attachment.
* @param boolean $deleteExtras Whether to delete extra preexisting
* attachments after setting new data.
*/
public function setAttachments($attachmentsData, $deleteExtras = true)
{
// We have to have an ID to proceed.
if (!$this->exists()) {
$this->save();
}
$existingAttachments = $this->getAttachments();
foreach ($attachmentsData as $i => $attachmentData) {
if (!empty($existingAttachments)) {
$attachment = array_pop($existingAttachments);
} else {
$attachment = new ExhibitBlockAttachment;
$attachment->block_id = $this->id;
}
$attachment->setData($attachmentData);
$attachment->save();
}
if ($deleteExtras) {
foreach ($existingAttachments as $extraAttachment) {
$extraAttachment->delete();
}
}
}
/**
* Get the layout object for this page's layout.
*
* @return ExhibitLayout
*/
public function getLayout()
{
return ExhibitLayout::getLayout($this->layout);
}
/**
* Get the stem for form name attributes for this block. The stem uses
* integer keys, based on the initial order of the block.
*
* @return string
*/
public function getFormStem()
{
return 'blocks[' . ($this->order - 1) . ']';
}
}