/
Collection.php
235 lines (214 loc) · 5.44 KB
/
Collection.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
<?php
/**
* Omeka
*
* @copyright Copyright 2007-2012 Roy Rosenzweig Center for History and New Media
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
*/
/**
* A collection and its metadata.
*
* @package Omeka\Record
*/
class Collection extends Omeka_Record_AbstractRecord implements Zend_Acl_Resource_Interface
{
/**
* Whether or not the collection is publicly accessible.
*
* @var bool
*/
public $public = 0;
/**
* Whether or not the collection is featured.
*
* @var bool
*/
public $featured = 0;
/**
* Date the collection was added.
*
* @var string
*/
public $added;
/**
* Date the collection was last modified.
*
* @var string
*/
public $modified;
/**
* ID for the User that created this collection.
*
* @var int
*/
public $owner_id = 0;
/**
* Related records.
*
* @see Omeka_Record_AbstractRecord::__get
*/
protected $_related = array(
'Elements' => 'getElements',
'ElementTexts'=>'getElementText'
);
/**
* Initialize the mixins.
*/
protected function _initializeMixins()
{
$this->_mixins[] = new Mixin_PublicFeatured($this);
$this->_mixins[] = new Mixin_Owner($this);
$this->_mixins[] = new Mixin_ElementText($this);
$this->_mixins[] = new Mixin_Timestamp($this);
$this->_mixins[] = new Mixin_Search($this);
}
/**
* Get a property about this collection.
*
* Valid properties for a Collection include:
* * (int) public
* * (int) featured
* * (string) added
* * (string) modified
* * (int) owner_id
* * (int) total_items
*
* @param string $property The property to get, always lowercase.
* @return mixed The value of the property
*/
public function getProperty($property)
{
switch ($property) {
case 'total_items':
return $this->totalItems();
default:
return parent::getProperty($property);
}
}
/**
* Get the total number of items in this collection.
*
* @return int
*/
public function totalItems()
{
return $this->getDb()->getTable('Item')->count(array('collection' => $this->id));
}
/**
* Set the user who added the collection.
*
* Note that this is not to be confused with the collection's "contributors".
*
* @param User $user
*/
public function setAddedBy(User $user)
{
if (!$user->exists()) {
throw new RuntimeException(__("Cannot associate the collection with an unsaved user."));
}
$this->owner_id = $user->id;
}
/**
* Required by Zend_Acl_Resource_Interface.
*
* Identifies Collection records as relating to the Collections ACL
* resource.
*
* @return string
*/
public function getResourceId()
{
return 'Collections';
}
/**
* Return whether the collection has at least 1 contributor element text.
*
* @return bool
*/
public function hasContributor()
{
return $this->hasElementText('Dublin Core', 'Contributor');
}
/**
* Filter the POST data from the form.
*
* Converts public/featured flags to booleans.
*
* @param array $post
* @return array
*/
protected function filterPostData($post)
{
$options = array('inputNamespace'=>'Omeka_Filter');
// User form input does not allow HTML tags or superfluous whitespace
$filters = array('public' => 'Boolean',
'featured' => 'Boolean');
$filter = new Zend_Filter_Input($filters, null, $post, $options);
$post = $filter->getUnescaped();
return $post;
}
/**
* All of the custom code for deleting an collection.
*
* Delete the element texts for this record.
*
* @return void
*/
protected function _delete()
{
$this->deleteElementTexts();
$this->_dissociateItems();
}
/**
* Set items attached to this collection back to "no collection."
*/
protected function _dissociateItems()
{
$db = $this->getDb();
$db->update($db->Item, array('collection_id' => null),
array('collection_id = ?' => $this->id));
}
/**
* Before-save hook.
*
* Fire the before-save element texts code.
*/
protected function beforeSave($args)
{
if ($args['post']) {
$post = $args['post'];
$this->beforeSaveElements($post);
}
}
/**
* After-save hook.
*
* Handle public/private status for search.
*/
protected function afterSave()
{
if (!$this->public) {
$this->setSearchTextPrivate();
}
}
/**
* Get a representative file for this Collection.
*
* @return File|null
*/
public function getFile()
{
$itemTable = $this->getDb()->getTable('Item');
$itemArray = $itemTable->findBy(array(
'collection' => $this->id,
'hasImage' => true,
'sort_field' => 'featured',
'sort_dir' => 'd'
), 1);
if ($itemArray) {
return ($itemArray[0]->getFile());
} else {
return null;
}
}
}