public function findWidgetPaths(array $userRoles, array $roots = array(), PathWidgetConfig $config = null) { $builder = new ResourceQueryBuilder(); $builder->selectAsEntity(false, 'Innova\\PathBundle\\Entity\\Path\\Path'); if (!empty($roots)) { $builder->whereRootIn($roots); } $builder->whereTypeIn(array('innova_path')); $builder->whereRoleIn($userRoles); // Add filters if defined if ($config) { // Add widget STATUS filters $statusList = $config->getStatus(); if (!empty($statusList)) { $whereStatus = array(); foreach ($statusList as $status) { switch ($status) { case 'draft': $whereStatus[] = 'node.published = 0'; break; case 'published': $whereStatus[] = '(node.published = 1 AND resource.modified = 0)'; break; case 'modified': $whereStatus[] = '(node.published = 1 AND resource.modified = 1)'; break; } } if (!empty($whereStatus)) { $builder->addWhereClause(implode($whereStatus, ' OR ')); } } // Add widget TAG filters $tagList = $config->getTags(); if (0 < count($tagList)) { $tags = array(); foreach ($tagList as $tag) { $tags[] = $tag->getId(); } // Join with the corresponding TaggedObject entities $builder->addJoinClause('LEFT JOIN ClarolineTagBundle:TaggedObject AS t WITH t.objectId = node.id'); $builder->addWhereClause('t.id IS NOT NULL'); $builder->addWhereClause('t.tag IN (' . implode($tags, ', ') . ')'); } } $builder->orderByName(); $dql = $builder->getDql(); $query = $this->_em->createQuery($dql); $query->setParameters($builder->getParameters()); $resources = $query->getResult(); return $resources; }
public function findAccessibleByUser(array $roots = array(), array $userRoles) { $builder = new ResourceQueryBuilder(); $builder->selectAsEntity(false, 'Innova\\PathBundle\\Entity\\Path\\Path'); if (!empty($roots)) { $builder->whereRootIn($roots); } $builder->whereTypeIn(array('innova_path')); $builder->whereRoleIn($userRoles); $builder->orderByName(); $dql = $builder->getDql(); $query = $this->_em->createQuery($dql); $query->setParameters($builder->getParameters()); $resources = $query->getResult(); return $resources; }
/** * Returns the immediate children of a resource that are openable by any of the given roles. * * @param ResourceNode $parent The id of the parent of the requested children * @param array $roles [string] $roles An array of roles * @param User $user the user opening * @param withLastOpenDate with the last openend node (with the last opened date) * * @throws \RuntimeException * @throw InvalidArgumentException if the array of roles is empty * * @return array[array] An array of resources represented as arrays */ public function findChildren(ResourceNode $parent, array $roles, $user, $withLastOpenDate = false) { if (count($roles) === 0) { throw new \RuntimeException('Roles cannot be empty'); } $builder = new ResourceQueryBuilder(); $returnedArray = array(); $isWorkspaceManager = $this->isWorkspaceManager($parent, $roles); //check if manager of the workspace. //if it's true, show every children if ($isWorkspaceManager) { $builder->selectAsArray()->whereParentIs($parent)->whereActiveIs(true)->orderByIndex(); $query = $this->_em->createQuery($builder->getDql()); $query->setParameters($builder->getParameters()); $items = $query->iterate(null, AbstractQuery::HYDRATE_ARRAY); foreach ($items as $key => $item) { $item[$key]['mask'] = 65535; $returnedArray[] = $item[$key]; } //otherwise only show visible children } else { $builder->selectAsArray(true)->whereParentIs($parent)->whereActiveIs(true)->whereHasRoleIn($roles)->whereIsAccessible($user); $query = $this->_em->createQuery($builder->getDql()); $query->setParameters($builder->getParameters()); $children = $this->executeQuery($query); $childrenWithMaxRights = array(); foreach ($children as $child) { if (!isset($childrenWithMaxRights[$child['id']])) { $childrenWithMaxRights[$child['id']] = $child; } foreach ($childrenWithMaxRights as $id => $childMaxRights) { if ($id === $child['id']) { $childrenWithMaxRights[$id]['mask'] |= $child['mask']; } } } $returnedArray = array(); foreach ($childrenWithMaxRights as $childMaxRights) { $returnedArray[] = $childMaxRights; } } //now we get the last open date for nodes. //We can't do one request because of the left join + max combination if ($withLastOpenDate && $user !== 'anon.') { $builder->selectAsArray(false, true)->whereParentIs($parent)->addLastOpenDate($user)->groupById(); if (!$isWorkspaceManager) { $builder->whereHasRoleIn($roles)->whereIsAccessible($user); } $query = $this->_em->createQuery($builder->getDql()); $query->setParameters($builder->getParameters()); $items = $this->executeQuery($query); foreach ($returnedArray as $key => $returnedElement) { foreach ($items as $item) { if ($item['id'] === $returnedElement['id']) { $returnedArray[$key]['last_opened'] = $item['last_opened']; } } } } return $returnedArray; }
/** * @expectedException Claroline\CoreBundle\Repository\Exception\MissingSelectClauseException */ public function testASelectClauseIsRequired() { $qb = new ResourceQueryBuilder(); $qb->getDql(); }