diff --git a/lib/classes/caption/record.php b/lib/classes/caption/record.php index 70bca259f1..91e00dd1e8 100644 --- a/lib/classes/caption/record.php +++ b/lib/classes/caption/record.php @@ -11,8 +11,6 @@ use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Model\RecordReferenceInterface; use Alchemy\Phrasea\Model\Serializer\CaptionSerializer; -use Alchemy\Phrasea\SearchEngine\SearchEngineInterface; -use Alchemy\Phrasea\SearchEngine\SearchEngineOptions; class caption_record implements cache_cacheableInterface { @@ -22,25 +20,19 @@ class caption_record implements cache_cacheableInterface protected $fields; /** - * @var record_adapter + * @var RecordReferenceInterface */ protected $record; - protected $databox; - protected $app; /** - * - * @param Application $app - * @param record_adapter $record - * @param databox $databox - * - * @return caption_record + * @var Application */ - public function __construct(Application $app, \record_adapter $record, databox $databox) + protected $app; + + public function __construct(Application $app, RecordReferenceInterface $record) { $this->app = $app; $this->record = $record; - $this->databox = $databox; } 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 ORDER BY s.sorter ASC SQL; - $fields = $this->databox->get_connection() + $fields = $this->getDatabox()->get_connection() ->executeQuery($sql, [':record_id' => $this->record->getRecordId()]) ->fetchAll(PDO::FETCH_ASSOC); @@ -88,7 +80,7 @@ SQL; $rec_fields = array(); if ($fields) { - $databox_descriptionStructure = $this->databox->get_meta_structure(); + $databox_descriptionStructure = $this->getDatabox()->get_meta_structure(); // first group values by field $caption_fields = []; @@ -156,7 +148,7 @@ SQL; $fields = []; 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; } @@ -198,15 +190,14 @@ SQL; } /** - * * @param string $label - * @return caption_field + * @return caption_field|null */ public function get_dc_field($label) { $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()])) { return $fields[$field->get_id()]; } @@ -265,7 +256,7 @@ SQL; */ 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) { - 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; - 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()); } } diff --git a/lib/classes/databox/field.php b/lib/classes/databox/field.php index 7fb8299d37..6d64e5d0b5 100644 --- a/lib/classes/databox/field.php +++ b/lib/classes/databox/field.php @@ -18,7 +18,7 @@ use Alchemy\Phrasea\Metadata\TagFactory; use Alchemy\Phrasea\Vocabulary; use Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface; use Alchemy\Phrasea\Metadata\Tag\NoSource; -use Doctrine\DBAL\Driver\Connection; +use Doctrine\DBAL\Connection; use PHPExiftool\Exception\TagUnknown; use Alchemy\Phrasea\Exception\InvalidArgumentException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -78,9 +78,24 @@ class databox_field implements cache_cacheableInterface 'Type' => 'databox_Field_DCES_Type', ]; + /** + * @var databox_Field_DCESAbstract|null + */ protected $dces_element; + + /** + * @var ControlProviderInterface|null + */ protected $Vocabulary; + + /** + * @var string|null + */ protected $VocabularyType; + + /** + * @var bool + */ protected $VocabularyRestriction = false; protected $on_error = false; protected $original_src; @@ -504,7 +519,6 @@ class databox_field implements cache_cacheableInterface } /** - * * @return databox_Field_DCESAbstract */ 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) { - $connbas = $this->get_connection(); - + $connection = $this->get_connection(); if (null !== $DCES_element) { - $sql = 'UPDATE metadatas_structure - SET dces_element = null WHERE dces_element = :dces_element'; - - $stmt = $connbas->prepare($sql); - $stmt->execute([ - ':dces_element' => $DCES_element->get_label() - ]); - $stmt->closeCursor(); + $connection->executeUpdate( + 'UPDATE metadatas_structure SET dces_element = null WHERE dces_element = :dces_element', + [ + 'dces_element' => $DCES_element->get_label(), + ] + ); } - $sql = 'UPDATE metadatas_structure - SET dces_element = :dces_element WHERE id = :id'; + $connection->executeUpdate( + '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->delete_data_from_cache(); @@ -934,12 +944,12 @@ class databox_field implements cache_cacheableInterface } /** - * * @return array */ public function __sleep() { $vars = []; + foreach ($this as $key => $value) { if (in_array($key, ['databox', 'app', 'Vocabulary'])) continue; @@ -997,10 +1007,14 @@ class databox_field implements cache_cacheableInterface private function loadVocabulary() { + if ($this->VocabularyType === '') { + return; + } + try { $this->Vocabulary = Vocabulary\Controller::get($this->app, $this->VocabularyType); } catch (\InvalidArgumentException $e) { - + // Could not find Vocabulary } } diff --git a/lib/classes/record/adapter.php b/lib/classes/record/adapter.php index 069637beb9..5d5b55ba28 100644 --- a/lib/classes/record/adapter.php +++ b/lib/classes/record/adapter.php @@ -754,7 +754,7 @@ class record_adapter implements RecordInterface, cache_cacheableInterface */ 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)