Beispiel #1
0
 /**
  * Returns array of special entity data which is used, for example, in "Who has access" datagrid,
  * "oro_share_select" form type search result.
  *
  * @param object $object
  *
  * @return array
  */
 public function getEntityDetails($object)
 {
     $id = $label = $details = $image = $avatar = $classLabel = null;
     if ($object instanceof Organization) {
         $id = $object->getId();
         $label = $object->getName();
         $image = 'avatar-organization-small.png';
         $classLabel = $this->translator->trans('oro.organization.entity_label');
         $details = $classLabel;
     } elseif ($object instanceof BusinessUnit) {
         $id = $object->getId();
         $label = $object->getName();
         $image = 'avatar-business-unit-small.png';
         $classLabel = $this->translator->trans('oro.organization.businessunit.entity_label');
         $details = $classLabel . ' ' . $this->translator->trans('oro.security.datagrid.share_grid_row_details_from') . ' ' . $object->getOrganization()->getName();
     } elseif ($object instanceof User) {
         $id = $object->getId();
         $label = $object->getFirstName() . ' ' . $object->getLastName();
         $image = 'avatar-small.png';
         $classLabel = $this->translator->trans('oro.user.entity_label');
         $avatar = $object->getAvatar() ? $this->attachmentManager->getResizedImageUrl($object->getAvatar(), AttachmentManager::SMALL_IMAGE_WIDTH, AttachmentManager::SMALL_IMAGE_HEIGHT) : null;
         $details = $classLabel . ' ' . $this->translator->trans('oro.security.datagrid.share_grid_row_details_from') . ' ' . $object->getOwner()->getName();
     }
     return ['id' => $id, 'label' => $label, 'image' => $image, 'avatar' => $avatar, 'details' => $details, 'classLabel' => $classLabel];
 }
 /**
  * {@inheritdoc}
  */
 public function serializeOne($id)
 {
     list($fileId, $ownerEntityClass, $ownerEntityId) = $this->attachmentManager->parseFileKey($id);
     if (!$this->securityFacade->isGranted('VIEW', new ObjectIdentity($ownerEntityId, $ownerEntityClass))) {
         throw new AccessDeniedException();
     }
     return parent::serializeOne($fileId);
 }
Beispiel #3
0
 /**
  * @param LifecycleEventArgs $args
  */
 public function postPersist(LifecycleEventArgs $args)
 {
     /** @var $entity File */
     $entity = $args->getEntity();
     if ($entity instanceof File && $entity->isUploaded()) {
         $this->manager->upload($entity);
         $this->manager->checkOnDelete($entity, $args->getEntityManager());
     }
 }
 /**
  * {@inheritdoc}
  */
 public function convertItem($user)
 {
     $result = parent::convertItem($user);
     $result['avatar'] = null;
     $avatar = $this->getPropertyValue('avatar', $user);
     if ($avatar) {
         $result['avatar'] = $this->attachmentManager->getFilteredImageUrl($avatar, self::IMAGINE_AVATAR_FILTER);
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function format($parameter, array $formatterArguments = [])
 {
     $height = AttachmentManager::DEFAULT_IMAGE_HEIGHT;
     if (array_key_exists(self::HEIGHT_ATTRIBUTE, $formatterArguments)) {
         $height = (int) $formatterArguments[self::HEIGHT_ATTRIBUTE];
     }
     $width = AttachmentManager::DEFAULT_IMAGE_WIDTH;
     if (array_key_exists(self::WIDTH_ATTRIBUTE, $formatterArguments)) {
         $width = (int) $formatterArguments[self::WIDTH_ATTRIBUTE];
     }
     return $this->manager->getResizedImageUrl($parameter, $width, $height, Router::ABSOLUTE_URL);
 }
 /**
  * {@inheritdoc}
  */
 public function format($parameter, array $formatterArguments = [])
 {
     $altValue = $parameter->getOriginalFilename();
     if (array_key_exists(self::ALT_ATTRIBUTE, $formatterArguments)) {
         $altValue = $formatterArguments[self::ALT_ATTRIBUTE];
     }
     $parameters = sprintf('alt = "%s"', $altValue);
     if (array_key_exists(self::HEIGHT_ATTRIBUTE, $formatterArguments)) {
         $parameters .= sprintf(' height = %s', $formatterArguments[self::HEIGHT_ATTRIBUTE]);
     }
     if (array_key_exists(self::WIDTH_ATTRIBUTE, $formatterArguments)) {
         $parameters .= sprintf(' width = %s', $formatterArguments[self::WIDTH_ATTRIBUTE]);
     }
     return $this->getData($parameter->getMimeType(), $this->manager->getContent($parameter), $parameters);
 }
 /**
  * Returns the list of fields responsible to store attachment associations
  *
  * @return array [target_entity_class => field_name]
  */
 protected function getAttachmentTargets()
 {
     if (null === $this->attachmentTargets) {
         $this->attachmentTargets = $this->attachmentManager->getAttachmentTargets();
     }
     return $this->attachmentTargets;
 }
 /**
  * {@inheritdoc}
  */
 public function format($parameter, array $formatterArguments = [])
 {
     $height = AttachmentManager::DEFAULT_IMAGE_HEIGHT;
     if (array_key_exists(self::HEIGHT_ATTRIBUTE, $formatterArguments)) {
         $height = (int) $formatterArguments[self::HEIGHT_ATTRIBUTE];
     }
     $width = AttachmentManager::DEFAULT_IMAGE_WIDTH;
     if (array_key_exists(self::WIDTH_ATTRIBUTE, $formatterArguments)) {
         $width = (int) $formatterArguments[self::WIDTH_ATTRIBUTE];
     }
     $title = $parameter->getOriginalFilename();
     if (array_key_exists(self::TITLE_ATTRIBUTE, $formatterArguments)) {
         $title = $formatterArguments[self::TITLE_ATTRIBUTE];
     }
     return sprintf('<a href="%s">%s</a>', $this->manager->getResizedImageUrl($parameter, $width, $height, Router::ABSOLUTE_URL), $title);
 }
 public function testDuplicate()
 {
     $image = new File();
     $imageCopy = new File();
     $attachmentFile1 = new File();
     $attachmentFileCopy1 = new File();
     $attachmentFile2 = new File();
     $attachmentFileCopy2 = new File();
     $attachment1 = (new Attachment())->setFile($attachmentFile1);
     $attachment2 = (new Attachment())->setFile($attachmentFile2);
     $product = (new StubProduct())->setSku(self::PRODUCT_SKU)->addUnitPrecision($this->prepareUnitPrecision(self::UNIT_PRECISION_CODE_1, self::UNIT_PRECISION_DEFAULT_PRECISION_1))->addUnitPrecision($this->prepareUnitPrecision(self::UNIT_PRECISION_CODE_2, self::UNIT_PRECISION_DEFAULT_PRECISION_2))->setImage($image);
     $this->skuIncrementor->expects($this->once())->method('increment')->with(self::PRODUCT_SKU)->will($this->returnValue(self::PRODUCT_COPY_SKU));
     $this->attachmentProvider->expects($this->once())->method('getEntityAttachments')->with($product)->will($this->returnValue([$attachment1, $attachment2]));
     $this->attachmentManager->expects($this->any())->method('copyAttachmentFile')->with($image)->will($this->returnValue($imageCopy));
     $this->attachmentManager->expects($this->any())->method('copyAttachmentFile')->with($attachmentFile1)->will($this->returnValue($attachmentFileCopy1));
     $this->attachmentManager->expects($this->any())->method('copyAttachmentFile')->with($attachmentFile2)->will($this->returnValue($attachmentFileCopy2));
     $this->connection->expects($this->once())->method('beginTransaction');
     $this->connection->expects($this->once())->method('commit');
     $productCopy = $this->duplicator->duplicate($product);
     $productCopyUnitPrecisions = $productCopy->getUnitPrecisions();
     $this->assertEquals(self::PRODUCT_COPY_SKU, $productCopy->getSku());
     $this->assertEquals($this->productStatusDisabled, $productCopy->getStatus());
     $this->assertCount(2, $productCopyUnitPrecisions);
     $this->assertEquals(self::UNIT_PRECISION_CODE_1, $productCopyUnitPrecisions[0]->getUnit()->getCode());
     $this->assertEquals(self::UNIT_PRECISION_DEFAULT_PRECISION_1, $productCopyUnitPrecisions[0]->getUnit()->getDefaultPrecision());
     $this->assertEquals(self::UNIT_PRECISION_CODE_2, $productCopyUnitPrecisions[1]->getUnit()->getCode());
     $this->assertEquals(self::UNIT_PRECISION_DEFAULT_PRECISION_2, $productCopyUnitPrecisions[1]->getUnit()->getDefaultPrecision());
     $this->assertEquals($imageCopy, $productCopy->getImage());
 }
 public function testGetFilteredImageUrl()
 {
     $this->attachment->setId(1);
     $filerName = 'testFilter';
     $this->attachment->setOriginalFilename('test.doc');
     $this->router->expects($this->once())->method('generate')->with('oro_filtered_attachment', ['id' => 1, 'filename' => 'test.doc', 'filter' => $filerName]);
     $this->attachmentManager->getFilteredImageUrl($this->attachment, $filerName);
 }
Beispiel #11
0
 /**
  * @param $user
  *
  * @return string|null
  */
 protected function getUserAvatar($user)
 {
     $avatar = $this->getPropertyValue('avatar', $user);
     if (!$avatar) {
         return null;
     }
     return $this->attachmentManager->getFilteredImageUrl($avatar, UserSearchHandler::IMAGINE_AVATAR_FILTER);
 }
 /**
  * @param array $result
  */
 protected function postSerializeContact(array &$result)
 {
     // @todo: an 'email' field is added only for backward compatibility with previous API
     $email = null;
     if (!empty($result['emails'])) {
         foreach ($result['emails'] as $item) {
             if ($item['primary']) {
                 $email = $item['email'];
                 break;
             }
         }
     }
     $result['email'] = $email;
     if (!empty($result['picture'])) {
         $result['picture'] = $this->attachmentManager->getFileRestApiUrl($result['picture'], $this->class, $result['id']);
     }
 }
Beispiel #13
0
 /**
  * @param Comment $entity
  *
  * @return array
  */
 protected function getAttachmentInfo(Comment $entity)
 {
     $result = [];
     $attachment = $this->getAttachment($entity);
     if ($attachment) {
         $result = ['attachmentURL' => $this->getAttachmentURL($entity, $attachment), 'attachmentSize' => $this->attachmentManager->getFileSize($attachment->getFileSize()), 'attachmentFileName' => $attachment->getOriginalFilename()];
     }
     return $result;
 }
Beispiel #14
0
 /**
  * @param array  $result
  * @param string $attrName
  * @param User   $user
  */
 protected function addUser(array &$result, $attrName, $user)
 {
     if ($user) {
         $result[$attrName] = $this->nameFormatter->format($user);
         $result[$attrName . '_id'] = $user->getId();
         $result[$attrName . '_viewable'] = $this->securityFacade->isGranted('VIEW', $user);
         $avatar = $user->getAvatar();
         $result[$attrName . '_avatar'] = $avatar ? $this->attachmentManager->getFilteredImageUrl($avatar, 'avatar_xsmall') : null;
     }
 }
Beispiel #15
0
 /**
  * {@inheritdoc}
  */
 public function convertItem($user)
 {
     $result = [];
     foreach ($this->fields as $field) {
         $result[$field] = $this->getPropertyValue($field, $user);
     }
     $result['avatar'] = null;
     $avatar = $this->getPropertyValue('avatar', $user);
     if ($avatar) {
         $result['avatar'] = $this->attachmentManager->getFilteredImageUrl($avatar, UserSearchHandler::IMAGINE_AVATAR_FILTER);
     }
     if (!$this->nameFormatter) {
         throw new \RuntimeException('Name formatter must be configured');
     }
     $result['fullName'] = $this->nameFormatter->format($user);
     return $result;
 }
 /**
  * @param User $user
  * @return UserDetails
  */
 public function fetchUserDetails(User $user)
 {
     $loadedUser = $this->getByUser($user);
     if (!$loadedUser) {
         throw new \RuntimeException('Failed to load details for given user');
     }
     $userAvatarUrl = null;
     if ($user->getType() == User::TYPE_ORO) {
         if ($loadedUser->getAvatar()) {
             $originalFilename = $loadedUser->getAvatar()->getOriginalFilename();
             if (!empty($originalFilename)) {
                 $userAvatarUrl = $this->attachmentManager->getFilteredImageUrl($loadedUser->getAvatar(), 'avatar_med');
             }
         }
     }
     return new UserDetails((string) $user, $user->getType(), $loadedUser->getEmail(), $loadedUser->getFirstName(), $loadedUser->getLastName(), $userAvatarUrl);
 }
 public function testCopyAttachmentFile()
 {
     $localFilePath = __DIR__ . '/../Fixtures/testFile/test.txt';
     $sourceStream = new InMemoryBuffer($this->filesystem, $this->attachment->getFilename());
     $sourceStream->open(new StreamMode('wb+'));
     $sourceStream->write(file_get_contents($localFilePath));
     $sourceStream->seek(0);
     $sourceStream->close();
     $resultStream = new InMemoryBuffer($this->filesystem, 'test2.txt');
     $this->filesystem->expects($this->at(0))->method('createStream')->with($this->attachment->getFilename())->will($this->returnValue($sourceStream));
     $this->filesystem->expects($this->at(1))->method('createStream')->with($this->anything())->will($this->returnValue($resultStream));
     $newAttachment = $this->attachmentManager->copyAttachmentFile($this->attachment);
     $this->assertEquals($this->attachment->getOriginalFilename(), $newAttachment->getOriginalFilename());
     $this->assertNotEquals($this->attachment->getFilename(), $newAttachment->getFilename());
     $resultStream->open(new StreamMode('rb+'));
     $resultStream->seek(0);
     $this->assertEquals('Test data', $resultStream->read(100));
 }
 /**
  * @param Product $product
  * @param Product $productCopy
  */
 protected function cloneChildObjects(Product $product, Product $productCopy)
 {
     foreach ($product->getUnitPrecisions() as $unitPrecision) {
         $productCopy->addUnitPrecision(clone $unitPrecision);
     }
     if ($imageFile = $product->getImage()) {
         $imageFileCopy = $this->attachmentManager->copyAttachmentFile($imageFile);
         $productCopy->setImage($imageFileCopy);
     }
     $attachments = $this->attachmentProvider->getEntityAttachments($product);
     foreach ($attachments as $attachment) {
         $attachmentCopy = clone $attachment;
         $attachmentFileCopy = $this->attachmentManager->copyAttachmentFile($attachment->getFile());
         $attachmentCopy->setFile($attachmentFileCopy);
         $attachmentCopy->setTarget($productCopy);
         $this->doctrineHelper->getEntityManager($attachmentCopy)->persist($attachmentCopy);
     }
 }
Beispiel #19
0
 /**
  * @param User $user
  * @return array
  */
 protected function createUserView(User $user)
 {
     return ['id' => $user->getId(), 'url' => $this->router->generate('oro_user_view', array('id' => $user->getId())), 'fullName' => $this->entityNameResolver->getName($user), 'avatar' => $user->getAvatar() ? $this->attachmentManager->getFilteredImageUrl($user->getAvatar(), 'avatar_xsmall') : null, 'permissions' => array('view' => $this->securityFacade->isGranted('VIEW', $user))];
 }
 /**
  * {@inheritdoc}
  */
 public function getData()
 {
     return $this->attachmentManager->getContent($this->fileName);
 }
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage Invalid file key: "Invalid Key".
  */
 public function testParseInvalidFileKey()
 {
     $this->attachmentManager->parseFileKey('Invalid Key');
 }
 /**
  * @param User $user
  * @return null|string
  */
 public function getUserAvatar(User $user)
 {
     return $user->getAvatar() ? $this->attachmentManager->getFilteredImageUrl($user->getAvatar(), 'avatar_xsmall') : null;
 }
Beispiel #23
0
 /**
  * @param File   $attachment
  * @param string $filterName
  * @return string
  */
 public function getFilteredImageUrl(File $attachment, $filterName)
 {
     return $this->manager->getFilteredImageUrl($attachment, $filterName);
 }
Beispiel #24
0
 /**
  * {@inheritdoc}
  */
 public function normalize($object, $format = null, array $context = [])
 {
     return $this->attachmentManager->getAttachment($context['entityName'], $context['entityId'], $context['fieldName'], $object, 'download', true);
 }