Refactor set_export::getCaption

This commit is contained in:
Romain Neutron
2012-03-20 19:11:09 +01:00
parent d1a5824c19
commit 384552d4b8
4 changed files with 13 additions and 187 deletions

View File

@@ -69,8 +69,6 @@ interface record_Interface
public function get_caption();
public function get_xml();
public function get_original_name();
public function get_title($highlight = false, searchEngine_adapter $searchEngine = null);

View File

@@ -771,57 +771,6 @@ class record_adapter implements record_Interface, cache_cacheableInterface
return $this->caption_record;
}
/**
*
* @return string
*/
public function get_xml()
{
if (!$this->xml)
{
$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->get_record_id());
$dom_doc->appendChild($record);
$description = $dom_doc->createElement('description');
$record->appendChild($description);
$caption = $this->get_caption();
foreach ($caption->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->get_technical_infos();
foreach ($tc_datas as $key => $data)
{
$doc->setAttribute($key, $data);
}
$record->appendChild($doc);
$this->xml = $dom_doc->saveXML();
}
return $this->xml;
}
/**
*
* @return string
@@ -1274,7 +1223,7 @@ class record_adapter implements record_Interface, cache_cacheableInterface
$this->caption_record = null;
$xml = new DOMDocument();
$xml->loadXML($this->get_xml());
$xml->loadXML($this->get_caption()->serialize(\caption_record::SERIALIZE_XML));
$this->set_xml($xml);
$this->reindex();
@@ -1383,7 +1332,6 @@ class record_adapter implements record_Interface, cache_cacheableInterface
return $this;
}
/**
*
* @param collection $collection
@@ -1591,9 +1539,12 @@ class record_adapter implements record_Interface, cache_cacheableInterface
public function get_hd_file()
{
$hd = $this->get_subdef('document');
if ($hd->is_physically_present())
if ($hd->is_physically_present())
{
return new system_file(p4string::addEndSlash($hd->get_path()) . $hd->get_file());
}
return null;
}
@@ -1626,8 +1577,10 @@ class record_adapter implements record_Interface, cache_cacheableInterface
$origcoll = phrasea::collFromBas($this->get_base_id());
$xml = $this->get_caption()->serialize(\caption_record::SERIALIZE_XML);
$appbox->get_session()->get_logger($this->get_databox())
->log($this, Session_Logger::EVENT_DELETE, $origcoll, $this->get_xml());
->log($this, Session_Logger::EVENT_DELETE, $origcoll, $xml);
$sql = "DELETE FROM record WHERE record_id = :record_id";
$stmt = $connbas->prepare($sql);

View File

@@ -687,10 +687,7 @@ class set_export extends set_abstract
system_file::mkdir($caption_dir);
$desc = self::get_caption(
$download_element->get_base_id()
, $download_element->get_record_id()
);
$desc = $download_element->get_caption()->serialize(\caption_record::SERIALIZE_XML);
$file = $files[$id]["export_name"]
. $files[$id]["subdefs"]['caption']["ajout"] . '.'
@@ -714,14 +711,7 @@ class set_export extends set_abstract
. $session->get_ses_id() . '/';
system_file::mkdir($caption_dir);
$desc = self::get_caption(
$download_element->get_base_id()
, $download_element->get_record_id()
, true
, 'yaml'
);
$desc = $download_element->get_caption()->serialize(\caption_record::SERIALIZE_YAML);
$file = $files[$id]["export_name"]
. $files[$id]["subdefs"]['caption-yaml']["ajout"] . '.'
@@ -837,121 +827,6 @@ class set_export extends set_abstract
return $zipFile;
}
/**
*
* @param Int $bas
* @param Int $rec
* @param boolean $check_rights
* @return string
*/
public static function get_caption($bas, $rec, $check_rights = true, $format = 'xml')
{
$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->xmlStandalone = true;
$dom->encoding = 'UTF-8';
$dom_record = $dom->createElement('record');
$dom_desc = $dom->createElement('description');
$dom_record->appendChild($dom_desc);
$dom->appendChild($dom_record);
$restrict = array();
$sbas_id = phrasea::sbasFromBas($bas);
$record = new record_adapter($sbas_id, $rec);
$desc = $record->get_caption()->serialize(caption_record::SERIALIZE_XML);
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$databox = databox::get_instance($sbas_id);
$struct = $databox->get_structure();
$rights = true;
if ($check_rights && $session->is_authenticated())
{
$user = User_Adapter::getInstance($session->get_usr_id(), $appbox);
$rights = $user->ACL()->has_right_on_base($bas, 'canmodifrecord');
if ($rights == false)
{
if ($sxe = simplexml_load_string($struct))
{
$z = $sxe->xpath('/record/description');
if ($z && is_array($z))
{
foreach ($z[0] as $ki => $vi)
{
if (isset($vi["export"])
&& ($vi["export"] == "0" || $vi["export"] == "off"))
$restrict[$ki] = true;
}
}
}
}
}
$buffer = array();
foreach ($record->get_caption()->get_fields() as $field)
{
if (($rights || !isset($restrict[$field->get_name()])))
{
switch ($format)
{
case 'yaml':
case 'yml':
$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;
}
break;
case 'xml':
default:
$dom_el = $dom->createElement($field->get_name());
$dom_el->appendChild($dom->createTextNode($field->get_serialized_values()));
$dom_desc->appendChild($dom_el);
break;
}
}
}
$buffer = array('record' => array('description' => $buffer));
$dumper = new Symfony\Component\Yaml\Dumper();
$buffer = $dumper->dump($buffer, 3);
switch ($format)
{
case 'xml':
default:
$ret = $dom->saveXML();
break;
case 'yaml':
case 'yml':
$ret = $buffer;
break;
}
return $ret;
}
/**
*
* @param string $file

View File

@@ -477,7 +477,7 @@ class task_period_ftp extends task_appboxAbstract
if ($subdef == 'caption')
{
$desc = set_export::get_caption($base_id, $record_id, false);
$desc = $record->get_caption()->serialize(\caption_record::SERIALIZE_XML);
$localfile = $registry->get('GV_RootPath') . 'tmp/' . md5($desc . time() . mt_rand());
if (file_put_contents($localfile, $desc) === false)
@@ -487,7 +487,7 @@ class task_period_ftp extends task_appboxAbstract
}
elseif ($subdef == 'caption-yaml')
{
$desc = set_export::get_caption($base_id, $record_id, false, 'yaml');
$desc = $record->get_caption()->serialize(\caption_record::SERIALIZE_YAML);
$localfile = $registry->get('GV_RootPath') . 'tmp/' . md5($desc . time() . mt_rand());
if (file_put_contents($localfile, $desc) === false)