/**
  * Executes a DQL query and returns resources as entities or arrays.
  * If it returns arrays, it add a "pathfordisplay" field to each item.
  *
  * @param Query   $query   The query to execute
  * @param integer $offset  First row to start with
  * @param integer $numrows Maximum number of rows to return
  * @param boolean $asArray Whether the resources must be returned as arrays or as objects
  *
  * @return array[AbstractResource|array]
  */
 private function executeQuery($query, $offset = null, $numrows = null, $asArray = true)
 {
     $query->setFirstResult($offset);
     $query->setMaxResults($numrows);
     if ($asArray) {
         $resources = $query->getArrayResult();
         $return = $resources;
         // Add a field "pathfordisplay" in each entity (as array) of the given array.
         foreach ($resources as $key => $resource) {
             if (isset($resource['path'])) {
                 $return[$key]['path_for_display'] = ResourceNode::convertPathForDisplay($resource['path']);
             }
         }
         return $return;
     }
     return $query->getResult();
 }
 /**
  * @EXT\Route(
  *     "directory/{nodeId}",
  *     name="claro_resource_directory",
  *     options={"expose"=true},
  *     defaults={"nodeId"=0}
  * )
  * @EXT\ParamConverter(
  *      "node",
  *      class="ClarolineCoreBundle:Resource\ResourceNode",
  *      options={"id" = "nodeId", "strictId" = true}
  * )
  *
  * Returns a json representation of a directory, containing the following items :
  * - The path of the directory
  * - The resource types the user is allowed to create in the directory
  * - The immediate children resources of the directory which are visible for the user
  *
  * If the directory id is '0', a pseudo-directory containing the root directories
  * of the workspaces whose the user is a member is returned.
  * If the directory id is a shortcut id, the directory targeted by the shortcut
  * is returned.
  *
  * @param ResourceNode $node the directory node
  *
  * @return \Symfony\Component\HttpFoundation\Response
  *
  * @throws Exception if the id doesn't match any existing directory
  */
 public function openDirectoryAction(ResourceNode $node = null)
 {
     $user = $this->tokenStorage->getToken()->getUser();
     $path = array();
     $creatableTypes = array();
     $currentRoles = $this->roleManager->getStringRolesFromToken($this->tokenStorage->getToken());
     $canChangePosition = false;
     $nodesWithCreatorPerms = array();
     if ($node === null) {
         $nodes = $this->resourceManager->getRoots($user);
         $isRoot = true;
         $workspaceId = 0;
         foreach ($nodes as $el) {
             $item = $el;
             $dateModification = $el['modification_date'];
             $item['modification_date'] = $dateModification->format($this->translator->trans('date_range.format.with_hours', array(), 'platform'));
             $dateCreation = $el['creation_date'];
             $item['creation_date'] = $dateCreation->format($this->translator->trans('date_range.format.with_hours', array(), 'platform'));
             $nodesWithCreatorPerms[] = $item;
         }
     } else {
         $isRoot = false;
         $workspaceId = $node->getWorkspace()->getId();
         $isPws = $node->getWorkspace()->isPersonal();
         $node = $this->getRealTarget($node);
         $collection = new ResourceCollection(array($node));
         $this->checkAccess('OPEN', $collection);
         if ($user !== 'anon.') {
             if ($user === $node->getCreator() || $this->authorization->isGranted('ROLE_ADMIN')) {
                 $canChangePosition = true;
             }
         }
         $path = $this->resourceManager->getAncestors($node);
         $nodes = $this->resourceManager->getChildren($node, $currentRoles, $user, true);
         //set "admin" mask if someone is the creator of a resource or the resource workspace owner.
         //if someone needs admin rights, the resource type list will go in this array
         $adminTypes = [];
         $isOwner = $this->resourceManager->isWorkspaceOwnerOf($node, $this->tokenStorage->getToken());
         if ($isOwner || $this->authorization->isGranted('ROLE_ADMIN')) {
             $resourceTypes = $this->resourceManager->getAllResourceTypes();
             foreach ($resourceTypes as $resourceType) {
                 $adminTypes[$resourceType->getName()] = $this->translator->trans($resourceType->getName(), array(), 'resource');
             }
         }
         $enableRightsEdition = true;
         if ($isPws && !$this->rightsManager->canEditPwsPerm($this->tokenStorage->getToken())) {
             $enableRightsEdition = false;
         }
         //get the file list in that directory to know their size.
         $files = $this->fileManager->getDirectoryChildren($node);
         foreach ($nodes as $el) {
             $item = $el;
             if ($user !== 'anon.') {
                 if ($item['creator_username'] === $user->getUsername() && !$this->isUsurpatingWorkspaceRole($this->tokenStorage->getToken())) {
                     $item['mask'] = 32767;
                 }
             }
             $item['new'] = true;
             $item['enableRightsEdition'] = $enableRightsEdition;
             $dateModification = $el['modification_date'];
             $item['modification_date'] = $dateModification->format($this->translator->trans('date_range.format.with_hours', array(), 'platform'));
             $dateCreation = $el['creation_date'];
             $item['timestamp_last_modification'] = $dateModification->getTimeStamp();
             if (isset($el['last_opened'])) {
                 $item['last_opened'] = $el['last_opened']->getTimeStamp();
                 if ($item['last_opened'] >= $item['timestamp_last_modification']) {
                     $item['new'] = false;
                 }
             }
             $item['creation_date'] = $dateCreation->format($this->translator->trans('date_range.format.with_hours', array(), 'platform'));
             foreach ($files as $file) {
                 if ($file->getResourceNode()->getId() === $el['id']) {
                     $item['size'] = $file->getFormattedSize();
                 }
             }
             $nodesWithCreatorPerms[] = $item;
         }
         $creatableTypes = $this->rightsManager->getCreatableTypes($currentRoles, $node);
         $creatableTypes = array_merge($creatableTypes, $adminTypes);
         asort($creatableTypes);
         $this->dispatcher->dispatch('log', 'Log\\LogResourceRead', array($node));
     }
     $directoryId = $node ? $node->getId() : '0';
     if ($this->request->query->has('keep-id')) {
         $this->request->getSession()->set('pickerDirectoryId', $directoryId);
     }
     foreach ($nodesWithCreatorPerms as &$element) {
         $element['path_for_display'] = ResourceNode::convertPathForDisplay($element['path']);
     }
     $jsonResponse = new JsonResponse(array('id' => $directoryId, 'path' => $path, 'creatableTypes' => $creatableTypes, 'nodes' => $nodesWithCreatorPerms, 'canChangePosition' => $canChangePosition, 'workspace_id' => $workspaceId, 'is_root' => $isRoot));
     $jsonResponse->headers->set('Cache-Control', 'no-cache, no-store, must-revalidate');
     $jsonResponse->headers->add(array('Expires' => '-1'));
     return $jsonResponse;
 }