Add localized labels to databox fields

This commit is contained in:
Romain Neutron
2013-05-30 16:03:14 +02:00
parent e3eee79cb3
commit 82af1ba69e
8 changed files with 179 additions and 6 deletions

View File

@@ -333,6 +333,10 @@ class Fields implements ControllerProviderInterface
->setVocabularyControl(null)
->setVocabularyRestricted(false);
foreach ($data['labels'] as $code => $label) {
$field->set_label($code, $label);
}
if (isset($data['sorter'])) {
$field->set_position($data['sorter']);
}
@@ -361,7 +365,7 @@ class Fields implements ControllerProviderInterface
return array(
'name', 'multi', 'thumbtitle', 'tag', 'business', 'indexable',
'required', 'separator', 'readonly', 'type', 'tbranch', 'report',
'vocabulary-type', 'vocabulary-restricted', 'dces-element'
'vocabulary-type', 'vocabulary-restricted', 'dces-element', 'labels'
);
}

View File

@@ -1783,6 +1783,12 @@ class API_V1_adapter extends API_V1_Abstract
'meta_id' => $value->getId(),
'meta_structure_id' => $field->get_meta_struct_id(),
'name' => $field->get_name(),
'labels' => array(
'fr' => $field->get_databox_field()->get_label('fr'),
'en' => $field->get_databox_field()->get_label('en'),
'de' => $field->get_databox_field()->get_label('de'),
'nl' => $field->get_databox_field()->get_label('nl'),
),
'value' => $value->getValue(),
);
}
@@ -2074,6 +2080,12 @@ class API_V1_adapter extends API_V1_Abstract
'source' => $databox_field->get_tag()->getTagname(),
'tagname' => $databox_field->get_tag()->getName(),
'name' => $databox_field->get_name(),
'labels' => array(
'fr' => $databox_field->get_label('fr'),
'en' => $databox_field->get_label('en'),
'de' => $databox_field->get_label('de'),
'nl' => $databox_field->get_label('nl'),
),
'separator' => $databox_field->get_separator(),
'thesaurus_branch' => $databox_field->get_tbranch(),
'type' => $databox_field->get_type(),

View File

@@ -280,8 +280,9 @@ class caption_record implements caption_interface, cache_cacheableInterface
);
$fields[$field->get_name()] = array(
'value' => $value
, 'separator' => $field->get_databox_field()->get_separator()
'value' => $value,
'label' => $field->get_databox_field()->get_label($this->app['locale.I18n']),
'separator' => $field->get_databox_field()->get_separator(),
);
}

View File

@@ -16,6 +16,7 @@ use Alchemy\Phrasea\Metadata\Tag\Nosource;
use PHPExiftool\Driver\TagInterface;
use PHPExiftool\Driver\TagFactory;
use PHPExiftool\Exception\TagUnknown;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
@@ -111,6 +112,12 @@ class databox_field implements cache_cacheableInterface
*/
protected $thumbtitle;
/**
*
* @var array
*/
protected $labels = array();
/**
*
* @var boolean
@@ -178,7 +185,8 @@ class databox_field implements cache_cacheableInterface
$sql = "SELECT `thumbtitle`, `separator`, `dces_element`, `tbranch`,
`type`, `report`, `multi`, `required`, `readonly`, `indexable`,
`name`, `src`, `business`, `VocabularyControlType`,
`RestrictToVocabularyControl`, `sorter`
`RestrictToVocabularyControl`, `sorter`,
`label_en`, `label_fr`, `label_de`, `label_nl`
FROM metadatas_structure WHERE id=:id";
$stmt = $connbas->prepare($sql);
@@ -198,6 +206,10 @@ class databox_field implements cache_cacheableInterface
$this->on_error = true;
}
foreach (array('en', 'fr', 'de', 'nl') as $code) {
$this->labels[$code] = $row['label_' . $code];
}
$this->name = $row['name'];
$this->indexable = (Boolean) $row['indexable'];
$this->readonly = (Boolean) $row['readonly'];
@@ -357,7 +369,11 @@ class databox_field implements cache_cacheableInterface
`sorter` = :position,
`thumbtitle` = :thumbtitle,
`VocabularyControlType` = :VocabularyControlType,
`RestrictToVocabularyControl` = :RestrictVocab
`RestrictToVocabularyControl` = :RestrictVocab,
`label_en` = :label_en,
`label_fr` = :label_fr,
`label_de` = :label_de,
`label_nl` = :label_nl
WHERE id = :id';
$params = array(
@@ -376,7 +392,11 @@ class databox_field implements cache_cacheableInterface
':thumbtitle' => $this->thumbtitle,
':VocabularyControlType' => $this->Vocabulary ? $this->Vocabulary->getType() : null,
':RestrictVocab' => $this->Vocabulary ? ($this->VocabularyRestriction ? '1' : '0') : '0',
':id' => $this->id
':id' => $this->id,
':label_en' => isset($this->labels['en']) ? $this->labels['en'] : null,
':label_fr' => isset($this->labels['fr']) ? $this->labels['fr'] : null,
':label_de' => isset($this->labels['de']) ? $this->labels['de'] : null,
':label_nl' => isset($this->labels['nl']) ? $this->labels['nl'] : null
);
$stmt = $connbas->prepare($sql);
@@ -431,6 +451,45 @@ class databox_field implements cache_cacheableInterface
return $this;
}
/**
* Sets a localized label for the field.
*
* @param string $code
* @param null|string $value
*
* @return \databox_field
*
* @throws InvalidArgumentException
*/
public function set_label($code, $value)
{
if (!array_key_exists($code, $this->labels)) {
throw new InvalidArgumentException(sprintf('Code %s is not defined', $code));
}
$this->labels[$code] = $value;
return $this;
}
/**
* Gets a localized label for the field.
*
* @param string $code
*
* @return string
*
* @throws InvalidArgumentException
*/
public function get_label($code)
{
if (!array_key_exists($code, $this->labels)) {
throw new InvalidArgumentException(sprintf('Code %s is not defined', $code));
}
return isset($this->labels[$code]) ? $this->labels[$code] : $this->name;
}
/**
*
* @param string $name
@@ -850,6 +909,7 @@ class databox_field implements cache_cacheableInterface
return array(
'id' => $this->id,
'sbas-id' => $this->sbas_id,
'labels' => $this->labels,
'name' => $this->name,
'tag' => $this->tag->getTagname(),
'business' => $this->Business,

View File

@@ -4620,6 +4620,38 @@
<default>0</default>
<comment></comment>
</field>
<field>
<name>label_en</name>
<type>char(128)</type>
<null>YES</null>
<extra></extra>
<default></default>
<comment></comment>
</field>
<field>
<name>label_fr</name>
<type>char(128)</type>
<null>YES</null>
<extra></extra>
<default></default>
<comment></comment>
</field>
<field>
<name>label_de</name>
<type>char(128)</type>
<null>YES</null>
<extra></extra>
<default></default>
<comment></comment>
</field>
<field>
<name>label_nl</name>
<type>char(128)</type>
<null>YES</null>
<extra></extra>
<default></default>
<comment></comment>
</field>
</fields>
<indexes>
<index>

View File

@@ -721,6 +721,7 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract
$this->assertArrayHasKey('separator', $metadatas);
$this->assertArrayHasKey('thesaurus_branch', $metadatas);
$this->assertArrayHasKey('type', $metadatas);
$this->assertArrayHasKey('labels', $metadatas);
$this->assertArrayHasKey('indexable', $metadatas);
$this->assertArrayHasKey('multivalue', $metadatas);
$this->assertArrayHasKey('readonly', $metadatas);
@@ -729,11 +730,14 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract
$this->assertTrue(is_int($metadatas['id']));
$this->assertTrue(is_string($metadatas['namespace']));
$this->assertTrue(is_string($metadatas['name']));
$this->assertTrue(is_array($metadatas['labels']));
$this->assertTrue(is_null($metadatas['source']) || is_string($metadatas['source']));
$this->assertTrue(is_string($metadatas['tagname']));
$this->assertTrue((strlen($metadatas['name']) > 0));
$this->assertTrue(is_string($metadatas['separator']));
$this->assertEquals(array('fr', 'en', 'de', 'nl'), array_keys($metadatas['labels']));
if ($metadatas['multivalue']) {
$this->assertTrue((strlen($metadatas['separator']) > 0));
}
@@ -2489,6 +2493,10 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract
$this->assertArrayHasKey('name', $meta);
$this->assertTrue(is_string($meta['name']));
$this->assertArrayHasKey('value', $meta);
$this->assertArrayHasKey('labels', $meta);
$this->assertTrue(is_array($meta['labels']));
$this->assertEquals(array('fr', 'en', 'de', 'nl'), array_keys($meta['labels']));
if (is_array($meta['value'])) {
foreach ($meta['value'] as $val) {

View File

@@ -223,6 +223,12 @@ class ControllerFieldsTest extends \PhraseanetWebTestCaseAuthenticatedAbstract
'business' => false,
'indexable' => true,
'required' => true,
'labels' => array(
'en' => 'Label',
'fr' => 'Libellé',
'de' => null,
'nl' => null,
),
'separator' => '=;',
'readonly' => false,
'type' => 'string',

View File

@@ -1,5 +1,7 @@
<?php
use Alchemy\Phrasea\Application;
class databox_fieldTest extends PhraseanetPHPUnitAbstract
{
/**
@@ -336,6 +338,54 @@ class databox_fieldTest extends PhraseanetPHPUnitAbstract
$this->assertFalse($this->object_multi->is_on_error());
}
/**
* @dataProvider provideLanguageCodes
*/
public function testGetSetLabel($code)
{
$this->object_mono->set_label($code, 'value')->save();
$this->assertSame('value', $this->object_mono->get_label($code));
$this->object_mono->set_label($code, null)->save();
$this->assertEquals($this->object_mono->get_name(), $this->object_mono->get_label($code));
}
public function provideLanguageCodes()
{
$codes = array();
foreach (Application::getAvailableLanguages() as $code => $language) {
$data = explode('_', $code);
$codes[] = array($data[0]);
}
return $codes;
}
/**
* @expectedException Alchemy\Phrasea\Exception\InvalidArgumentException
*/
public function testGetInvalidCodeLabel()
{
$this->object_mono->get_label('gloubi');
}
/**
* @expectedException Alchemy\Phrasea\Exception\InvalidArgumentException
*/
public function testSetNullInvalidCodeLabel()
{
$this->object_mono->set_label('gloubi', null);
}
/**
* @expectedException Alchemy\Phrasea\Exception\InvalidArgumentException
*/
public function testSetInvalidCodeLabel()
{
$this->object_mono->set_label('gloubi', 'value');
}
public function testRenameField()
{
$AddedValue = 'scalar value';