/** * 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; }
/** * 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; }
/** * 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; }
/** * @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; }
/** * 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; }
/** * @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); }
/** * @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; }