/** * Parses the given $value for the field $fieldDefIdentifier in the content * identified by $contentInfoId. * * @param string $contentInfoId * @param string $fieldDefIdentifier * @param mixed $value * * @return mixed */ public function parseFieldValue($contentInfoId, $fieldDefIdentifier, $value) { $contentInfo = $this->contentService->loadContentInfo($contentInfoId); $contentType = $this->contentTypeService->loadContentType($contentInfo->contentTypeId); $fieldDefinition = $contentType->getFieldDefinition($fieldDefIdentifier); return $this->parseValue($fieldDefinition->fieldTypeIdentifier, $value); }
/** * Converts internal links (ezcontent:// and ezlocation://) to URLs. * * @param \DOMDocument $document * * @return \DOMDocument */ public function convert(DOMDocument $document) { $document = clone $document; $xpath = new DOMXPath($document); $xpath->registerNamespace("docbook", "http://docbook.org/ns/docbook"); $linkAttributeExpression = "starts-with( @xlink:href, 'ezlocation://' ) or starts-with( @xlink:href, 'ezcontent://' )"; $xpathExpression = "//docbook:link[{$linkAttributeExpression}]|//docbook:ezlink"; /** @var \DOMElement $link */ foreach ($xpath->query($xpathExpression) as $link) { // Set resolved href to number character as a default if it can't be resolved $hrefResolved = "#"; $href = $link->getAttribute("xlink:href"); $location = null; preg_match("~^(.+://)?([^#]*)?(#.*|\\s*)?\$~", $href, $matches); list(, $scheme, $id, $fragment) = $matches; if ($scheme === "ezcontent://") { try { $contentInfo = $this->contentService->loadContentInfo($id); $location = $this->locationService->loadLocation($contentInfo->mainLocationId); $hrefResolved = $this->urlAliasRouter->generate($location) . $fragment; } catch (APINotFoundException $e) { if ($this->logger) { $this->logger->warning("While generating links for richtext, could not locate " . "Content object with ID " . $id); } } catch (APIUnauthorizedException $e) { if ($this->logger) { $this->logger->notice("While generating links for richtext, unauthorized to load " . "Content object with ID " . $id); } } } else { if ($scheme === "ezlocation://") { try { $location = $this->locationService->loadLocation($id); $hrefResolved = $this->urlAliasRouter->generate($location) . $fragment; } catch (APINotFoundException $e) { if ($this->logger) { $this->logger->warning("While generating links for richtext, could not locate " . "Location with ID " . $id); } } catch (APIUnauthorizedException $e) { if ($this->logger) { $this->logger->notice("While generating links for richtext, unauthorized to load " . "Location with ID " . $id); } } } else { $hrefResolved = $href; } } $hrefAttributeName = "xlink:href"; // For embeds set the resolved href to the separate attribute // Original href needs to be preserved in order to generate link parameters // This will need to change with introduction of UrlService and removal of URL link // resolving in external storage if ($link->localName === "ezlink") { $hrefAttributeName = "href_resolved"; } $link->setAttribute($hrefAttributeName, $hrefResolved); } return $document; }
/** * Renders the comments list for content with id $contentId * Comment form might also be included * * @param mixed $contentId */ public function renderForContentAction( $contentId ) { return new Response( $this->commentsRenderer->renderForContent( $this->contentService->loadContentInfo( $contentId ), $this->request ) ); }
public function setPostCategories($postId, Request $request) { $this->login($request->request->get('username'), $request->request->get('password')); // @todo Replace categories instead of adding $contentInfo = $this->contentService->loadContentInfo($postId); foreach ($request->request->get('categories') as $category) { $this->locationService->createLocation($contentInfo, $this->locationService->newLocationCreateStruct($category['categoryId'])); } return new Response(true); }
public function purgeForContent($contentId) { $contentInfo = $this->contentService->loadContentInfo($contentId); $event = new ContentCacheClearEvent($contentInfo); $this->eventDispatcher->dispatch(MVCEvents::CACHE_CLEAR_CONTENT, $event); $locationIds = []; foreach ($event->getLocationsToClear() as $location) { $locationIds[] = $location->id; } $this->purgeClient->purge(array_unique($locationIds)); }
/** * {@inheritdoc} */ public function purgeForContent($contentId, $locationIds = []) { $contentInfo = $this->contentService->loadContentInfo($contentId); // Can only gather relevant locations using ContentCacheClearEvent on published content if ($contentInfo->published) { $event = new ContentCacheClearEvent($contentInfo); $this->eventDispatcher->dispatch(MVCEvents::CACHE_CLEAR_CONTENT, $event); foreach ($event->getLocationsToClear() as $location) { $locationIds[] = $location->id; } } $this->purgeClient->purge(array_unique($locationIds)); }
/** * Returns a valid Location object for $contentId. * Will either load mainLocationId (if available) or build a virtual Location object. * * @param mixed $contentId * * @return \eZ\Publish\API\Repository\Values\Content\Location|null Null when content does not have location */ public function getPreviewLocation($contentId) { // contentInfo must be reloaded as content is not published yet (e.g. no mainLocationId) $contentInfo = $this->contentService->loadContentInfo($contentId); // mainLocationId already exists, content has been published at least once. if ($contentInfo->mainLocationId) { $location = $this->locationService->loadLocation($contentInfo->mainLocationId); } else { // @todo In future releases this will be a full draft location when this feature // is implemented. Or it might return null when content does not have location, // but for now we can't detect that so we return a virtual draft location $location = new Location(array('contentInfo' => $contentInfo, 'status' => Location::STATUS_DRAFT)); } return $location; }
/** * Updates object states of content * An object state in the input overrides the state of the object state group. * * @param $contentId * * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException * * @return \eZ\Publish\Core\REST\Common\Values\ContentObjectStates */ public function setObjectStatesForContent($contentId, Request $request) { $newObjectStates = $this->inputDispatcher->parse(new Message(array('Content-Type' => $request->headers->get('Content-Type')), $request->getContent())); $countByGroups = array(); foreach ($newObjectStates as $newObjectState) { $groupId = (int) $newObjectState->groupId; if (array_key_exists($groupId, $countByGroups)) { ++$countByGroups[$groupId]; } else { $countByGroups[$groupId] = 1; } } foreach ($countByGroups as $groupId => $count) { if ($count > 1) { throw new ForbiddenException("Multiple object states provided for group with ID {$groupId}"); } } $contentInfo = $this->contentService->loadContentInfo($contentId); $contentObjectStates = array(); foreach ($newObjectStates as $newObjectState) { $objectStateGroup = $this->objectStateService->loadObjectStateGroup($newObjectState->groupId); $this->objectStateService->setContentState($contentInfo, $objectStateGroup, $newObjectState->objectState); $contentObjectStates[(int) $objectStateGroup->id] = $newObjectState; } return new ContentObjectStates($contentObjectStates); }
/** * Generates a URL for a location, from the given parameters. * * It is possible to directly pass a Location object as the route name, as the ChainRouter allows it through ChainedRouterInterface. * * If $name is a route name, the "location" key in $parameters must be set to a valid eZ\Publish\API\Repository\Values\Content\Location object. * "locationId" can also be provided. * * If the generator is not able to generate the url, it must throw the RouteNotFoundException * as documented below. * * @see UrlAliasRouter::supports() * * @param string|\eZ\Publish\API\Repository\Values\Content\Location $name The name of the route or a Location instance * @param mixed $parameters An array of parameters * @param int $referenceType The type of reference to be generated (one of the constants) * * @throws \LogicException * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException * @throws \InvalidArgumentException * * @return string The generated URL * * @api */ public function generate($name, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) { // Direct access to Location if ($name instanceof Location) { return $this->generator->generate($name, $parameters, $referenceType); } // Normal route name if ($name === self::URL_ALIAS_ROUTE_NAME) { if (isset($parameters['location']) || isset($parameters['locationId'])) { // Check if location is a valid Location object if (isset($parameters['location']) && !$parameters['location'] instanceof Location) { throw new LogicException("When generating an UrlAlias route, 'location' parameter must be a valid eZ\\Publish\\API\\Repository\\Values\\Content\\Location."); } $location = isset($parameters['location']) ? $parameters['location'] : $this->locationService->loadLocation($parameters['locationId']); unset($parameters['location'], $parameters['locationId'], $parameters['viewType'], $parameters['layout']); return $this->generator->generate($location, $parameters, $referenceType); } if (isset($parameters['contentId'])) { $contentInfo = $this->contentService->loadContentInfo($parameters['contentId']); unset($parameters['contentId'], $parameters['viewType'], $parameters['layout']); if (empty($contentInfo->mainLocationId)) { throw new LogicException('Cannot generate an UrlAlias route for content without main location.'); } return $this->generator->generate($this->locationService->loadLocation($contentInfo->mainLocationId), $parameters, $referenceType); } throw new InvalidArgumentException("When generating an UrlAlias route, either 'location', 'locationId' or 'contentId' must be provided."); } throw new RouteNotFoundException('Could not match route'); }
/** * Returns a valid Location object for $contentId. * Will either load mainLocationId (if available) or build a virtual Location object. * * @param mixed $contentId * * @return \eZ\Publish\API\Repository\Values\Content\Location|null Null when content does not have location */ public function getPreviewLocation( $contentId ) { // contentInfo must be reloaded as content is not published yet (e.g. no mainLocationId) $contentInfo = $this->contentService->loadContentInfo( $contentId ); // mainLocationId already exists, content has been published at least once. if ( $contentInfo->mainLocationId ) { $location = $this->locationService->loadLocation( $contentInfo->mainLocationId ); } // New Content, never published, create a virtual location object. else { // @todo In future releases this will be a full draft location when this feature // is implemented. Or it might return null when content does not have location, // but for now we can't detect that so we return a virtual draft location $location = new Location( array( // Faking using root locationId 'id' => $this->configResolver->getParameter( 'content.tree_root.location_id' ), 'contentInfo' => $contentInfo, 'status' => Location::STATUS_DRAFT ) ); } return $location; }
/** * Converts internal links (eznode:// and ezobject://) to URLs. * * @param \DOMDocument $xmlDoc * * @return string|null */ public function convert(DOMDocument $xmlDoc) { $xpath = new DOMXPath($xmlDoc); $elements = $xpath->query('//link|//embed|//embed-inline'); /** @var \DOMElement $element */ foreach ($elements as $element) { $location = null; if ($this->elementHasAttribute($element, 'object_id')) { try { $contentInfo = $this->contentService->loadContentInfo($this->getElementAttribute($element, 'object_id')); $location = $this->locationService->loadLocation($contentInfo->mainLocationId); } catch (APINotFoundException $e) { if ($this->logger) { $this->logger->warning('While generating links for xmltext, could not locate ' . 'Content object with ID ' . $this->getElementAttribute($element, 'object_id')); } } catch (APIUnauthorizedException $e) { if ($this->logger) { $this->logger->notice('While generating links for xmltext, unauthorized to load ' . 'Content object with ID ' . $this->getElementAttribute($element, 'object_id')); } } } if ($this->elementHasAttribute($element, 'node_id')) { try { $location = $this->locationService->loadLocation($this->getElementAttribute($element, 'node_id')); } catch (APINotFoundException $e) { if ($this->logger) { $this->logger->warning('While generating links for xmltext, could not locate ' . 'Location with ID ' . $this->getElementAttribute($element, 'node_id')); } } catch (APIUnauthorizedException $e) { if ($this->logger) { $this->logger->notice('While generating links for xmltext, unauthorized to load ' . 'Location with ID ' . $this->getElementAttribute($element, 'node_id')); } } } if ($location !== null) { $element->setAttribute('url', $this->urlAliasRouter->generate($location)); } // Copy temporary URL if it exists and is not set at this point if (!$element->hasAttribute('url') && $element->hasAttribute(EmbedLinking::TEMP_PREFIX . 'url')) { $element->setAttribute('url', $element->getAttribute(EmbedLinking::TEMP_PREFIX . 'url')); } if ($this->elementHasAttribute($element, 'anchor_name')) { $element->setAttribute('url', $element->getAttribute('url') . '#' . $this->getElementAttribute($element, 'anchor_name')); } } }
/** * Loads the main location for $contentId * * If the content does not have a location (yet), but has a Location draft, it is returned instead. * Location drafts do not have an id (it is set to null), and can be tested using the isDraft() method. * * If the content doesn't have a location nor a location draft, null is returned. * * @param mixed $contentInfo * * @return \eZ\Publish\API\Repository\Values\Content\Location|null */ public function loadMainLocation($contentId) { $contentInfo = $this->contentService->loadContentInfo($contentId); // mainLocationId already exists, content has been published at least once. if ($contentInfo->mainLocationId) { $location = $this->locationService->loadLocation($contentInfo->mainLocationId); } else { // @todo In future releases this will be a full draft location when this feature // is implemented. Or it might return null when content does not have location, // but for now we can't detect that so we return a virtual draft location $parentLocations = $this->locationHandler->loadParentLocationsForDraftContent($contentInfo->id); if (count($parentLocations) === 0) { return null; } $location = new Location(array('contentInfo' => $contentInfo, 'status' => Location::STATUS_DRAFT, 'parentLocationId' => $parentLocations[0]->id, 'depth' => $parentLocations[0]->depth + 1)); } return $location; }
/** * Loads all locations for content object. * * @param mixed $contentId * * @return \eZ\Publish\Core\REST\Server\Values\LocationList */ public function loadLocationsForContent($contentId, Request $request) { $restLocations = array(); $contentInfo = $this->contentService->loadContentInfo($contentId); foreach ($this->locationService->loadLocations($contentInfo) as $location) { $restLocations[] = new Values\RestLocation($location, $this->locationService->getLocationChildCount($location)); } return new Values\CachedValue(new Values\LocationList($restLocations, $request->getPathInfo()), array('locationId' => $contentInfo->mainLocationId)); }
/** * Loads the main location for $contentId. * * If the content does not have a location (yet), but has a Location draft, it is returned instead. * Location drafts do not have an id (it is set to null), and can be tested using the isDraft() method. * * If the content doesn't have a location nor a location draft, null is returned. * * @param mixed $contentId * * @return \eZ\Publish\API\Repository\Values\Content\Location|null */ public function loadMainLocation($contentId) { $location = null; $contentInfo = $this->contentService->loadContentInfo($contentId); // mainLocationId already exists, content has been published at least once. if ($contentInfo->mainLocationId) { $location = $this->locationService->loadLocation($contentInfo->mainLocationId); } elseif (!$contentInfo->published) { // New Content, never published, create a virtual location object. // In cases content is missing locations this will return empty array $parentLocations = $this->locationHandler->loadParentLocationsForDraftContent($contentInfo->id); if (empty($parentLocations)) { return null; } $location = new Location(array('contentInfo' => $contentInfo, 'status' => Location::STATUS_DRAFT, 'parentLocationId' => $parentLocations[0]->id, 'depth' => $parentLocations[0]->depth + 1, 'pathString' => $parentLocations[0]->pathString . '/x')); } return $location; }
/** * Gets ContentType ID based on $contentId. * * @param mixed $contentId * * @return int|null */ protected function getContentTypeId($contentId) { $contentTypeId = null; try { $contentTypeId = $this->contentService->loadContentInfo($contentId)->contentTypeId; } catch (\Exception $e) { } return $contentTypeId; }
/** * Converts internal links (ezcontent:// and ezlocation://) to URLs. * * @param \DOMDocument $document * * @return \DOMDocument */ public function convert(DOMDocument $document) { $document = clone $document; $xpath = new DOMXPath($document); $xpath->registerNamespace("docbook", "http://docbook.org/ns/docbook"); $xpathExpression = "//docbook:link[starts-with( @xlink:href, 'ezlocation://' ) or starts-with( @xlink:href, 'ezcontent://' )]"; /** @var \DOMElement $link */ foreach ($xpath->query($xpathExpression) as $link) { $location = null; preg_match("~^(.+)://([^#]*)?(#.*|\\s*)?\$~", $link->getAttribute("xlink:href"), $matches); list(, $scheme, $id, $fragment) = $matches; if ($scheme === "ezcontent") { try { $contentInfo = $this->contentService->loadContentInfo($id); $location = $this->locationService->loadLocation($contentInfo->mainLocationId); } catch (APINotFoundException $e) { if ($this->logger) { $this->logger->warning("While generating links for richtext, could not locate " . "Content object with ID " . $id); } } catch (APIUnauthorizedException $e) { if ($this->logger) { $this->logger->notice("While generating links for richtext, unauthorized to load " . "Content object with ID " . $id); } } } if ($scheme === "ezlocation") { try { $location = $this->locationService->loadLocation($id); } catch (APINotFoundException $e) { if ($this->logger) { $this->logger->warning("While generating links for richtext, could not locate " . "Location with ID " . $id); } } catch (APIUnauthorizedException $e) { if ($this->logger) { $this->logger->notice("While generating links for richtext, unauthorized to load " . "Location with ID " . $id); } } } if ($location !== null) { $link->setAttribute('xlink:href', $this->urlAliasRouter->generate($location) . $fragment); } } return $document; }
/** * Converts internal links (eznode:// and ezobject://) to URLs. * * @param \DOMDocument $xmlDoc * * @return string|null */ public function convert(DOMDocument $xmlDoc) { foreach ($xmlDoc->getElementsByTagName("link") as $link) { $location = null; if ($link->hasAttribute('object_id')) { try { $contentInfo = $this->contentService->loadContentInfo($link->getAttribute('object_id')); $location = $this->locationService->loadLocation($contentInfo->mainLocationId); } catch (APINotFoundException $e) { if ($this->logger) { $this->logger->warning("While generating links for xmltext, could not locate " . "Content object with ID " . $link->getAttribute('object_id')); } } catch (APIUnauthorizedException $e) { if ($this->logger) { $this->logger->notice("While generating links for xmltext, unauthorized to load " . "Content object with ID " . $link->getAttribute('object_id')); } } } if ($link->hasAttribute('node_id')) { try { $location = $this->locationService->loadLocation($link->getAttribute('node_id')); } catch (APINotFoundException $e) { if ($this->logger) { $this->logger->warning("While generating links for xmltext, could not locate " . "Location with ID " . $link->getAttribute('node_id')); } } catch (APIUnauthorizedException $e) { if ($this->logger) { $this->logger->notice("While generating links for xmltext, unauthorized to load " . "Location with ID " . $link->getAttribute('node_id')); } } } if ($location !== null) { $link->setAttribute('url', $this->urlAliasRouter->generate($location)); } if ($link->hasAttribute('anchor_name')) { $link->setAttribute('url', $link->getAttribute('url') . "#" . $link->getAttribute('anchor_name')); } } }
/** * Loads a content info object. * * To load fields use loadContent * * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist * * @param int $contentId * * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo */ public function loadContentInfo($contentId) { return $this->service->loadContentInfo($contentId); }
/** * Get the main location Id * * @param $contentId * @return \eZ\Publish\API\Repository\Values\Content\Location */ public function getMainLocation($contentId) { $mainLocationId = $this->contentService->loadContentInfo($contentId)->mainLocationId; return $this->locationService->loadLocation($mainLocationId); }