Fix left Vocabulary\Controller

This commit is contained in:
Benoît Burnichon
2016-03-31 11:30:42 +02:00
parent 90e25f5a79
commit ca4ef347d2
3 changed files with 155 additions and 175 deletions

View File

@@ -1,5 +1,4 @@
<?php <?php
/* /*
* This file is part of Phraseanet * This file is part of Phraseanet
* *
@@ -36,7 +35,6 @@ interface ControlProviderInterface
/** /**
* @return string returns the value corresponding to an id * @return string returns the value corresponding to an id
* @throws \Exception if the $id is invalid
*/ */
public function getValue($id); public function getValue($id);

View File

@@ -11,30 +11,46 @@
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Utilities\StringHelper; use Alchemy\Phrasea\Utilities\StringHelper;
use Alchemy\Phrasea\Vocabulary; use Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface;
class caption_Field_Value implements cache_cacheableInterface class caption_Field_Value implements cache_cacheableInterface
{ {
const RETRIEVE_VALUES = true; const RETRIEVE_VALUES = true;
const DONT_RETRIEVE_VALUES = false; const DONT_RETRIEVE_VALUES = false;
/** @var int */ /**
* @var int
*/
protected $id; protected $id;
/** @var string */ /**
* @var string
*/
protected $value; protected $value;
/** @var \Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface */ /**
protected $VocabularyType; * @var ControlProviderInterface
*/
protected $vocabularyType;
/** @var int */ /**
protected $VocabularyId; * @var int
*/
protected $vocabularyId;
/** @var databox_field */ /**
* @var databox_field
*/
protected $databox_field; protected $databox_field;
/** @var record_adapter */ /**
* @var record_adapter
*/
protected $record; protected $record;
/**
* @var Application
*/
protected $app; protected $app;
/** /**
@@ -45,21 +61,20 @@ class caption_Field_Value implements cache_cacheableInterface
*/ */
protected $qjs; protected $qjs;
/* /**
* Tells whether the value is matched against a thesaurus value. * Tells whether the value is matched against a thesaurus value.
* @var bool
*/ */
protected $isThesaurusValue; protected $isThesaurusValue;
protected static $localCache = []; protected static $localCache = [];
/** /**
*
* @param Application $app * @param Application $app
* @param databox_field $databox_field * @param databox_field $databox_field
* @param record_adapter $record * @param record_adapter $record
* @param mixed $id * @param mixed $id
* @param bool $retrieveValues * @param bool $retrieveValues
* @return \caption_Field_Value
*/ */
public function __construct(Application $app, databox_field $databox_field, record_adapter $record, $id, $retrieveValues = self::RETRIEVE_VALUES) public function __construct(Application $app, databox_field $databox_field, record_adapter $record, $id, $retrieveValues = self::RETRIEVE_VALUES)
{ {
@@ -68,107 +83,94 @@ class caption_Field_Value implements cache_cacheableInterface
$this->record = $record; $this->record = $record;
$this->app = $app; $this->app = $app;
if($retrieveValues == self::RETRIEVE_VALUES) { if ($retrieveValues === self::RETRIEVE_VALUES) {
$this->retrieveValues(); $this->retrieveValues();
} }
} }
/**
* @return string
*/
public function getQjs() public function getQjs()
{ {
return $this->qjs; return $this->qjs;
} }
public function injectValues($value, $VocabularyType, $VocabularyId) /**
* @param string $value
* @param string $vocabularyType
* @param string $vocabularyId
*/
public function injectValues($value, $vocabularyType, $vocabularyId)
{ {
$this->value = StringHelper::crlfNormalize($value); $this->value = StringHelper::crlfNormalize($value);
try { $this->fetchVocabulary($vocabularyType, $vocabularyId);
$this->VocabularyType = $VocabularyType ? Vocabulary\Controller::get($this->app, $VocabularyType) : null;
$this->VocabularyId = $VocabularyId;
} catch (\InvalidArgumentException $e) {
} if ($this->vocabularyType) {
if ($this->VocabularyType) {
/**
* Vocabulary Control has been deactivated
*/
if (! $this->databox_field->getVocabularyControl()) { if (! $this->databox_field->getVocabularyControl()) {
// Vocabulary Control has been deactivated
$this->removeVocabulary(); $this->removeVocabulary();
} } elseif ($this->databox_field->getVocabularyControl()->getType() !== $this->vocabularyType->getType()) {
/** // Vocabulary Control has changed
* Vocabulary Control has changed
*/
elseif ($this->databox_field->getVocabularyControl()->getType() !== $this->VocabularyType->getType()) {
$this->removeVocabulary(); $this->removeVocabulary();
} } elseif (!$this->vocabularyType->validate($this->vocabularyId)) {
/** // Current Id is not available anymore
* Current Id is not available anymore
*/
elseif ( ! $this->VocabularyType->validate($this->VocabularyId)) {
$this->removeVocabulary(); $this->removeVocabulary();
} } elseif ($this->vocabularyType->getValue($this->vocabularyId) !== $this->value) {
/** // String equivalence has changed
* String equivalence has changed $this->set_value($this->vocabularyType->getValue($this->vocabularyId));
*/
elseif ($this->VocabularyType->getValue($this->VocabularyId) !== $this->value) {
$this->set_value($this->VocabularyType->getValue($this->VocabularyId));
} }
} }
$datas = [
'value' => $this->value,
'vocabularyId' => $this->VocabularyId,
'vocabularyType' => $this->VocabularyType ? $this->VocabularyType->getType() : null,
];
$this->set_data_to_cache($datas);
} }
protected function retrieveValues() protected function retrieveValues()
{ {
try { try {
$datas = $this->get_data_from_cache(); $data = $this->get_data_from_cache();
$cacheRefreshNeeded = false;
$this->value = $datas['value'];
$this->VocabularyType = $datas['vocabularyType'] ? Vocabulary\Controller::get($this->app, $datas['vocabularyType']) : null;
$this->VocabularyId = $datas['vocabularyId'];
return $this;
} catch (\Exception $e) { } catch (\Exception $e) {
$data = $this->databox_field->get_databox()->get_connection()
->fetchAssoc(
'SELECT value, VocabularyType as vocabularyType, VocabularyId as vocabularyId FROM metadatas WHERE id = :id',
[':id' => $this->id]
);
if (!is_array($data)) {
$data = [
'value' => null,
'vocabularyId' => null,
'vocabularyType' => null,
];
} }
$connbas = $this->databox_field->get_databox()->get_connection(); $cacheRefreshNeeded = true;
$sql = 'SELECT record_id, value, VocabularyType, VocabularyId FROM metadatas WHERE id = :id';
$stmt = $connbas->prepare($sql);
$stmt->execute([':id' => $this->id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
if($row) {
$this->injectValues($row['value'], $row['VocabularyType'], $row['VocabularyId']);
} }
else {
$this->injectValues(null, null, null); $this->injectValues($data['value'], $data['vocabularyType'], $data['vocabularyId']);
if ($cacheRefreshNeeded) {
$this->set_data_to_cache([
'value' => $this->value,
'vocabularyId' => $this->vocabularyId,
'vocabularyType' => $this->vocabularyType ? $this->vocabularyType->getType() : null,
]);
} }
return $this; return $this;
} }
/** /**
* @return Vocabulary\ControlProvider\ControlProviderInterface * @return ControlProviderInterface|null
*/ */
public function getVocabularyType() public function getVocabularyType()
{ {
return $this->VocabularyType; return $this->vocabularyType;
} }
public function getVocabularyId() public function getVocabularyId()
{ {
return $this->VocabularyId; return $this->vocabularyId;
} }
public function getId() public function getId()
@@ -183,7 +185,7 @@ class caption_Field_Value implements cache_cacheableInterface
public function getResource() public function getResource()
{ {
return $this->VocabularyType ? $this->VocabularyType->getResource($this->VocabularyId) : null; return $this->vocabularyType ? $this->vocabularyType->getResource($this->vocabularyId) : null;
} }
public function getDatabox_field() public function getDatabox_field()
@@ -198,12 +200,7 @@ class caption_Field_Value implements cache_cacheableInterface
public function delete() public function delete()
{ {
$connbas = $this->databox_field->get_connection(); $this->getConnection()->delete('metadatas', ['id' => $this->id]);
$sql = 'DELETE FROM metadatas WHERE id = :id';
$stmt = $connbas->prepare($sql);
$stmt->execute([':id' => $this->id]);
$stmt->closeCursor();
$this->delete_data_from_cache(); $this->delete_data_from_cache();
$this->databox_field->delete_data_from_cache(); $this->databox_field->delete_data_from_cache();
@@ -215,44 +212,29 @@ class caption_Field_Value implements cache_cacheableInterface
public function removeVocabulary() public function removeVocabulary()
{ {
$connbas = $this->databox_field->get_connection(); $this->getConnection()->executeUpdate(
'UPDATE metadatas SET VocabularyType = NULL, VocabularyId = NULL WHERE id = :meta_id',
['meta_id' => $this->getId()]
);
$params = [ $this->vocabularyId = null;
':VocabType' => null $this->vocabularyType = null;
, ':VocabularyId' => null
, ':meta_id' => $this->getId()
];
$sql_up = 'UPDATE metadatas'
. ' SET VocabularyType = :VocabType, VocabularyId = :VocabularyId'
. ' WHERE id = :meta_id';
$stmt_up = $connbas->prepare($sql_up);
$stmt_up->execute($params);
$stmt_up->closeCursor();
$this->VocabularyId = $this->VocabularyType = null;
$this->delete_data_from_cache(); $this->delete_data_from_cache();
return $this; return $this;
} }
public function setVocab(Vocabulary\ControlProvider\ControlProviderInterface $vocabulary, $vocab_id) public function setVocab(ControlProviderInterface $vocabulary, $vocab_id)
{ {
$connbas = $this->databox_field->get_connection(); $this->getConnection()->executeUpdate(
'UPDATE metadatas SET VocabularyType = :VocabType, VocabularyId = :VocabularyId WHERE id = :meta_id',
$params = [ [
':VocabType' => $vocabulary->getType() 'VocabType' => $vocabulary->getType(),
, ':VocabularyId' => $vocab_id 'VocabularyId' => $vocab_id,
, ':meta_id' => $this->getId() 'meta_id' => $this->getId(),
]; ]
);
$sql_up = 'UPDATE metadatas'
. ' SET VocabularyType = :VocabType, VocabularyId = :VocabularyId'
. ' WHERE id = :meta_id';
$stmt_up = $connbas->prepare($sql_up);
$stmt_up->execute($params);
$stmt_up->closeCursor();
$this->set_value($vocabulary->getValue($vocab_id)); $this->set_value($vocabulary->getValue($vocab_id));
@@ -263,50 +245,30 @@ class caption_Field_Value implements cache_cacheableInterface
{ {
$this->value = $value; $this->value = $value;
$connbas = $this->databox_field->get_connection(); $this->getConnection()->executeUpdate(
'UPDATE metadatas SET value = :value WHERE id = :meta_id',
$params = [ [
':meta_id' => $this->id 'meta_id' => $this->id,
, ':value' => $value 'value' => $value,
]; ]
);
$sql_up = 'UPDATE metadatas SET value = :value WHERE id = :meta_id';
$stmt_up = $connbas->prepare($sql_up);
$stmt_up->execute($params);
$stmt_up->closeCursor();
$this->delete_data_from_cache(); $this->delete_data_from_cache();
$this->update_cache_value($value);
return $this; return $this;
} }
/** public static function create(Application $app, databox_field $databox_field, \record_adapter $record, $value, ControlProviderInterface $vocabulary = null, $vocabularyId = null)
*
* @param array $value
* @return caption_field
*/
public function update_cache_value($value)
{ {
$this->record->get_caption()->delete_data_from_cache(); $connection = $databox_field->get_connection();
return $this; // Check consistency
}
public static function create(Application $app, databox_field $databox_field, \record_adapter $record, $value, Vocabulary\ControlProvider\ControlProviderInterface $vocabulary = null, $vocabularyId = null)
{
$connbas = $databox_field->get_connection();
/**
* Check consistency
*/
if (! $databox_field->is_multi()) { if (! $databox_field->is_multi()) {
try {
$field = $record->get_caption()->get_field($databox_field->get_name()); $field = $record->get_caption()->get_field($databox_field->get_name());
$values = $field->get_values(); $values = $field->get_values();
if (! empty($values)) {
$caption_field_value = array_pop($values); $caption_field_value = array_pop($values);
/* @var $value \caption_Field_Value */ /* @var \caption_Field_Value $caption_field_value */
$caption_field_value->set_value($value); $caption_field_value->set_value($value);
if (! $vocabulary || ! $vocabularyId) { if (! $vocabulary || ! $vocabularyId) {
@@ -316,30 +278,23 @@ class caption_Field_Value implements cache_cacheableInterface
} }
return $caption_field_value; return $caption_field_value;
} catch (\Exception $e) {
} }
} }
$sql_ins = 'INSERT INTO metadatas (id, record_id, meta_struct_id, value, VocabularyType, VocabularyId)' $data = [
. ' VALUES (null, :record_id, :field, :value, :VocabType, :VocabId)'; 'record_id' => $record->getRecordId(),
'meta_struct_id' => $databox_field->get_id(),
$params = [ 'value' => $value,
':record_id' => $record->getRecordId(), 'VocabularyType' => $vocabulary ? $vocabulary->getType() : null,
':field' => $databox_field->get_id(), 'VocabularyId' => $vocabulary ? $vocabularyId : null,
':value' => $value,
':VocabType' => $vocabulary ? $vocabulary->getType() : null,
':VocabId' => $vocabulary ? $vocabularyId : null,
]; ];
$stmt_ins = $connbas->prepare($sql_ins); $connection->insert('metadatas', $data);
$stmt_ins->execute($params);
$stmt_ins->closeCursor(); $meta_id = $connection->lastInsertId();
$meta_id = $connbas->lastInsertId();
$caption_field_value = new self($app, $databox_field, $record, $meta_id); $caption_field_value = new self($app, $databox_field, $record, $meta_id, self::DONT_RETRIEVE_VALUES);
$caption_field_value->update_cache_value($value); $caption_field_value->injectValues($data['value'], $data['VocabularyType'], $data['VocabularyId']);
$record->get_caption()->delete_data_from_cache(); $record->get_caption()->delete_data_from_cache();
$databox_field->delete_data_from_cache(); $databox_field->delete_data_from_cache();
@@ -350,7 +305,6 @@ class caption_Field_Value implements cache_cacheableInterface
} }
/** /**
*
* @return string * @return string
*/ */
public function highlight_thesaurus() public function highlight_thesaurus()
@@ -390,8 +344,9 @@ class caption_Field_Value implements cache_cacheableInterface
$note = 0; $note = 0;
$note += ($node->getAttribute("lng") == $this->app['locale']) ? 4 : 0; $note += ($node->getAttribute("lng") == $this->app['locale']) ? 4 : 0;
$note += ($node->getAttribute("w") == $term_noacc) ? 2 : 0; $note += ($node->getAttribute("w") == $term_noacc) ? 2 : 0;
if($context_noacc != "") if ($context_noacc != "") {
$note += ($node->getAttribute("k") == $context_noacc) ? 1 : 0; $note += ($node->getAttribute("k") == $context_noacc) ? 1 : 0;
}
if ($note > $bestnote) { if ($note > $bestnote) {
$bestnode = $node; $bestnode = $node;
} }
@@ -411,12 +366,12 @@ class caption_Field_Value implements cache_cacheableInterface
} }
/** /**
* @return boolean * @return bool
*/ */
public function isThesaurusValue() public function isThesaurusValue()
{ {
if (null === $this->isThesaurusValue) { if (null === $this->isThesaurusValue) {
$this->highlight_thesaurus(); throw new LogicException('Value was not checked against thesaurus yet. Call hightlight_thesaurus() first');
} }
return $this->isThesaurusValue; return $this->isThesaurusValue;
@@ -486,7 +441,7 @@ class caption_Field_Value implements cache_cacheableInterface
*/ */
public function delete_data_from_cache($option = null) public function delete_data_from_cache($option = null)
{ {
$this->value = $this->VocabularyId = $this->VocabularyType = null; $this->value = $this->vocabularyId = $this->vocabularyType = null;
$this->record->delete_data_from_cache(record_adapter::CACHE_TITLE); $this->record->delete_data_from_cache(record_adapter::CACHE_TITLE);
try { try {
@@ -507,4 +462,26 @@ class caption_Field_Value implements cache_cacheableInterface
{ {
self::$localCache = []; self::$localCache = [];
} }
/**
* @param $vocabularyType
* @param $vocabularyId
*/
private function fetchVocabulary($vocabularyType, $vocabularyId)
{
try {
$this->vocabularyType = $vocabularyType ? $this->app['vocabularies'][strtolower($vocabularyType)] : null;
$this->vocabularyId = $vocabularyId;
} catch (\InvalidArgumentException $e) {
// Invalid or unknown Vocabulary
}
}
/**
* @return \Doctrine\DBAL\Connection
*/
private function getConnection()
{
return $this->databox_field->get_connection();
}
} }

View File

@@ -224,9 +224,14 @@ SQL;
foreach ($field->get_values() as $metaId => $v) { foreach ($field->get_values() as $metaId => $v) {
$values[$metaId] = [ $values[$metaId] = [
'value' => $v->getValue(), 'value' => $v->getValue(),
'from_thesaurus' => $highlight ? $v->isThesaurusValue() : false, 'from_thesaurus' => false,
'qjs' => $v->getQjs(), 'qjs' => null,
]; ];
if ($highlight) {
$v->highlight_thesaurus();
$values[$metaId]['from_thesaurus'] = $v->isThesaurusValue();
$values[$metaId]['qjs'] = $v->getQjs();
}
} }
$fields[$field->get_name()] = [ $fields[$field->get_name()] = [
'values' => $values, 'values' => $values,