Exemplo n.º 1
0
 /**
  * @covers Alchemy\Phrasea\Border\MetadataBag::toMetadataArray
  */
 public function testToMetadataArray()
 {
     $structure = self::$DI['collection']->get_databox()->get_meta_structure();
     $monoAdded = $multiAdded = false;
     foreach ($structure as $databox_field) {
         if (!$monoAdded) {
             $this->object->set($databox_field->get_name(), new MetaField($databox_field, ['mono value']));
             $monoAdded = $databox_field->get_id();
         } elseif (!$multiAdded) {
             if ($databox_field->is_multi()) {
                 $this->object->set($databox_field->get_name(), new MetaField($databox_field, ['multi', 'value']));
                 $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));
 }
Exemplo n.º 2
0
 protected function readXMLForDatabox(Application $app, \databox_descriptionStructure $metadatasStructure, $pathfile)
 {
     if (false === $app['filesystem']->exists($pathfile)) {
         throw new \InvalidArgumentException(sprintf('file %s does not exists', $pathfile));
     }
     if (false === ($sxcaption = @simplexml_load_file($pathfile))) {
         throw new \InvalidArgumentException(sprintf('Invalid XML file %s', $pathfile));
     }
     $metadataBag = new MetaFieldsBag();
     foreach ($sxcaption->description->children() as $tagname => $field) {
         $field = trim($field);
         $meta = $metadatasStructure->get_element_by_name(trim($tagname));
         if (!$meta) {
             continue;
         }
         if ($meta->is_multi()) {
             $fields = caption_field::get_multi_values($field, $meta->get_separator());
             if (!$metadataBag->containsKey($meta->get_name())) {
                 $values = $fields;
             } else {
                 $values = array_merge($metadataBag->get($meta->get_name())->getValue(), $fields);
             }
             $metadataBag->set($meta->get_name(), new BorderAttribute\MetaField($meta, $values));
         } else {
             $metadataBag->set($meta->get_name(), new BorderAttribute\MetaField($meta, [$field]));
         }
     }
     return $metadataBag;
 }
Exemplo n.º 3
0
 /**
  * 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);
 }
Exemplo n.º 4
0
 /**
  * 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);
 }