Esempio n. 1
0
 /**
  * {@inheritDoc}
  */
 public function process(DatagridInterface $grid, array $config)
 {
     $this->datagrid = $grid;
     if (isset($config['query'])) {
         $queryConfig = array_intersect_key($config, array_flip(['query']));
         $converter = new YamlConverter();
         $this->qb = $converter->parse($queryConfig, $this->em->createQueryBuilder());
     } elseif (isset($config['entity']) && isset($config['repository_method'])) {
         $entity = $config['entity'];
         $method = $config['repository_method'];
         $repository = $this->em->getRepository($entity);
         if (method_exists($repository, $method)) {
             $qb = $repository->{$method}();
             if ($qb instanceof QueryBuilder) {
                 $this->qb = $qb;
             } else {
                 throw new DatasourceException(sprintf('%s::%s() must return an instance of Doctrine\\ORM\\QueryBuilder, %s given', get_class($repository), $method, is_object($qb) ? get_class($qb) : gettype($qb)));
             }
         } else {
             throw new DatasourceException(sprintf('%s has no method %s', get_class($repository), $method));
         }
     } else {
         throw new DatasourceException(get_class($this) . ' expects to be configured with query or repository method');
     }
     if (isset($config['hints'])) {
         $this->queryHints = $config['hints'];
     }
     $grid->setDatasource(clone $this);
 }
 /**
  * @param DatagridInterface $dataGrid
  * @return bool
  */
 public function isDatagridApplicable(DatagridInterface $dataGrid)
 {
     if (!$dataGrid->getDatasource() instanceof OrmDatasource) {
         return false;
     }
     return $dataGrid->getConfig()->offsetGetByPath(EntityPaginationExtension::ENTITY_PAGINATION_PATH) === true;
 }
 /**
  * {@inheritDoc}
  */
 public function process(DatagridInterface $grid, array $config)
 {
     $queryConfig = array_intersect_key($config, array_flip(['query']));
     $converter = new YamlConverter();
     $this->qb = $converter->parse($queryConfig, $this->em->createQueryBuilder());
     $grid->setDatasource(clone $this);
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function toArray()
 {
     if (null === $this->data) {
         $resultData = $this->datagrid->getData();
         $this->data = $resultData['data'];
     }
     return $this->data;
 }
 function it_processes_a_datasource_with_default_query_builder($manager, DatagridInterface $grid, ProductRepository $repository)
 {
     $config = ['entity' => 'Product'];
     $manager->getRepository('Product')->willReturn($repository);
     $repository->createQueryBuilder([])->shouldBeCalled();
     $grid->setDatasource($this)->shouldBeCalled();
     $this->process($grid, $config);
 }
 /**
  * @param DatagridInterface $dataGrid
  * @return string
  */
 protected function getEntityName(DatagridInterface $dataGrid)
 {
     /** @var OrmDatasource $dataSource */
     $dataSource = $dataGrid->getDatasource();
     $queryBuilder = $dataSource->getQueryBuilder();
     $entityName = $queryBuilder->getRootEntities()[0];
     return $this->doctrineHelper->getEntityMetadata($entityName)->getName();
 }
Esempio n. 7
0
 /**
  * Get grid mass action by name
  *
  * @param string           $name
  * @param DatagridInterface $datagrid
  *
  * @return bool|ActionInterface
  */
 public function getMassAction($name, DatagridInterface $datagrid)
 {
     $config = $datagrid->getAcceptor()->getConfig();
     $action = false;
     if (isset($config[static::ACTION_KEY][$name])) {
         $action = $this->getActionObject($name, $config[static::ACTION_KEY][$name]);
     }
     return $action;
 }
 function let(MassActionHandlerRegistry $handlerRegistry, ManagerInterface $manager, RequestParameters $requestParams, MassActionParametersParser $parametersParser, DatagridInterface $grid, Acceptor $acceptor, DatasourceInterface $acceptedDatasource, DatasourceInterface $datasource, QueryBuilder $queryBuilder)
 {
     $this->beConstructedWith($handlerRegistry, $manager, $requestParams, $parametersParser);
     $acceptedDatasource->getQueryBuilder()->willReturn($queryBuilder);
     $grid->getAcceptor()->willReturn($acceptor);
     $grid->getAcceptedDatasource()->willReturn($acceptedDatasource);
     $grid->getDatasource()->willReturn($datasource);
     $manager->getDatagrid('grid')->willReturn($grid);
 }
 function it_gives_the_label_of_a_filter($manager, $configurator, DatagridInterface $datagrid, Acceptor $acceptor, DatagridConfiguration $configuration)
 {
     $acceptor->getConfig()->willReturn($configuration);
     $datagrid->getAcceptor()->willReturn($acceptor);
     $manager->getDatagrid('product-grid')->willReturn($datagrid);
     $configurator->configure($configuration)->shouldBeCalled();
     $configuration->offsetGetByPath('[filters][columns][foo][label]')->willReturn('Foo');
     $this->filterLabel('foo')->shouldReturn('Foo');
 }
 /**
  * @test
  */
 public function testProcess()
 {
     $this->doctrineRegistry->expects($this->any())->method('getManager')->will($this->returnValue($this->manager));
     $this->manager->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($this->queryBuilder))->with($this->equalTo('a'));
     $this->grid->expects($this->once())->method('getParameters')->will($this->returnValue($this->parameterBag));
     $this->parameterBag->expects($this->once())->method('get')->will($this->returnValue(1));
     $this->grid->expects($this->once())->method('setDatasource');
     $this->combinedAuditDatasource->process($this->grid, []);
 }
 /**
  * @param DatagridInterface $datagrid
  *
  * @return array
  */
 protected function getEntityNameWithAlias(DatagridInterface $datagrid)
 {
     $fromItems = $datagrid->getConfig()->offsetGetByPath('[source][query][from]', false);
     if (empty($fromItems[0]['table'])) {
         return [$datagrid->getParameters()->get('class_name'), null];
     } else {
         $alias = empty($fromItems[0]['alias']) ? null : $fromItems[0]['alias'];
         return [$fromItems[0]['table'], $alias];
     }
 }
 function it_handles_export_mass_action($eventDispatcher, $hydrator, DatagridInterface $datagrid, DatasourceInterface $datasource, EditMassAction $massAction)
 {
     $objectIds = array('foo', 'bar', 'baz');
     $eventDispatcher->dispatch(MassActionEvents::MASS_EXPORT_PRE_HANDLER, Argument::type('Pim\\Bundle\\DataGridBundle\\Extension\\MassAction\\Event\\MassActionEvent'))->shouldBeCalled();
     $eventDispatcher->dispatch(MassActionEvents::MASS_EXPORT_POST_HANDLER, Argument::type('Pim\\Bundle\\DataGridBundle\\Extension\\MassAction\\Event\\MassActionEvent'))->shouldBeCalled();
     $datagrid->getDatasource()->willReturn($datasource);
     $datasource->setHydrator($hydrator)->shouldBeCalled();
     $datasource->getResults()->willReturn($objectIds);
     $this->handle($datagrid, $massAction)->shouldReturn($objectIds);
 }
 function it_configure_choices_and_skip_disallowed($manager, $configurator, DatagridInterface $datagrid, Acceptor $acceptor, DatagridConfiguration $configuration, OptionsResolver $resolver)
 {
     $acceptor->getConfig()->willReturn($configuration);
     $datagrid->getAcceptor()->willReturn($acceptor);
     $manager->getDatagrid('foo-grid')->willReturn($datagrid);
     $configurator->configure($configuration)->shouldBeCalled();
     $configuration->offsetGetByPath('[filters][columns]')->willReturn(['foobar' => ['label' => 'FooBar'], 'foo-1' => ['label' => 'Foo 1'], 'foo-2' => ['label' => 'Foo 2'], 'bar-1' => ['label' => 'Bar 1'], 'scope' => ['label' => 'Scope'], 'locale' => ['label' => 'Locale']]);
     $configuration->offsetGetByPath('[source][attributes_configuration]')->willReturn(['foo-1' => ['group' => 'foo'], 'foo-2' => ['group' => 'foo'], 'bar-1' => ['group' => 'bar']]);
     $resolver->setDefaults(['choices' => ['System' => ['foobar' => 'FooBar'], 'foo' => ['foo-1' => 'Foo 1', 'foo-2' => 'Foo 2'], 'bar' => ['bar-1' => 'Bar 1']]])->shouldBeCalled();
     $this->configureOptions($resolver);
 }
 function let(HydratorInterface $hydrator, TranslatorInterface $translator, EventDispatcherInterface $eventDispatcher, ProductManager $productManager, DatagridInterface $datagrid, DatasourceInterface $datasource, DeleteMassAction $massAction, ActionConfiguration $options, ProductMassActionRepository $massActionRepo)
 {
     $this->beConstructedWith($hydrator, $translator, $eventDispatcher, $productManager);
     $translator->trans('qux')->willReturn('qux');
     $datagrid->getDatasource()->willReturn($datasource);
     $datasource->setHydrator($hydrator)->shouldBeCalled();
     $datasource->getMassActionRepository()->willReturn($massActionRepo);
     // prepare mass action response
     $massAction->getOptions()->willReturn($options);
     $options->offsetGetByPath(Argument::cetera())->willReturn('qux');
 }
 /**
  * @param DatagridInterface $datagrid
  *
  * @return string|null
  */
 public function getEntityName(DatagridInterface $datagrid)
 {
     /** @var OrmDatasource $dataSource */
     $dataSource = $datagrid->getDatasource();
     if (!$dataSource) {
         return null;
     }
     /** @var QueryBuilder $queryBuilder */
     $queryBuilder = $dataSource->getQueryBuilder();
     $rootEntities = $queryBuilder->getRootEntities();
     return reset($rootEntities);
 }
 /**
  * @param array $rows
  *
  * @return array
  */
 protected function applyPagination(array $rows)
 {
     $originalTotals = count($rows);
     $this->grid->getConfig()->offsetAddToArrayByPath(static::PATH_PAGER_ORIGINAL_TOTALS, [$originalTotals]);
     if (count($rows) > $this->requestPagerParameters['_per_page']) {
         $offset = ($this->requestPagerParameters['_page'] - 1) * $this->requestPagerParameters['_per_page'];
         if ($offset < 0) {
             $offset = 0;
         }
         $rows = array_slice($rows, $offset, $this->requestPagerParameters['_per_page']);
     }
     return $rows;
 }
 /**
  * {@inheritdoc}
  */
 public function handle(DatagridInterface $datagrid, MassActionInterface $massAction)
 {
     // dispatch pre handler event
     $massActionEvent = new MassActionEvent($datagrid, $massAction, []);
     $this->eventDispatcher->dispatch(MassActionEvents::MASS_EXPORT_PRE_HANDLER, $massActionEvent);
     $datasource = $datagrid->getDatasource();
     $datasource->setHydrator($this->hydrator);
     $results = $datasource->getResults();
     // dispatch post handler event
     $massActionEvent = new MassActionEvent($datagrid, $massAction, $results);
     $this->eventDispatcher->dispatch(MassActionEvents::MASS_EXPORT_POST_HANDLER, $massActionEvent);
     return $results;
 }
 /**
  * {@inheritdoc}
  */
 public function handle(DatagridInterface $datagrid, MassActionInterface $massAction)
 {
     // dispatch pre handler event
     $massActionEvent = new MassActionEvent($datagrid, $massAction, array());
     $this->eventDispatcher->dispatch(MassActionEvents::MASS_EDIT_PRE_HANDLER, $massActionEvent);
     $datasource = $datagrid->getDatasource();
     $datasource->setHydrator($this->hydrator);
     // hydrator uses index by id
     $objectIds = $datasource->getResults();
     // dispatch post handler event
     $massActionEvent = new MassActionEvent($datagrid, $massAction, array());
     $this->eventDispatcher->dispatch(MassActionEvents::MASS_EDIT_POST_HANDLER, $massActionEvent);
     return $objectIds;
 }
 /**
  * {@inheritdoc}
  */
 public function process(DatagridInterface $grid, array $config)
 {
     $this->configuration = $config;
     if (isset($config['repository_method']) && ($method = $config['repository_method'])) {
         if (isset($config[ContextConfigurator::REPOSITORY_PARAMETERS_KEY])) {
             $this->qb = $this->getRepository()->{$method}($config[ContextConfigurator::REPOSITORY_PARAMETERS_KEY]);
         } else {
             $this->qb = $this->getRepository()->{$method}();
         }
     } else {
         $this->qb = $this->getRepository()->createQueryBuilder('o');
     }
     $grid->setDatasource(clone $this);
 }
Esempio n. 20
0
 /**
  * {@inheritdoc}
  */
 public function process(DatagridInterface $grid, array $config)
 {
     $this->configuration = $config;
     $queryBuilderConfig = [];
     if (isset($config['repository_method']) && ($method = $config['repository_method'])) {
         if (isset($config[ContextConfigurator::REPOSITORY_PARAMETERS_KEY])) {
             $queryBuilderConfig = $config[ContextConfigurator::REPOSITORY_PARAMETERS_KEY];
         }
     } else {
         $method = 'createQueryBuilder';
     }
     $this->initializeQueryBuilder($method, $queryBuilderConfig);
     $grid->setDatasource(clone $this);
 }
 function it_applies_when_user_preference_is_filled_and_skip_disallowed($context, UserInterface $user, DatagridInterface $datagrid, Acceptor $acceptor, DatagridConfiguration $config, BuildAfter $event)
 {
     $config->offsetGet('filters')->willReturn(['columns' => ['foo' => [], 'baz' => [], 'scope' => [], 'locale' => []]]);
     $config->offsetSetByPath('[filters][columns][foo][enabled]', true)->shouldBeCalled();
     $config->offsetSetByPath('[filters][columns][baz][enabled]', false)->shouldBeCalled();
     $config->offsetSetByPath('[filters][columns][bar][enabled]', Argument::any())->shouldNotBeCalled();
     $config->offsetSetByPath('[filters][columns][scope][enabled]', Argument::any())->shouldNotBeCalled();
     $config->offsetSetByPath('[filters][columns][locale][enabled]', Argument::any())->shouldNotBeCalled();
     $user->getProductGridFilters()->willReturn(['foo', 'bar']);
     $context->getUser()->willReturn($user);
     $acceptor->getConfig()->willReturn($config);
     $datagrid->getAcceptor()->willReturn($acceptor);
     $event->getDatagrid()->willReturn($datagrid);
     $this->onBuildAfter($event);
 }
Esempio n. 22
0
 /**
  * @return ResultsObject
  */
 protected function getGridData()
 {
     if (null !== $this->gridDataSource) {
         $this->grid->setDatasource(clone $this->gridDataSource);
     } else {
         $this->gridDataSource = clone $this->grid->getDatasource();
     }
     $this->grid->getParameters()->set(PagerInterface::PAGER_ROOT_PARAM, [PagerInterface::PAGE_PARAM => $this->page, PagerInterface::PER_PAGE_PARAM => $this->pageSize]);
     return $this->grid->getData();
 }
 /**
  * {@inheritdoc}
  */
 public function handle(DatagridInterface $datagrid, MassActionInterface $massAction)
 {
     // dispatch pre handler event
     $massActionEvent = new MassActionEvent($datagrid, $massAction, []);
     $this->eventDispatcher->dispatch(MassActionEvents::MASS_DELETE_PRE_HANDLER, $massActionEvent);
     $datasource = $datagrid->getDatasource();
     $datasource->setHydrator($this->hydrator);
     // hydrator uses index by id
     $objectIds = $datasource->getResults();
     try {
         $countRemoved = $datasource->getMassActionRepository()->deleteFromIds($objectIds);
     } catch (\Exception $e) {
         $errorMessage = $e->getMessage();
         return new MassActionResponse(false, $this->translator->trans($errorMessage));
     }
     // dispatch post handler event
     $massActionEvent = new MassActionEvent($datagrid, $massAction, $objectIds);
     $this->eventDispatcher->dispatch(MassActionEvents::MASS_DELETE_POST_HANDLER, $massActionEvent);
     return $this->getResponse($massAction, $countRemoved);
 }
Esempio n. 24
0
 /**
  * Binds datagrid parameters to datasource query.
  *
  * Example of usage:
  * <code>
  *  // get parameter "name" from datagrid parameter bag and add it to datasource query
  *  $queryParameterBinder->bindParameters($datagrid, ['name']);
  *
  *  // get parameter "id" from datagrid parameter bag and add it to datasource query as parameter "client_id"
  *  $queryParameterBinder->bindParameters($datagrid, ['client_id' => 'id']);
  *
  *  // get parameter "email" from datagrid parameter bag and add it to datasource query, all other existing query
  *  // parameters will be cleared
  *  $queryParameterBinder->bindParameters($datagrid, ['email'], false);
  *
  *  // get parameter with path "_parameters.data_in" from datagrid parameter
  *  // and add it to datasource query as parameter "data_in"
  *  $queryParameterBinder->bindParameters($datagrid, ['data_in' => '_parameters.data_in']);
  *
  *  // get parameter with path "_parameters.data_in" from datagrid parameter
  *  // and add it to datasource query as parameter "data_in",
  *  // if parameter is not exist, set default value - empty array,
  *  // and do the same for data_not_in
  *  $queryParameterBinder->bindParameters(
  *      $datagrid,
  *      [
  *          'data_in' => [
  *              'path' => '_parameters.data_in',
  *              'default' => [],
  *          ],
  *          [
  *              'name' => 'data_not_in'
  *              'path' => '_parameters.data_not_in',
  *              'default' => [],
  *          ]
  *      ]
  *  );
  * </code>
  *
  * @param DatagridInterface $datagrid
  * @param array $datasourceToDatagridParameters
  * @param bool $append
  * @throws InvalidArgumentException When datasource of grid is not ORM
  * @throws NoSuchPropertyException When datagrid has no parameter with specified name or path
  */
 public function bindParameters(DatagridInterface $datagrid, array $datasourceToDatagridParameters, $append = true)
 {
     if (!$datasourceToDatagridParameters) {
         return;
     }
     $datasource = $datagrid->getDatasource();
     if (!$datasource instanceof OrmDatasource) {
         throw new InvalidArgumentException(sprintf('Datagrid datasource has unexpected type "%s", "%s" is expected.', get_class($datasource), 'Oro\\Bundle\\DataGridBundle\\Datasource\\Orm\\OrmDatasource'));
     }
     /** @var QueryBuilder $queryBuilder */
     $queryBuilder = $datasource->getQueryBuilder();
     $queryParameters = $queryBuilder->getParameters();
     if (!$append) {
         $queryParameters->clear();
     }
     $datagridParameters = $datagrid->getParameters()->all();
     foreach ($datasourceToDatagridParameters as $index => $value) {
         $config = $this->parseArrayParameterConfig($index, $value);
         $value = $this->getParameterValue($datagridParameters, $config);
         $type = isset($config['type']) ? $config['type'] : null;
         $this->addOrReplaceParameter($queryParameters, new Parameter($config['name'], $value, $type));
     }
 }
Esempio n. 25
0
 /**
  * {@inheritDoc}
  */
 public function process(DatagridInterface $grid, array $config)
 {
     $this->query = new IndexerQuery($this->indexer, $this->indexer->select());
     $grid->setDatasource(clone $this);
 }
Esempio n. 26
0
 /**
  * @param string            $massActionName
  * @param DatagridInterface $datagrid
  *
  * @return \Oro\Bundle\DataGridBundle\Extension\MassAction\Actions\MassActionInterface
  * @throws LogicException
  */
 protected function getMassActionByName($massActionName, DatagridInterface $datagrid)
 {
     $massAction = null;
     $extensions = array_filter($datagrid->getAcceptor()->getExtensions(), function (ExtensionVisitorInterface $extension) {
         return $extension instanceof MassActionExtension;
     });
     /** @var MassActionExtension|bool $extension */
     $extension = reset($extensions);
     if ($extension === false) {
         throw new LogicException("MassAction extension is not applied to datagrid.");
     }
     $massAction = $extension->getMassAction($massActionName, $datagrid);
     if (!$massAction) {
         throw new LogicException(sprintf('Can\'t find mass action "%s"', $massActionName));
     }
     return $massAction;
 }
 /**
  * @param DatagridInterface $grid
  * @param array $config
  */
 public function process(DatagridInterface $grid, array $config)
 {
     $grid->setDatasource(clone $this);
 }
Esempio n. 28
0
 /**
  * {@inheritDoc}
  */
 public function process(DatagridInterface $grid, array $config)
 {
     $parameters = $grid->getParameters();
     $this->object = $this->routingHelper->getEntity($parameters->get('entityClass'), $parameters->get('entityId'));
     $grid->setDatasource(clone $this);
 }
Esempio n. 29
0
 /**
  * @param DatagridInterface $grid
  * @param array $params
  * @return string
  */
 protected function generateUrl(DatagridInterface $grid, $params)
 {
     $gridFullName = $this->nameStrategy->buildGridFullName($grid->getName(), $grid->getScope());
     return $this->router->generate(self::ROUTE, ['gridName' => $gridFullName, $gridFullName => $params]);
 }
Esempio n. 30
0
 /**
  * @param DatagridInterface $dataGrid
  * @return string
  */
 protected function generateStateHash(DatagridInterface $dataGrid)
 {
     $state = $dataGrid->getMetadata()->offsetGetByPath('[state]');
     $data = ['filters' => !empty($state['filters']) ? $state['filters'] : [], 'sorters' => !empty($state['sorters']) ? $state['sorters'] : []];
     return md5(json_encode($data));
 }