-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElementSet.php
165 lines (151 loc) · 3.8 KB
/
ElementSet.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
<?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
*/
/**
* An element set and its metadata.
*
* @package Omeka\Record
*/
class ElementSet extends Omeka_Record_AbstractRecord implements Zend_Acl_Resource_Interface
{
/**
* Type of record this set applies to.
*
* @var string
*/
public $record_type;
/**
* Name for the element set.
*
* @var string
*/
public $name;
/**
* Description for the element set.
*
* @var string
*/
public $description;
/**
* Child Element records to save when saving this set.
*
* @var array
*/
protected $_elementsToSave = array();
/**
* The name of the item type element set.
*
* This is used wherever it is important to distinguish this special case
* element set from others.
*/
const ITEM_TYPE_NAME = 'Item Type Metadata';
/**
* Get the Elements that are in this set.
*
* @return array
*/
public function getElements()
{
return $this->getTable('Element')->findBySet($this->name);
}
/**
* Add elements to the element set.
*
* @param array $elements
*/
public function addElements(array $elements)
{
foreach ($elements as $order => $options) {
$record = $this->_buildElementRecord($options);
$this->_elementsToSave[] = $record;
}
}
/**
* Create a new Element record with the given data.
*
* @param array $options Data to set on the Element.
* @return Element
*/
private function _buildElementRecord($options)
{
$obj = new Element;
$obj->setArray($options);
return $obj;
}
/**
* After-save hook.
*
* Save the $_elementsToSave and set their orders.
*/
protected function afterSave($args)
{
$maxOrder = $this->_getNextElementOrder();
foreach ($this->_elementsToSave as $order => $obj) {
$obj->element_set_id = $this->id;
$obj->setOrder($maxOrder + (int)$order);
$obj->save();
unset($this->_elementsToSave[$order]);
}
}
/**
* Delete all the elements associated with an element set.
*
* @return void
*/
protected function _delete()
{
// Delete all elements that belong to this element set.
$elements = $this->getElements();
foreach ($elements as $element) {
$element->delete();
}
}
/**
* Get an order value to place an Element at the end of this set.
*
* @return int
*/
private function _getNextElementOrder()
{
$db = $this->getDb();
$sql = "
SELECT MAX(`order`) + 1
FROM $db->Element e
WHERE e.`element_set_id` = ?";
$nextElementOrder = $db->fetchOne($sql, $this->id);
// In MySQL, NULL + 1 = NULL.
if (!$nextElementOrder) {
$nextElementOrder = 1;
}
return $nextElementOrder;
}
/**
* Validate the element set.
*
* Tests that name is non-empty and unique.
*/
protected function _validate()
{
if (!$this->fieldIsUnique('name')) {
$this->addError('Name', __('Name of element set must be unique.'));
}
if (empty($this->name)) {
$this->addError('Name', __('Name of element set must not be empty.'));
}
}
/**
* Identify ElementSet records as relating to the ElementSets ACL resource.
*
* Required by Zend_Acl_Resource_Interface.
*
* @return string
*/
public function getResourceId()
{
return 'ElementSets';
}
}