mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-10 11:33:17 +00:00
Refactor caption serialization
This commit is contained in:
@@ -53,6 +53,97 @@ class caption_record implements caption_interface, cache_cacheableInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
const SERIALIZE_XML = 'xml';
|
||||
const SERIALIZE_YAML = 'yaml';
|
||||
|
||||
public function serialize($format)
|
||||
{
|
||||
switch ($format)
|
||||
{
|
||||
case self::SERIALIZE_XML:
|
||||
return $this->serializeXML();
|
||||
break;
|
||||
case self::SERIALIZE_YAML:
|
||||
return $this->serializeYAML();
|
||||
break;
|
||||
default:
|
||||
throw new \Exception(sprintf('Unknown format %s', $format));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected function serializeYAML()
|
||||
{
|
||||
$buffer = array();
|
||||
|
||||
foreach ($this->get_fields() as $field)
|
||||
{
|
||||
$vi = $field->get_values();
|
||||
|
||||
if ($field->is_multi())
|
||||
{
|
||||
$buffer[$field->get_name()] = array();
|
||||
foreach ($vi as $value)
|
||||
{
|
||||
$val = $value->getValue();
|
||||
$buffer[$field->get_name()][] = ctype_digit($val) ? (int) $val : $val;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$value = array_pop($vi);
|
||||
$val = $value->getValue();
|
||||
$buffer[$field->get_name()] = ctype_digit($val) ? (int) $val : $val;
|
||||
}
|
||||
}
|
||||
|
||||
$buffer = array('record' => array('description' => $buffer));
|
||||
|
||||
$dumper = new Symfony\Component\Yaml\Dumper();
|
||||
|
||||
return $dumper->dump($buffer, 3);
|
||||
}
|
||||
|
||||
protected function serializeXML()
|
||||
{
|
||||
$dom_doc = new DOMDocument('1.0', 'UTF-8');
|
||||
$dom_doc->formatOutput = true;
|
||||
$dom_doc->standalone = true;
|
||||
|
||||
$record = $dom_doc->createElement('record');
|
||||
$record->setAttribute('record_id', $this->record->get_record_id());
|
||||
$dom_doc->appendChild($record);
|
||||
$description = $dom_doc->createElement('description');
|
||||
$record->appendChild($description);
|
||||
|
||||
foreach ($this->get_fields() as $field)
|
||||
{
|
||||
$values = $field->get_values();
|
||||
|
||||
foreach ($values as $value)
|
||||
{
|
||||
$elem = $dom_doc->createElement($field->get_name());
|
||||
$elem->appendChild($dom_doc->createTextNode($value->getValue()));
|
||||
$elem->setAttribute('meta_id', $value->getId());
|
||||
$elem->setAttribute('meta_struct_id', $field->get_meta_struct_id());
|
||||
$description->appendChild($elem);
|
||||
}
|
||||
}
|
||||
|
||||
$doc = $dom_doc->createElement('doc');
|
||||
|
||||
$tc_datas = $this->record->get_technical_infos();
|
||||
|
||||
foreach ($tc_datas as $key => $data)
|
||||
{
|
||||
$doc->setAttribute($key, $data);
|
||||
}
|
||||
|
||||
$record->appendChild($doc);
|
||||
|
||||
return $dom_doc->saveXML();
|
||||
}
|
||||
|
||||
protected function retrieve_fields()
|
||||
{
|
||||
if (is_array($this->fields))
|
||||
@@ -83,7 +174,7 @@ class caption_record implements caption_interface, cache_cacheableInterface
|
||||
try
|
||||
{
|
||||
$databox_meta_struct = databox_field::get_instance($this->databox, $row['structure_id']);
|
||||
$metadata = new caption_field($databox_meta_struct, $this->record);
|
||||
$metadata = new caption_field($databox_meta_struct, $this->record);
|
||||
|
||||
$rec_fields[$databox_meta_struct->get_id()] = $metadata;
|
||||
$dces_element = $metadata->get_databox_field()->get_dces_element();
|
||||
@@ -133,7 +224,6 @@ class caption_record implements caption_interface, cache_cacheableInterface
|
||||
foreach ($this->retrieve_fields() as $meta_struct_id => $field)
|
||||
{
|
||||
if ($field->get_name() == $fieldname)
|
||||
|
||||
return $field;
|
||||
}
|
||||
|
||||
@@ -206,7 +296,7 @@ class caption_record implements caption_interface, cache_cacheableInterface
|
||||
|
||||
foreach ($fields as $key => $value)
|
||||
{
|
||||
if(!isset($fields[$key]))
|
||||
if (!isset($fields[$key]))
|
||||
continue;
|
||||
|
||||
//if(strpos($fields[$key]['value'], '<a ') === false)
|
||||
|
Reference in New Issue
Block a user