Add Caption Service and Repositories

This commit is contained in:
Benoît Burnichon
2016-05-09 18:52:08 +02:00
parent da63360865
commit 7aa499716b
11 changed files with 518 additions and 64 deletions

View File

@@ -29,10 +29,16 @@ class caption_record implements cache_cacheableInterface
*/
protected $app;
public function __construct(Application $app, RecordReferenceInterface $record)
/**
* @param Application $app
* @param RecordReferenceInterface $record
* @param array[]|null $fieldsData
*/
public function __construct(Application $app, RecordReferenceInterface $record, array $fieldsData = null)
{
$this->app = $app;
$this->record = $record;
$this->fields = null === $fieldsData ? null : $this->mapFieldsFromData($fieldsData);
}
public function toArray($includeBusinessFields)
@@ -61,8 +67,6 @@ class caption_record implements cache_cacheableInterface
return $this->fields;
}
$databox = $this->getDatabox();
try {
$fields = $this->get_data_from_cache();
} catch (\Exception $e) {
@@ -72,70 +76,14 @@ 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 = $databox->get_connection()
$fields = $this->getDatabox()->get_connection()
->executeQuery($sql, [':record_id' => $this->record->getRecordId()])
->fetchAll(PDO::FETCH_ASSOC);
$this->set_data_to_cache($fields);
}
$rec_fields = array();
if ($fields) {
$databox_descriptionStructure = $databox->get_meta_structure();
$record = $databox->get_record($this->record->getRecordId());
// first group values by field
$caption_fields = [];
foreach ($fields as $row) {
$structure_id = $row['structure_id'];
if(!array_key_exists($structure_id, $caption_fields)) {
$caption_fields[$structure_id] = [
'db_field' => $databox_descriptionStructure->get_element($structure_id),
'values' => []
];
}
if (count($caption_fields[$structure_id]['values']) > 0 && !$caption_fields[$structure_id]['db_field']->is_multi()) {
// Inconsistent, should not happen
continue;
}
// build an EMPTY caption_Field_Value
$cfv = new caption_Field_Value(
$this->app,
$caption_fields[$structure_id]['db_field'],
$record,
$row['meta_id'],
caption_Field_Value::DONT_RETRIEVE_VALUES // ask caption_Field_Value "no n+1 sql"
);
// inject the value we already know
$cfv->injectValues($row['value'], $row['VocabularyType'], $row['VocabularyId']);
// add the value to the field
$caption_fields[$structure_id]['values'][] = $cfv;
}
// now build a "caption_field" with already known "caption_Field_Value"s
foreach($caption_fields as $structure_id => $caption_field) {
// build an EMPTY caption_field
$cf = new caption_field(
$this->app,
$caption_field['db_field'],
$record,
caption_field::DONT_RETRIEVE_VALUES // ask caption_field "no n+1 sql"
);
// inject the value we already know
$cf->injectValues($caption_field['values']);
// add the field to the fields
$rec_fields[$structure_id] = $cf;
}
}
$this->fields = $rec_fields;
$this->fields = $this->mapFieldsFromData($fields);
return $this->fields;
}
@@ -315,4 +263,73 @@ SQL;
{
return $this->app->findDataboxById($this->record->getDataboxId());
}
/**
* @param array $data
* @return caption_field[]
*/
protected function mapFieldsFromData($data)
{
if (!$data) {
return [];
}
$rec_fields = array();
$databox = $this->getDatabox();
$databox_descriptionStructure = $databox->get_meta_structure();
$record = $databox->get_record($this->record->getRecordId());
// first group values by field
$caption_fields = [];
foreach ($data as $row) {
$structure_id = $row['structure_id'];
if (!array_key_exists($structure_id, $caption_fields)) {
$caption_fields[$structure_id] = [
'db_field' => $databox_descriptionStructure->get_element($structure_id),
'values' => []
];
}
if (count($caption_fields[$structure_id]['values']) > 0 && !$caption_fields[$structure_id]['db_field']->is_multi()) {
// Inconsistent, should not happen
continue;
}
// build an EMPTY caption_Field_Value
$cfv = new caption_Field_Value(
$this->app,
$caption_fields[$structure_id]['db_field'],
$record,
$row['meta_id'],
caption_Field_Value::DONT_RETRIEVE_VALUES // ask caption_Field_Value "no n+1 sql"
);
// inject the value we already know
$cfv->injectValues($row['value'], $row['VocabularyType'], $row['VocabularyId']);
// add the value to the field
$caption_fields[$structure_id]['values'][] = $cfv;
}
// now build a "caption_field" with already known "caption_Field_Value"s
foreach ($caption_fields as $structure_id => $caption_field) {
// build an EMPTY caption_field
$cf = new caption_field(
$this->app,
$caption_field['db_field'],
$record,
caption_field::DONT_RETRIEVE_VALUES // ask caption_field "no n+1 sql"
);
// inject the value we already know
$cf->injectValues($caption_field['values']);
// add the field to the fields
$rec_fields[$structure_id] = $cf;
}
return $rec_fields;
}
}