Skip to content

Commit

Permalink
Added property "settings" to the resource template.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-KM committed Aug 24, 2020
1 parent 1cf0f04 commit dc97c02
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public function __construct(?\Closure $initializer = null, ?\Closure $cloner = n
public function __sleep()
{
if ($this->__isInitialized__) {
return ['__isInitialized__', 'id', 'label', 'owner', 'resourceClass', 'titleProperty', 'descriptionProperty', 'resourceTemplateProperties', 'resources'];
return ['__isInitialized__', 'id', 'label', 'owner', 'resourceClass', 'titleProperty', 'descriptionProperty', 'settings', 'resourceTemplateProperties', 'resources'];
}

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

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

/**
* {@inheritDoc}
*/
public function setSettings(array $settings)
{

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

return parent::setSettings($settings);
}

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

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

return parent::getSettings();
}

/**
* {@inheritDoc}
*/
Expand Down
1 change: 1 addition & 0 deletions application/data/install/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ CREATE TABLE `resource_template` (
`title_property_id` int(11) DEFAULT NULL,
`description_property_id` int(11) DEFAULT NULL,
`label` varchar(190) COLLATE utf8mb4_unicode_ci NOT NULL,
`settings` longtext COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '(DC2Type:json_array)',
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_39ECD52EEA750E8` (`label`),
KEY `IDX_39ECD52E7E3C61F9` (`owner_id`),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Omeka\Db\Migrations;

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

class AddResourceTemplateSettings implements MigrationInterface
{
public function up(Connection $conn)
{
$sql = <<<SQL
ALTER TABLE `resource_template`
ADD `settings` longtext COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '(DC2Type:json_array)';
SQL;
$conn->exec($sql);

$sql = <<<SQL
UPDATE `resource_template`
SET `settings` = '[]';
SQL;
$conn->exec($sql);
}
}
5 changes: 5 additions & 0 deletions application/src/Api/Adapter/ResourceTemplateAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public function hydrate(Request $request, EntityInterface $entity,
$entity->setDescriptionProperty($descriptionProperty);
}

if ($this->shouldHydrate($request, 'o:settings')) {
$settings = $request->getValue('o:settings') ?: [];
$entity->setSettings($settings);
}

if ($this->shouldHydrate($request, 'o:resource_template_property')
&& isset($data['o:resource_template_property'])
&& is_array($data['o:resource_template_property'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function getJsonLd()
'o:resource_class' => $resourceClass,
'o:title_property' => $titleProperty,
'o:description_property' => $descriptionProperty,
'o:settings' => $this->settings(),
'o:resource_template_property' => $this->resourceTemplateProperties(),
];
}
Expand Down Expand Up @@ -95,6 +96,27 @@ public function descriptionProperty()
->getRepresentation($this->resource->getDescriptionProperty());
}

/**
* @return array
*/
public function settings()
{
return $this->resource->getSettings();
}

/**
* @param string $name
* @param mixed $default
* @return mixed
*/
public function setting($name, $default = null)
{
$settings = $this->resource->getSettings();
return array_key_exists($name, $settings)
? $settings[$name]
: $default;
}

/**
* Return the properties assigned to this resource template.
*
Expand Down
15 changes: 15 additions & 0 deletions application/src/Entity/ResourceTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class ResourceTemplate extends AbstractEntity
*/
protected $descriptionProperty;

/**
* @Column(type="json_array", nullable=false)
*/
protected $settings;

/**
* @OneToMany(
* targetEntity="ResourceTemplateProperty",
Expand Down Expand Up @@ -124,6 +129,16 @@ public function getDescriptionProperty()
return $this->descriptionProperty;
}

public function setSettings(array $settings)
{
$this->settings = $settings;
}

public function getSettings()
{
return $this->settings;
}

public function getResourceTemplateProperties()
{
return $this->resourceTemplateProperties;
Expand Down

0 comments on commit dc97c02

Please sign in to comment.