Skip to content

Commit

Permalink
ref: Minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
joeirimpan committed May 24, 2023
1 parent 3f1efc8 commit 436c834
Showing 1 changed file with 51 additions and 52 deletions.
103 changes: 51 additions & 52 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use Generic\AbstractModule;
use Laminas\EventManager\Event;
use Laminas\Mvc\Controller\AbstractController;
use Laminas\EventManager\SharedEventManagerInterface;
use Omeka\Stdlib\Message;
use Omeka\Mvc\Controller\Plugin\Messenger;
Expand Down Expand Up @@ -45,52 +44,54 @@ public function attachListeners(SharedEventManagerInterface $sharedEventManager)

public function dedupAction(Event $event): void
{
$request = $event->getParam('request');
// Ignore other requests
if ($request->getOperation() != 'create' && $request->getOperation() != 'update') {
return;
}

$serviceLocator = $this->getServiceLocator();
$logger = $serviceLocator->get('Omeka\Logger');
$settings = $serviceLocator->get('Omeka\Settings');
$messenger = new Messenger;

$request = $event->getParam('request');
if ($request->getOperation() == 'create' || $request->getOperation() == 'update') {
$itemId = (int) $request->getId();
$data = $request->getContent();
$propIds = $settings->get('unique_properties');

$allowedProps = array();
foreach ($propIds as $propId) {
$allowedProps[$propId] = true;
}
$itemId = (int) $request->getId();
$data = $request->getContent();
$propIds = $settings->get('unique_properties');

$itemPropIDs = array();
// Fetch the property values from this request.
$propValues = array();
foreach ($data as $key => $value) {
if (is_array($value)) {
foreach ($value as $innerKey => $innerValue) {
if (isset($innerValue['property_id']) && !empty($innerValue['@value'])) {
// ignore property ids that are not matching
if (!$allowedProps[$innerValue['property_id']]) {
continue;
}
array_push($propValues, $innerValue['@value']);
array_push($itemPropIDs, $innerValue['property_id']);
$allowedProps = array();
foreach ($propIds as $propId) {
$allowedProps[$propId] = true;
}

$itemPropIDs = array();
// Fetch the property values from this request.
$propValues = array();
foreach ($data as $key => $value) {
if (is_array($value)) {
foreach ($value as $innerKey => $innerValue) {
if (isset($innerValue['property_id']) && !empty($innerValue['@value'])) {
// ignore property ids that are not matching
if (!$allowedProps[$innerValue['property_id']]) {
continue;
}
array_push($propValues, $innerValue['@value']);
array_push($itemPropIDs, $innerValue['property_id']);
}
}
}
}

// Convert property-ids, property-values into json
// This allows us to load this into a derived table in MySQL
$jsonPairs = json_encode(array_map(function ($value, $property_id) {
return ['value' => $value, 'property_id' => $property_id];
}, $propValues, $itemPropIDs), JSON_UNESCAPED_UNICODE);
// $logger->info(new Message(print_r($jsonPairs, true)));
// Convert property-ids, property-values into json
// This allows us to load this into a derived table in MySQL
$jsonPairs = json_encode(array_map(function ($value, $property_id) {
return ['value' => $value, 'property_id' => $property_id];
}, $propValues, $itemPropIDs), JSON_UNESCAPED_UNICODE);

$services = $this->getServiceLocator();
$connection = $services->get('Omeka\Connection');
$services = $this->getServiceLocator();
$connection = $services->get('Omeka\Connection');

// Check if same property-id, property-value exists in any other items.
$sql = <<<'SQL'
// Check if same property-id, property-value exists in any other items.
$sql = <<<'SQL'
SELECT
CONCAT(
`vocabulary`.`label`,
Expand Down Expand Up @@ -123,23 +124,21 @@ public function dedupAction(Event $event): void
AND derived.property_id = value.property_id
)
SQL;
$stmt = $connection->executeQuery($sql, [
'id' => $itemId,
'property_ids' => $itemPropIDs,
'property_values' => $propValues,
'json_pairs' => $jsonPairs,
], [
'property_ids' => \Doctrine\DBAL\Connection::PARAM_STR_ARRAY,
'property_values' => \Doctrine\DBAL\Connection::PARAM_STR_ARRAY,
'json_pairs' => \PDO::PARAM_STR,
]);
$results = $stmt->fetchAll();
$logger->info(new Message(print_r($results, true)));

foreach ($results as $result) {
$messenger->addError(new Message('Duplicate property value `%s` found for `%s` in #%d', $result['data'], $result['field'], $result['resource_id']));
throw new ValidationException('');
}
$stmt = $connection->executeQuery($sql, [
'id' => $itemId,
'property_ids' => $itemPropIDs,
'property_values' => $propValues,
'json_pairs' => $jsonPairs,
], [
'property_ids' => \Doctrine\DBAL\Connection::PARAM_STR_ARRAY,
'property_values' => \Doctrine\DBAL\Connection::PARAM_STR_ARRAY,
'json_pairs' => \PDO::PARAM_STR,
]);
$results = $stmt->fetchAll();

foreach ($results as $result) {
$messenger->addError(new Message('Duplicate property value `%s` found for `%s` in #%d', $result['data'], $result['field'], $result['resource_id']));
throw new ValidationException('');
}
}
}

0 comments on commit 436c834

Please sign in to comment.