/**
  * {@inheritdoc}
  */
 public function transform($value)
 {
     if (!$value) {
         return '';
     }
     if (is_array($value)) {
         $result = [];
         $user = $this->securityFacade->getToken()->getUser();
         foreach ($value as $target) {
             if (ClassUtils::getClass($user) === ClassUtils::getClass($target) && $user->getId() === $target->getId()) {
                 continue;
             }
             if ($fields = $this->mapper->getEntityMapParameter(ClassUtils::getClass($target), 'title_fields')) {
                 $text = [];
                 foreach ($fields as $field) {
                     $text[] = $this->mapper->getFieldValue($target, $field);
                 }
             } else {
                 $text = [(string) $target];
             }
             $text = implode(' ', $text);
             if ($label = $this->getClassLabel(ClassUtils::getClass($target))) {
                 $text .= ' (' . $label . ')';
             }
             $result[] = json_encode(['text' => $text, 'id' => json_encode(['entityClass' => ClassUtils::getClass($target), 'entityId' => $target->getId()])]);
         }
         $value = implode(';', $result);
     }
     return $value;
 }
Exemple #2
0
 /**
  * @param User $user
  * @param string $searchString
  * @return array
  */
 public function autocompleteSearchById(User $user, $searchString)
 {
     $results = [];
     if ($searchString) {
         $targets = explode(';', $searchString);
         foreach ($targets as $target) {
             if (!$target) {
                 continue;
             }
             $target = json_decode($target, true);
             if (!isset($target['entityClass']) || !$target['entityClass'] || !isset($target['entityId']) || !$target['entityId']) {
                 continue;
             }
             if (ClassUtils::getClass($user) === $target['entityClass'] && $user->getId() === $target['entityId']) {
                 continue;
             }
             $entity = $this->em->getRepository($target['entityClass'])->find($target['entityId']);
             if ($fields = $this->mapper->getEntityMapParameter($target['entityClass'], 'title_fields')) {
                 $text = [];
                 foreach ($fields as $field) {
                     $text[] = $this->mapper->getFieldValue($entity, $field);
                 }
             } else {
                 $text = [(string) $entity];
             }
             $text = implode(' ', $text);
             if ($label = $this->getClassLabel($target['entityClass'])) {
                 $text .= ' (' . $label . ')';
             }
             $results[] = ['text' => $text, 'id' => json_encode(['entityClass' => $target['entityClass'], 'entityId' => $target['entityId']])];
         }
     }
     return $results;
 }
 /**
  * Get entity string
  *
  * @param object $entity
  *
  * @return string
  */
 public function getEntityTitle($entity)
 {
     if ($this->mapper->getEntityMapParameter(get_class($entity), 'title_fields')) {
         $fields = $this->mapper->getEntityMapParameter(get_class($entity), 'title_fields');
         $title = array();
         foreach ($fields as $field) {
             $title[] = $this->mapper->getFieldValue($entity, $field);
         }
     } else {
         $title = array((string) $entity);
     }
     return implode(' ', $title);
 }
 /**
  * {@inheritdoc}
  */
 public function transform($value)
 {
     if (!$value) {
         return '';
     }
     if (is_array($value)) {
         $result = [];
         $user = $this->securityTokenStorage->getToken()->getUser();
         foreach ($value as $target) {
             // Exclude current user
             $targetClass = ClassUtils::getClass($target);
             if (ClassUtils::getClass($user) === $targetClass && $user->getId() === $target->getId()) {
                 continue;
             }
             if ($fields = $this->mapper->getEntityMapParameter($targetClass, 'title_fields')) {
                 $text = [];
                 foreach ($fields as $field) {
                     $text[] = $this->mapper->getFieldValue($target, $field);
                 }
             } else {
                 $text = [$this->translator->trans('oro.entity.item', ['%id%' => $target->getId()])];
             }
             $text = implode(' ', $text);
             if ($label = $this->getClassLabel($targetClass)) {
                 $text .= ' (' . $label . ')';
             }
             $item['title'] = $text;
             $item['targetId'] = $target->getId();
             $event = new PrepareContextTitleEvent($item, $targetClass);
             $this->dispatcher->dispatch(PrepareContextTitleEvent::EVENT_NAME, $event);
             $item = $event->getItem();
             $text = $item['title'];
             $result[] = json_encode(['text' => $text, 'id' => json_encode(['entityClass' => ClassUtils::getClass($target), 'entityId' => $target->getId()])]);
         }
         $value = implode(';', $result);
     }
     return $value;
 }
Exemple #5
0
 /**
  * Get entity string
  *
  * @param object $entity
  *
  * @return string
  */
 protected function getEntityTitle($entity)
 {
     $entityClass = ClassUtils::getClass($entity);
     $fields = $this->mapper->getEntityMapParameter($entityClass, 'title_fields');
     if ($fields) {
         $title = [];
         foreach ($fields as $field) {
             $title[] = $this->mapper->getFieldValue($entity, $field);
         }
     } else {
         $title = [(string) $entity];
     }
     return implode(' ', $title);
 }
 /**
  * Get entity string
  *
  * @param $entity object
  * @param $item \Oro\Bundle\SearchBundle\Query\Result\Item
  *
  * @return string
  */
 protected function getEntityTitle($entity, $item)
 {
     $name = $item->getEntityName();
     if (!$entity) {
         $entity = $this->em->getRepository($name)->find($item->getRecordId());
     }
     if ($fields = $this->mapper->getEntityMapParameter($name, 'title_fields')) {
         $title = array();
         foreach ($fields as $field) {
             $title[] = $this->mapper->getFieldValue($entity, $field);
         }
     } else {
         $title = array((string) $entity);
     }
     return implode(' ', $title);
 }
 /**
  * Returns the context for the given activity class and id
  *
  * @param string $class The FQCN of the activity entity
  * @param        $id
  *
  * @return array
  */
 public function getActivityContext($class, $id)
 {
     $currentUser = $this->securityTokenStorage->getToken()->getUser();
     $userClass = ClassUtils::getClass($currentUser);
     $entity = $this->doctrineHelper->getEntity($class, $id);
     $result = [];
     if (!$entity || !$entity instanceof ActivityInterface) {
         return $result;
     }
     $targets = $entity->getActivityTargetEntities();
     $entityProvider = $this->configManager->getProvider('entity');
     foreach ($targets as $target) {
         $targetClass = ClassUtils::getClass($target);
         $targetId = $target->getId();
         if ($userClass === $targetClass && $currentUser->getId() === $targetId) {
             continue;
         }
         $item = [];
         $config = $entityProvider->getConfig($targetClass);
         $metadata = $this->configManager->getEntityMetadata($targetClass);
         $safeClassName = $this->entityClassNameHelper->getUrlSafeClassName($targetClass);
         $link = null;
         if ($metadata) {
             $link = $this->router->generate($metadata->getRoute(), ['id' => $targetId]);
         } elseif ($link === null && ExtendHelper::isCustomEntity($targetClass)) {
             // Generate view link for the custom entity
             $link = $this->router->generate('oro_entity_view', ['id' => $targetId, 'entityName' => $safeClassName]);
         }
         if ($fields = $this->mapper->getEntityMapParameter($targetClass, 'title_fields')) {
             $text = [];
             foreach ($fields as $field) {
                 $text[] = $this->mapper->getFieldValue($target, $field);
             }
             $item['title'] = implode(' ', $text);
         } else {
             $item['title'] = $this->translator->trans('oro.entity.item', ['%id%' => $targetId]);
         }
         $item['activityClassAlias'] = $this->entityAliasResolver->getPluralAlias($class);
         $item['entityId'] = $id;
         $item['targetId'] = $targetId;
         $item['targetClassName'] = $safeClassName;
         $item['icon'] = $config->get('icon');
         $item['link'] = $link;
         $result[] = $item;
     }
     return $result;
 }
 /**
  * @param $item
  * @param $targetClass
  * @param $target
  * @param $targetId
  *
  * @return mixed
  */
 protected function prepareItemTitle($item, $targetClass, $target, $targetId)
 {
     if (!array_key_exists('title', $item) || !$item['title']) {
         if ($fields = $this->mapper->getEntityMapParameter($targetClass, 'title_fields')) {
             $text = [];
             foreach ($fields as $field) {
                 $text[] = $this->mapper->getFieldValue($target, $field);
             }
             $item['title'] = implode(' ', $text);
             return $item;
         } else {
             $item['title'] = $this->translator->trans('oro.entity.item', ['%id%' => $targetId]);
             return $item;
         }
     }
     return $item;
 }
 /**
  * Returns the context for the given activity class and id
  *
  * @param string $class The FQCN of the activity entity
  * @param        $id
  *
  * @return array
  */
 public function getActivityContext($class, $id)
 {
     $currentUser = $this->securityTokenStorage->getToken()->getUser();
     $userClass = ClassUtils::getClass($currentUser);
     $entity = $this->doctrineHelper->getEntity($class, $id);
     $result = [];
     if (!$entity || !$entity instanceof ActivityInterface) {
         return $result;
     }
     $targets = $entity->getActivityTargetEntities();
     $entityProvider = $this->configManager->getProvider('entity');
     foreach ($targets as $target) {
         $targetClass = ClassUtils::getClass($target);
         $targetId = $target->getId();
         if ($userClass === $targetClass && $currentUser->getId() === $targetId) {
             continue;
         }
         $item = [];
         $config = $entityProvider->getConfig($targetClass);
         $safeClassName = $this->entityClassNameHelper->getUrlSafeClassName($targetClass);
         if (!array_key_exists('title', $item) || !$item['title']) {
             if ($fields = $this->mapper->getEntityMapParameter($targetClass, 'title_fields')) {
                 $text = [];
                 foreach ($fields as $field) {
                     $text[] = $this->mapper->getFieldValue($target, $field);
                 }
                 $item['title'] = implode(' ', $text);
             } else {
                 $item['title'] = $this->translator->trans('oro.entity.item', ['%id%' => $targetId]);
             }
         }
         $item['activityClassAlias'] = $this->entityAliasResolver->getPluralAlias($class);
         $item['entityId'] = $id;
         $item['targetId'] = $targetId;
         $item['targetClassName'] = $safeClassName;
         $item['icon'] = $config->get('icon');
         $item['link'] = $this->getContextLink($targetClass, $targetId);
         $event = new PrepareContextTitleEvent($item, $targetClass);
         $this->dispatcher->dispatch(PrepareContextTitleEvent::EVENT_NAME, $event);
         $item = $event->getItem();
         $result[] = $item;
     }
     return $result;
 }