Exemple #1
0
 /**
  * {@inheritDoc}
  */
 public function hydrate(Request $request, EntityInterface $entity, ErrorStore $errorStore)
 {
     parent::hydrate($request, $entity, $errorStore);
     if ($this->shouldHydrate($request, 'o:is_open')) {
         $entity->setIsOpen($request->getValue('o:is_open'));
     }
 }
Exemple #2
0
 /**
  * {@inheritDoc}
  */
 public function hydrate(Request $request, EntityInterface $entity, ErrorStore $errorStore)
 {
     parent::hydrate($request, $entity, $errorStore);
     if ($this->shouldHydrate($request, 'o:item_set')) {
         $itemSetsData = $request->getValue('o:item_set', []);
         $itemSetAdapter = $this->getAdapter('item_sets');
         $itemSets = $entity->getItemSets();
         $itemSetsToRetain = [];
         foreach ($itemSetsData as $itemSetData) {
             if (is_array($itemSetData) && array_key_exists('o:id', $itemSetData) && is_numeric($itemSetData['o:id'])) {
                 $itemSetId = $itemSetData['o:id'];
             } elseif (is_numeric($itemSetData)) {
                 $itemSetId = $itemSetData;
             } else {
                 continue;
             }
             if (!($itemSet = $itemSets->get($itemSetId))) {
                 // Item set not already assigned. Assign it.
                 $itemSet = $itemSetAdapter->findEntity($itemSetId);
                 $itemSets->add($itemSet);
             }
             $itemSetsToRetain[] = $itemSet;
         }
         // Unassign item sets that were not included in the passed data.
         foreach ($itemSets as $itemSet) {
             if (!in_array($itemSet, $itemSetsToRetain)) {
                 $itemSets->removeElement($itemSet);
             }
         }
     }
     if ($this->shouldHydrate($request, 'o:media')) {
         $mediasData = $request->getValue('o:media', []);
         $adapter = $this->getAdapter('media');
         $class = $adapter->getEntityClass();
         $retainMedia = [];
         $position = 1;
         foreach ($mediasData as $mediaData) {
             $subErrorStore = new ErrorStore();
             if (isset($mediaData['o:id'])) {
                 $media = $adapter->findEntity($mediaData['o:id']);
                 $media->setPosition($position);
                 if (isset($mediaData['o:is_public'])) {
                     $media->setIsPublic($mediaData['o:is_public']);
                 }
                 $retainMedia[] = $media;
             } else {
                 // Create a new media.
                 $media = new $class();
                 $media->setItem($entity);
                 $media->setPosition($position);
                 $subrequest = new Request(Request::CREATE, 'media');
                 $subrequest->setContent($mediaData);
                 $subrequest->setFileData($request->getFileData());
                 try {
                     $adapter->hydrateEntity($subrequest, $media, $subErrorStore);
                 } catch (Exception\ValidationException $e) {
                     $errorStore->mergeErrors($e->getErrorStore(), 'o:media');
                 }
                 $entity->getMedia()->add($media);
                 $retainMedia[] = $media;
             }
             $position++;
         }
         // Remove media not included in request.
         foreach ($entity->getMedia() as $media) {
             if (!in_array($media, $retainMedia, true)) {
                 $entity->getMedia()->removeElement($media);
             }
         }
     }
 }
Exemple #3
0
 /**
  * {@inheritDoc}
  */
 public function hydrate(Request $request, EntityInterface $entity, ErrorStore $errorStore)
 {
     $data = $request->getContent();
     if (Request::CREATE === $request->getOperation()) {
         // Accept the passed ingester only on CREATE to prevent overwriting
         // on subsequent UPDATE requests.
         $ingesterName = $request->getValue('o:ingester');
     } else {
         $ingesterName = $entity->getIngester();
     }
     $ingester = $this->getServiceLocator()->get('Omeka\\MediaIngesterManager')->get($ingesterName);
     if (Request::CREATE === $request->getOperation()) {
         if ($ingester instanceof Fallback) {
             $errorStore->addError('o:ingester', 'Media must set a valid ingester.');
             return;
         }
         $entity->setIngester($ingesterName);
         $entity->setRenderer($ingester->getRenderer());
         if (isset($data['o:item']['o:id'])) {
             $item = $this->getAdapter('items')->findEntity($data['o:item']['o:id']);
             $entity->setItem($item);
         }
         if (isset($data['data'])) {
             $entity->setData($data['data']);
         }
         if (isset($data['o:source'])) {
             $entity->setSource($data['o:source']);
         }
     }
     parent::hydrate($request, $entity, $errorStore);
     if ($this->shouldHydrate($request, 'o:lang')) {
         $entity->setLang($request->getValue('o:lang', null));
     }
     if (Request::CREATE === $request->getOperation()) {
         $ingester->ingest($entity, $request, $errorStore);
     } elseif ($ingester instanceof MutableIngesterInterface) {
         $ingester->update($entity, $request, $errorStore);
     }
 }