Skip to content

Commit

Permalink
Begin implementing resource titles
Browse files Browse the repository at this point in the history
refs #1377
  • Loading branch information
jimsafley committed Apr 19, 2019
1 parent e0ae7ee commit f75acfe
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 8 deletions.
26 changes: 24 additions & 2 deletions application/data/doctrine-proxies/__CG__OmekaEntityItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public function __construct($initializer = null, $cloner = null)
public function __sleep()
{
if ($this->__isInitialized__) {
return ['__isInitialized__', 'id', 'media', 'siteBlockAttachments', 'itemSets', 'owner', 'resourceClass', 'resourceTemplate', 'thumbnail', 'isPublic', 'created', 'modified', 'values'];
return ['__isInitialized__', 'id', 'media', 'siteBlockAttachments', 'itemSets', 'owner', 'resourceClass', 'resourceTemplate', 'thumbnail', 'title', 'isPublic', 'created', 'modified', 'values'];
}

return ['__isInitialized__', 'id', 'media', 'siteBlockAttachments', 'itemSets', 'owner', 'resourceClass', 'resourceTemplate', 'thumbnail', 'isPublic', 'created', 'modified', 'values'];
return ['__isInitialized__', 'id', 'media', 'siteBlockAttachments', 'itemSets', 'owner', 'resourceClass', 'resourceTemplate', 'thumbnail', 'title', 'isPublic', 'created', 'modified', 'values'];
}

/**
Expand Down Expand Up @@ -320,6 +320,28 @@ public function getThumbnail()
return parent::getThumbnail();
}

/**
* {@inheritDoc}
*/
public function setTitle($title)
{

$this->__initializer__ && $this->__initializer__->__invoke($this, 'setTitle', [$title]);

return parent::setTitle($title);
}

/**
* {@inheritDoc}
*/
public function getTitle()
{

$this->__initializer__ && $this->__initializer__->__invoke($this, 'getTitle', []);

return parent::getTitle();
}

/**
* {@inheritDoc}
*/
Expand Down
26 changes: 24 additions & 2 deletions application/data/doctrine-proxies/__CG__OmekaEntityItemSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public function __construct($initializer = null, $cloner = null)
public function __sleep()
{
if ($this->__isInitialized__) {
return ['__isInitialized__', 'id', 'isOpen', 'items', 'siteItemSets', 'owner', 'resourceClass', 'resourceTemplate', 'thumbnail', 'isPublic', 'created', 'modified', 'values'];
return ['__isInitialized__', 'id', 'isOpen', 'items', 'siteItemSets', 'owner', 'resourceClass', 'resourceTemplate', 'thumbnail', 'title', 'isPublic', 'created', 'modified', 'values'];
}

return ['__isInitialized__', 'id', 'isOpen', 'items', 'siteItemSets', 'owner', 'resourceClass', 'resourceTemplate', 'thumbnail', 'isPublic', 'created', 'modified', 'values'];
return ['__isInitialized__', 'id', 'isOpen', 'items', 'siteItemSets', 'owner', 'resourceClass', 'resourceTemplate', 'thumbnail', 'title', 'isPublic', 'created', 'modified', 'values'];
}

/**
Expand Down Expand Up @@ -331,6 +331,28 @@ public function getThumbnail()
return parent::getThumbnail();
}

/**
* {@inheritDoc}
*/
public function setTitle($title)
{

$this->__initializer__ && $this->__initializer__->__invoke($this, 'setTitle', [$title]);

return parent::setTitle($title);
}

/**
* {@inheritDoc}
*/
public function getTitle()
{

$this->__initializer__ && $this->__initializer__->__invoke($this, 'getTitle', []);

return parent::getTitle();
}

/**
* {@inheritDoc}
*/
Expand Down
26 changes: 24 additions & 2 deletions application/data/doctrine-proxies/__CG__OmekaEntityMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public function __construct($initializer = null, $cloner = null)
public function __sleep()
{
if ($this->__isInitialized__) {
return ['__isInitialized__', 'id', 'ingester', 'renderer', 'data', 'source', 'mediaType', 'storageId', 'extension', 'sha256', 'size', 'hasOriginal', 'hasThumbnails', 'position', 'item', 'lang', 'owner', 'resourceClass', 'resourceTemplate', 'thumbnail', 'isPublic', 'created', 'modified', 'values'];
return ['__isInitialized__', 'id', 'ingester', 'renderer', 'data', 'source', 'mediaType', 'storageId', 'extension', 'sha256', 'size', 'hasOriginal', 'hasThumbnails', 'position', 'item', 'lang', 'owner', 'resourceClass', 'resourceTemplate', 'thumbnail', 'title', 'isPublic', 'created', 'modified', 'values'];
}

return ['__isInitialized__', 'id', 'ingester', 'renderer', 'data', 'source', 'mediaType', 'storageId', 'extension', 'sha256', 'size', 'hasOriginal', 'hasThumbnails', 'position', 'item', 'lang', 'owner', 'resourceClass', 'resourceTemplate', 'thumbnail', 'isPublic', 'created', 'modified', 'values'];
return ['__isInitialized__', 'id', 'ingester', 'renderer', 'data', 'source', 'mediaType', 'storageId', 'extension', 'sha256', 'size', 'hasOriginal', 'hasThumbnails', 'position', 'item', 'lang', 'owner', 'resourceClass', 'resourceTemplate', 'thumbnail', 'title', 'isPublic', 'created', 'modified', 'values'];
}

/**
Expand Down Expand Up @@ -606,6 +606,28 @@ public function getThumbnail()
return parent::getThumbnail();
}

/**
* {@inheritDoc}
*/
public function setTitle($title)
{

$this->__initializer__ && $this->__initializer__->__invoke($this, 'setTitle', [$title]);

return parent::setTitle($title);
}

/**
* {@inheritDoc}
*/
public function getTitle()
{

$this->__initializer__ && $this->__initializer__->__invoke($this, 'getTitle', []);

return parent::getTitle();
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public function __construct($initializer = null, $cloner = null)
public function __sleep()
{
if ($this->__isInitialized__) {
return ['__isInitialized__', 'id', 'label', 'owner', 'resourceClass', 'resourceTemplateProperties', 'resources'];
return ['__isInitialized__', 'id', 'label', 'owner', 'resourceClass', 'titleProperty', 'resourceTemplateProperties', 'resources'];
}

return ['__isInitialized__', 'id', 'label', 'owner', 'resourceClass', 'resourceTemplateProperties', 'resources'];
return ['__isInitialized__', 'id', 'label', 'owner', 'resourceClass', 'titleProperty', 'resourceTemplateProperties', 'resources'];
}

/**
Expand Down Expand Up @@ -254,6 +254,28 @@ public function getResourceClass()
return parent::getResourceClass();
}

/**
* {@inheritDoc}
*/
public function setTitleProperty(\Omeka\Entity\Property $titleProperty = NULL)
{

$this->__initializer__ && $this->__initializer__->__invoke($this, 'setTitleProperty', [$titleProperty]);

return parent::setTitleProperty($titleProperty);
}

/**
* {@inheritDoc}
*/
public function getTitleProperty()
{

$this->__initializer__ && $this->__initializer__->__invoke($this, 'getTitleProperty', []);

return parent::getTitleProperty();
}

/**
* {@inheritDoc}
*/
Expand Down
5 changes: 5 additions & 0 deletions application/data/install/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ CREATE TABLE `resource` (
`resource_class_id` int(11) DEFAULT NULL,
`resource_template_id` int(11) DEFAULT NULL,
`thumbnail_id` int(11) DEFAULT NULL,
`title` varchar(190) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`is_public` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime DEFAULT NULL,
Expand All @@ -128,6 +129,7 @@ CREATE TABLE `resource` (
KEY `IDX_BC91F416448CC1BD` (`resource_class_id`),
KEY `IDX_BC91F41616131EA` (`resource_template_id`),
KEY `IDX_BC91F416FDFF2E92` (`thumbnail_id`),
KEY `title` (`title`),
CONSTRAINT `FK_BC91F41616131EA` FOREIGN KEY (`resource_template_id`) REFERENCES `resource_template` (`id`) ON DELETE SET NULL,
CONSTRAINT `FK_BC91F416448CC1BD` FOREIGN KEY (`resource_class_id`) REFERENCES `resource_class` (`id`) ON DELETE SET NULL,
CONSTRAINT `FK_BC91F4167E3C61F9` FOREIGN KEY (`owner_id`) REFERENCES `user` (`id`) ON DELETE SET NULL,
Expand All @@ -151,12 +153,15 @@ CREATE TABLE `resource_template` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`owner_id` int(11) DEFAULT NULL,
`resource_class_id` int(11) DEFAULT NULL,
`title_property_id` int(11) DEFAULT NULL,
`label` varchar(190) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_39ECD52EEA750E8` (`label`),
KEY `IDX_39ECD52E7E3C61F9` (`owner_id`),
KEY `IDX_39ECD52E448CC1BD` (`resource_class_id`),
KEY `IDX_39ECD52E724734A3` (`title_property_id`),
CONSTRAINT `FK_39ECD52E448CC1BD` FOREIGN KEY (`resource_class_id`) REFERENCES `resource_class` (`id`) ON DELETE SET NULL,
CONSTRAINT `FK_39ECD52E724734A3` FOREIGN KEY (`title_property_id`) REFERENCES `property` (`id`) ON DELETE SET NULL,
CONSTRAINT `FK_39ECD52E7E3C61F9` FOREIGN KEY (`owner_id`) REFERENCES `user` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `resource_template_property` (
Expand Down
18 changes: 18 additions & 0 deletions application/data/migrations/20190307043537_AddResourceTitle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Omeka\Db\Migrations;

use Doctrine\DBAL\Connection;
use Omeka\Db\Migration\MigrationInterface;

class AddResourceTitle implements MigrationInterface
{
public function up(Connection $conn)
{
$conn->exec('
ALTER TABLE resource ADD title VARCHAR(190) DEFAULT NULL;
CREATE INDEX title ON resource (title);
ALTER TABLE resource_template ADD title_property_id INT DEFAULT NULL;
ALTER TABLE resource_template ADD CONSTRAINT FK_39ECD52E724734A3 FOREIGN KEY (title_property_id) REFERENCES property (id) ON DELETE SET NULL;
CREATE INDEX IDX_39ECD52E724734A3 ON resource_template (title_property_id);');
}
}
3 changes: 3 additions & 0 deletions application/src/Api/Adapter/AbstractResourceEntityAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public function hydrate(Request $request, EntityInterface $entity,
// o:resource_template
$this->hydrateResourceTemplate($request, $entity);

// o:title
(new ResourceTitleHydrator)->hydrate($request, $entity, $this);

// o:thumbnail
$this->hydrateThumbnail($request, $entity);

Expand Down
1 change: 1 addition & 0 deletions application/src/Api/Adapter/ItemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ItemAdapter extends AbstractResourceEntityAdapter
'is_public' => 'isPublic',
'created' => 'created',
'modified' => 'modified',
'title' => 'title',
];

public function getResourceName()
Expand Down
1 change: 1 addition & 0 deletions application/src/Api/Adapter/ItemSetAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ItemSetAdapter extends AbstractResourceEntityAdapter
'id' => 'id',
'created' => 'created',
'modified' => 'modified',
'title' => 'title',
];

public function getResourceName()
Expand Down
1 change: 1 addition & 0 deletions application/src/Api/Adapter/MediaAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MediaAdapter extends AbstractResourceEntityAdapter
'is_public' => 'isPublic',
'created' => 'created',
'modified' => 'modified',
'title' => 'title',
];

public function getResourceName()
Expand Down
55 changes: 55 additions & 0 deletions application/src/Api/Adapter/ResourceTitleHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Omeka\Api\Adapter;

use Doctrine\Common\Collections\Criteria;
use Omeka\Api\Request;
use Omeka\Entity\Property;
use Omeka\Entity\Resource;

class ResourceTitleHydrator
{
/**
* Hydrate the title of the resource entity.
*
* Attempts to get the title value of the resource template's title property
* first. If that's not available, attempts to get the title value of the
* dcterms:title property.
*
* @param Request $request
* @param Resource $entity
* @param AbstractResourceEntityAdapter $adapter
*/
public function hydrate(Request $request, Resource $entity,
AbstractResourceEntityAdapter $adapter
) {
$title = null;
$resourceTemplate = $entity->getResourceTemplate();
if ($resourceTemplate && $resourceTemplate->getTitleProperty()) {
$titleProperty = $resourceTemplate->getTitleProperty();
$title = $this->getResourceTitle($titleProperty, $entity);
}
if (null === $title) {
$titleProperty = $adapter->getPropertyByTerm('dcterms:title');
$title = $this->getResourceTitle($titleProperty, $entity);
}
$entity->setTitle($title);
}

/**
* Get the resource title.
*
* @param Property $titleProperty
* @param Resource $entity
* @return string|null
*/
public function getResourceTitle(Property $titleProperty, Resource $entity)
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq('property', $titleProperty))
->andWhere(Criteria::expr()->neq('value', null))
->andWhere(Criteria::expr()->neq('value', ''))
->setMaxResults(1);
$titleValues = $entity->getValues()->matching($criteria);
return $titleValues->isEmpty() ? null : $titleValues->first()->getValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function getJsonLd()
'o:resource_class' => $resourceClass,
'o:resource_template' => $resourceTemplate,
'o:thumbnail' => $thumbnail,
'o:title' => $this->title(),
],
$dateTime,
$this->getResourceJsonLd(),
Expand Down Expand Up @@ -162,6 +163,16 @@ public function thumbnail()
->getRepresentation($this->resource->getThumbnail());
}

/**
* Get the title of this resource.
*
* @return string
*/
public function title()
{
return $this->resource->getTitle();
}

/**
* Get the owner representation of this resource.
*
Expand Down Expand Up @@ -471,6 +482,11 @@ public function displaySubjectValues($page = null, $perPage = null, $property =
*/
public function displayTitle($default = null)
{
$title = $this->title();
if (null !== $title) {
return $title;
}

$title = $this->value('dcterms:title', [
'default' => null,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ public function getJsonLd()
if ($this->resourceClass()) {
$resourceClass = $this->resourceClass()->getReference();
}
$titleProperty = null;
if ($this->titleProperty()) {
$titleProperty = $this->titleProperty()->getReference();
}
return [
'o:label' => $this->label(),
'o:owner' => $owner,
'o:resource_class' => $resourceClass,
'o:title_property' => $titleProperty,
'o:resource_template_property' => $this->resourceTemplateProperties(),
];
}
Expand Down Expand Up @@ -63,6 +68,17 @@ public function resourceClass()
->getRepresentation($this->resource->getResourceClass());
}

/**
* Return the title property of this resource template.
*
* @return ResourceClassRepresentation
*/
public function titleProperty()
{
return $this->getAdapter('properties')
->getRepresentation($this->resource->getTitleProperty());
}

/**
* Return the properties assigned to this resource template.
*
Expand Down
Loading

0 comments on commit f75acfe

Please sign in to comment.