/**
  * @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;
 }
 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 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;
 }
 /**
  * {@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');
 }
 public function testVisitMetadata()
 {
     $metadata = MetadataObject::create([]);
     $this->extension->visitMetadata($this->config, $metadata);
     $totalsData = $metadata->offsetGet('state');
     $initialTotalsData = $metadata->offsetGet('initialState');
     $this->assertEquals($totalsData, $initialTotalsData);
     $this->assertEquals($this->config->offsetGetByPath(Configuration::TOTALS_PATH), $totalsData['totals']);
     $this->assertEquals('orodatagrid/js/totals-builder', $metadata->offsetGet('requireJSModules')[0]);
 }
Beispiel #6
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;
 }
Beispiel #7
0
 /**
  * {@inheritDoc}
  */
 public function isApplicable(DatagridConfiguration $config)
 {
     if ($config->offsetGetByPath(Builder::DATASOURCE_TYPE_PATH) !== OrmDatasource::TYPE) {
         return false;
     }
     if (!$config->offsetGetByPath(self::OPTIONS_MIXIN_PATH, false)) {
         return false;
     }
     $segmentId = $this->segmentHelper->getSegmentIdByGridName($config->offsetGetByPath('[name]'));
     if (!$segmentId) {
         return false;
     }
     return (bool) $this->segmentHelper->getMarketingListBySegment($segmentId);
 }
 /**
  * {@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;
 }
 /**
  * Remove extra sorters, ie, sorters defined in datagrid.yml but columns are not displayed
  */
 protected function removeExtraSorters()
 {
     $displayedColumns = $this->configuration->offsetGetByPath(sprintf('[%s]', FormatterConfiguration::COLUMNS_KEY));
     $columnsCodes = array_keys($displayedColumns);
     $sorters = $this->configuration->offsetGetByPath(sprintf('%s', OrmSorterConfiguration::COLUMNS_PATH));
     if (!empty($sorters)) {
         $sortersCodes = array_keys($sorters);
         foreach ($sortersCodes as $sorterCode) {
             if (!in_array($sorterCode, $columnsCodes)) {
                 unset($sorters[$sorterCode]);
             }
         }
         $this->configuration->offsetSetByPath(sprintf('%s', OrmSorterConfiguration::COLUMNS_PATH), $sorters);
     }
 }
 /**
  * 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);
     }
 }
 /**
  * {@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);
 }
 /**
  * {@inheritdoc}
  */
 public function configure(DatagridConfiguration $configuration)
 {
     $path = sprintf(self::SOURCE_PATH, self::USEABLE_ATTRIBUTES_KEY);
     $attributes = $configuration->offsetGetByPath($path);
     $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);
     foreach ($displayedFilters as $attributeCode => $filterConfig) {
         $configuration->offsetSetByPath(sprintf('%s[%s]', FilterConfiguration::COLUMNS_PATH, $attributeCode), $filterConfig);
     }
 }
 /**
  * {@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);
 }
 /**
  * @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}
  *
  * 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);
     }
 }
 /**
  * @param DatagridConfiguration $config
  */
 protected function showAllAccountItems(DatagridConfiguration $config)
 {
     $config->offsetSetByPath(Builder::DATASOURCE_SKIP_ACL_CHECK, true);
     $user = $this->getUser();
     $where = $config->offsetGetByPath('[source][query][where]', ['and' => []]);
     $where['and'][] = sprintf('(%s.account = %d OR %s.accountUser = %d)', $this->entityAlias, $user->getAccount()->getId(), $this->entityAlias, $user->getId());
     $config->offsetSetByPath('[source][query][where]', $where);
 }
 /**
  * {@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);
 }
 /**
  * {@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();
 }
Beispiel #20
0
 /**
  * {@inheritDoc}
  */
 public function isApplicable(DatagridConfiguration $config)
 {
     $gridName = $config->offsetGetByPath(self::NAME_PATH);
     if (!empty($this->appliedFor[$gridName])) {
         return false;
     }
     if ($config->offsetGetByPath(Builder::DATASOURCE_TYPE_PATH) !== OrmDatasource::TYPE) {
         return false;
     }
     $marketingListId = $this->marketingListHelper->getMarketingListIdByGridName($gridName);
     if (!$marketingListId) {
         return false;
     }
     $marketingList = $this->marketingListHelper->getMarketingList($marketingListId);
     // Accept only segment based marketing lists
     return $marketingList && !$marketingList->isManual();
 }
 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]];
     $path = sprintf('[source][%s]', ContextConfigurator::USEABLE_ATTRIBUTES_KEY);
     $configuration->offsetGetByPath($path)->willReturn($attributes);
     $registry->getConfiguration('pim_catalog_identifier')->willReturn(['filter' => ['identifier_config']]);
     $registry->getConfiguration('pim_catalog_text')->willReturn([]);
     $this->shouldThrow('\\LogicException')->duringConfigure($configuration);
 }
Beispiel #22
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)
 {
     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;
 }
Beispiel #24
0
 /**
  * {@inheritDoc}
  */
 public function visitMetadata(DatagridConfiguration $config, MetadataObject $data)
 {
     /**
      * Default toolbar options
      *  [
      *      'hide'       => false,
      *      'pageSize'   => [
      *          'hide'  => false,
      *          'items' => [10, 25, 50, 100],
      *          'default_per_page' => 10
      *       ],
      *      'pagination' => [
      *          'hide' => false,
      *          'onePage' => false,
      *      ]
      *  ];
      */
     $perPageDefault = $config->offsetGetByPath(self::PAGER_DEFAULT_PER_PAGE_OPTION_PATH);
     $pageSizeItems = $config->offsetGetByPath(self::PAGER_ITEMS_OPTION_PATH);
     $exist = array_filter($pageSizeItems, function ($item) use($perPageDefault) {
         if (is_array($item) && isset($item['size'])) {
             return $perPageDefault == $item['size'];
         } elseif (is_numeric($item)) {
             return $perPageDefault == $item;
         }
         return false;
     });
     if (empty($exist)) {
         throw new LogicException(sprintf('Default page size "%d" must present in size items array', $perPageDefault));
     }
     $options = $config->offsetGetByPath(ToolbarExtension::OPTIONS_PATH, []);
     // get user specified require js modules from options
     if (isset($options[MetadataObject::REQUIRED_MODULES_KEY])) {
         $data->offsetAddToArray(MetadataObject::REQUIRED_MODULES_KEY, $options[MetadataObject::REQUIRED_MODULES_KEY]);
         unset($options[MetadataObject::REQUIRED_MODULES_KEY]);
     }
     // in case of one page pagination page selector should be hidden
     if ($config->offsetGetByPath(self::PAGER_ONE_PAGE_OPTION_PATH, false)) {
         $options['toolbarOptions']['pageSize']['hide'] = true;
     }
     // grid options passed under "options" node
     $data->offsetAddToArray(self::METADATA_KEY, $options);
 }
 /**
  * @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 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 #27
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);
 }
Beispiel #28
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);
     }
 }
 /**
  * Inject requested repository parameters in the datagrid configuration
  */
 protected function addRepositoryParameters()
 {
     $path = $this->getSourcePath(self::REPOSITORY_PARAMETERS_KEY);
     $repositoryParams = $this->configuration->offsetGetByPath($path, null);
     if ($repositoryParams) {
         $params = [];
         foreach ($repositoryParams as $paramName) {
             $params[$paramName] = $this->requestParams->get($paramName, $this->request->get($paramName, null));
         }
         $this->configuration->offsetSetByPath($path, $params);
     }
 }
 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);
 }