Ejemplo n.º 1
0
 /**
  * Hydrate attachment data for a block
  *
  * @param array $attachmentData
  * @param SitePageBlock $block
  * @param ErrorStore $errorStore
  * @return bool true on success, false on error
  */
 private function hydrateAttachments(array $attachmentData, SitePageBlock $block, ErrorStore $errorStore)
 {
     $itemAdapter = $this->getAdapter('items');
     $attachments = $block->getAttachments();
     $existingAttachments = $attachments->toArray();
     $newAttachments = [];
     $position = 1;
     foreach ($attachmentData as $inputAttachment) {
         if (!is_array($inputAttachment)) {
             continue;
         }
         $attachment = current($existingAttachments);
         if ($attachment === false) {
             $attachment = new SiteBlockAttachment();
             $attachment->setBlock($block);
             $newAttachments[] = $attachment;
         } else {
             // Null out values as we re-use them
             $existingAttachments[key($existingAttachments)] = null;
             next($existingAttachments);
         }
         try {
             $item = $itemAdapter->findEntity($inputAttachment['o:item']['o:id']);
         } catch (NotFoundException $e) {
             $item = null;
         }
         if ($item && isset($inputAttachment['o:media']['o:id'])) {
             $itemMedia = $item->getMedia();
             $media = $itemMedia->get($inputAttachment['o:media']['o:id']);
         } else {
             $media = null;
         }
         $caption = isset($inputAttachment['o:caption']) ? $inputAttachment['o:caption'] : '';
         $purifier = $this->getServiceLocator()->get('Omeka\\HtmlPurifier');
         $caption = $purifier->purify($caption);
         $attachment->setItem($item);
         $attachment->setMedia($media);
         $attachment->setCaption($caption);
         $attachment->setPosition($position++);
     }
     // Remove any blocks that weren't reused
     foreach ($existingAttachments as $key => $existingAttachment) {
         if ($existingAttachment !== null) {
             $attachments->remove($key);
         }
     }
     // Add any new blocks that had to be created
     foreach ($newAttachments as $newAttachment) {
         $attachments->add($newAttachment);
     }
     return true;
 }
 /**
  * {@inheritDoc}
  */
 public function setPosition($position)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setPosition', array($position));
     return parent::setPosition($position);
 }