/**
  * {@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;
 }
示例#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;
 }
 public function process(PrepareResultItemEvent $event)
 {
     $entity = $event->getEntity();
     if (!$entity instanceof Ticket) {
         return;
     }
     $item = $event->getResultItem();
     $name = $item->getEntityName();
     $routeParameters = $this->mapper->getEntityMapParameter($name, 'route');
     $routeData = array();
     foreach ($routeParameters['parameters'] as $parameter => $field) {
         $routeData[$parameter] = $entity->getKey();
         break;
     }
     $router = $this->container->get('router');
     $url = $router->generate($routeParameters['name'], $routeData, true);
     $item->setRecordUrl($url);
     $title = $entity->getKey() . static::TITLE_PART_SEPARATOR . $entity->getSubject();
     $item->setRecordTitle($title);
 }
示例#4
0
 /**
  * Returns a DQL expression that can be used to get a text representation of the given type of entities.
  *
  * @param string $className The FQCN of the entity
  * @param string $alias     The alias in SELECT or JOIN statement
  *
  * @return string|false
  */
 protected function getNameDQL($className, $alias)
 {
     $fields = $this->mapper->getEntityMapParameter($className, 'title_fields');
     if ($fields) {
         $titleParts = [];
         foreach ($fields as $field) {
             $titleParts[] = $alias . '.' . $field;
             $titleParts[] = '\' \'';
         }
         return QueryUtils::buildConcatExpr($titleParts);
     }
     return false;
 }
示例#5
0
 /**
  * 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;
 }
示例#7
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;
 }
示例#12
0
 public function testGetEntityMapParameter()
 {
     $this->assertEquals('test_product', $this->mapper->getEntityMapParameter(self::ENTITY_PRODUCT, 'alias'));
     $this->assertEquals(false, $this->mapper->getEntityMapParameter(self::ENTITY_PRODUCT, 'non exists parameter'));
 }
 public function testGetEntityMapParameter()
 {
     $this->assertEquals('test_product', $this->mapper->getEntityMapParameter('Oro\\Bundle\\SearchBundle\\Tests\\Unit\\Fixture\\Entity\\Product', 'alias'));
     $this->assertEquals(false, $this->mapper->getEntityMapParameter('Oro\\Bundle\\SearchBundle\\Tests\\Unit\\Fixture\\Entity\\Product', 'non exists parameter'));
 }