Ejemplo n.º 1
0
    public function previewContentAction( $contentId, $versionNo, $language, $siteAccessName = null )
    {
        try
        {
            $content = $this->contentService->loadContent( $contentId, array( $language ), $versionNo );
            $location = $this->previewHelper->getPreviewLocation( $contentId );
        }
        catch ( UnauthorizedException $e )
        {
            throw new AccessDeniedException();
        }

        if ( !$this->securityContext->isGranted( new AuthorizationAttribute( 'content', 'versionread', array( 'valueObject' => $content ) ) ) )
        {
            throw new AccessDeniedException();
        }

        $siteAccess = $this->previewHelper->getOriginalSiteAccess();
        // Only switch if $siteAccessName is set and different from original
        if ( $siteAccessName !== null && $siteAccessName !== $siteAccess->name )
        {
            $siteAccess = $this->previewHelper->changeConfigScope( $siteAccessName );
        }

        $response = $this->kernel->handle(
            $this->getForwardRequest( $location, $content, $siteAccess ),
            HttpKernelInterface::SUB_REQUEST
        );
        $response->headers->remove( 'cache-control' );
        $response->headers->remove( 'expires' );

        $this->previewHelper->restoreConfigScope();

        return $response;
    }
Ejemplo n.º 2
0
 public function findNodes(LocationQuery $query)
 {
     $searchResult = $this->searchService->findLocations($query, ['languages' => $this->prioritizedLanguages, 'useAlwaysAvailable' => $this->useAlwaysAvailable]);
     foreach ($searchResult->searchHits as $searchHit) {
         /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
         $location = $searchHit->valueObject;
         $searchHit->valueObject = $this->domainObjectMapper->mapNode($location, $this->contentService->loadContent($location->contentInfo->id, [$searchHit->matchedTranslation], $location->contentInfo->currentVersionNo), $searchHit->matchedTranslation);
     }
     return $searchResult;
 }
 /**
  * Used by the REST API to reference downloadable files.
  * It redirects (permanently) to the standard ez_content_download route, based on the language of the field
  * passed as an argument, using the language switcher.
  *
  * @param mixed $contentId
  * @param int $fieldId
  * @param \Symfony\Component\HttpFoundation\Request $request
  *
  * @return \Symfony\Component\HttpFoundation\RedirectResponse
  */
 public function redirectToContentDownloadAction($contentId, $fieldId, Request $request)
 {
     $content = $this->contentService->loadContent($contentId);
     $field = $this->findFieldInContent($fieldId, $content);
     $params = array('content' => $content, 'fieldIdentifier' => $field->fieldDefIdentifier, 'language' => $field->languageCode);
     if ($request->query->has('version')) {
         $params['version'] = $request->query->get('version');
     }
     $downloadUrl = $this->router->generate($this->routeReferenceGenerator->generate('ez_content_download', $params));
     return new RedirectResponse($downloadUrl, 301);
 }
Ejemplo n.º 4
0
 /**
  * @param mixed $contentId ID of a valid Content
  * @param string $fieldIdentifier Field Definition identifier of the Field the file must be downloaded from
  * @param string $filename
  * @param \Symfony\Component\HttpFoundation\Request $request
  *
  * @return \eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse
  */
 public function downloadBinaryFileAction($contentId, $fieldIdentifier, $filename, Request $request)
 {
     if ($request->query->has('version')) {
         $content = $this->contentService->loadContent($contentId, null, $request->query->get('version'));
     } else {
         $content = $this->contentService->loadContent($contentId);
     }
     $field = $this->translationHelper->getTranslatedField($content, $fieldIdentifier, $request->query->has('inLanguage') ? $request->query->get('inLanguage') : null);
     if (!$field instanceof Field) {
         throw new InvalidArgumentException("'{$fieldIdentifier}' field not present on content #{$content->contentInfo->id} '{$content->contentInfo->name}'");
     }
     $response = new BinaryStreamResponse($this->ioService->loadBinaryFile($field->value->id), $this->ioService);
     $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename);
     return $response;
 }
Ejemplo n.º 5
0
 /**
  * Returns Site Node object for the given Repository $location.
  *
  * @throws \Netgen\EzPlatformSiteApi\Core\Site\Exceptions\TranslationNotMatchedException
  *
  * @param \eZ\Publish\API\Repository\Values\Content\Location $location
  *
  * @return \Netgen\EzPlatformSiteApi\API\Values\Node
  */
 private function getSiteNode(APILocation $location)
 {
     $versionInfo = $this->contentService->loadVersionInfoById($location->contentInfo->id);
     $languageCode = $this->getLanguage($versionInfo->languageCodes, $versionInfo->contentInfo->mainLanguageCode, $versionInfo->contentInfo->alwaysAvailable);
     if ($languageCode === null) {
         throw new TranslationNotMatchedException($versionInfo->contentInfo->id, $this->getContext($versionInfo));
     }
     return $this->domainObjectMapper->mapNode($location, $this->contentService->loadContent($location->contentInfo->id, [$languageCode], $location->contentInfo->currentVersionNo), $languageCode);
 }
Ejemplo n.º 6
0
    /**
     * @throws NotImplementedException If Content is missing location as this is not supported in current version
     */
    public function previewContentAction(Request $request, $contentId, $versionNo, $language, $siteAccessName = null)
    {
        $this->previewHelper->setPreviewActive(true);
        try {
            $content = $this->contentService->loadContent($contentId, array($language), $versionNo);
            $location = $this->locationProvider->loadMainLocation($contentId);
            if (!$location instanceof Location) {
                throw new NotImplementedException('Preview for content without locations');
            }
            $this->previewHelper->setPreviewedContent($content);
            $this->previewHelper->setPreviewedLocation($location);
        } catch (UnauthorizedException $e) {
            throw new AccessDeniedException();
        }
        if (!$this->authorizationChecker->isGranted(new AuthorizationAttribute('content', 'versionread', array('valueObject' => $content)))) {
            throw new AccessDeniedException();
        }
        $siteAccess = $this->previewHelper->getOriginalSiteAccess();
        // Only switch if $siteAccessName is set and different from original
        if ($siteAccessName !== null && $siteAccessName !== $siteAccess->name) {
            $siteAccess = $this->previewHelper->changeConfigScope($siteAccessName);
        }
        try {
            $response = $this->kernel->handle($this->getForwardRequest($location, $content, $siteAccess, $request, $language), HttpKernelInterface::SUB_REQUEST, false);
        } catch (\Exception $e) {
            if ($location->isDraft() && $this->controllerChecker->usesCustomController($content, $location)) {
                // @todo This should probably be an exception that embeds the original one
                $message = <<<EOF
<p>The view that rendered this location draft uses a custom controller, and resulted in a fatal error.</p>
<p>Location View is deprecated, as it causes issues with preview, such as an empty location id when previewing the first version of a content.</p>
EOF;
                throw new Exception($message, 0, $e);
            } else {
                throw $e;
            }
        }
        $response->headers->remove('cache-control');
        $response->headers->remove('expires');
        $this->previewHelper->restoreConfigScope();
        $this->previewHelper->setPreviewActive(false);
        return $response;
    }
 /**
  * Publishes URL aliases in all languages for the given parameters.
  *
  * @throws \Exception
  *
  * @param int|string $locationId
  * @param int|string $parentLocationId
  * @param int|string $contentId
  *
  * @return int
  */
 protected function publishAliases($locationId, $parentLocationId, $contentId)
 {
     $content = $this->contentService->loadContent($contentId);
     $urlAliasNames = $this->nameSchemaResolver->resolveUrlAliasSchema($content);
     foreach ($urlAliasNames as $languageCode => $name) {
         try {
             $this->setMigrationTable();
             $this->urlAliasHandler->publishUrlAliasForLocation($locationId, $parentLocationId, $name, $languageCode, $content->contentInfo->alwaysAvailable);
             $this->setDefaultTable();
         } catch (Exception $e) {
             $this->setDefaultTable();
             throw $e;
         }
     }
     return count($urlAliasNames);
 }
 /**
  * Return identifier of a field of ezimage type.
  *
  * @param mixed $contentId
  * @param string $language
  * @param bool $related
  *
  * @return string
  */
 private function getImageFieldIdentifier($contentId, $language, $related = false)
 {
     $content = $this->contentService->loadContent($contentId);
     $contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId);
     $fieldDefinitions = $this->getFieldDefinitionList();
     $fieldNames = array_flip($fieldDefinitions);
     if (in_array('ezimage', $fieldDefinitions)) {
         return $fieldNames['ezimage'];
     } elseif (in_array('ezobjectrelation', $fieldDefinitions) && !$related) {
         $field = $content->getFieldValue($fieldNames['ezobjectrelation'], $language);
         if (!empty($field->destinationContentId)) {
             return $this->getImageFieldIdentifier($field->destinationContentId, $language, true);
         }
     } else {
         return $this->getConfiguredFieldIdentifier('image', $contentType);
     }
 }
Ejemplo n.º 9
0
 /**
  * Loads Block object for the given $id.
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If block could not be found.
  *
  * @param int|string $id
  *
  * @return \eZ\Publish\Core\FieldType\Page\Parts\Block
  */
 public function loadBlock($id)
 {
     if (isset($this->blocksById[$id])) {
         return $this->blocksById[$id];
     }
     $contentId = $this->getStorageGateway()->getContentIdByBlockId($id);
     $content = $this->contentService->loadContent($contentId);
     foreach ($content->getFields() as $field) {
         if (!$field->value instanceof Value) {
             continue;
         }
         foreach ($field->value->page->zones as $zone) {
             foreach ($zone->blocks as $block) {
                 if ($block->id === $id) {
                     return $this->blocksById[$id] = $block;
                 }
             }
         }
     }
     throw new NotFoundException('Block', $id);
 }
 /**
  * Returns location path string based on $contentId.
  *
  * @param int|mixed $contentId
  *
  * @return string
  */
 protected function getLocationPathString($contentId)
 {
     $content = $this->contentService->loadContent($contentId);
     $location = $this->locationService->loadLocation($content->contentInfo->mainLocationId);
     return $location->pathString;
 }
 /**
  * Loads content in a version of the given content object.
  *
  * If no version number is given, the method returns the current version
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content or version with the given id and languages does not exist
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version
  *
  * @param int $contentId
  * @param array $languages A language filter for fields. If not given all languages are returned
  * @param int $versionNo the version number. If not given the current version is returned.
  *
  * @return \eZ\Publish\API\Repository\Values\Content\Content
  */
 public function loadContent($contentId, array $languages = null, $versionNo = null)
 {
     return $this->service->loadContent($contentId, $languages, $versionNo);
 }
 /**
  * @return \eZ\Publish\API\Repository\Values\Content\Content
  */
 protected function loadContentByLocationId($locationId)
 {
     return $this->contentService->loadContent($this->locationService->loadLocation($locationId)->contentId);
 }
Ejemplo n.º 13
0
 /**
  * Loads content in a version of the given content object.
  *
  * If no version number is given, the method returns the current version
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content or version with the given id and languages does not exist
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version
  *
  * @param int $contentId
  * @param array $languages A language filter for fields. If not given all languages are returned
  * @param int $versionNo the version number. If not given the current version is returned
  * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true
  *
  * @return \eZ\Publish\API\Repository\Values\Content\Content
  */
 public function loadContent($contentId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true)
 {
     return $this->service->loadContent($contentId, $languages, $versionNo, $useAlwaysAvailable);
 }
 /**
  * Returns ContentType identifier based on $contentId.
  *
  * @param int|mixed $contentId
  * @return string
  */
 private function getContentIdentifier($contentId)
 {
     $contentType = $this->contentTypeService->loadContentType($this->contentService->loadContent($contentId)->contentInfo->contentTypeId);
     return $contentType->identifier;
 }
 protected function loadValueObject($id)
 {
     return $this->contentService->loadContent($id);
 }
 /**
  * Fetches a subtree for a given location object.
  *
  * @param mixed $locationObj
  * @param array $params
  *
  * @return array
  */
 private function fetchSubtree($locationObj, array $params = array())
 {
     $criterion = array();
     // ToDo: ignore visibility
     $criterion[] = new Criterion\Visibility(Criterion\Visibility::VISIBLE);
     $locationList = array();
     // List fetch direct children
     $paramDepth = 1;
     if (isset($params['depth'])) {
         $paramDepth = (int) $params['depth'];
     }
     // list fetch use parentNodeId for faster fetch
     if ($paramDepth <= 1) {
         $depth = $locationObj->depth + $paramDepth;
         // http://share.ez.no/blogs/thiago-campos-viana/ez-publish-5-tip-search-cheat-sheet
         $criterion[] = new Criterion\ParentLocationId($locationObj->id);
     } else {
         $depth = $locationObj->depth + $paramDepth;
         // http://share.ez.no/blogs/thiago-campos-viana/ez-publish-5-tip-search-cheat-sheet
         $criterion[] = new Criterion\Subtree($locationObj->pathString);
         $criterion[] = new Criterion\Location\Depth(Criterion\Operator::GT, $locationObj->depth);
         $criterion[] = new Criterion\Location\Depth(Criterion\Operator::LTE, $depth);
     }
     // main_node_only => true => only include mainlocatione
     if (isset($params['main_location_only'])) {
         // true, 1, '1'
         if ((int) $params['main_location_only'] === 1) {
             // only include mainLocations
             $criterion[] = new Criterion\Location\IsMainLocation(Criterion\Location\IsMainLocation::MAIN);
         }
     }
     if (isset($params['include']) && is_array($params['include']) && count($params['include']) > 0) {
         $criterion[] = new Criterion\ContentTypeIdentifier($params['include']);
         // TODO get ID for Identifier and use this for search so we save a subrequest and its lots faster
         //            if ( in_array( 'article', $params['include'] ) )
         //            {
         //                $criterion[] = new Criterion\ContentTypeId( array( 16 ) );
         //            }
         //            else
         //            {
         //                $criterion[] = new Criterion\ContentTypeIdentifier( $params['include'] );
         //            }
     }
     // ToDo: role and rights, visibility, date, object states criterion
     $offset = 0;
     if (isset($params['offset']) && $params['offset'] > 0) {
         $offset = $params['offset'];
     }
     $limit = null;
     if (isset($params['limit']) && $params['limit'] > 0) {
         $limit = $params['limit'];
     }
     $sortClauses = array();
     if (isset($params['sortby']) && is_array($params['sortby']) && count($params['sortby']) > 0) {
         foreach ($params['sortby'] as $sortKey => $sortArray) {
             // deprecated, for backwards compability (sort by content meta / attribute)
             if (is_array($sortArray) === false) {
                 $newSortClause = $this->generateSortClauseFromString($sortKey, $sortArray);
                 if ($newSortClause !== false) {
                     $sortClauses[] = $newSortClause;
                 }
             }
             // 2 array items means sorts by content meta / attribute
             if (is_array($sortArray) && count($sortArray) == 2) {
                 $newSortClause = $this->generateSortClauseFromString($sortArray['0'], $sortArray['1']);
                 if ($newSortClause !== false) {
                     $sortClauses[] = $newSortClause;
                 }
             }
             // 4 array items means sorts by content field
             if (is_array($sortArray) && count($sortArray) == 4) {
                 $sortOrder = 'ascending';
                 if ($sortArray['2'] == 'DESC' || $sortArray['2'] == 'descending') {
                     $sortOrder = 'descending';
                 }
                 //                    $lang = $this->getPrioritizedLanguages();
                 $sortClauses[] = new SortClause\Field($sortArray['0'], $sortArray['1'], $sortOrder, $sortArray['3']);
             }
         }
     } else {
         // default sort by parent object sort clause
         $sortClauses[] = $this->generateSortClauseFromId($locationObj->sortField, $locationObj->sortOrder);
     }
     /* vendor/ezsystems/ezpublish-kernel/eZ/Publish/API/Repository/Values/Content/Query/Criterion/Operator.php
        abstract class Operator
        {
            const EQ = "=";
            const GT = ">";
            const GTE = ">=";
            const LT = "<";
            const LTE = "<=";
            const IN = "in";
            const BETWEEN = "between";
            const LIKE = "like";
            const CONTAINS = "contains";
        }
        */
     if (isset($params['filter_relations']) && is_array($params['filter_relations']) && count($params['filter_relations']) > 0) {
         foreach ($params['filter_relations'] as $fieldCriterion) {
             // todo check valid syntax
             if (is_array($fieldCriterion) && count($fieldCriterion == 3)) {
                 $criterion[] = new Criterion\FieldRelation($fieldCriterion['0'], $fieldCriterion['1'], $fieldCriterion['2']);
             }
         }
     }
     if (isset($params['filter_fields']) && is_array($params['filter_fields']) && count($params['filter_fields']) > 0) {
         foreach ($params['filter_fields'] as $fieldCriterion) {
             // todo check valid syntax
             if (is_array($fieldCriterion) && count($fieldCriterion == 3)) {
                 // Attention! criterion field must set to be searchable
                 $criterion[] = new Criterion\Field($fieldCriterion['0'], $fieldCriterion['1'], $fieldCriterion['2']);
             }
         }
     }
     if (isset($params['language']) && is_array($params['language']) && count($params['language']) > 0) {
         // ToDo: combine with and, always available?
         $criterion[] = new Criterion\LanguageCode($params['language']);
     } else {
         // only include languages which are defined in siteaccess config  system default|siteaccess languages
         // if more than on a correct bitmask is build to include all object where one of the language is set
         $criterion[] = new Criterion\LanguageCode($this->getPrioritizedLanguages());
     }
     $querySearch = new LocationQuery(array('offset' => $offset, 'limit' => $limit));
     $querySearch->criterion = new Criterion\LogicalAnd($criterion);
     $querySearch->sortClauses = $sortClauses;
     $searchResult = $this->searchService->findLocations($querySearch);
     $searchCount = false;
     // only if count parameter is set return count
     // in newer ez version > 5.4 and in 5.3.5 the count is optional so you can
     // save 1 sql query
     if (isset($params['count']) && $params['count'] === true) {
         // use count from searchResult => the fastest way :-)
         $searchCount = $searchResult->totalCount;
     }
     foreach ($searchResult->searchHits as $searchItem) {
         $childLocationId = $searchItem->valueObject->id;
         if (isset($params['datamap']) && $params['datamap'] === true) {
             $childContentId = $searchItem->valueObject->contentInfo->id;
             $locationList[] = $this->contentService->loadContent($childContentId);
         } else {
             $locationList[] = $searchItem->valueObject;
         }
     }
     return array('searchResult' => $locationList, 'searchCount' => $searchCount);
 }