Get the list of entities allowed for the BusinessTemplate page.

@param BusinessTemplate $businessTemplate
@param EntityManager    $em

@throws \Exception

@return QueryBuilder
547173
public function getEntitiesAllowedQueryBuilder(BusinessTemplate $businessTemplate, EntityManager $em)
    {
        //the base of the query
        $baseQuery = $this->queryHelper->getQueryBuilder($businessTemplate, $em);

        // add this fake condition to ensure that there is always a "where" clause.
        // In query mode, usage of "AND" will be always valid instead of "WHERE"
        $baseQuery->andWhere('1 = 1');

        //filter with the query of the page
        return $this->queryHelper->buildWithSubQuery($businessTemplate, $baseQuery, $em);
    }
947383
private function getBusinessProperties(View $view)
    {
        $businessProperties = [];

        if ($view instanceof BusinessTemplate) {
            //we can use the business entity properties on the seo
            $businessEntity = $this->get('victoire_core.helper.business_entity_helper')->findById($view->getBusinessEntityId());
            $businessProperties = $businessEntity->getBusinessPropertiesByType('seoable');
        }

        return $businessProperties;
    }
403199
public function getAllClassNamesByEntityManager(EntityManagerInterface $em)
    {
        $hash = spl_object_hash($em);
        if (array_key_exists($hash, $this->entityManagerAllClassNames)) {
            return $this->entityManagerAllClassNames[$hash];
        }

        $this->entityManagerAllClassNames[$hash] = $em->getConfiguration()->getMetadataDriverImpl()->getAllClassNames();

        return $this->entityManagerAllClassNames[$hash];
    }
1275068
protected function buildCountQuery(EntityManager $em, Request $request)
	{
		$searchWords = preg_split('/[^[:alnum:]]+/', $request->getSearchText());
		$selects = array();
		foreach($this->databaseMapping as $tableMapping)
		{
			$score = $this->buildScoreColumn($tableMapping['columns'], count($searchWords));
			$joinsExpressions = $this->buildJoinExpressions($tableMapping['joins']);

			$selects[] = 'SELECT COUNT(*) '
					. 'FROM ' . $tableMapping['table'] . ' ' . $tableMapping['tableAlias'] . ' '
					. implode(' ', $joinsExpressions) . ' '
					. 'WHERE ' . $score['expression'] . '<>0 '
			;
		}
		$bigSelect = 'SELECT (' . implode(')+(', $selects) . ')';
		$stmt = $em->getConnection()->prepare($bigSelect);
		foreach($searchWords as $key => $searchWord)
		{
			$stmt->bindValue('p' . $key, $searchWord);
		}
		return $stmt;
	}
350047
public function getReferencableViews($views, EntityManager $em)
    {
        $referencableViews = [];
        if (!$views instanceof \Traversable && !is_array($views)) {
            $views = [$views];
        }

        $businessPages = $this->findBusinessPages($views);
        foreach ($views as $view) {
            if ($view instanceof BusinessTemplate) {
                $entities = $this->businessPageHelper->getEntitiesAllowed($view, $em);

                // for each business entity
                foreach ($entities as $k => $entity) {
                    $currentTemplate = clone $view;
                    $page = $this->businessPageBuilder->generateEntityPageFromTemplate($currentTemplate, $entity, $em);
                    $this->businessPageBuilder->updatePageParametersByEntity($page, $entity);
                    if (!array_key_exists(ViewReferenceHelper::generateViewReferenceId($page, $entity), $businessPages)) {
                        $referencableViews[] = ['view' => $page];
                    }
                }
            } elseif ($view instanceof WebViewInterface) {
                $referencableView = ['view' => $view];
                if ($view->getChildren()) {
                    $referencableView['children'] = $this->getReferencableViews($view->getChildren(), $em);
                }
                $referencableViews[] = $referencableView;
            }
        }

        return $referencableViews;
    }
320152
protected function getView(Entity $entity, array $params = [])
    {
        switch (true) {
            case $entity instanceof ContentItem:
                $template = $entity->isIndex() ? 'index.twig' : 'entry.twig';
                $params['entry'] = $entity;
                break;
            case $entity instanceof Taxonomy:
                $template = 'taxonomy.twig';
                $params['taxonomy'] = $entity;
                break;
            case $entity instanceof Term:
                $template = 'term.twig';
                $params['term'] = $entity;
                break;
            default:
                throw new \LogicException('Cannot render entity of type '.get_class($entity));
        }

        return new View($template, $params);
    }
1232975
public function setEntityManager(EntityManager $em)
    {
        if ($this->em !== null) {
            throw new \LogicException('Entity manager can only be set before any results are fetched');
        }

        $this->em = $em;
    }
1339681
public function getCount(EntityManager $em, Request $request)
	{
		$stmt = $this->buildCountQuery($em, $request);
		$stmt->execute();
		return (int) $stmt->fetchColumn();
	}
887755
public List<?> getList(PMContext ctx) throws PMException {
        final EntityFilter filter = (ctx != null && this.equals(ctx.getEntity())) ? ctx.getEntityContainer().getFilter() : null;
        return getList(ctx, filter, null, null, null);
    }
1145472
public function buildWithSubQuery(VictoireQueryInterface $containerEntity, QueryBuilder $itemsQueryBuilder, EntityManager $em)
    {
        //get the query of the container entity
        $query = $containerEntity->getQuery();
        if (method_exists($containerEntity, 'additionnalQueryPart')) {
            $query = $containerEntity->additionnalQueryPart();
        }

        if ($query !== '' && $query !== null) {
            $subQuery = $em->createQueryBuilder()
                                ->select('item.id')
                                ->from($itemsQueryBuilder->getRootEntities()[0], 'item');

            $itemsQueryBuilder->andWhere(
                sprintf('main_item.id IN (%s %s)', $subQuery->getQuery()->getDql(), $query)
            );
        }

        //Add ORDER BY if set
        if ($orderBy = json_decode($containerEntity->getOrderBy(), true)) {
            foreach ($orderBy as $addOrderBy) {
                $reflectionClass = new \ReflectionClass($itemsQueryBuilder->getRootEntities()[0]);
                $reflectionProperty = $reflectionClass->getProperty($addOrderBy['by']);

                //If ordering field is an association, treat it as a boolean
                if ($this->isAssociationField($reflectionProperty)) {
                    $itemsQueryBuilder->addSelect('CASE WHEN main_item.'.$addOrderBy['by'].' IS NULL THEN 0 ELSE 1 END AS HIDDEN caseOrder');
                    $itemsQueryBuilder->addOrderBy('caseOrder', $addOrderBy['order']);
                } else {
                    $itemsQueryBuilder->addOrderBy('main_item.'.$addOrderBy['by'], $addOrderBy['order']);
                }
            }
        }

        $currentView = $this->currentView;

        // If the current page is a BEP, we parse all its properties and inject them as query parameters
        if ($currentView() && $currentView() instanceof BusinessPage && null !== $currentEntity = $currentView()->getBusinessEntity()) {

            // NEW
            $metadatas = $em->getClassMetadata(get_class($currentEntity));
            foreach ($metadatas->fieldMappings as $fieldName => $field) {
                if (strpos($query, ':'.$fieldName) !== false) {
                    $itemsQueryBuilder->setParameter($fieldName, $metadatas->getFieldValue($currentEntity, $fieldName));
                }
            }
            foreach ($metadatas->associationMappings as $fieldName => $field) {
                if (strpos($query, ':'.$fieldName) !== false) {
                    $itemsQueryBuilder->setParameter($fieldName, $metadatas->getFieldValue($currentEntity, $fieldName)->getId());
                }
            }

            if (strpos($query, ':currentEntity') !== false) {
                $itemsQueryBuilder->setParameter('currentEntity', $currentEntity->getId());
            }
        } elseif ($currentView() instanceof BusinessTemplate && strpos($query, ':currentEntity') !== false) {
            $itemsQueryBuilder->setParameter('currentEntity', $containerEntity->getEntity()->getId());
        }

        if (strpos($query, ':currentUser') !== false && is_object($this->getCurrentUser())) {
            $itemsQueryBuilder->setParameter('currentUser', $this->getCurrentUser()->getId());
        }

        return $itemsQueryBuilder;
    }
106824
public static function getScheduledEntityInsertionsByClassname(EntityManager $em, $classnames)
    {
        $entities = array();
        $classnames = (array) $classnames;

        foreach ($em->getUnitOfWork()->getScheduledEntityInsertions() as $entity) {
            if (true === in_array(ClassUtils::getRealClass($entity), $classnames)) {
                $entities[] = $entity;
            }
        }

        return $entities;
    }
1296638
public function getBusiness($businessId, $parameters = [])
    {
        $path = $this->appendParametersToUrl('/v3/businesses/'.$businessId, $parameters);
        $request = $this->getRequest('GET', $path, $this->getDefaultHeaders());

        return $this->processRequest($request);
    }
473340
public function getQueryBuilder()
    {
        $queryBuilder = $this->entityManager->createQueryBuilder();
        $queryBuilder
            ->select('e')
            ->from($this->admin->getEntityClass(), 'e')
            ->orderBy('e.id', 'DESC');

        $expr = $queryBuilder->expr()->andX();

        /** @var DatagridFilter $filter */
        $i = 0;
        foreach ($this->filters as $filter) {
            $filterExprName = $filter->getExpression();
            // Value can be false but valid, it is only invalid when null.
            if ($filterExprName && !is_null($filter->getValue())) {
                $filterExpr = $queryBuilder->expr()->{$filterExprName}('e.'.$filter->getName(), ':param_'.$i);
                $queryBuilder->setParameter('param_'.$i, $filter->getValue());
                $expr->add($filterExpr);
                $i++;
            }
        }

        if ($expr->count() > 0) {
            $queryBuilder->where($expr);
        }

        return $queryBuilder;
    }
1001399
private function getResult($entity, Constraint $constraint, array $criteria, ObjectManager $em)
    {
        /** @var UniqueEntity $constraint */
        $filters = SqlFilterUtil::findFilters($em, (array) $constraint->filters, $constraint->allFilters);

        SqlFilterUtil::disableFilters($em, $filters);
        $repository = $em->getRepository(ClassUtils::getClass($entity));
        $result = $repository->{$constraint->repositoryMethod}($criteria);
        SqlFilterUtil::enableFilters($em, $filters);

        if (\is_array($result)) {
            reset($result);
        }

        return $result;
    }
722161
private function _getCreateEmptyJobTemplateContext(JobTemplate $jobTemplate)
    {
        $result = new HttpCallContext();
        $result->setMethod(Resources::HTTP_POST);
        $result->setHeaders($this->_batchHeaders);
        $result->setUri($this->getUri());
        $result->setPath('/JobTemplates');
        $result->setBody($this->wrapAtomEntry($jobTemplate));
        $result->addStatusCode(Resources::STATUS_CREATED);

        return $result;
    }
843714
public function getBusinessEntity()
    {
        //if there is no entity
        if ($this->businessEntity === null) {
            //if there is a proxy
            if ($this->getEntityProxy() !== null) {
                $this->businessEntity = $this->getEntityProxy()->getEntity($this->getBusinessEntityId());

                return $this->businessEntity;
            }
        }

        return $this->businessEntity;
    }
221143
private function getBusinessProperties(BusinessTemplate $view)
    {
        $businessTemplateHelper = $this->get('victoire_business_page.business_page_helper');
        //the business property link to the page
        $businessEntityId = $view->getBusinessEntityId();
        $businessEntity = $this->get('victoire_core.helper.business_entity_helper')->findById($businessEntityId);

        $businessProperties = $businessTemplateHelper->getBusinessProperties($businessEntity);

        return $businessProperties;
    }