/** * Dispatches the frontend of a redirect node * @param integer $node Id of the node * @return null */ public function indexAction(Cms $cms, I18n $i18n, $site, $node, $locale = null) { if (!$cms->resolveNode($site, null, $node, RedirectNodeType::NAME)) { return; } if ($locale === null) { $locale = $i18n->getLocale()->getCode(); } else { $i18n->setCurrentLocale($locale); } $path = $this->request->getBasePath(true); if (!$path || $path !== $node->getRoute($locale)) { return $this->chainWebRequest(); } $url = $node->getRedirectUrl($locale); $url = $node->resolveUrl($locale, $this->request->getBaseScript(), $url); if ($url) { $this->response->setRedirect($url); return; } $node = $node->getRedirectNode($locale); if (!$node) { throw new CmsException('No redirect properties set to this node for locale "' . $locale . '".'); } $revision = $site->getRevision(); $site = $site->getId(); if (!$cms->resolveNode($site, $revision, $node)) { return; } $redirectUrl = $this->request->getBaseScript() . $node->getRoute($locale); $this->response->setRedirect($redirectUrl); }
/** * Invokes the command * @param \ride\library\i18n\I18n $i18n * @return null */ public function invoke(I18n $i18n) { $translations = array(); // get all locales $locales = $i18n->getLocales(); // gather all translation keys foreach ($locales as $locale) { $translator = $i18n->getTranslator($locale); $localeTranslations = $translator->getTranslations(); foreach ($localeTranslations as $key => $translation) { $translations[$key] = '[' . $key . ']'; } } // add missing translation keys to all locales foreach ($locales as $locale) { $this->output->writeLine($locale); $translator = $i18n->getTranslator($locale); foreach ($translations as $key => $value) { $translation = $translator->getTranslation($key); if ($translation === null) { $translator->setTranslation($key, $value); $this->output->writeLine('- ' . $key); } } } }
/** * Invokes the command * @param \ride\library\i18n\I18n $i18n * @return null */ public function invoke(I18n $i18n) { $locales = $i18n->getLocales(); foreach ($locales as $locale) { $this->output->writeLine('- ' . $locale->getName() . ' [' . $locale->getCode() . ']'); $properties = $locale->getProperties(); foreach ($properties as $key => $value) { $this->output->writeLine(' #' . $key . ' = ' . $value); } } }
/** * Sets a cookie for the current locale when offering a HTML view * @param \ride\library\event\Event $event * @param \ride\library\i18n\I18n $i18n * @return null */ public function handleLocaleCookie(Event $event, I18n $i18n) { $web = $event->getArgument('web'); $request = $web->getRequest(); $response = $web->getResponse(); $view = $response->getView(); if (!$view instanceof HtmlView) { return; } $cookieName = $this->getCookieName(); $locale = $i18n->getLocale()->getCode(); $domain = $request->getHeader(Header::HEADER_HOST); $cookie = new Cookie($cookieName, $locale, 0, $domain, '/', $request->isSecure()); $response->setCookie($cookie); }
/** * Invokes the command * @param \ride\library\i18n\I18n $i18n * @param string $locale * @param string $query * @return null */ public function invoke(I18n $i18n, $locale, $query = null) { $translator = $i18n->getTranslator($locale); $translations = $translator->getTranslations(); if ($query) { foreach ($translations as $key => $translation) { if (strpos($key, $query) !== false || strpos($translation, $query) !== false) { continue; } unset($translations[$key]); } } ksort($translations); foreach ($translations as $key => $translation) { $this->output->writeLine($key . ' = ' . $translation); } }
/** * Sets a title view to the response * @return null */ public function indexAction(I18n $i18n) { $urls = array(); $locales = $i18n->getLocales(); $node = $this->properties->getNode(); $site = $node->getRootNodeId(); $content = $this->getContext('content'); if (isset($content->type)) { $contentMapper = $this->getContentFacade()->getContentMapper($content->type); } else { $content = null; } foreach ($locales as $localeCode => $locale) { if (!$node->isAvailableInLocale($localeCode)) { continue; } if ($content) { $urls[$localeCode] = array('url' => $contentMapper->getUrl($site, $localeCode, $content->data), 'locale' => $locale); } else { $urls[$localeCode] = array('url' => $this->getUrl('cms.front.' . $site . '.' . $node->getId() . '.' . $localeCode), 'locale' => $locale); } } $this->setTemplateView($this->getTemplate(static::TEMPLATE_NAMESPACE . '/default'), array('locales' => $urls)); }
/** * Action to add or edit a asset * @param \ride\library\i18n\I18n $i18n Instance of I18n * @param \ride\library\orm\OrmManager $orm * @param \ride\web\cms\form\AssetComponent * @param string $locale * @param string $asset * @return null */ public function assetFormAction(I18n $i18n, OrmManager $orm, AssetComponent $assetComponent, $locale, $asset = null) { $folderModel = $orm->getAssetFolderModel(); $assetModel = $orm->getAssetModel(); $styleModel = $orm->getImageStyleModel(); // prepare or lookup asset if ($asset) { $asset = $assetModel->getById($asset, $locale, true); if (!$asset) { $this->response->setNotFound(); return; } $folder = $asset->getFolder(); } else { $asset = $assetModel->createEntry(); $folder = $this->request->getQueryParameter('folder'); if ($folder) { $folder = $folderModel->createProxy($folder, $locale); $asset->setFolder($folder); } } $media = null; $referer = $this->getAssetReferer($asset, $locale); $embed = $this->request->getQueryParameter('embed', false); if ($asset->isUrl()) { try { $media = $assetModel->getMediaFactory()->createMediaItem($asset->value); } catch (UnsupportedMediaException $exception) { } } $data = array('asset' => $asset); $styles = $styleModel->find(); foreach ($styles as $style) { $data['style-' . $style->getSlug()] = $asset->getStyleImage($style->getSlug()); } // create form $form = $this->createFormBuilder($data); $form->addRow('asset', 'component', array('component' => $assetComponent, 'embed' => true)); foreach ($styles as $style) { $form->addRow('style-' . $style->getSlug(), 'image', array('path' => $assetComponent->getDirectory())); } $form = $form->build(); // process form if ($form->isSubmitted()) { try { $form->validate(); $data = $form->getData(); $asset = $data['asset']; $asset->setLocale($locale); $assetStyleModel = $orm->getAssetImageStyleModel(); foreach ($styles as $style) { $image = $data['style-' . $style->getSlug()]; $assetStyle = $asset->getStyle($style->getSlug()); if ($image) { if (!$assetStyle) { // style addition $assetStyle = $assetStyleModel->createEntry(); $assetStyle->setStyle($style); $asset->addToStyles($assetStyle); } $assetStyle->setImage($image); } elseif ($assetStyle) { // style removal $asset->removeFromStyles($assetStyle); } } $assetModel->save($asset); if ($this->request->isXmlHttpRequest()) { // ajax request $this->setTemplateView('assets/detail', array('item' => $asset, 'embed' => $embed, 'referer' => $referer, 'locale' => $locale)); } else { // regular client $this->response->setRedirect($referer); } return; } catch (ValidationException $exception) { k($exception->getErrorsAsString()); $this->setValidationException($exception, $form); } } $view = $this->setTemplateView('assets/asset', array('form' => $form->getView(), 'folder' => $folder, 'styles' => $styles, 'asset' => $asset, 'embed' => $embed, 'referer' => $referer, 'media' => $media, 'dimension' => $assetModel->getDimension($asset), 'locales' => $i18n->getLocaleCodeList(), 'locale' => $locale, 'maxSize' => $this->config->get('asset.upload.size.max'))); $form->processView($view); }
/** * Action to display the widget * @return null */ public function indexAction(OrmManager $orm, ContentService $contentService, I18n $i18n, ReflectionHelper $reflectionHelper, $id = null) { $contentProperties = $this->getContentProperties(); $modelName = $contentProperties->getModelName(); if (!$modelName) { return; } $action = $contentProperties->getNoParametersAction(); if ($id === null) { if ($action != ContentProperties::NONE_IGNORE) { $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND); } return; } $this->entryFormatter = $orm->getEntryFormatter(); $this->model = $orm->getModel($modelName); $query = $this->getModelQuery($contentProperties, $this->locale, $id); $content = $this->getResult($contentProperties, $contentService, $query); if (!$content && $contentProperties->getIncludeUnlocalized()) { // no content, look for localized version $locales = $i18n->getLocaleList(); foreach ($locales as $localeCode => $locale) { if ($localeCode == $this->locale) { continue; } $query = $this->getModelQuery($contentProperties, $localeCode, $id); $content = $this->getResult($contentProperties, $contentService, $query); if ($content) { break; } } } if (!$content && $contentProperties->getIdField() != ModelTable::PRIMARY_KEY) { // no content, look for slug in history and redirect if possible $entryId = $this->getIdFromLog($orm, $reflectionHelper, $contentProperties, $this->locale, $id); if ($entryId) { $url = $this->getUrl('detail', array('id' => $entryId)); $this->response->setRedirect($url, Response::STATUS_CODE_MOVED_PERMANENTLY); return; } } if ($content && $content->data instanceof LocalizedEntry && !$content->data->isLocalized() && !$contentProperties->getIncludeUnlocalized()) { $content = null; } if (!$content) { if ($action != ContentProperties::NONE_IGNORE) { $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND); } return; } $this->setContext('content', $content); $url = $this->request->getBaseScript() . $this->properties->getNode()->getRoute($this->locale) . '/' . $id; $this->addBreadcrumb($url, $content->title); if ($contentProperties->getTitle()) { $this->setPageTitle($content->title); } $this->setView($contentProperties, $content); if ($contentProperties->getMetaOg()) { $this->setMetaOg($contentProperties, $content, $this->dependencyInjector->get('ride\\library\\image\\ImageUrlGenerator')); } if ($this->properties->getWidgetProperty('region')) { $this->setIsRegion(true); } if ($this->properties->getWidgetProperty('section')) { $this->setIsSection(true); } if ($this->properties->getWidgetProperty('block')) { $this->setIsBlock(true); } }
/** * Invokes the command * @param \ride\library\i18n\I18n $i18n * @param string $locale * @param string $key * @return null */ public function invoke(I18n $i18n, $locale, $key) { $translator = $i18n->getTranslator($locale); $translator->setTranslation($key, null); }
/** * Sets a error view to the response if a status code above 399 is set * @return null */ public function handleHttpError(Event $event, I18n $i18n) { $web = $event->getArgument('web'); $request = $web->getRequest(); $response = $web->getResponse(); $route = $request->getRoute(); if (!$route) { return; } $routeId = $route->getId(); if ($routeId && substr($routeId, 0, 10) !== 'cms.front.') { // don't act on non cms pages return; } $statusCode = $response->getStatusCode(); if ($statusCode != Response::STATUS_CODE_FORBIDDEN && $statusCode != Response::STATUS_CODE_NOT_FOUND || $request->isXmlHttpRequest() && $response->getView() instanceof JsonView) { // js request or not a forbidden or not a not found status return; } $locale = $i18n->getLocale()->getCode(); // lookup site with the current base URL $baseUrl = $request->getBaseUrl(); $site = null; $defaultSite = null; $sites = $this->cms->getSites(); foreach ($sites as $site) { if ($site->getBaseUrl($locale) == $baseUrl) { break; } $defaultSite = $site; $site = null; } if (!$site) { if (!$defaultSite) { return; } $site = $defaultSite; } // get the error node $node = $site->get('error.' . $statusCode); if (!$node) { // no node setup for this error return; } // resolve the locale $locales = $site->getAvailableLocales(); if ($locales != Node::LOCALES_ALL && !isset($locales[$locale])) { $locale = array_pop($locales); $i18n->setCurrentLocale($locale); } // dispatch the error page $routeContainer = $web->getRouter()->getRouteContainer(); $route = $routeContainer->getRouteById('cms.front.' . $site->getId() . '.' . $node . '.' . $locale); if (!$route) { $route = $routeContainer->getRouteById('cms.node.frontend.locale'); $route->setArguments(array('node' => $node, 'locale' => $locale)); } else { $route->setArguments(); } $loginRequest = $web->createRequest($route->getPath()); $loginRequest->setRoute($route); $loginRequest->getHeaders()->setHeader(Header::HEADER_REFERER, $request->getUrl()); if ($request->hasSession()) { $loginRequest->setSession($request->getSession()); } $response->getMessageContainer()->removeAll(); $response->setView(null); $response->setStatusCode(Response::STATUS_CODE_OK); $response->removeHeader(Header::HEADER_CONTENT_TYPE); $response->clearRedirect(); try { $dispatcher = $web->getDispatcher(); $dispatcher->dispatch($loginRequest, $response); } catch (UnauthorizedException $exception) { if ($statusCode === Response::STATUS_CODE_FORBIDDEN) { $response->setRedirect($request->getBaseUrl()); return; } else { throw $exception; } } $response->setStatusCode($statusCode); if ($loginRequest->hasSession()) { $request->setSession($loginRequest->getSession()); } if ($statusCode === Response::STATUS_CODE_FORBIDDEN) { $response->addMessage(new Message($i18n->getTranslator()->translate('error.unauthorized'), Message::TYPE_ERROR)); } }
/** * Action to handle and show the properties of this widget * @param \ride\library\i18n\I18n $i18n * @return null */ public function propertiesAction(I18n $i18n, NodeModel $nodeModel) { $locales = $i18n->getLocaleCodeList(); $hasMultipleLocales = count($locales) != 1; // get the data $io = $this->getTextIO(); $existing = $this->request->getQueryParameter('text'); if ($existing) { $text = $io->getExistingText($this->properties, $this->locale, $existing, $this->request->getQueryParameter('new')); } else { $text = $io->getText($this->properties, $this->locale); } if (!$text->getFormat()) { $text->setFormat($this->getDefaultTextFormat()); } $format = $this->getTextFormat($text->getFormat()); $data = array(self::PROPERTY_TITLE => $text->getTitle(), self::PROPERTY_SUBTITLE => $text->getSubtitle(), self::PROPERTY_BODY => $text->getBody(), self::PROPERTY_IMAGE => $text->getImage(), self::PROPERTY_IMAGE_ALIGNMENT => $text->getImageAlignment(), self::PROPERTY_CTA => $text->getCallToActions(), self::PROPERTY_TEMPLATE => $this->getTemplate(static::TEMPLATE_NAMESPACE . '/' . static::TEMPLATE_DEFAULT)); $data['title-use'] = $data[self::PROPERTY_TITLE] || $data[self::PROPERTY_SUBTITLE]; $data['image-use'] = $data[self::PROPERTY_IMAGE]; $io->processFormData($text, $data); // create the form $translator = $this->getTranslator(); $ctaComponent = new CallToActionComponent(); $ctaComponent->setNodes($this->getNodeList($nodeModel)); $types = $this->config->get('cms.text.cta.type'); if (is_array($types)) { $types = $this->config->getConfigHelper()->flattenConfig($types); foreach ($types as $index => $label) { $types[$index] = $translator->translate($label); } $ctaComponent->setTypes(array('' => '---') + $types); } $form = $this->createFormBuilder($data); $form->setId('form-text'); $format->processForm($form, $translator, $this->locale); $io->processForm($this->properties, $this->locale, $translator, $text, $form); $form->addRow('title-use', 'option', array('label' => ' ', 'description' => $translator->translate('label.title.use'), 'attributes' => array('data-toggle-dependant' => 'option-title'))); $form->addRow(self::PROPERTY_TITLE, 'string', array('label' => $translator->translate('label.title'), 'attributes' => array('class' => 'option-title option-title-1'), 'filters' => array('trim' => array()))); $form->addRow(self::PROPERTY_SUBTITLE, 'string', array('label' => $translator->translate('label.subtitle'), 'attributes' => array('class' => 'option-title option-title-1'), 'filters' => array('trim' => array()))); $form->addRow('image-use', 'option', array('label' => ' ', 'description' => $translator->translate('label.image.use'), 'attributes' => array('data-toggle-dependant' => 'option-image'))); $form->addRow(self::PROPERTY_IMAGE, 'image', array('label' => $translator->translate('label.image'), 'attributes' => array('class' => 'option-image option-image-1'))); $form->addRow(self::PROPERTY_IMAGE_ALIGNMENT, 'select', array('label' => $translator->translate('label.alignment.image'), 'attributes' => array('class' => 'option-image option-image-1'), 'options' => array(Text::ALIGN_NONE => $translator->translate('align.none'), Text::ALIGN_LEFT => $translator->translate('align.left'), Text::ALIGN_RIGHT => $translator->translate('align.right'), Text::ALIGN_JUSTIFY => $translator->translate('align.justify')))); $form->addRow(self::PROPERTY_CTA, 'collection', array('label' => $translator->translate('label.cta'), 'type' => 'component', 'options' => array('component' => $ctaComponent))); $form->addRow(self::PROPERTY_TEMPLATE, 'select', array('label' => $translator->translate('label.template'), 'description' => $translator->translate('label.template.widget.description'), 'options' => $this->getAvailableTemplates(static::TEMPLATE_NAMESPACE), 'validators' => array('required' => array()))); if ($hasMultipleLocales) { $form->addRow('locales-all', 'option', array('label' => '', 'description' => $translator->translate('label.text.locales.all'))); } // handle form submission $form = $form->build(); if ($form->isSubmitted()) { if ($existing) { $form->setData($data); } else { try { $form->validate(); $data = $form->getData(); if (!$data['title-use']) { $data[self::PROPERTY_TITLE] = ''; $data[self::PROPERTY_SUBTITLE] = ''; } if (!$data['image-use']) { $data[self::PROPERTY_IMAGE] = ''; } $this->properties->setWidgetProperty(self::PROPERTY_IO, $io->getName()); $format->setText($text, $data); if ($hasMultipleLocales && $data['locales-all']) { $io->setText($this->properties, $locales, $text, $data); } else { $io->setText($this->properties, $this->locale, $text, $data); } $this->setTemplate($data[self::PROPERTY_TEMPLATE]); return true; } catch (ValidationException $exception) { $this->setValidationException($exception, $form); } } } // set view $node = $this->properties->getNode(); $action = $this->getUrl('cms.node.content.widget.properties', array('locale' => $this->locale, 'site' => $node->getRootNodeId(), 'revision' => $node->getRevision(), 'node' => $node->getId(), 'region' => $this->region, 'section' => $this->section, 'block' => $this->block, 'widget' => $this->id)); $view = $this->setTemplateView(static::TEMPLATE_NAMESPACE . '/properties', array('form' => $form->getView(), 'io' => $io, 'format' => $format, 'text' => $text, 'action' => $action)); $view->addJavascript('js/cms/text.js'); $io->processFormView($this->properties, $translator, $text, $view); $form->processView($view); return false; }
/** * Action to add or edit a question * @param \ride\library\i18n\I18n $i18n * @param string $locale Locale code of the data * @param string $survey Id of the survey * @param string $id Id of the question * @return null */ public function questionFormAction(I18n $i18n, $locale, WebApplication $web, ReflectionHelper $reflectionHelper, $survey, $id = null) { // resolve locale $this->locale = $i18n->getLocale($locale)->getCode(); $this->orm->setLocale($this->locale); // resolve event if (!$this->isReadable($survey)) { throw new UnauthorizedException(); } $survey = $this->getEntry($survey); if (!$survey) { $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND); return; } // resolve performance $this->model = $this->orm->getSurveyQuestionModel(); if ($id) { $question = $this->getEntry($id); if (!$question || $question->getSurvey()->getId() !== $survey->getId()) { $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND); return; } } else { $question = $this->createEntry(); $question->setSurvey($survey); } // create form $translator = $this->getTranslator(); $likertModel = $this->orm->getSurveyLikertModel(); $answerComponent = new QuestionAnswerComponent(); $answerComponent->setLikertScales($likertModel->find()); $form = $this->createFormBuilder($question); $form->setId('form-survey-question'); $form->addRow('question', 'text', array('label' => $translator->translate('label.question'), 'validators' => array('required' => array()))); $form->addRow('description', 'wysiwyg', array('label' => $translator->translate('label.description'))); $form->addRow('isOpen', 'option', array('label' => $translator->translate('label.question.open'), 'description' => $translator->translate('label.question.open.description'))); $form->addRow('isMultiple', 'option', array('label' => $translator->translate('label.question.multiple'), 'description' => $translator->translate('label.question.multiple.description'))); $form->addRow('answers', 'collection', array('label' => $translator->translate('label.answers'), 'type' => 'component', 'order' => true, 'options' => array('component' => $answerComponent))); $form = $form->build(); // handle form if ($form->isSubmitted()) { try { $form->validate(); // obtain performance from form $question = $form->getData(); $question->setLocale($locale); foreach ($question->getAnswers() as $answer) { $answer->setLocale($locale); } $this->model->save($question); $this->addSuccess('success.data.saved', array('data' => $question->getQuestion())); $this->response->setRedirect($this->getAction(self::ACTION_DETAIL, array('id' => $survey->getId()))); return; } catch (ValidationException $exception) { $this->setValidationException($exception, $form); } } $referer = $this->request->getQueryParameter('referer'); $this->templateForm = 'orm/scaffold/form'; if ($question && $question->getId()) { $localizeUrl = $this->getUrl('survey.question.edit', array('locale' => '%locale%', 'survey' => $survey->getId(), 'id' => $question->getId())); } else { $localizeUrl = $this->getUrl('survey.question.add', array('locale' => '%locale%', 'survey' => $survey->getId())); } $localizeUrl .= '?referer=' . urlencode($this->request->getUrl()); $view = $this->setFormView($form, $referer, $i18n->getLocaleCodeList(), $locale, $survey); $view->getTemplate()->set('localizeUrl', $localizeUrl); }