-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
93 lines (81 loc) · 3.13 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
<?php
namespace ItemSetTotalPages;
use Omeka\Module\AbstractModule;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Omeka\Entity\Item;
use Omeka\Entity\ItemSet;
class Module extends AbstractModule
{
private $isUpdating = false;
private $itemSets = [];
private $oldExtent = null;
public function attachListeners(SharedEventManagerInterface $sharedEventManager)
{
// Attach to events that modify an item's extent, visibility, or its collection membership
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.update.post',
[$this, 'updateTotalPages']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.delete.post',
[$this, 'updateTotalPages']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.create.post',
[$this, 'updateTotalPages']
);
$sharedEventManager->attach(
'Omeka\Api\Adapter\ItemSetAdapter',
'api.update.post',
[$this, 'updateTotalPages']
);
}
public function updateTotalPages(Event $event) {
if($this->isUpdating) {
return;
}
$this->isUpdating = true;
$response = $event->getParam('response');
$resource = $response->getContent();
if ($resource instanceof Item) {
$itemSets = $resource->getItemSets();
foreach ($itemSets as $itemSet) {
$this->calculateTotalPages($itemSet->getId());
}
} elseif ($resource instanceof ItemSet) {
$this->calculateTotalPages($resource->getId());
}
}
private function calculateTotalPages($itemSetId)
{
// Get total pages.
$sql = "SELECT SUM(CAST(value.value AS UNSIGNED)) AS total_pages
FROM value
INNER JOIN property ON value.property_id = property.id
INNER JOIN resource ON value.resource_id = resource.id
INNER JOIN item_item_set ON item_item_set.item_id = resource.id
WHERE property.local_name = 'extent' AND resource.is_public = true
AND item_item_set.item_set_id = ".$itemSetId;
$services = $this->getServiceLocator();
$conn = $services->get('Omeka\Connection');
$count = $conn->fetchOne($sql);
if(!$count) {
return;
}
// See if the property exists on the itemset already.
$propID = $conn->fetchOne("SELECT id FROM `value` WHERE resource_id='".$itemSetId."' AND property_id=(SELECT id FROM property WHERE local_name = 'extent')");
if($propID) {
$sql = "UPDATE `value` SET `value` = '".$count."' WHERE id='".$propID."'";
} else {
$sql = "
INSERT INTO `value` (`type`, `is_public`, `property_id`, `value`, `resource_id`)
VALUES ('literal', true, (SELECT id FROM property WHERE local_name = 'extent'), '".$count."', '".$itemSetId."')
";
}
$conn->exec($sql);
}
}