/**
  * {@inheritdoc}
  */
 public function visitMetadata(DatagridConfiguration $config, MetadataObject $data)
 {
     $currentViewId = $this->getCurrentViewId($config->getName());
     $this->setDefaultParams($config->getName());
     $data->offsetAddToArray('initialState', ['gridView' => self::DEFAULT_VIEW_ID]);
     $data->offsetAddToArray('state', ['gridView' => $currentViewId]);
     $allLabel = null;
     if (isset($config['options'], $config['options']['gridViews'], $config['options']['gridViews']['allLabel'])) {
         $allLabel = $this->translator->trans($config['options']['gridViews']['allLabel']);
     }
     /** @var AbstractViewsList $list */
     $list = $config->offsetGetOr(self::VIEWS_LIST_KEY, false);
     $systemGridView = new View(self::DEFAULT_VIEW_ID);
     $systemGridView->setDefault($this->getDefaultViewId($config->getName()) === null);
     $gridViews = ['choices' => [['label' => $allLabel, 'value' => self::DEFAULT_VIEW_ID]], 'views' => [$systemGridView->getMetadata()]];
     if ($list !== false) {
         $configuredGridViews = $list->getMetadata();
         $configuredGridViews['views'] = array_merge($gridViews['views'], $configuredGridViews['views']);
         $configuredGridViews['choices'] = array_merge($gridViews['choices'], $configuredGridViews['choices']);
         $gridViews = $configuredGridViews;
     }
     if ($this->eventDispatcher->hasListeners(GridViewsLoadEvent::EVENT_NAME)) {
         $event = new GridViewsLoadEvent($config->getName(), $gridViews);
         $this->eventDispatcher->dispatch(GridViewsLoadEvent::EVENT_NAME, $event);
         $gridViews = $event->getGridViews();
     }
     $gridViews['gridName'] = $config->getName();
     $gridViews['permissions'] = $this->getPermissions();
     $data->offsetAddToArray('gridViews', $gridViews);
 }
 /**
  * {@inheritdoc}
  */
 public function configure(DatagridConfiguration $configuration)
 {
     $attributes = $configuration->offsetGet(self::SOURCE_KEY)[self::USEABLE_ATTRIBUTES_KEY];
     $attributes = $attributes === null ? [] : $attributes;
     $displayedFilters = [];
     foreach ($attributes as $attributeCode => $attribute) {
         if (!$attribute['useableAsGridFilter']) {
             continue;
         }
         $attributeType = $attribute['attributeType'];
         $attributeTypeConf = $this->registry->getConfiguration($attributeType);
         if (!$attributeTypeConf || !isset($attributeTypeConf['filter'])) {
             throw new \LogicException(sprintf('Attribute type %s must be configured to allow grid filter on attribute %s', $attributeType, $attributeCode));
         }
         $filterConfig = $attributeTypeConf['filter'];
         $filterConfig = $filterConfig + [ProductFilterUtility::DATA_NAME_KEY => $attributeCode, 'label' => $attribute['label'], 'enabled' => AttributeTypes::IDENTIFIER === $attributeType, 'order' => $attribute['sortOrder'], 'group' => $attribute['group'], 'groupOrder' => $attribute['groupOrder']];
         if (AttributeTypes::METRIC === $attributeType) {
             $filterConfig['family'] = $attribute['metricFamily'];
         }
         $displayedFilters[$attributeCode] = $filterConfig;
     }
     $this->sortFilters($displayedFilters);
     $filters = $configuration->offsetGet(FilterConfiguration::FILTERS_KEY);
     foreach ($displayedFilters as $attributeCode => $filterConfig) {
         $filters['columns'][$attributeCode] = $filterConfig;
     }
     $configuration->offsetSet(FilterConfiguration::FILTERS_KEY, $filters);
 }
 /**
  * {@inheritDoc}
  */
 public function visitMetadata(DatagridConfiguration $config, MetadataObject $data)
 {
     $params = $this->getParameters()->get(ParameterBag::ADDITIONAL_PARAMETERS, []);
     $currentView = isset($params[self::VIEWS_PARAM_KEY]) ? $params[self::VIEWS_PARAM_KEY] : null;
     $data->offsetAddToArray('initialState', ['gridView' => '__all__']);
     $data->offsetAddToArray('state', ['gridView' => $currentView]);
     $allLabel = null;
     if (isset($config['options']) && isset($config['options']['gridViews']) && isset($config['options']['gridViews']['allLabel'])) {
         $allLabel = $this->translator->trans($config['options']['gridViews']['allLabel']);
     }
     /** @var AbstractViewsList $list */
     $list = $config->offsetGetOr(self::VIEWS_LIST_KEY, false);
     $gridViews = ['choices' => [['label' => $allLabel, 'value' => '__all__']], 'views' => [(new View('__all__'))->getMetadata()]];
     if ($list !== false) {
         $configuredGridViews = $list->getMetadata();
         $configuredGridViews['views'] = array_merge($gridViews['views'], $configuredGridViews['views']);
         $configuredGridViews['choices'] = array_merge($gridViews['choices'], $configuredGridViews['choices']);
         $gridViews = $configuredGridViews;
     }
     if ($this->eventDispatcher->hasListeners(GridViewsLoadEvent::EVENT_NAME)) {
         $event = new GridViewsLoadEvent($config->getName(), $gridViews);
         $this->eventDispatcher->dispatch(GridViewsLoadEvent::EVENT_NAME, $event);
         $gridViews = $event->getGridViews();
     }
     $gridViews['gridName'] = $config->getName();
     $gridViews['permissions'] = $this->getPermissions();
     $data->offsetSet('gridViews', $gridViews);
 }
 /**
  * {@inheritDoc}
  */
 public function visitDatasource(DatagridConfiguration $config, DatasourceInterface $datasource)
 {
     $entityName = $config->offsetGetByPath(self::EXTEND_ENTITY_CONFIG_PATH);
     $fields = $this->getDynamicFields($entityName);
     if ($datasource instanceof OrmDatasource && !empty($fields)) {
         /** @var QueryBuilder $qb */
         $qb = $datasource->getQueryBuilder();
         $fromParts = $qb->getDQLPart('from');
         $alias = false;
         foreach ($fromParts as $fromPart) {
             if ($fromPart->getFrom() == $entityName) {
                 $alias = $fromPart->getAlias();
             }
         }
         if ($alias === false) {
             // add entity if it not exists in from clause
             $alias = ExtendConfigDumper::FIELD_PREFIX . 'o';
             $qb->from($entityName, $alias);
         }
         foreach ($fields as $field) {
             $fn = ExtendConfigDumper::FIELD_PREFIX . $field->getFieldName();
             $qb->addSelect(sprintf('%s.%s', $alias, $fn));
             // set real "data name" for filters and sorters
             $config->offsetSetByPath(sprintf('%s[%s][%s]', OrmSorterConfiguration::COLUMNS_PATH, $fn, PropertyInterface::DATA_NAME_KEY), sprintf('%s.%s', $alias, $fn));
             $config->offsetSetByPath(sprintf('%s[%s][%s]', FilterConfiguration::COLUMNS_PATH, $fn, FilterUtility::DATA_NAME_KEY), sprintf('%s.%s', $alias, $fn));
         }
     }
 }
 function let(DatagridConfiguration $configuration, ConfigurationRegistry $registry)
 {
     $registry->getConfiguration('pim_catalog_identifier')->willReturn(['column' => ['identifier_config']]);
     $registry->getConfiguration('pim_catalog_text')->willReturn(['column' => ['text_config']]);
     $configuration->offsetGetByPath(sprintf('[%s]', FormatterConfiguration::COLUMNS_KEY))->willReturn(['family' => ['family_config']]);
     $this->beConstructedWith($registry);
 }
 /**
  * Add user limitation
  *
  * @param DatagridConfiguration $config
  * @param string                $accessLevel
  * @param User                  $user
  * @param Organization          $organization
  *
  * @throws \Exception
  */
 protected function applyACL(DatagridConfiguration $config, $accessLevel, User $user, Organization $organization)
 {
     $where = $config->offsetGetByPath('[source][query][where][and]', []);
     /** todo: refactor this check usages */
     if ($accessLevel == AccessLevel::BASIC_LEVEL) {
         $where = array_merge($where, ['u.id = ' . $user->getId()]);
     } elseif ($accessLevel == AccessLevel::GLOBAL_LEVEL) {
         $leftJoins = $config->offsetGetByPath('[source][query][join][inner]', []);
         $leftJoins[] = ['join' => 'u.organizations', 'alias' => 'org'];
         $config->offsetSetByPath('[source][query][join][inner]', $leftJoins);
         $where = array_merge($where, ['org.id in (' . $organization->getId() . ')']);
     } elseif ($accessLevel !== AccessLevel::SYSTEM_LEVEL) {
         $resultBuIds = [];
         if ($accessLevel == AccessLevel::LOCAL_LEVEL) {
             $resultBuIds = $this->treeProvider->getTree()->getUserBusinessUnitIds($user->getId(), $organization->getId());
         } elseif ($accessLevel == AccessLevel::DEEP_LEVEL) {
             $resultBuIds = $this->treeProvider->getTree()->getUserSubordinateBusinessUnitIds($user->getId(), $organization->getId());
         }
         $leftJoins = $config->offsetGetByPath('[source][query][join][inner]', []);
         $leftJoins[] = ['join' => 'u.businessUnits', 'alias' => 'bu'];
         $config->offsetSetByPath('[source][query][join][inner]', $leftJoins);
         $where = array_merge($where, ['bu.id in (' . implode(', ', $resultBuIds) . ')']);
     }
     if (count($where)) {
         $config->offsetSetByPath('[source][query][where][and]', $where);
     }
 }
 /**
  * @param DatagridConfiguration $config
  *
  * @return bool
  */
 public function isApplicable(DatagridConfiguration $config)
 {
     $columns = $config->offsetGetByPath(Configuration::COLUMNS_PATH);
     $type = $config->offsetGetByPath(Builder::DATASOURCE_TYPE_PATH);
     $isApplicable = ($type == CombinedAuditDatasource::TYPE || $type == CombinedUsersDatasource::TYPE) && is_array($columns);
     return $isApplicable;
 }
 /**
  * {@inheritdoc}
  */
 public function visitDatasource(DatagridConfiguration $config, DatasourceInterface $datasource)
 {
     /** @var QueryBuilder $qb */
     $qb = $datasource->getQueryBuilder();
     $ds = new GroupingOrmFilterDatasourceAdapter($qb);
     $filters = $config->offsetGetByPath('[source][query_config][filters]');
     $this->restrictionBuilder->buildRestrictions($filters, $ds);
 }
 /**
  * Build references data
  *
  * @param DatasourceInterface   $dataSource
  * @param DatagridConfiguration $configuration
  */
 protected function buildReferenceData(DatasourceInterface $dataSource, DatagridConfiguration $configuration)
 {
     $source = $configuration->offsetGet('source');
     $qb = $dataSource->getQueryBuilder();
     foreach ($configuration->offsetGet('columns') as $column => $config) {
         $this->buildQueryBuilder($qb, $source, $column);
     }
 }
 /**
  * @param DatagridConfiguration $config
  *
  * @return null
  */
 public function getEntityRootAlias(DatagridConfiguration $config)
 {
     $from = $config->offsetGetByPath('[source][query][from]');
     if ($from) {
         return $from[0]['alias'];
     }
     return null;
 }
 /**
  * {@inheritDoc}
  */
 public function visitDatasource(DatagridConfiguration $config, DatasourceInterface $datasource)
 {
     $defaultPerPage = $config->offsetGetByPath(ToolbarExtension::PAGER_DEFAULT_PER_PAGE_OPTION_PATH, 10);
     $this->pager->setQuery($datasource->getQuery());
     $this->pager->setPage($this->getOr(self::PAGE_PARAM, 1));
     $this->pager->setMaxPerPage($this->getOr(self::PER_PAGE_PARAM, $defaultPerPage));
     $this->pager->init();
 }
 function let(DatagridConfiguration $configuration, ConfigurationRegistry $registry, ProductManager $manager)
 {
     $attributes = ['sku' => ['code' => 'sku', 'attributeType' => 'pim_catalog_identifier'], 'name' => ['code' => 'name', 'attributeType' => 'pim_catalog_text']];
     $configuration->offsetGetByPath(sprintf('[source][%s]', ContextConfigurator::USEABLE_ATTRIBUTES_KEY))->willReturn($attributes);
     $configuration->offsetGetByPath(sprintf('[%s]', FormatterConfiguration::COLUMNS_KEY))->willReturn(['family' => ['family_config'], 'sku' => [], 'name' => []]);
     $registry->getConfiguration('pim_catalog_identifier')->willReturn(['column' => ['identifier_config'], 'sorter' => 'flexible_field']);
     $this->beConstructedWith($registry, $manager);
 }
 /**
  * {@inheritdoc}
  */
 public function processConfigs(DatagridConfiguration $config)
 {
     $pagination = $config->offsetGetByPath(self::ENTITY_PAGINATION_PATH);
     if ($pagination !== null && !is_bool($pagination)) {
         throw new \LogicException('Entity pagination is not boolean');
     }
     $config->offsetSetByPath(self::ENTITY_PAGINATION_PATH, (bool) $pagination);
 }
 /**
  * {@inheritdoc}
  *
  * Gets stored SQL and parameters prepared by
  * Oro\Bundle\DataGridBundle\EventListener\StoreSqlListener
  * and puts them into metadata of ResultsObject
  */
 public function visitResult(DatagridConfiguration $config, ResultsObject $result)
 {
     $value = $config->offsetGetByPath(self::STORED_SQL_PATH);
     if ($value) {
         $result->offsetAddToArray('metadata', [self::DISPLAY_SQL_QUERY => true]);
         $result->offsetAddToArrayByPath(self::STORED_SQL_PATH, $value);
     }
 }
 function it_cannot_handle_misconfigured_attribute_type(DatagridConfiguration $configuration, $registry)
 {
     $attributes = ['sku' => ['code' => 'sku', 'label' => 'Sku', 'useableAsGridFilter' => 1, 'attributeType' => 'pim_catalog_identifier', 'sortOrder' => 2, 'group' => 'Foo', 'groupOrder' => 3], 'name' => ['code' => 'name', 'label' => 'Name', 'useableAsGridFilter' => 1, 'attributeType' => 'pim_catalog_text', 'sortOrder' => 4, 'group' => 'Bar', 'groupOrder' => 5]];
     $attributesConf = [ContextConfigurator::USEABLE_ATTRIBUTES_KEY => $attributes];
     $configuration->offsetGet(ContextConfigurator::SOURCE_KEY)->willReturn($attributesConf);
     $registry->getConfiguration('pim_catalog_identifier')->willReturn(['filter' => ['identifier_config']]);
     $registry->getConfiguration('pim_catalog_text')->willReturn([]);
     $this->shouldThrow('\\LogicException')->duringConfigure($configuration);
 }
 /**
  * {@inheritDoc}
  */
 public function isApplicable(DatagridConfiguration $config)
 {
     $filters = $config->offsetGetByPath(Configuration::COLUMNS_PATH);
     if ($filters === null) {
         return false;
     }
     $type = $config->offsetGetByPath(Builder::DATASOURCE_TYPE_PATH);
     return $type == CombinedUsersDatasource::TYPE || $type == CombinedAuditDatasource::TYPE;
 }
Beispiel #17
0
 /**
  * {@inheritDoc}
  */
 public function visitMetadata(DatagridConfiguration $config, MetadataObject $data)
 {
     $defaultPage = 1;
     $defaultPerPage = $config->offsetGetByPath(ToolbarExtension::PAGER_DEFAULT_PER_PAGE_OPTION_PATH, 10);
     $initialState = ['currentPage' => $defaultPage, 'pageSize' => $defaultPerPage];
     $state = ['currentPage' => $this->getOr(PagerInterface::PAGE_PARAM, $defaultPage), 'pageSize' => $this->getOr(PagerInterface::PER_PAGE_PARAM, $defaultPerPage)];
     $data->offsetAddToArray('initialState', $initialState);
     $data->offsetAddToArray('state', $state);
 }
 /**
  * {@inheritdoc}
  */
 public function apply(DatasourceInterface $datasource, DatagridConfiguration $configuration)
 {
     $qb = $datasource->getQueryBuilder();
     $joinAlias = 'selectCompleteness';
     $util = new CompletenessJoin($qb);
     $locale = $configuration->offsetGetByPath('[source][locale_code]');
     $scope = $configuration->offsetGetByPath('[source][scope_code]');
     $util->addJoins($joinAlias, $locale, $scope);
     $qb->addSelect($joinAlias . '.ratio AS ratio');
 }
 /**
  * {@inheritdoc}
  */
 public function apply(DatasourceInterface $datasource, DatagridConfiguration $configuration)
 {
     if ($this->applied === false) {
         $rootAlias = $datasource->getQueryBuilder()->getRootAlias();
         $path = sprintf('[source][%s]', ContextConfigurator::DISPLAYED_ATTRIBUTES_KEY);
         $attributeIds = $configuration->offsetGetByPath($path);
         $datasource->getQueryBuilder()->leftJoin($rootAlias . '.values', 'values', 'WITH', 'values.attribute IN (:attributeIds) ' . 'AND (values.locale = :dataLocale OR values.locale IS NULL) ' . 'AND (values.scope = :scopeCode OR values.scope IS NULL)')->addSelect('values')->leftJoin('values.attribute', 'attribute')->addSelect('attribute')->setParameter('attributeIds', $attributeIds);
     }
     $this->applied = true;
 }
 /**
  * @param DatagridConfiguration $config
  * @param string $path
  * @param mixed $element
  * @param mixed $key
  */
 protected function addConfigElement(DatagridConfiguration $config, $path, $element, $key = null)
 {
     $select = $config->offsetGetByPath($path);
     if ($key) {
         $select[$key] = $element;
     } else {
         $select[] = $element;
     }
     $config->offsetSetByPath($path, $select);
 }
 /**
  * {@inheritDoc}
  */
 public function visitMetadata(DatagridConfiguration $config, MetadataObject $data)
 {
     $params = $this->getRequestParams()->get(RequestParameters::ADDITIONAL_PARAMETERS);
     $currentView = isset($params[self::VIEWS_PARAM_KEY]) ? $params[self::VIEWS_PARAM_KEY] : null;
     $data->offsetAddToArray('state', ['gridView' => $currentView]);
     /** @var AbstractViewsList $list */
     $list = $config->offsetGetOr(self::VIEWS_LIST_KEY, false);
     if ($list !== false) {
         $data->offsetSet('gridViews', $list->getMetadata());
     }
 }
 public function testRemoveColumn()
 {
     $this->configuration->addColumn('testColumn', ['param' => 123], null, ['param' => 123], ['param' => 123]);
     $configArray = $this->configuration->toArray();
     $this->assertTrue(isset($configArray['columns']['testColumn']));
     $this->configuration->removeColumn('testColumn');
     $configArray = $this->configuration->toArray();
     $this->assertEmpty($configArray['columns']);
     $this->assertEmpty($configArray['sorters']['columns']);
     $this->assertEmpty($configArray['filters']['columns']);
 }
Beispiel #23
0
 /**
  * {@inheritdoc}
  */
 public function processConfigs(DatagridConfiguration $config)
 {
     $columns = $config->offsetGetByPath('[columns]', []);
     $column = [self::COLUMN_NAME => $this->getColumnDefinition($config)];
     $config->offsetSetByPath('[columns]', array_merge($columns, $column));
     // do not add tag filter if $filters are empty(case when they are disabled).
     $filters = $config->offsetGetByPath(self::GRID_FILTERS_PATH, []);
     if (!empty($filters)) {
         $filters[self::FILTER_COLUMN_NAME] = $this->getColumnFilterDefinition($config);
         $config->offsetSetByPath(self::GRID_FILTERS_PATH, $filters);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function visitDatasource(DatagridConfiguration $config, DatasourceInterface $datasource)
 {
     $path = $config->offsetGetByPath(self::CHANNEL_RELATION_OPTION_PATH);
     list($mainEntity, $relationName) = explode('.', $path);
     /** @var OrmDatasource $datasource */
     $queryBuilder = $datasource->getQueryBuilder();
     $mainEntity = $this->ensureJoined($queryBuilder, $mainEntity);
     $relationName = $this->ensureJoined($queryBuilder, $relationName, $mainEntity);
     $channelIds = explode(',', $this->getParameters()->get('channelIds'));
     $queryBuilder->andWhere($relationName . '.id in (:channelIds)');
     $queryBuilder->setParameter('channelIds', $channelIds);
 }
Beispiel #25
0
 /**
  * {@inheritDoc}
  */
 public function isApplicable(DatagridConfiguration $config)
 {
     // validate configuration and fill default values
     $options = $this->validateConfiguration(new Configuration(), ['export' => $config->offsetGetByPath(self::EXPORT_OPTION_PATH, false)]);
     // translate labels
     foreach ($options as &$option) {
         $option['label'] = $this->translator->trans($option['label']);
     }
     // push options back to config
     $config->offsetSetByPath(self::EXPORT_OPTION_PATH, $options);
     return !empty($options);
 }
 function it_applies_join_on_datasource_query(Datasource $datasource, DatagridConfiguration $configuration, QueryBuilder $queryBuilder)
 {
     $datasource->getQueryBuilder()->willReturn($queryBuilder);
     $configuration->offsetGetByPath('[source][displayed_attribute_ids]')->willReturn([1, 2]);
     $queryBuilder->getRootAlias()->willReturn('p');
     $queryBuilder->leftJoin('p.values', 'values', 'WITH', sprintf('%s AND %s AND %s', 'values.attribute IN (:attributeIds)', '(values.locale = :dataLocale OR values.locale IS NULL)', '(values.scope = :scopeCode OR values.scope IS NULL)'))->willReturn($queryBuilder);
     $queryBuilder->addSelect('values')->willReturn($queryBuilder);
     $queryBuilder->leftJoin('values.attribute', 'attribute')->willReturn($queryBuilder);
     $queryBuilder->addSelect('attribute')->willReturn($queryBuilder);
     $queryBuilder->setParameter('attributeIds', [1, 2])->willReturn($queryBuilder);
     $this->apply($datasource, $configuration);
 }
Beispiel #27
0
 /**
  * @param DatagridConfiguration $config
  *
  * @return null|string
  */
 protected function getEntity(DatagridConfiguration $config)
 {
     $entityClassName = $config->offsetGetByPath(self::GRID_EXTEND_ENTITY_PATH);
     if (!$entityClassName) {
         $from = $config->offsetGetByPath(self::GRID_FROM_PATH);
         if (!$from) {
             return null;
         }
         $entityClassName = $this->entityClassResolver->getEntityClass($from[0]['table']);
     }
     return $entityClassName;
 }
 /**
  * {@inheritDoc}
  */
 public function visitMetadata(DatagridConfiguration $config, MetadataObject $data)
 {
     $actionsMetadata = [];
     $actions = $config->offsetGetOr(static::ACTION_KEY, []);
     foreach ($actions as $name => $action) {
         $action = $this->getActionObject($name, $action);
         if ($action !== false) {
             $metadata = $action->getOptions()->toArray([], static::$excludeParams);
             $metadata['label'] = isset($metadata['label']) ? $this->translator->trans($metadata['label']) : null;
             $actionsMetadata[$action->getName()] = $metadata;
         }
     }
     $data->offsetAddToArray(static::METADATA_ACTION_KEY, $actionsMetadata);
 }
 /**
  * {@inheritdoc}
  */
 public function visitDatasource(DatagridConfiguration $config, DatasourceInterface $datasource)
 {
     $gridName = $config->offsetGetByPath(self::NAME_PATH);
     $parametersKey = md5(json_encode($this->parameters->all()));
     if (!empty($this->appliedFor[$gridName . $parametersKey])) {
         return;
     }
     /** @var QueryBuilder $qb */
     $qb = $datasource->getQueryBuilder();
     $ds = new GroupingOrmFilterDatasourceAdapter($qb);
     $filters = $config->offsetGetByPath('[source][query_config][filters]');
     $this->restrictionBuilder->buildRestrictions($filters, $ds);
     $this->appliedFor[$gridName . $parametersKey] = true;
 }
 /**
  * {@inheritDoc}
  */
 public function visitMetadata(DatagridConfiguration $config, MetadataObject $data)
 {
     // get only columns here because columns will be represented on frontend
     $columns = $config->offsetGetOr(Configuration::COLUMNS_KEY, []);
     $propertiesMetadata = [];
     foreach ($columns as $name => $fieldConfig) {
         $fieldConfig = PropertyConfiguration::createNamed($name, $fieldConfig);
         $metadata = $this->getPropertyObject($fieldConfig)->getMetadata();
         // translate label on backend
         $metadata['label'] = $this->translator->trans($metadata['label']);
         $propertiesMetadata[] = $metadata;
     }
     $data->offsetAddToArray('columns', $propertiesMetadata);
 }