Slight refactoring of caption_record, databox_field and record_adapter

This commit is contained in:
Benoît Burnichon
2016-03-30 11:49:50 +02:00
parent c89bf664b5
commit 35a4111cf3
3 changed files with 58 additions and 45 deletions

View File

@@ -11,8 +11,6 @@
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Model\RecordReferenceInterface; use Alchemy\Phrasea\Model\RecordReferenceInterface;
use Alchemy\Phrasea\Model\Serializer\CaptionSerializer; use Alchemy\Phrasea\Model\Serializer\CaptionSerializer;
use Alchemy\Phrasea\SearchEngine\SearchEngineInterface;
use Alchemy\Phrasea\SearchEngine\SearchEngineOptions;
class caption_record implements cache_cacheableInterface class caption_record implements cache_cacheableInterface
{ {
@@ -22,25 +20,19 @@ class caption_record implements cache_cacheableInterface
protected $fields; protected $fields;
/** /**
* @var record_adapter * @var RecordReferenceInterface
*/ */
protected $record; protected $record;
protected $databox;
protected $app;
/** /**
* * @var Application
* @param Application $app
* @param record_adapter $record
* @param databox $databox
*
* @return caption_record
*/ */
public function __construct(Application $app, \record_adapter $record, databox $databox) protected $app;
public function __construct(Application $app, RecordReferenceInterface $record)
{ {
$this->app = $app; $this->app = $app;
$this->record = $record; $this->record = $record;
$this->databox = $databox;
} }
public function toArray($includeBusinessFields) public function toArray($includeBusinessFields)
@@ -78,7 +70,7 @@ FROM metadatas m INNER JOIN metadatas_structure s ON s.id = m.meta_struct_id
WHERE m.record_id = :record_id WHERE m.record_id = :record_id
ORDER BY s.sorter ASC ORDER BY s.sorter ASC
SQL; SQL;
$fields = $this->databox->get_connection() $fields = $this->getDatabox()->get_connection()
->executeQuery($sql, [':record_id' => $this->record->getRecordId()]) ->executeQuery($sql, [':record_id' => $this->record->getRecordId()])
->fetchAll(PDO::FETCH_ASSOC); ->fetchAll(PDO::FETCH_ASSOC);
@@ -88,7 +80,7 @@ SQL;
$rec_fields = array(); $rec_fields = array();
if ($fields) { if ($fields) {
$databox_descriptionStructure = $this->databox->get_meta_structure(); $databox_descriptionStructure = $this->getDatabox()->get_meta_structure();
// first group values by field // first group values by field
$caption_fields = []; $caption_fields = [];
@@ -156,7 +148,7 @@ SQL;
$fields = []; $fields = [];
foreach ($this->retrieve_fields() as $meta_struct_id => $field) { foreach ($this->retrieve_fields() as $meta_struct_id => $field) {
if ($grep_fields && ! in_array($field->get_name(), $grep_fields)) { if ($grep_fields && ! in_array($field->get_name(), $grep_fields, true)) {
continue; continue;
} }
@@ -198,15 +190,14 @@ SQL;
} }
/** /**
*
* @param string $label * @param string $label
* @return caption_field * @return caption_field|null
*/ */
public function get_dc_field($label) public function get_dc_field($label)
{ {
$fields = $this->retrieve_fields(); $fields = $this->retrieve_fields();
if (null !== $field = $this->databox->get_meta_structure()->get_dces_field($label)) { if (null !== $field = $this->getDatabox()->get_meta_structure()->get_dces_field($label)) {
if (isset($fields[$field->get_id()])) { if (isset($fields[$field->get_id()])) {
return $fields[$field->get_id()]; return $fields[$field->get_id()];
} }
@@ -265,7 +256,7 @@ SQL;
*/ */
public function get_data_from_cache($option = null) public function get_data_from_cache($option = null)
{ {
return $this->record->getDatabox()->get_data_from_cache($this->get_cache_key($option)); return $this->getDatabox()->get_data_from_cache($this->get_cache_key($option));
} }
/** /**
@@ -278,7 +269,7 @@ SQL;
*/ */
public function set_data_to_cache($value, $option = null, $duration = 0) public function set_data_to_cache($value, $option = null, $duration = 0)
{ {
return $this->record->getDatabox()->set_data_to_cache($value, $this->get_cache_key($option), $duration); return $this->getDatabox()->set_data_to_cache($value, $this->get_cache_key($option), $duration);
} }
/** /**
@@ -291,6 +282,14 @@ SQL;
{ {
$this->fields = null; $this->fields = null;
return $this->record->getDatabox()->delete_data_from_cache($this->get_cache_key($option)); return $this->getDatabox()->delete_data_from_cache($this->get_cache_key($option));
}
/**
* @return databox
*/
private function getDatabox()
{
return $this->app->findDataboxById($this->record->getDataboxId());
} }
} }

View File

@@ -18,7 +18,7 @@ use Alchemy\Phrasea\Metadata\TagFactory;
use Alchemy\Phrasea\Vocabulary; use Alchemy\Phrasea\Vocabulary;
use Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface; use Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface;
use Alchemy\Phrasea\Metadata\Tag\NoSource; use Alchemy\Phrasea\Metadata\Tag\NoSource;
use Doctrine\DBAL\Driver\Connection; use Doctrine\DBAL\Connection;
use PHPExiftool\Exception\TagUnknown; use PHPExiftool\Exception\TagUnknown;
use Alchemy\Phrasea\Exception\InvalidArgumentException; use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -78,9 +78,24 @@ class databox_field implements cache_cacheableInterface
'Type' => 'databox_Field_DCES_Type', 'Type' => 'databox_Field_DCES_Type',
]; ];
/**
* @var databox_Field_DCESAbstract|null
*/
protected $dces_element; protected $dces_element;
/**
* @var ControlProviderInterface|null
*/
protected $Vocabulary; protected $Vocabulary;
/**
* @var string|null
*/
protected $VocabularyType; protected $VocabularyType;
/**
* @var bool
*/
protected $VocabularyRestriction = false; protected $VocabularyRestriction = false;
protected $on_error = false; protected $on_error = false;
protected $original_src; protected $original_src;
@@ -504,7 +519,6 @@ class databox_field implements cache_cacheableInterface
} }
/** /**
*
* @return databox_Field_DCESAbstract * @return databox_Field_DCESAbstract
*/ */
public function get_dces_element() public function get_dces_element()
@@ -514,29 +528,25 @@ class databox_field implements cache_cacheableInterface
public function set_dces_element(databox_Field_DCESAbstract $DCES_element = null) public function set_dces_element(databox_Field_DCESAbstract $DCES_element = null)
{ {
$connbas = $this->get_connection(); $connection = $this->get_connection();
if (null !== $DCES_element) { if (null !== $DCES_element) {
$sql = 'UPDATE metadatas_structure $connection->executeUpdate(
SET dces_element = null WHERE dces_element = :dces_element'; 'UPDATE metadatas_structure SET dces_element = null WHERE dces_element = :dces_element',
[
$stmt = $connbas->prepare($sql); 'dces_element' => $DCES_element->get_label(),
$stmt->execute([ ]
':dces_element' => $DCES_element->get_label() );
]);
$stmt->closeCursor();
} }
$sql = 'UPDATE metadatas_structure $connection->executeUpdate(
SET dces_element = :dces_element WHERE id = :id'; 'UPDATE metadatas_structure SET dces_element = :dces_element WHERE id = :id',
[
'dces_element' => $DCES_element ? $DCES_element->get_label() : null,
'id' => $this->id,
]
);
$stmt = $connbas->prepare($sql);
$stmt->execute([
':dces_element' => $DCES_element ? $DCES_element->get_label() : null
, ':id' => $this->id
]);
$stmt->closeCursor();
$this->dces_element = $DCES_element; $this->dces_element = $DCES_element;
$this->delete_data_from_cache(); $this->delete_data_from_cache();
@@ -934,12 +944,12 @@ class databox_field implements cache_cacheableInterface
} }
/** /**
*
* @return array * @return array
*/ */
public function __sleep() public function __sleep()
{ {
$vars = []; $vars = [];
foreach ($this as $key => $value) { foreach ($this as $key => $value) {
if (in_array($key, ['databox', 'app', 'Vocabulary'])) if (in_array($key, ['databox', 'app', 'Vocabulary']))
continue; continue;
@@ -997,10 +1007,14 @@ class databox_field implements cache_cacheableInterface
private function loadVocabulary() private function loadVocabulary()
{ {
if ($this->VocabularyType === '') {
return;
}
try { try {
$this->Vocabulary = Vocabulary\Controller::get($this->app, $this->VocabularyType); $this->Vocabulary = Vocabulary\Controller::get($this->app, $this->VocabularyType);
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
// Could not find Vocabulary
} }
} }

View File

@@ -754,7 +754,7 @@ class record_adapter implements RecordInterface, cache_cacheableInterface
*/ */
public function get_caption() public function get_caption()
{ {
return new caption_record($this->app, $this, $this->getDatabox()); return new caption_record($this->app, $this);
} }
public function getCaption(array $fields = null) public function getCaption(array $fields = null)