/**
  * @param string $datagridName
  * @param string $actionName
  * @param array  $parameters
  * @param array  $data
  *
  * @throws \LogicException
  *
  * @return MassActionResponseInterface
  */
 public function dispatch($datagridName, $actionName, array $parameters, array $data = [])
 {
     $inset = true;
     if (isset($parameters['inset'])) {
         $inset = $parameters['inset'];
     }
     $values = [];
     if (isset($parameters['values'])) {
         $values = $parameters['values'];
     }
     $filters = [];
     if (isset($parameters['filters'])) {
         $filters = $parameters['filters'];
     }
     if ($inset && empty($values)) {
         throw new \LogicException(sprintf('There is nothing to do in mass action "%s"', $actionName));
     }
     // create datagrid
     $datagrid = $this->manager->getDatagrid($datagridName);
     // set filter data
     $this->requestParams->set(OrmFilterExtension::FILTER_ROOT_PARAM, $filters);
     // create mediator
     $massAction = $this->getMassActionByName($actionName, $datagrid);
     $identifier = $this->getIdentifierField($massAction);
     $qb = $this->getDatagridQuery($datagrid, $identifier, $inset, $values);
     $resultIterator = $this->getResultIterator($qb);
     $mediator = new MassActionMediator($massAction, $datagrid, $resultIterator, $data);
     // perform mass action
     $handle = $this->getMassActionHandler($massAction);
     $result = $handle->handle($mediator);
     return $result;
 }
 /**
  * @param string $code
  *
  * @return string
  */
 public function filterLabel($code)
 {
     $configuration = $this->manager->getDatagrid('product-grid')->getAcceptor()->getConfig();
     $this->configurator->configure($configuration);
     $label = $configuration->offsetGetByPath(sprintf('[filters][columns][%s][label]', $code));
     if (null === $label) {
         throw new \LogicException(sprintf('Attribute "%s" does not exists', $code));
     }
     return $label;
 }
 /**
  * Return the filter configured in the grid ($datagridName)
  *
  * @return array
  */
 protected function getSystemFilters()
 {
     $systemFilters = $this->datagridManager->getConfigurationForGrid($this->datagridName)->offsetGetByPath('[filters][columns]');
     $formattedSystemFilters = [];
     foreach ($systemFilters as $code => $systemFilter) {
         if (!in_array($code, $this->excludedFilters)) {
             $formattedSystemFilters['System'][$code] = $systemFilter['label'];
         }
     }
     return $formattedSystemFilters;
 }
 /**
  * Get datagrid column choices for the provided datagrid alias
  *
  * @param string  $alias
  * @param boolean $displayedColumns
  *
  * @return array
  */
 public function getColumnChoices($alias, $displayedColumns = false)
 {
     $choices = array();
     $path = true === $displayedColumns ? sprintf('[%s]', FormatterConfiguration::COLUMNS_KEY) : sprintf(ContextConfigurator::SOURCE_PATH, ContextConfigurator::AVAILABLE_COLUMNS_KEY);
     $columnsConfig = $this->datagridManager->getDatagrid($alias)->getAcceptor()->getConfig()->offsetGetByPath($path);
     if ($columnsConfig) {
         foreach ($columnsConfig as $code => $meta) {
             $choices[$code] = $meta['label'];
         }
     }
     return $choices;
 }
Exemplo n.º 5
0
 /**
  * @dataProvider datagridConfigurationProvider
  */
 public function testGetDatagridConfiguration($names, $expectedResolverCall, $expectedException)
 {
     if ($expectedException) {
         $this->setExpectedException('\\RuntimeException');
     }
     $this->resolver->expects($this->exactly($expectedResolverCall))->method('resolve')->will($this->returnArgument(1));
     foreach ($names as $name) {
         $result = $this->manager->getConfigurationForGrid($name);
         $this->assertInstanceOf('Oro\\Bundle\\DataGridBundle\\Datagrid\\Common\\DatagridConfiguration', $result);
         $this->assertEquals($name, $result->getName());
     }
 }
 /**
  * Get datagrid column choices for the provided datagrid alias
  *
  * @param string $alias
  * @param bool   $displayedColumns
  *
  * @return array
  */
 public function getColumnChoices($alias, $displayedColumns = false)
 {
     $choices = array();
     $path = true === $displayedColumns ? sprintf('[%s]', FormatterConfiguration::COLUMNS_KEY) : sprintf(ContextConfigurator::SOURCE_PATH, ContextConfigurator::AVAILABLE_COLUMNS_KEY);
     $columnsConfig = $this->datagridManager->getDatagrid($alias)->getAcceptor()->getConfig()->offsetGetByPath($path);
     if ($columnsConfig) {
         $properties = ['label', 'sortOrder', 'group', 'groupOrder'];
         foreach ($columnsConfig as $code => $meta) {
             $choice = ['code' => $code];
             foreach ($properties as $property) {
                 if (isset($meta[$property])) {
                     $choice[$property] = $meta[$property];
                 }
             }
             $choices[$code] = $choice;
         }
     }
     return $choices;
 }
 /**
  * {@inheritdoc}
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     $configuration = $this->manager->getDatagrid($this->datagrid)->getAcceptor()->getConfig();
     $this->configurator->configure($configuration);
     $attributes = $configuration->offsetGetByPath('[filters][columns]');
     $configs = $configuration->offsetGetByPath(sprintf(ConfiguratorInterface::SOURCE_PATH, ConfiguratorInterface::USEABLE_ATTRIBUTES_KEY));
     $choices = [];
     foreach ($attributes as $code => $filter) {
         if (in_array($code, $this->disallowed)) {
             continue;
         }
         $group = 'System';
         if (isset($configs[$code])) {
             $group = $configs[$code]['group'];
         }
         $choices[$group][$code] = $filter['label'];
     }
     $resolver->setDefaults(['choices' => $choices]);
 }
 /**
  * @dataProvider marketingListTypeDataProvider
  * @param string $type
  */
 public function testGetDataGridColumns($type)
 {
     /** @var \PHPUnit_Framework_MockObject_MockObject|MarketingList $marketingList */
     $marketingList = $this->getMockBuilder('OroCRM\\Bundle\\MarketingListBundle\\Entity\\MarketingList')->disableOriginalConstructor()->getMock();
     $marketingList->expects($this->any())->method('getId')->will($this->returnValue(1));
     $marketingList->expects($this->any())->method('isManual')->will($this->returnValue($type === MarketingListType::TYPE_MANUAL));
     $config = $this->getMockBuilder('Oro\\Bundle\\DataGridBundle\\Datagrid\\Common\\DatagridConfiguration')->disableOriginalConstructor()->getMock();
     $dataGrid = $this->getMock('Oro\\Bundle\\DataGridBundle\\Datagrid\\DatagridInterface');
     $dataGrid->expects($this->once())->method('getConfig')->will($this->returnValue($config));
     $this->dataGridManager->expects($this->atLeastOnce())->method('getDatagrid')->with(ConfigurationProvider::GRID_PREFIX . $marketingList->getId(), $this->logicalAnd($this->arrayHasKey('grid-mixin'), $this->callback(function ($other) use($type) {
         if ($type === MarketingListType::TYPE_MANUAL) {
             $mixin = MarketingListProvider::MANUAL_RESULT_ENTITIES_MIXIN;
         } else {
             $mixin = MarketingListProvider::RESULT_ENTITIES_MIXIN;
         }
         return $other['grid-mixin'] === $mixin;
     })))->will($this->returnValue($dataGrid));
     $dataGridConfiguration = $this->dataGridProvider->getDataGridConfiguration($marketingList);
     $this->assertEquals($config, $dataGridConfiguration);
 }
Exemplo n.º 9
0
 /**
  * @param MarketingList $marketingList
  * @param \PHPUnit_Framework_MockObject_MockObject $queryBuilder
  * @param \PHPUnit_Framework_MockObject_MockObject $dataGrid
  * @param null|string $mixin
  */
 protected function assertGetQueryBuilder(MarketingList $marketingList, $queryBuilder, $dataGrid, $mixin = null)
 {
     $dataSource = $this->getMockBuilder('Oro\\Bundle\\DataGridBundle\\Datasource\\Orm\\OrmDatasource')->disableOriginalConstructor()->getMock();
     $dataSource->expects($this->once())->method('getQueryBuilder')->will($this->returnValue($queryBuilder));
     $dataGrid->expects($this->once())->method('getAcceptedDatasource')->will($this->returnValue($dataSource));
     $parameters = [PagerInterface::PAGER_ROOT_PARAM => [PagerInterface::DISABLED_PARAM => true], TagsExtension::TAGS_ROOT_PARAM => [PagerInterface::DISABLED_PARAM => true]];
     if ($mixin) {
         $parameters['grid-mixin'] = $mixin;
     }
     $this->dataGridManager->expects($this->atLeastOnce())->method('getDatagrid')->with(ConfigurationProvider::GRID_PREFIX . $marketingList->getId(), $parameters)->will($this->returnValue($dataGrid));
 }
 /**
  * Creates a query builder based on the grid
  *
  * @param Request $request
  * @param string  $datagridName
  *
  * @return QueryBuilder
  *
  * @throws \LogicException
  */
 public function createQueryBuilder(Request $request, $datagridName)
 {
     $parameters = $this->parametersParser->parse($request);
     if ($parameters['inset'] && empty($parameters['values'])) {
         throw new \LogicException(sprintf('This request is empty'));
     }
     $datagrid = $this->manager->getDatagrid($datagridName);
     $this->requestParams->set(OrmFilterExtension::FILTER_ROOT_PARAM, $parameters['filters']);
     $datasource = $datagrid->getDatasource();
     if (!$datasource instanceof Datasource) {
         throw new \LogicException("Mass actions applicable only for datagrids with ORM datasource.");
     }
     $qb = $datagrid->getAcceptedDatasource()->getQueryBuilder();
     if (count($parameters['values'])) {
         $valueWhereCondition = $parameters['inset'] ? $qb->expr()->in('o.id', $parameters['values']) : $qb->expr()->notIn('o.id', $parameters['values']);
         $qb->andWhere($valueWhereCondition);
     }
     $qb->setMaxResults(null);
     $qb->setFirstResult(null);
     return $qb;
 }
Exemplo n.º 11
0
 /**
  * @param Segment $segment
  * @param null|string $mixin
  * @return DatagridInterface
  */
 protected function getSegmentDataGrid(Segment $segment, $mixin = null)
 {
     $dataGridName = $segment->getGridPrefix() . $segment->getId();
     $resultKey = $dataGridName . $mixin;
     if (empty($this->dataGrid[$resultKey])) {
         $gridParameters = array(PagerInterface::PAGER_ROOT_PARAM => array(PagerInterface::DISABLED_PARAM => true));
         if ($mixin) {
             $gridParameters['grid-mixin'] = $mixin;
         }
         $this->dataGrid[$resultKey] = $this->dataGridManager->getDatagrid($dataGridName, $gridParameters);
     }
     return $this->dataGrid[$resultKey];
 }
Exemplo n.º 12
0
 /**
  * @param MarketingList $marketingList
  * @param null|string $mixin
  *
  * @return DatagridInterface
  */
 protected function getMarketingListDataGrid(MarketingList $marketingList, $mixin = null)
 {
     $dataGridName = ConfigurationProvider::GRID_PREFIX . $marketingList->getId();
     $resultKey = $dataGridName . $mixin;
     if (empty($this->dataGrid[$resultKey])) {
         $gridParameters = [PagerInterface::PAGER_ROOT_PARAM => [PagerInterface::DISABLED_PARAM => true], TagsExtension::TAGS_ROOT_PARAM => [TagsExtension::DISABLED_PARAM => true]];
         if ($mixin) {
             $gridParameters['grid-mixin'] = $mixin;
         }
         $this->dataGrid[$resultKey] = $this->dataGridManager->getDatagrid($dataGridName, $gridParameters);
     }
     return $this->dataGrid[$resultKey];
 }
Exemplo n.º 13
0
 /**
  * @param string $datagridName
  * @param string $actionName
  * @param array  $parameters
  * @param array  $data
  *
  * @throws LogicException
  *
  * @return MassActionResponseInterface
  */
 public function dispatch($datagridName, $actionName, array $parameters, array $data = [])
 {
     $inset = true;
     if (isset($parameters['inset'])) {
         $inset = $parameters['inset'];
     }
     $values = [];
     if (isset($parameters['values'])) {
         $values = $parameters['values'];
     }
     $filters = [];
     if (isset($parameters['filters'])) {
         $filters = $parameters['filters'];
     }
     if ($inset && empty($values)) {
         throw new LogicException(sprintf('There is nothing to do in mass action "%s"', $actionName));
     }
     // create datagrid
     $datagrid = $this->manager->getDatagridByRequestParams($datagridName);
     // set filter data
     $datagrid->getParameters()->mergeKey(OrmFilterExtension::FILTER_ROOT_PARAM, $filters);
     // create mediator
     $massAction = $this->getMassActionByName($actionName, $datagrid);
     $identifier = $this->getIdentifierField($massAction);
     $qb = $this->getDatagridQuery($datagrid, $identifier, $inset, $values);
     //prepare query builder
     $qb->setMaxResults(null);
     if (!$datagrid->getConfig()->offsetGetByPath(Builder::DATASOURCE_SKIP_ACL_CHECK, false)) {
         $qb = $this->aclHelper->apply($qb);
     }
     $resultIterator = $this->getResultIterator($qb);
     $handlerArgs = new MassActionHandlerArgs($massAction, $datagrid, $resultIterator, $data);
     // perform mass action
     $handler = $this->getMassActionHandler($massAction);
     $result = $handler->handle($handlerArgs);
     return $result;
 }
 /**
  * Get the context (locale and scope) from the datagrid
  *
  * @throws \LogicException If datasource is not a ProductDatasource
  *
  * @return string[] Returns [] || ['locale' => 'en_US', 'scope' => 'mobile']
  */
 protected function getContextParameters()
 {
     $datagridName = $this->request->get('gridName');
     $datagrid = $this->datagridManager->getDatagrid($datagridName);
     $dataSource = $datagrid->getDatasource();
     if (!$dataSource instanceof ProductDatasource) {
         throw new \LogicException('getContextParameters is only implemented for ProductDatasource');
     }
     $dataSourceParams = $dataSource->getParameters();
     $contextParams = [];
     if (is_array($dataSourceParams)) {
         $contextParams = ['locale' => $dataSourceParams['dataLocale'], 'scope' => $dataSourceParams['scopeCode']];
     }
     return $contextParams;
 }
 /**
  * Returns grid metadata array
  *
  * @param string $name
  * @param array  $params
  *
  * @return \stdClass
  */
 public function getGridMetadata($name, $params = [])
 {
     $metaData = $this->manager->getDatagrid($name)->getMetadata();
     $metaData->offsetAddToArray('options', ['url' => $this->generateUrl($name, $params)]);
     return $metaData->toArray();
 }
Exemplo n.º 16
0
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage $parameters must be an array or instance of ParameterBag.
  */
 public function testGetDatagridThrowsException()
 {
     $datagridName = 'test_grid';
     $parameters = new \stdClass();
     $this->manager->getDatagrid($datagridName, $parameters);
 }