Esempio n. 1
0
 /**
  * Gets names of all configurable entities
  *
  * @return array
  *      key   => full class name of an entity
  *      value => translated entity name
  */
 public function getEntityNames()
 {
     if (!$this->entityNames) {
         $this->entityNames = [];
         foreach ($this->entityProvider->getEntities() as $entity) {
             $this->entityNames[$entity['name']] = $entity['label'];
         }
     }
     return $this->entityNames;
 }
 /**
  * Applies dynamic attributes for 'configs' options
  *
  * @param Options $options
  * @param array   $configs
  * @param array   $defaultConfigs
  * @return array
  */
 protected function configsNormalizer(Options $options, &$configs, &$defaultConfigs)
 {
     $configs = array_merge($defaultConfigs, $configs);
     if ($options['multiple'] && $configs['placeholder'] === $defaultConfigs['placeholder']) {
         $configs['placeholder'] .= 's';
     }
     if ($options['with_relations'] && !$options['skip_load_entities']) {
         $configs['entities'] = $this->entityProvider->getEntities();
     }
     return $configs;
 }
Esempio n. 3
0
 /**
  * Returns a list of entities
  *
  * @param bool $showPlural If true a plural label will be used as a choice text; otherwise, a label will be used
  *
  * @return array [{full class name} => [{attr1} => {val1}, ...], ...]
  */
 protected function getEntities($showPlural)
 {
     if (null === $this->itemsCache) {
         $this->itemsCache = [];
         foreach ($this->provider->getEntities($showPlural) as $entity) {
             $entityClass = $entity['name'];
             unset($entity['name']);
             $this->itemsCache[$entityClass] = $entity;
         }
     }
     return $this->itemsCache;
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function getEntitiesMetadata()
 {
     $result = [];
     foreach ($this->getChannelEntities() as $entityName) {
         $entityConfig = $this->entityProvider->getEntity($entityName, true);
         $extendConfig = $this->configManager->getProvider('extend')->getConfig($entityName);
         $configEntityModel = $this->configManager->getConfigEntityModel($entityName);
         if ($configEntityModel instanceof EntityConfigModel) {
             $entityConfig = array_merge($entityConfig, $this->getEntityLinks($configEntityModel));
         }
         $result[$entityName] = array_merge($entityConfig, ['type' => $extendConfig->get('owner')]);
     }
     return $result;
 }
Esempio n. 5
0
 /**
  * Returns a list of choices
  *
  * @param bool $showPlural If true a plural label will be used as a choice text; otherwise, a label will be used
  *
  * @return array of entities
  *               key = full class name, value = ChoiceListItem
  */
 protected function getChoices($showPlural)
 {
     $choices = array();
     $entities = $this->provider->getEntities($showPlural);
     foreach ($entities as $entity) {
         $attributes = [];
         foreach ($entity as $key => $val) {
             if (!in_array($key, ['name'])) {
                 $attributes['data-' . $key] = $val;
             }
         }
         $choices[$entity['name']] = new ChoiceListItem($showPlural ? $entity['plural_label'] : $entity['label'], $attributes);
     }
     return $choices;
 }
Esempio n. 6
0
 /**
  * @param User   $user
  * @param string $searchString
  * @param int    $offset
  * @param int    $maxResults
  * @return array
  */
 public function autocompleteSearch(User $user, $searchString, $offset = 0, $maxResults = 0)
 {
     $classNameMap = [];
     $entities = $this->entityProvider->getEntities();
     foreach ($entities as $description) {
         $classNameMap[$description['name']] = true;
     }
     $tables = [];
     $configs = $this->configManager->getProvider('activity')->getConfigs();
     foreach ($configs as $config) {
         $className = $config->getId()->getClassName();
         if (!isset($classNameMap[$className])) {
             continue;
         }
         $activities = $config->get('activities');
         if (!empty($activities) && in_array(Email::ENTITY_CLASS, $activities, true)) {
             $tables[] = $this->em->getClassMetadata($className)->getTableName();
         }
     }
     $results = [];
     $searchResults = $this->simpleSearch($searchString, $offset, $maxResults, $tables);
     foreach ($searchResults->getElements() as $item) {
         $this->dispatcher->dispatch(PrepareResultItemEvent::EVENT_NAME, new PrepareResultItemEvent($item));
         $className = $item->getEntityName();
         if (ClassUtils::getClass($user) === $className && $user->getId() === $item->getRecordId()) {
             continue;
         }
         $text = $item->getRecordTitle();
         if ($label = $this->getClassLabel($className)) {
             $text .= ' (' . $label . ')';
         }
         $results[] = ['text' => $text, 'id' => json_encode(['entityClass' => $className, 'entityId' => $item->getRecordId()])];
     }
     return $results;
 }
 /**
  * @param object $entity
  * @return array
  */
 public function getSupportedTargets($entity)
 {
     $targetEntities = $this->entityProvider->getEntities();
     $entityTargets = [];
     if (!is_object($entity) || !method_exists($entity, 'supportActivityTarget')) {
         return $entityTargets;
     }
     $count = count($targetEntities);
     for ($i = 0; $i < $count; $i++) {
         $targetEntity = $targetEntities[$i];
         $className = $targetEntity['name'];
         if (!empty($className) && $entity->supportActivityTarget($className)) {
             $entityTargets[] = ['label' => $targetEntity['label'], 'className' => $this->routingHelper->getUrlSafeClassName($targetEntity['name']), 'first' => !(bool) $i, 'gridName' => $this->getContextGridByEntity($className)];
             $i++;
         }
     }
     return $entityTargets;
 }
 /**
  * @param string $relatedEntityName
  * @param array  $relation
  * @param bool   $translate
  *
  * @return array
  */
 protected function addEntityDetails($relatedEntityName, array &$relation, $translate)
 {
     $entity = $this->entityProvider->getEntity($relatedEntityName, $translate);
     foreach ($entity as $key => $val) {
         if (!in_array($key, ['name'])) {
             $relation['related_entity_' . $key] = $val;
         }
     }
     return $relation;
 }
 /**
  * Applies dynamic attributes for 'configs' options
  *
  * @param Options $options
  * @param array   $configs
  * @param array   $defaultConfigs
  * @return array
  */
 protected function configsNormalizer(Options $options, &$configs, &$defaultConfigs)
 {
     $configs = array_merge($defaultConfigs, $configs);
     $configs['multiple'] = $options['multiple'];
     if ($options['multiple'] && $configs['placeholder'] === $defaultConfigs['placeholder']) {
         $configs['placeholder'] .= 's';
     }
     if ($configs['selection_template_twig'] === $defaultConfigs['selection_template_twig']) {
         $suffix = $options['multiple'] ? '_multiple' : '';
         if ($options['with_relations']) {
             $suffix .= '_with_relations';
         }
         $configs['selection_template_twig'] = sprintf($configs['selection_template_twig'], $suffix);
     }
     if ($options['with_relations'] && !$options['skip_load_entities']) {
         $configs['entities'] = $this->entityProvider->getEntities();
     }
     return $configs;
 }
Esempio n. 10
0
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testGetEntities()
 {
     $entityClassName1 = 'Acme\\Entity\\Test1';
     $entityClassName2 = 'Acme\\Entity\\Test2';
     $entityClassName3 = 'Acme\\Entity\\Test3';
     $entityClassName4 = 'Acme\\Entity\\Test4';
     $entityClassName5 = 'Acme\\Entity\\Test5';
     $entityConfig1 = $this->getEntityConfig($entityClassName1, ['label' => 'C', 'plural_label' => 'B', 'icon' => 'icon-test1']);
     $entityConfig2 = $this->getEntityConfig($entityClassName2, ['label' => 'B', 'plural_label' => 'A', 'icon' => 'icon-test2']);
     $entityConfig3 = $this->getEntityConfig($entityClassName3, ['label' => 'A', 'plural_label' => 'C', 'icon' => 'icon-test3']);
     $entityConfig4 = $this->getEntityConfig($entityClassName4, ['label' => 'A', 'plural_label' => 'C', 'icon' => 'icon-test3']);
     $entityConfig5 = $this->getEntityConfig($entityClassName5, ['label' => 'A', 'plural_label' => 'C', 'icon' => 'icon-test3']);
     $map = [$entityClassName1 => $entityConfig1, $entityClassName2 => $entityConfig2, $entityClassName3 => $entityConfig3, $entityClassName4 => $entityConfig4, $entityClassName5 => $entityConfig5];
     $this->extendConfigProvider->expects($this->any())->method('getConfigById')->will($this->returnCallback(function (EntityConfigId $configId) use($map) {
         $className = $configId->getClassName();
         /** @var ConfigInterface $config */
         $config = $map[$className];
         $config->set('state', ExtendScope::STATE_ACTIVE);
         return $config;
     }));
     $this->entityConfigProvider->expects($this->any())->method('getConfig')->will($this->returnValueMap([[$entityClassName1, $entityConfig1], [$entityClassName2, $entityConfig2], [$entityClassName3, $entityConfig3], [$entityClassName4, $entityConfig4], [$entityClassName5, $entityConfig5]]));
     $this->entityConfigProvider->expects($this->any())->method('getConfigs')->will($this->returnValue([$entityConfig1, $entityConfig2, $entityConfig3]));
     $this->extendConfigProvider->expects($this->any())->method('getConfig')->will($this->returnCallback(function ($param) {
         $this->extendConfig->set('state', ExtendScope::STATE_ACTIVE);
         if ($param == 'Acme\\Entity\\Test4') {
             $this->extendConfig->set('state', ExtendScope::STATE_NEW);
         }
         if ($param == 'Acme\\Entity\\Test4') {
             $this->extendConfig->set('state', ExtendScope::STATE_DELETE);
         }
         return $this->extendConfig;
     }));
     $this->extendConfigProvider->expects($this->any())->method('getConfig')->will($this->returnValue($this->extendConfig));
     // sort by plural label
     $result = $this->provider->getEntities();
     $expected = [['name' => $entityClassName2, 'label' => 'B', 'plural_label' => 'A', 'icon' => 'icon-test2'], ['name' => $entityClassName1, 'label' => 'C', 'plural_label' => 'B', 'icon' => 'icon-test1'], ['name' => $entityClassName3, 'label' => 'A', 'plural_label' => 'C', 'icon' => 'icon-test3']];
     $this->assertEquals($expected, $result);
     // sort by label
     $result = $this->provider->getEntities(false);
     $expected = [['name' => $entityClassName3, 'label' => 'A', 'plural_label' => 'C', 'icon' => 'icon-test3'], ['name' => $entityClassName2, 'label' => 'B', 'plural_label' => 'A', 'icon' => 'icon-test2'], ['name' => $entityClassName1, 'label' => 'C', 'plural_label' => 'B', 'icon' => 'icon-test1']];
     $this->assertEquals($expected, $result);
 }
 protected function setUp()
 {
     $this->entityConfigProvider = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Provider\\ConfigProvider')->disableOriginalConstructor()->getMock();
     $this->extendConfigProvider = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Provider\\ConfigProvider')->disableOriginalConstructor()->getMock();
     $this->entityClassResolver = $this->getMockBuilder('Oro\\Bundle\\EntityBundle\\ORM\\EntityClassResolver')->disableOriginalConstructor()->getMock();
     $this->entityClassResolver->expects($this->any())->method('getEntityClass')->will($this->returnCallback(function ($entityName) {
         return str_replace(':', '\\Entity\\', $entityName);
     }));
     $translator = $this->getMockBuilder('Symfony\\Component\\Translation\\Translator')->disableOriginalConstructor()->getMock();
     $translator->expects($this->any())->method('trans')->will($this->returnArgument(0));
     $this->exclusionProvider = $this->getMock('Oro\\Bundle\\EntityBundle\\Provider\\ExclusionProviderInterface');
     $entityProvider = new EntityProvider($this->entityConfigProvider, $this->extendConfigProvider, $this->entityClassResolver, $translator);
     $entityProvider->setExclusionProvider($this->exclusionProvider);
     $this->doctrine = $this->getMockBuilder('Symfony\\Bridge\\Doctrine\\ManagerRegistry')->disableOriginalConstructor()->getMock();
     $this->virtualFieldProvider = $this->getMockBuilder('Oro\\Bundle\\EntityBundle\\Provider\\ConfigVirtualFieldProvider')->disableOriginalConstructor()->getMock();
     $this->provider = new EntityFieldProvider($this->entityConfigProvider, $this->extendConfigProvider, $this->entityClassResolver, new FieldTypeHelper([]), $this->doctrine, $translator, []);
     $this->provider->setEntityProvider($entityProvider);
     $this->provider->setVirtualFieldProvider($this->virtualFieldProvider);
     $this->provider->setExclusionProvider($this->exclusionProvider);
 }
Esempio n. 12
0
 public function testGetEntitiesMetadata()
 {
     $this->entityProvider->expects($this->at(0))->method('getEntity')->will($this->returnvalue($this->entityConfig1));
     $this->entityProvider->expects($this->at(1))->method('getEntity')->will($this->returnvalue($this->entityConfig2));
     $this->entityProvider->expects($this->at(2))->method('getEntity')->will($this->returnvalue($this->entityConfig3));
     $extendConfigModel = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigInterface');
     $extendConfigModel->expects($this->any())->method('get')->with($this->equalTo('owner'))->will($this->returnValue('Custom'));
     $extendProvider = $this->getMockBuilder('Oro\\Bundle\\EntityConfigBundle\\Provider\\ConfigProvider')->disableOriginalConstructor()->getMock();
     $extendProvider->expects($this->once())->method('map')->will($this->returnValue([]));
     $extendProvider->expects($this->any())->method('getConfig')->will($this->returnValue($extendConfigModel));
     $this->configManager->expects($this->any())->method('getProvider')->with($this->equalTo('extend'))->will($this->returnValue($extendProvider));
     $this->configManager->expects($this->any())->method('getConfigEntityModel')->will($this->onConsecutiveCalls($this->entityConfigModel1, $this->entityConfigModel2));
     $this->router->expects($this->exactly(4))->method('generate');
     /** @var MetadataProvider $provider */
     $provider = new MetadataProvider($this->settingsProvider, $this->entityProvider, $this->configManager, $this->router);
     $result = $provider->getEntitiesMetadata();
     for ($i = 1; $i < 3; $i++) {
         $expectedConfig = $this->getExpectedConfig($i);
         $entityName = $expectedConfig['name'];
         $this->assertEquals($expectedConfig, $result[$entityName]);
     }
 }
 /**
  * Adds a relation to $result
  *
  * @param array  $result
  * @param string $name
  * @param string $type
  * @param string $label
  * @param string $relationType
  * @param string $relatedEntityName
  * @param bool   $withEntityDetails
  * @param int    $relationDeepLevel
  * @param bool   $lastDeepLevelRelations
  */
 protected function addRelation(array &$result, $name, $type, $label, $relationType, $relatedEntityName, $withEntityDetails, $relationDeepLevel, $lastDeepLevelRelations)
 {
     $relation = array('name' => $name, 'type' => $type, 'label' => $label, 'relation_type' => $relationType, 'related_entity_name' => $relatedEntityName);
     if ($withEntityDetails) {
         $entity = $this->entityProvider->getEntity($relatedEntityName);
         foreach ($entity as $key => $val) {
             if (!in_array($key, ['name'])) {
                 $relation['related_entity_' . $key] = $val;
             }
         }
     }
     if ($relationDeepLevel >= 0) {
         // set some exceptions
         // todo: we need to find more proper way to do this
         if ($relationDeepLevel > 0 && ($name === 'owner' || $name === 'createdBy' || $name === 'updatedBy')) {
             $relationDeepLevel = 0;
         }
         $relation['related_entity_fields'] = $this->getFields($relatedEntityName, $withEntityDetails && ($relationDeepLevel > 0 || $lastDeepLevelRelations), $withEntityDetails, $relationDeepLevel, $lastDeepLevelRelations);
     }
     $result[] = $relation;
 }
Esempio n. 14
0
 /**
  * @param User   $user
  * @param string $searchString
  * @param int    $offset
  * @param int    $maxResults
  * @return array
  */
 public function autocompleteSearch(User $user, $searchString, $offset = 0, $maxResults = 0)
 {
     $entityDescriptions = $this->entityProvider->getEntities();
     $classNames = [];
     foreach ($entityDescriptions as $description) {
         $classNames[] = $description['name'];
     }
     $entities = $this->em->getRepository(EntityConfigModel::ENTITY_NAME)->findEntitiesByClassNames($classNames);
     $tables = [];
     foreach ($entities as $entity) {
         /** @var EntityConfigModel $entity */
         $data = $entity->toArray('activity');
         if (!isset($data['activities']) || !$data['activities']) {
             continue;
         }
         if (!in_array(Email::ENTITY_CLASS, $data['activities'])) {
             continue;
         }
         $metadata = $this->em->getClassMetadata($entity->getClassName());
         $tables[] = $metadata->getTableName();
     }
     $results = [];
     $searchResults = $this->simpleSearch($searchString, $offset, $maxResults, $tables);
     foreach ($searchResults->getElements() as $item) {
         $this->dispatcher->dispatch(PrepareResultItemEvent::EVENT_NAME, new PrepareResultItemEvent($item));
         $className = $item->getEntityName();
         if (ClassUtils::getClass($user) === $className && $user->getId() === $item->getRecordId()) {
             continue;
         }
         $text = $item->getRecordTitle();
         if ($label = $this->getClassLabel($className)) {
             $text .= ' (' . $label . ')';
         }
         $results[] = ['text' => $text, 'id' => json_encode(['entityClass' => $className, 'entityId' => $item->getRecordId()])];
     }
     return $results;
 }
 /**
  * Constructor
  *
  * @param ConfigProvider      $entityConfigProvider
  * @param ConfigProvider      $extendConfigProvider
  * @param EntityClassResolver $entityClassResolver
  * @param Translator          $translator
  * @param EntityManager       $em
  */
 public function __construct(ConfigProvider $entityConfigProvider, ConfigProvider $extendConfigProvider, EntityClassResolver $entityClassResolver, Translator $translator, EntityManager $em)
 {
     parent::__construct($entityConfigProvider, $extendConfigProvider, $entityClassResolver, $translator);
     $this->em = $em;
 }