/** * Constructs a new theme * @param string $name Machine name of the theme * @param string $displayName Display name of the theme * @param array $engines Array with the engine name as key and as value * @param array $regions Array with the region name as key and as value * @param string $parent Machine name of the parent * @return null */ public function __construct($name, $displayName, $engines, $regions, $parent = null) { $this->name = StringHelper::safeString($name); $this->displayName = $displayName; $this->engines = $engines; $this->regions = $regions; $this->parent = $parent; }
/** * Get a preview of the properties of this widget * @return string */ public function getPropertiesPreview() { $embed = $this->properties->getWidgetProperty(self::PROPERTY_EMBED); $translator = $this->getTranslator(); $preview = ''; $preview .= '<strong>' . $translator->translate('label.template') . '</strong>: ' . $this->getTemplate(static::TEMPLATE_NAMESPACE . '/default') . '<br>'; $preview .= StringHelper::truncate(htmlentities($embed), 120) . '<br>'; return $preview; }
/** * Gets a resource instance for the provided model data * @param mixed $data Data to adapt * @param \ride\library\http\jsonapi\JsonApiDocument $document Document * which is requested * @param string $relationshipPath dot-separated list of relationship names * @return JsonApiResource|null */ public function getResource($data, JsonApiDocument $document, $relationshipPath = null) { $resource = parent::getResource($data, $document, $relationshipPath); $value = $resource->getAttribute('value'); if ($value && !StringHelper::startsWith($value, array('http://', 'https://'))) { $dataUri = $this->web->getHttpFactory()->createDataUriFromFile($value); if ($dataUri === null) { $resource->setAttribute('value', null); } else { $resource->setAttribute('value', $dataUri->encode()); } } $query = $document->getQuery()->getParameter('url'); if ($this->assetService && ($query == '1' || $query == 'true')) { try { $url = $this->assetService->getAssetUrl($data); } catch (ImageException $exception) { $url = null; } $resource->setMeta('url', $url); } $query = $document->getQuery()->getParameter('images'); if ($this->assetService && ($query == '1' || $query == 'true')) { $images = array(); $imageStyles = $this->assetService->getImageStyles(); foreach ($imageStyles as $imageStyle => $null) { try { $url = $this->assetService->getAssetUrl($data, $imageStyle, true); } catch (ImageException $exception) { $url = null; } if ($url) { $images[$imageStyle] = $url; } } $resource->setMeta('images', $images); } return $resource; }
/** * Saves the data in the model * @param mixed $entry * @return nulls */ public function saveEntry($entry) { if (!$this->willSave($entry)) { return; } // generate a name for the text if ($entry->getTitle()) { $entry->name = $entry->getTitle(); } elseif ($entry->getBody()) { $strippedBody = strip_tags($entry->getBody()); if ($strippedBody) { $body = $strippedBody; } else { $body = $entry->getBody(); } $entry->name = StringHelper::truncate($body, 30); } elseif ($entry->getImage()) { $entry->name = $entry->getImage(); } else { $entry->name = 'Text'; } // perform the actual saving return parent::saveEntry($entry); }
/** * Gets a id for a new node * @param \ride\library\cms\node\Node $node Node in need of a id * @return string */ protected function getNewNodeId(Node $node) { $revision = $node->getRevision(); $nodeName = $node->getName(); if (!$nodeName) { $nodeName = 'node'; } $baseId = StringHelper::safeString($nodeName); $baseId = str_replace(array('.', '-', '_', ' '), '', $baseId); $id = $baseId; $index = 1; try { $siteId = $node->getRootNodeId(); $this->getNodes($siteId, $revision); while (isset($this->nodes[$siteId][$revision][$id])) { $id = $baseId . $index; $index++; } } catch (CmsException $exception) { // new site node $this->getSites(); while (isset($this->sites[$id])) { $id = $baseId . $index; $index++; } } return $id; }
/** * Caches the response * @param \ride\web\WebApplication $web * @return null */ public function cacheResponse(WebApplication $web) { if (!isset($this->cache)) { return; } if (!isset($this->cacheTtl)) { $this->cacheTtl = 0; } $request = $web->getRequest(); $response = $web->getResponse(); $cacheKey = 'node.response.' . StringHelper::safeString($request->getUrl()); if ($this->log) { $this->log->logDebug('Caching the request', 'Ttl: ' . $this->cacheTtl . ' - ' . $cacheKey, ApplicationListener::LOG_SOURCE); } $cachedResponse = $this->cache->create($cacheKey); $cachedResponse->setValue($response); $cachedResponse->setTtl($this->cacheTtl); $this->cache->set($cachedResponse); }
/** * Gets the file name for a newly uploaded file * @param \ride\library\orm\model\Model $model * @param \ride\library\orm\definition\field\ModelField $field * @param \ride\library\orm\entry\Entry $entry * @return string Name for the file without path and extension */ protected function getFileName(Model $model, ModelField $field, Entry $entry) { $entryFormatter = $model->getOrmManager()->getEntryFormatter(); $format = $model->getMeta()->getFormat(EntryFormatter::FORMAT_TITLE); $name = $entryFormatter->formatEntry($entry, $format); if ($name) { $name = StringHelper::safeString($name); } else { $name = StringHelper::generate(); } return $name; }
/** * Gets a preview for the properties of this widget * @return string */ public function getPropertiesPreview() { $text = $this->getTextIO()->getText($this->properties, $this->locale); $textFormat = $this->getTextFormat($text->getFormat()); $title = $text->getTitle(); $subtitle = $text->getSubtitle(); $body = $textFormat->getHtml($text->getBody()); $image = $text->getImage(); $bodyStripped = trim(strip_tags($body)); if (!$bodyStripped) { $body = htmlentities($body); } else { $body = StringHelper::truncate($bodyStripped, 120); } $translator = $this->getTranslator(); $preview = ''; if ($title) { $preview .= '<strong>' . $translator->translate('label.title') . '</strong>: ' . $title . '<br>'; } if ($subtitle) { $preview .= '<strong>' . $translator->translate('label.subtitle') . '</strong>: ' . $subtitle . '<br>'; } $preview .= '<strong>' . $translator->translate('label.template') . '</strong>: ' . $this->getTemplate(static::TEMPLATE_NAMESPACE . '/' . static::TEMPLATE_DEFAULT) . '<br>'; $preview .= $body; return $preview; }
/** * Gets the source snippet where the exception has been thrown * @param Exception $exception * @param integer $offset Number of lines before and after the thrown line * @return array Array containing the values needed to display the exception */ protected function getExceptionSource(Exception $exception, $offset = 5) { if (!file_exists($exception->getFile())) { return ''; } $source = file_get_contents($exception->getFile()); $source = StringHelper::addLineNumbers($source); $source = explode("\n", $source); $line = $exception->getLine(); $offsetAfter = ceil($offset / 2); $offsetBefore = $offset + ($offset - $offsetAfter); $sourceOffset = max(0, $line - $offsetBefore); $sourceLength = min(count($source), $line + $offsetAfter) - $sourceOffset; $source = array_slice($source, $sourceOffset, $sourceLength); $source = implode("\n", $source); return $source; }
/** * Validates the route of the node * @param \ride\library\cms\node\Node $node Node to be validated * @param \ride\library\cms\node\NodeModel $nodeModel Model of the nodes * @param \ride\library\validation\exception\ValidationException $exception * @return null */ protected function validateRoute(Node $node, NodeModel $nodeModel, ValidationException $exception) { if (!$node->getParent()) { return; } $rootNode = $node->getRootNode(); $rootNodeId = $rootNode->getId(); $nodeId = $node->getId(); $modelNodes = $nodeModel->getNodes($rootNodeId, $node->getRevision()); $propertyPrefix = Node::PROPERTY_ROUTE . '.'; $lengthPropertyPrefix = strlen($propertyPrefix); // loop all properties $properties = $node->getProperties(); foreach ($properties as $key => $property) { if (strpos($key, $propertyPrefix) !== 0) { // we're only interested in route properties continue; } $routeLocale = substr($key, $lengthPropertyPrefix); $route = $property->getValue(); // normalize route $route = trim($route, '/'); $baseUrls[$routeLocale] = $rootNode->getBaseUrl($routeLocale); $tokens = explode('/', $route); foreach ($tokens as $index => $token) { if ($token) { $token = StringHelper::safeString($token); } if (empty($token)) { unset($tokens[$index]); } else { $tokens[$index] = $token; } } $route = '/' . implode('/', $tokens); // check for duplicate routes $errors = array(); foreach ($modelNodes as $modelNode) { $modelNodeId = $modelNode->getId(); if ($modelNodeId == $nodeId || $modelNode->getRootNodeId() != $rootNodeId || !$modelNode->hasParent() || $modelNode->getType() == ReferenceNodeType::NAME) { // same node, different site or root node or a reference node continue; } $modelNodeRoutes = $modelNode->getRoutes(); foreach ($modelNodeRoutes as $locale => $modelNodeRoute) { if (!array_key_exists($locale, $baseUrls)) { $baseUrls[$locale] = $rootNode->getBaseUrl($locale); } if ($baseUrls[$routeLocale] . $route != $baseUrls[$locale] . $modelNodeRoute) { continue; } $errors[$modelNodeId] = new ValidationError('error.route.used.node', "Route '%route%' is already used by node %node% in locale %locale%", array('route' => $route, 'node' => $modelNodeId, 'locale' => $locale)); } } foreach ($errors as $error) { $exception->addErrors(Node::PROPERTY_ROUTE, array($error)); } // update property with normalized route $property->setValue($route); } }
/** * Handles a file upload via a dataURI string * @param string $fileName Filename * @param string $dataUri DataURI with file content * @return \ride\library\system\file\File * @throws \ride\library\system\exception\FileSystemException */ public function handleDataUri($fileName, $dataUriString) { // decode dataUri $uploadFile = null; $dataUri = null; try { $dataUri = DataUri::decode($dataUriString); } catch (HttpException $err) { } if ($dataUri instanceof DataUri) { // add extension based on claimed mime type $mimeType = $dataUri->getMimeType(); $mimeExtension = $this->mimeResolver->getExtensionForMimeType($mimeType); if (is_string($mimeExtension)) { $fileName .= '.' . $mimeExtension; } // prepare file name $fileName = StringHelper::safeString($fileName, '_', false); $uploadFile = $this->getUploadDirectoryTemporary()->getChild($fileName); $uploadFile = $uploadFile->getCopyFile(); $uploadFile->write($dataUri->getData()); $uploadFile->setPermissions(0644); } return $uploadFile; }
/** * Checks if the provided value is a URL * @param string $value Value to check * @return boolean */ private function isUrl($value) { return StringHelper::startsWith($value, array('http://', 'https://')); }