/**
  * @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;
 }
Example #2
0
 /**
  * {@inheritDoc}
  */
 public function getDatagrid($name)
 {
     // prepare for work with current grid
     $this->requestParams->setRootParameter($name);
     $config = $this->getConfigurationForGrid($name);
     $datagrid = $this->getDatagridBuilder()->build($config);
     return $datagrid;
 }
 /**
  * @return GroupInterface
  */
 protected function getGroup()
 {
     $groupId = $this->request->get('id', null);
     if (!$groupId) {
         $groupId = $this->requestParams->get('currentGroup', null);
     }
     $group = $this->groupRepository->find($groupId);
     return $group;
 }
 /**
  * {@inheritdoc}
  */
 public function apply(DatasourceInterface $datasource, $field, $direction)
 {
     $groupId = $this->requestParams->get('currentGroup', null);
     if (!$groupId) {
         throw new \LogicException('The current product group must be configured');
     }
     $field = 'in_group_' . $groupId;
     $datasource->getProductQueryBuilder()->addSorter($field, $direction);
 }
 /**
  * {@inheritdoc}
  */
 protected function prepareQuery(QueryBuilder $query, $rootAlias, $alias, $itemsType)
 {
     $entityId = $this->requestParams->get(self::ENTITY_PARAM, 0);
     $query->where($rootAlias . '.mode <> :mode');
     $query->setParameter('mode', ConfigModelManager::MODE_HIDDEN);
     $query->innerJoin($rootAlias . '.entity', 'ce', 'WITH', 'ce.id=' . $entityId);
     $query->addSelect('ce.id as entity_id');
     return parent::prepareQuery($query, $rootAlias, $alias, $itemsType);
 }
 /**
  * Add locale parameter to the querybuilder
  *
  * @param BuildAfter $event
  */
 public function onBuildAfter(BuildAfter $event)
 {
     $datagrid = $event->getDatagrid();
     $datasource = $datagrid->getDatasource();
     $config = $datagrid->getAcceptor()->getConfig();
     $localeParameter = $config->offsetGetByPath(self::LOCALE_PARAMETER);
     if ($localeParameter && $datasource instanceof Datasource) {
         $queryBuilder = $datasource->getQueryBuilder();
         $queryBuilder->setParameter($localeParameter, $this->requestParams->get($localeParameter, null));
     }
 }
 /**
  * Adjust query for tag-results-grid (tag search result grid)
  * after datasource has been built
  *
  * @param BuildAfter $event
  */
 public function onBuildAfter(BuildAfter $event)
 {
     $datasource = $event->getDatagrid()->getDatasource();
     if ($datasource instanceof OrmDatasource) {
         /** @var QueryBuilder $query */
         $queryBuilder = $datasource->getQueryBuilder();
         $queryBuilder->setParameter('tag', $this->requestParams->get('tag_id', 0));
         $searchEntity = $this->requestParams->get('from', '*');
         if ($searchEntity != '*' && !empty($searchEntity)) {
             $queryBuilder->andWhere('tt.alias = :alias')->setParameter('alias', $searchEntity);
         }
     }
 }
 /**
  * Adjust query for tag-results-grid (tag search result grid)
  * after datasource has been built
  *
  * @param BuildAfter $event
  */
 public function onBuildAfter(BuildAfter $event)
 {
     $datasource = $event->getDatagrid()->getDatasource();
     if ($datasource instanceof SearchDatasource) {
         /** @var $query Query */
         $query = new IndexerQuery($this->indexer, $this->indexer->select());
         $searchEntity = $this->requestParams->get('from', '*');
         $searchEntity = empty($searchEntity) ? '*' : $searchEntity;
         $searchString = $this->requestParams->get('search', '');
         $query->from($searchEntity)->andWhere(Indexer::TEXT_ALL_DATA_FIELD, '~', $searchString, 'text');
         $datasource->setQuery($query);
     }
 }
 /**
  * Dispatch datagrid mass action
  *
  * @param Request $request
  *
  * @throws \LogicException
  *
  * @return MassActionResponseInterface
  */
 public function dispatch(Request $request)
 {
     $parameters = $this->parametersParser->parse($request);
     $datagridName = $request->get('gridName');
     $actionName = $request->get('actionName');
     $inset = isset($parameters['inset']) ? $parameters['inset'] : true;
     $values = isset($parameters['values']) ? $parameters['values'] : [];
     $filters = isset($parameters['filters']) ? $parameters['filters'] : [];
     if ($inset && empty($values)) {
         throw new \LogicException(sprintf('There is nothing to do in mass action "%s"', $actionName));
     }
     $datagrid = $this->manager->getDatagrid($datagridName);
     $massAction = $this->getMassActionByName($actionName, $datagrid);
     $this->requestParams->set(FilterExtension::FILTER_ROOT_PARAM, $filters);
     return $this->performMassAction($datagrid, $massAction, $inset, $values);
 }
 /**
  * Dispatch datagrid mass action
  *
  * @param Request $request
  *
  * @throws \LogicException
  *
  * @return array
  */
 protected function prepareMassActionParameters(Request $request)
 {
     $parameters = $this->parametersParser->parse($request);
     $inset = $this->prepareInsetParameter($parameters);
     $values = $this->prepareValuesParameter($parameters);
     $filters = $this->prepareFiltersParameter($parameters);
     $actionName = $request->get('actionName');
     if ($inset && empty($values)) {
         throw new \LogicException(sprintf('There is nothing to do in mass action "%s"', $actionName));
     }
     $datagridName = $request->get('gridName');
     $datagrid = $this->manager->getDatagrid($datagridName);
     $massAction = $this->getMassActionByName($actionName, $datagrid);
     $this->requestParams->set(FilterExtension::FILTER_ROOT_PARAM, $filters);
     $qb = $datagrid->getAcceptedDatasource()->getQueryBuilder();
     if (self::FAMILY_GRID_NAME === $datagridName) {
         $qbLocaleParameter = $qb->getParameter('localeCode');
         if (null !== $qbLocaleParameter && null === $qbLocaleParameter->getValue()) {
             $qb->setParameter('localeCode', $request->query->get('dataLocale'));
         }
     }
     $repository = $datagrid->getDatasource()->getMassActionRepository();
     $repository->applyMassActionParameters($qb, $inset, $values);
     return ['datagrid' => $datagrid, 'massAction' => $massAction, 'inset' => $inset, 'values' => $values];
 }
 /**
  * Get user configured datagrid columns
  *
  * @return null|string[]
  */
 protected function getUserGridColumns()
 {
     $params = $this->requestParams->get(RequestParameters::ADDITIONAL_PARAMETERS);
     if (isset($params['view']) && isset($params['view']['columns'])) {
         return explode(',', $params['view']['columns']);
     }
     return null;
 }
 /**
  * @param BuildAfter $event
  */
 public function onBuildAfter(BuildAfter $event)
 {
     $datasource = $event->getDatagrid()->getDatasource();
     if ($datasource instanceof OrmDatasource) {
         /** @var QueryBuilder $query */
         $queryBuilder = $datasource->getQueryBuilder();
         $queryParameters = array('objectClass' => str_replace('_', '\\', $this->requestParams->get(self::GRID_PARAM_CLASS, '')));
         if (in_array('objectId', $this->paramsToBind)) {
             $queryParameters['objectId'] = $this->requestParams->get(self::GRID_PARAM_OBJECT_ID, 0);
         }
         $fieldName = $this->requestParams->get(self::GRID_PARAM_FIELD_NAME, false);
         if (!empty($fieldName) && in_array('fieldName', $this->paramsToBind)) {
             $queryParameters['fieldName'] = $fieldName;
         }
         $queryBuilder->setParameters($queryParameters);
     }
 }
 /**
  * Trying to get request param
  * - first from current request query
  * - then from master request attributes
  *
  * @param $paramName
  * @param bool $default
  * @return mixed
  */
 protected function getRequestParam($paramName, $default = false)
 {
     $paramValue = $this->requestParams->get($paramName, $default);
     if ($paramValue === false) {
         $paramNameCamelCase = str_replace(' ', '', lcfirst(ucwords(str_replace('_', ' ', $paramName))));
         $paramValue = $this->request->attributes->get($paramNameCamelCase, $default);
     }
     return $paramValue;
 }
 /**
  * @return array
  */
 protected function prepareParameters()
 {
     $queryParameters = [];
     foreach ($this->paramNames as $paramName) {
         $queryParameters[$paramName] = $this->requestParams->get($paramName, null);
     }
     if ($this->isEditMode) {
         $params = $this->requestParams->get(RequestParameters::ADDITIONAL_PARAMETERS);
         $queryParameters[self::GRID_PARAM_DATA_IN] = isset($params[self::GRID_PARAM_DATA_IN]) ? $params[self::GRID_PARAM_DATA_IN] : [0];
         $queryParameters[self::GRID_PARAM_DATA_NOT_IN] = isset($params[self::GRID_PARAM_DATA_NOT_IN]) ? $params[self::GRID_PARAM_DATA_NOT_IN] : [0];
         foreach ($this->paramNames as $paramName) {
             if (isset($params[$paramName])) {
                 $queryParameters[$paramName] = $params[$paramName];
             }
         }
     }
     return $queryParameters;
 }
 /**
  * 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;
 }
 public function onBuildAfter(BuildAfter $event)
 {
     $datasource = $event->getDatagrid()->getDatasource();
     if ($datasource instanceof OrmDatasource) {
         /** @var QueryBuilder $query */
         $queryBuilder = $datasource->getQueryBuilder();
         $this->queryFactory->prepareQuery($queryBuilder);
         if ($id = $this->requestParams->get('userId')) {
             $user = $this->em->getRepository('OroUserBundle:User')->find($id);
             // TODO: select imap configuration by userId
             $origin = $user->getImapConfiguration();
             $originId = $origin !== null ? $origin->getId() : 0;
             if (array_key_exists('refresh', $this->requestParams->get(RequestParameters::ADDITIONAL_PARAMETERS)) && $originId) {
                 $this->imapSync->syncOrigins(array($originId));
             }
         } else {
             $originId = 0;
             // to make sure param bind passed
         }
         $queryBuilder->setParameter('origin_id', $originId);
     }
 }
 /**
  * @param BuildBefore $event
  */
 public function onBuildBefore(BuildBefore $event)
 {
     $config = $event->getConfig();
     $repositoryParameters = array('objectClass' => str_replace('_', '\\', $this->requestParams->get(self::GRID_PARAM_CLASS, '')), 'objectId' => $this->requestParams->get(self::GRID_PARAM_OBJECT_ID, 0));
     $config->offsetSetByPath(sprintf(ContextConfigurator::SOURCE_PATH, ContextConfigurator::REPOSITORY_PARAMETERS_KEY), $repositoryParameters);
 }
 /**
  * {@inheritdoc}
  */
 public function getDatagridParameter($key, $defaultValue = null)
 {
     return $this->requestParams->get($key, $defaultValue);
 }
 /**
  * Get the object class parameter from the request.
  * It can be an empty string, a entity type (eg product, group, attribute) or an FQCN
  *
  * @return string
  */
 protected function getObjectClassParameter()
 {
     return $this->requestParams->get(self::GRID_PARAM_CLASS, '');
 }
 /**
  * @param string $name
  * @param array  $params
  * @param bool   $mixRequest
  *
  * @return string
  */
 protected function generateUrl($name, $params, $mixRequest = false)
 {
     $additional = $mixRequest ? $this->requestParams->getRootParameterValue() : [];
     $params = [$name => array_merge($params, $additional), 'gridName' => $name];
     return $this->router->generate(self::ROUTE, $params);
 }