/** * @covers Alchemy\Phrasea\Border\MetadataBag::toMetadataArray */ public function testToMetadataArray() { /** @var \collection $collection */ $collection = self::$DI['collection']; $structure = $collection->get_databox()->get_meta_structure(); $valueMono = new Mono('mono value'); $valueMulti = new Multi(['multi', 'value']); $monoAdded = $multiAdded = false; foreach ($structure as $databox_field) { $tag = $databox_field->get_tag(); if (!$monoAdded) { $this->object->set($tag->getTagname(), new Metadata($tag, $valueMono)); $monoAdded = $databox_field->get_id(); } elseif (!$multiAdded) { if ($databox_field->is_multi()) { $this->object->set($tag->getTagname(), new Metadata($tag, $valueMulti)); $multiAdded = $databox_field->get_id(); } } else { break; } } if (!$multiAdded || !$monoAdded) { $this->markTestSkipped('Unable to find multi value field'); } $this->assertEquals([['meta_struct_id' => $monoAdded, 'value' => 'mono value', 'meta_id' => null], ['meta_struct_id' => $multiAdded, 'value' => 'multi', 'meta_id' => null], ['meta_struct_id' => $multiAdded, 'value' => 'value', 'meta_id' => null]], $this->object->toMetadataArray($structure)); }
/** * Map a Bag of metadatas indexed by **Tagnames** to a bag of metadatas * indexed by **FieldNames** * * @param \databox_descriptionStructure $metadatasStructure The databox structure related * @param ExiftoolMetadataBag $bag The metadata bag * @return MetadataBag */ protected function getIndexByFieldName(\databox_descriptionStructure $metadatasStructure, ExiftoolMetadataBag $bag) { $ret = new MetadataBag(); foreach ($metadatasStructure as $databox_field) { if ($bag->containsKey($databox_field->get_tag()->getTagname())) { $ret->set($databox_field->get_name(), $bag->get($databox_field->get_tag()->getTagname())); } } return $ret; }
/** * Add an element to phraseanet * * @param Application $app A Silex application * @param Request $request The current request * @param int $file_id A lazaret element id * * parameters : 'bas_id' int (mandatory) : The id of the destination collection * 'keep_attributes' boolean (optional) : Keep all attributes attached to the lazaret element * 'attributes' array (optional) : Attributes id's to attach to the lazaret element * * @return Response */ public function addElement(Application $app, Request $request, $file_id) { $ret = ['success' => false, 'message' => '', 'result' => []]; //Optional parameter $keepAttributes = !!$request->request->get('keep_attributes', false); $attributesToKeep = $request->request->get('attributes', []); //Mandatory parameter if (null === $request->request->get('bas_id')) { $ret['message'] = $app->trans('You must give a destination collection'); return $app->json($ret); } $lazaretFile = $app['EM']->find('Phraseanet:LazaretFile', $file_id); /* @var $lazaretFile LazaretFile */ if (null === $lazaretFile) { $ret['message'] = $app->trans('File is not present in quarantine anymore, please refresh'); return $app->json($ret); } $lazaretFileName = $app['root.path'] . '/tmp/lazaret/' . $lazaretFile->getFilename(); $lazaretThumbFileName = $app['root.path'] . '/tmp/lazaret/' . $lazaretFile->getThumbFilename(); try { $borderFile = Border\File::buildFromPathfile($lazaretFileName, $lazaretFile->getCollection($app), $app, $lazaretFile->getOriginalName()); $record = null; /* @var $record \record_adapter */ //Post record creation $callBack = function ($element, $visa, $code) use(&$record) { $record = $element; }; //Force creation record $app['border-manager']->process($lazaretFile->getSession(), $borderFile, $callBack, Border\Manager::FORCE_RECORD); $app['phraseanet.SE']->addRecord($record); if ($keepAttributes) { //add attribute $metaFields = new Border\MetaFieldsBag(); $metadataBag = new Border\MetadataBag(); foreach ($lazaretFile->getAttributes() as $attr) { //Check which ones to keep if (!!count($attributesToKeep)) { if (!in_array($attr->getId(), $attributesToKeep)) { continue; } } try { $attribute = Border\Attribute\Factory::getFileAttribute($app, $attr->getName(), $attr->getValue()); } catch (\InvalidArgumentException $e) { continue; } /* @var $attribute AttributeInterface */ switch ($attribute->getName()) { case AttributeInterface::NAME_METADATA: $value = $attribute->getValue(); $metadataBag->set($value->getTag()->getTagname(), new \PHPExiftool\Driver\Metadata\Metadata($value->getTag(), $value->getValue())); break; case AttributeInterface::NAME_STORY: $attribute->getValue()->appendChild($record); break; case AttributeInterface::NAME_STATUS: $record->set_binary_status($attribute->getValue()); break; case AttributeInterface::NAME_METAFIELD: $metaFields->set($attribute->getField()->get_name(), $attribute->getValue()); break; } } $datas = $metadataBag->toMetadataArray($record->get_databox()->get_meta_structure()); $record->set_metadatas($datas); $fields = $metaFields->toMetadataArray($record->get_databox()->get_meta_structure()); $record->set_metadatas($fields); } //Delete lazaret file $app['EM']->remove($lazaretFile); $app['EM']->flush(); $ret['success'] = true; } catch (\Exception $e) { $ret['message'] = $app->trans('An error occured'); } try { $app['filesystem']->remove([$lazaretFileName, $lazaretThumbFileName]); } catch (IOException $e) { } return $app->json($ret); }
/** * Add an element to phraseanet * * @param Request $request The current request * @param int $file_id A lazaret element id * * parameters : 'bas_id' int (mandatory) : The id of the destination collection * 'keep_attributes' boolean (optional) : Keep all attributes attached to the lazaret element * 'attributes' array (optional) : Attributes id's to attach to the lazaret element * * @return Response */ public function addElement(Request $request, $file_id) { $ret = ['success' => false, 'message' => '', 'result' => []]; //Optional parameter $keepAttributes = !!$request->request->get('keep_attributes', false); $attributesToKeep = $request->request->get('attributes', []); //Mandatory parameter if (null === $request->request->get('bas_id')) { $ret['message'] = $this->app->trans('You must give a destination collection'); return $this->app->json($ret); } /* @var LazaretFile $lazaretFile */ $lazaretFile = $this->getLazaretFileRepository()->find($file_id); if (null === $lazaretFile) { $ret['message'] = $this->app->trans('File is not present in quarantine anymore, please refresh'); return $this->app->json($ret); } $path = $this->app['tmp.lazaret.path']; $lazaretFileName = $path . '/' . $lazaretFile->getFilename(); $lazaretThumbFileName = $path . '/' . $lazaretFile->getThumbFilename(); try { $borderFile = Border\File::buildFromPathfile($lazaretFileName, $lazaretFile->getCollection($this->app), $this->app, $lazaretFile->getOriginalName()); //Post record creation /** @var \record_adapter $record */ $record = null; $callBack = function ($element) use(&$record) { $record = $element; }; //Force creation record $this->getBorderManager()->process($lazaretFile->getSession(), $borderFile, $callBack, Border\Manager::FORCE_RECORD); if ($keepAttributes) { //add attribute $metaFields = new Border\MetaFieldsBag(); $metadataBag = new Border\MetadataBag(); foreach ($lazaretFile->getAttributes() as $attr) { //Check which ones to keep if (!!count($attributesToKeep)) { if (!in_array($attr->getId(), $attributesToKeep)) { continue; } } try { $attribute = Border\Attribute\Factory::getFileAttribute($this->app, $attr->getName(), $attr->getValue()); } catch (\InvalidArgumentException $e) { continue; } switch ($attribute->getName()) { case AttributeInterface::NAME_METADATA: /** @var Metadata $value */ $value = $attribute->getValue(); $metadataBag->set($value->getTag()->getTagname(), new Metadata($value->getTag(), $value->getValue())); break; case AttributeInterface::NAME_STORY: /** @var \record_adapter $value */ $value = $attribute->getValue(); $value->appendChild($record); break; case AttributeInterface::NAME_STATUS: $record->set_binary_status($attribute->getValue()); break; case AttributeInterface::NAME_METAFIELD: /** @var Border\Attribute\MetaField $attribute */ $metaFields->set($attribute->getField()->get_name(), $attribute->getValue()); break; } } $data = $metadataBag->toMetadataArray($record->get_databox()->get_meta_structure()); $record->set_metadatas($data); $fields = $metaFields->toMetadataArray($record->get_databox()->get_meta_structure()); $record->set_metadatas($fields); } //Delete lazaret file $manager = $this->getEntityManager(); $manager->remove($lazaretFile); $manager->flush(); $ret['success'] = true; } catch (\Exception $e) { $ret['message'] = $this->app->trans('An error occured'); } try { $this->getFilesystem()->remove([$lazaretFileName, $lazaretThumbFileName]); } catch (IOException $e) { } return $this->app->json($ret); }