-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModule.php
230 lines (205 loc) · 9.36 KB
/
Module.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
<?php
namespace Hierarchy;
use Omeka\Module\AbstractModule;
use Omeka\Entity\Item;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Laminas\View\Renderer\PhpRenderer;
use Laminas\Mvc\Controller\AbstractController;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\Form\Fieldset;
use Laminas\Mvc\MvcEvent;
use Laminas\EventManager\Event;
use Hierarchy\Form\ConfigForm;
class Module extends AbstractModule
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function onBootstrap(MvcEvent $event)
{
parent::onBootstrap($event);
$acl = $this->getServiceLocator()->get('Omeka\Acl');
// Allow all users to access public hierarchies
$acl->allow(
null,
['Hierarchy\Entity\Hierarchy',
'Hierarchy\Entity\HierarchyGrouping',
'Hierarchy\Api\Adapter\HierarchyGroupingAdapter',
'Hierarchy\Api\Adapter\HierarchyAdapter',
],
);
$acl->allow(null, 'Hierarchy\Controller\Site\Index', 'hierarchy');
}
public function install(ServiceLocatorInterface $serviceLocator)
{
$connection = $serviceLocator->get('Omeka\Connection');
$connection->exec('CREATE TABLE hierarchy_grouping (id INT AUTO_INCREMENT NOT NULL, item_set_id INT DEFAULT NULL, hierarchy_id INT NOT NULL, parent_grouping INT DEFAULT NULL, `label` VARCHAR(255) DEFAULT NULL, position INT NOT NULL, INDEX IDX_DCDE57FF960278D7 (item_set_id), INDEX IDX_DCDE57FF582A8328 (hierarchy_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;');
$connection->exec('CREATE TABLE hierarchy (id INT AUTO_INCREMENT NOT NULL, `label` VARCHAR(255) DEFAULT NULL, position INT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;');
$connection->exec('ALTER TABLE hierarchy_grouping ADD CONSTRAINT FK_DCDE57FF960278D7 FOREIGN KEY (item_set_id) REFERENCES item_set (id) ON DELETE SET NULL;');
$connection->exec('ALTER TABLE hierarchy_grouping ADD CONSTRAINT FK_DCDE57FF582A8328 FOREIGN KEY (hierarchy_id) REFERENCES hierarchy (id) ON DELETE CASCADE;');
$api = $serviceLocator->get('Omeka\ApiManager');
$sites = $api->search('sites', [])->getContent();
$siteSettings = $serviceLocator->get('Omeka\Settings\Site');
// Turn all hierarchy site view settings on by default
foreach ($sites as $site) {
$siteSettings->setTargetId($site->id());
$siteSettings->set('hierarchy_show_label', '1');
$siteSettings->set('hierarchy_show_count', '1');
$siteSettings->set('hierarchy_group_resources', '1');
}
}
public function uninstall(ServiceLocatorInterface $serviceLocator)
{
$connection = $serviceLocator->get('Omeka\Connection');
$connection->exec('ALTER TABLE hierarchy_grouping DROP FOREIGN KEY FK_DCDE57FF960278D7');
$connection->exec('ALTER TABLE hierarchy_grouping DROP FOREIGN KEY FK_DCDE57FF582A8328');
$connection->exec('DROP TABLE hierarchy');
$connection->exec('DROP TABLE hierarchy_grouping');
$api = $serviceLocator->get('Omeka\ApiManager');
$sites = $api->search('sites', [])->getContent();
$siteSettings = $serviceLocator->get('Omeka\Settings\Site');
foreach ($sites as $site) {
$siteSettings->setTargetId($site->id());
$siteSettings->delete('hierarchy_show_label');
$siteSettings->delete('hierarchy_show_count');
$siteSettings->delete('hierarchy_group_resources');
$siteSettings->delete('site_hierarchies');
}
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager)
{
$sharedEventManager->attach(
'Omeka\Form\SiteSettingsForm',
'form.add_elements',
[$this, 'addSiteSettings']
);
$sharedEventManager->attach(
'Omeka\Form\SiteSettingsForm',
'form.add_input_filters',
[$this, 'addSiteSettingsInputFilters']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\Item',
'view.show.sidebar',
[$this, 'addItemAdminHierarchies']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\ItemSet',
'view.show.sidebar',
[$this, 'addItemSetAdminHierarchies']
);
}
/**
* Add elements to the site settings form.
*
* @param Event $event
*/
public function addSiteSettings(Event $event)
{
$services = $this->getServiceLocator();
$siteSettings = $services->get('Omeka\Settings\Site');
$form = $event->getTarget();
$groups = $form->getOption('element_groups');
$groups['hierarchy'] = 'Hierarchy'; // @translate
$form->setOption('element_groups', $groups);
$form->add([
'type' => 'checkbox',
'name' => 'hierarchy_show_label',
'options' => [
'element_group' => 'hierarchy',
'label' => 'Show hierarchy label', // @translate
'info' => 'If checked, assigned label will display as hierarchy header on public pages.', // @translate
],
'attributes' => [
'id' => 'show-label',
'value' => $siteSettings->get('hierarchy_show_label', true),
],
]);
$form->add([
'type' => 'checkbox',
'name' => 'hierarchy_show_count',
'options' => [
'element_group' => 'hierarchy',
'label' => 'Show hierarchy resource counts', // @translate
'info' => 'If checked, hierarchy groupings will show # of resources within currently assigned itemSet.', // @translate
],
'attributes' => [
'id' => 'show-count',
'value' => $siteSettings->get('hierarchy_show_count', true),
],
]);
$form->add([
'type' => 'checkbox',
'name' => 'hierarchy_group_resources',
'options' => [
'element_group' => 'hierarchy',
'label' => 'Combine hierarchy resources', // @translate
'info' => 'If checked, groupings will display resources of all child groupings in resource counts and on hierarchy grouping browse pages.', // @translate
],
'attributes' => [
'id' => 'group-resources',
'value' => $siteSettings->get('hierarchy_group_resources', true),
],
]);
}
/**
* Add input filters to the site settings form.
*
* @param Event $event
*/
public function addSiteSettingsInputFilters(Event $event)
{
$inputFilter = $event->getParam('inputFilter');
$inputFilter->add([
'name' => 'hierarchy_show_label',
'required' => false,
'allow_empty' => true,
]);
$inputFilter->add([
'name' => 'hierarchy_show_count',
'required' => false,
'allow_empty' => true,
]);
$inputFilter->add([
'name' => 'hierarchy_group_resources',
'required' => false,
'allow_empty' => true,
]);
}
// Add relevant hierarchy nested lists to item admin display sidebar
public function addItemAdminHierarchies(Event $event)
{
$view = $event->getTarget();
$view->headLink()->appendStylesheet($view->assetUrl('css/hierarchy.css', 'Hierarchy'));
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
if ($view->item->itemSets()) {
echo '<div class="meta-group">';
echo '<h4>' . $view->translate('Hierarchies') . '</h4>';
echo '<div class="hierarchies value">';
// Get order for printing item's sets from position on hierarchy page
$itemSetOrder = array_filter($api->search('hierarchy_grouping', ['sort_by' => 'position'], ['returnScalar' => 'item_set'])->getContent());
$itemSets = array_replace(array_flip($itemSetOrder), $view->item->itemSets());
foreach ($itemSets as $currentItemSet) {
if (is_numeric($currentItemSet)) {
continue;
}
$groupings = $api->search('hierarchy_grouping', ['item_set' => $currentItemSet->id(), 'sort_by' => 'position'])->getContent();
$view->hierarchyHelper()->buildNestedList($groupings, $currentItemSet, $view->item);
}
echo '</div></div>';
}
}
// Add relevant hierarchy nested lists to item set admin display sidebar
public function addItemSetAdminHierarchies(Event $event)
{
$view = $event->getTarget();
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
$groupings = $api->search('hierarchy_grouping', ['item_set' => $view->resource->id(), 'sort_by' => 'position'])->getContent();
echo '<div class="meta-group">';
echo '<h4>' . $view->translate('Hierarchies') . '</h4>';
echo '<div class="hierarchies value">';
$view->hierarchyHelper()->buildNestedList($groupings, $view->resource, $view->item);
echo '</div></div>';
}
}