diff --git a/config/bolt/contenttypes.yaml b/config/bolt/contenttypes.yaml index bf56efe4..9449bc63 100644 --- a/config/bolt/contenttypes.yaml +++ b/config/bolt/contenttypes.yaml @@ -40,7 +40,7 @@ homepage: uses: title group: meta viewless: true - locales: ['nl', 'en', 'pt_BR'] + locales: ['en', 'nl', 'pt_BR'] singleton: true default_status: published icon_many: "fa:home" diff --git a/src/Configuration/Parser/ContentTypesParser.php b/src/Configuration/Parser/ContentTypesParser.php index 834eb65e..8d724bc3 100644 --- a/src/Configuration/Parser/ContentTypesParser.php +++ b/src/Configuration/Parser/ContentTypesParser.php @@ -114,9 +114,9 @@ class ContentTypesParser extends BaseParser } if (!isset($contentType['locales'])) { - $contentType['locales'] = false; + $contentType['locales'] = []; } else if (is_string($contentType['locales'])) { - $contentType['locales'] = false; + $contentType['locales'] = (array) $contentType['locales']; } [$fields, $groups] = $this->parseFieldsAndGroups($contentType['fields']); diff --git a/src/Controller/Backend/ContentEditController.php b/src/Controller/Backend/ContentEditController.php index c2f9fcd5..aaa048c8 100644 --- a/src/Controller/Backend/ContentEditController.php +++ b/src/Controller/Backend/ContentEditController.php @@ -43,8 +43,8 @@ class ContentEditController extends BaseController $twigvars = [ 'record' => $content, - 'locales' => $content->getDefinition()->get('locales'), - 'currentlocale' => $request->query->get('locale'), + 'locales' => $content->getLocales(), + 'currentlocale' => $this->getEditLocale($request, $content), ]; return $this->renderTemplate('content/edit.html.twig', $twigvars); @@ -57,8 +57,6 @@ class ContentEditController extends BaseController { $token = new CsrfToken('editrecord', $request->request->get('_csrf_token')); -// dd($token); - if (! $this->csrfTokenManager->isTokenValid($token)) { throw new InvalidCsrfTokenException(); } @@ -72,14 +70,13 @@ class ContentEditController extends BaseController $url = $urlGenerator->generate('bolt_content_edit', ['id' => $content->getId()]); - dump($url); - - return new RedirectResponse('http://nu.nl'); + return new RedirectResponse($url); } private function contentFromPost(?Content $content, Request $request): Content { $post = $request->request->all(); + $locale = $this->getPostedLocale($post); if (! $content) { @@ -89,20 +86,18 @@ class ContentEditController extends BaseController $content->setConfig($this->config); } - $locale = $this->getPostedLocale($post); - $content->setStatus(current($post['status'])); $content->setPublishedAt(new Carbon($post['publishedAt'])); $content->setDepublishedAt(new Carbon($post['depublishedAt'])); foreach ($post['fields'] as $key => $postfield) { - $this->updateFieldFromPost($key, $postfield, $content); + $this->updateFieldFromPost($key, $postfield, $content, $locale); } return $content; } - private function updateFieldFromPost(string $key, $postfield, Content $content): void + private function updateFieldFromPost(string $key, $postfield, Content $content, string $locale): void { if ($content->hasField($key)) { $field = $content->getField($key); @@ -114,11 +109,31 @@ class ContentEditController extends BaseController } $field->setValue((array) $postfield); + + if ($field->getDefinition()->get('localise')) { + $field->setLocale($locale); + } else { + $field->setLocale(''); + } } - private function getPostedLocale($post) + + private function getEditLocale(Request $request, Content $content): string { - $locale = $post->get('locale'); + $locale = $request->query->get('locale'); + $locales = $content->getLocales(); + + if (!$locales->contains($locale)) { + $locale = $locales->first(); + } + + return $locale; + } + + + private function getPostedLocale(array $post): string + { + $locale = $post['_edit_locale'] ?: ''; return $locale; } diff --git a/src/Controller/Backend/ContentLocalisationController.php b/src/Controller/Backend/ContentLocalisationController.php index 290470ad..60650228 100644 --- a/src/Controller/Backend/ContentLocalisationController.php +++ b/src/Controller/Backend/ContentLocalisationController.php @@ -49,62 +49,4 @@ class ContentLocalisationController extends BaseController ]); } - /** - * @Route("/edit/{id}", name="bolt_content_edit_post", methods={"POST"}) - */ - public function editPost(Request $request, ObjectManager $manager, UrlGeneratorInterface $urlGenerator, ?Content $content = null): Response - { - $token = new CsrfToken('editrecord', $request->request->get('_csrf_token')); - - if (! $this->csrfTokenManager->isTokenValid($token)) { - throw new InvalidCsrfTokenException(); - } - - $content = $this->contentFromPost($content, $request); - - $manager->persist($content); - $manager->flush(); - - $this->addFlash('success', 'content.updated_successfully'); - - $url = $urlGenerator->generate('bolt_content_edit', ['id' => $content->getId()]); - - return new RedirectResponse($url); - } - - private function contentFromPost(?Content $content, Request $request): Content - { - $post = $request->request->all(); - - if (! $content) { - $content = new Content(); - $content->setAuthor($this->getUser()); - $content->setContentType($request->attributes->get('id')); - $content->setConfig($this->config); - } - - $content->setStatus(current($post['status'])); - $content->setPublishedAt(new Carbon($post['publishedAt'])); - $content->setDepublishedAt(new Carbon($post['depublishedAt'])); - - foreach ($post['fields'] as $key => $postfield) { - $this->updateFieldFromPost($key, $postfield, $content); - } - - return $content; - } - - private function updateFieldFromPost(string $key, $postfield, Content $content): void - { - if ($content->hasField($key)) { - $field = $content->getField($key); - } else { - $fields = collect($content->getDefinition()->get('fields')); - $field = Field::factory($fields->get($key)['type']); - $field->setName($key); - $content->addField($field); - } - - $field->setValue((array) $postfield); - } } diff --git a/src/Entity/Content.php b/src/Entity/Content.php index e72b5776..a870a93f 100644 --- a/src/Entity/Content.php +++ b/src/Entity/Content.php @@ -175,6 +175,18 @@ class Content return $this->contentTypeDefinition; } + public function getLocales() + { + $locales = $this->getDefinition()->get('locales'); + + return collect($locales); + } + + public function getDefaultLocale() + { + return $this->getLocales()->first(); + } + public function getSummary(): array { return [ @@ -297,6 +309,52 @@ class Content return $this->fields; } + /** + * @return Collection|Field[] + */ + public function getLocalisedFields(string $locale = '', bool $fallback = true): \Tightenco\Collect\Support\Collection + { + if (!$locale) { + $locale = $this->getDefaultLocale(); + } + + $fields = collect([]); + + foreach ($this->getDefinition()->get('fields') as $name => $field) { + $field = $this->getLocalisedField($name, $locale, $fallback, $field); + $fields->put($name, $field); + } + + return $fields; + } + + public function getLocalisedField(string $name, string $locale = '', bool $fallback = true, array $definition = []) + { + // First, see if we have the field, in the correct locale + foreach ($this->fields as $field) { + if ($field->getName() == $name && in_array($field->getLocale(), [$locale, ''])) { + return $field; + } + } + + // Second, see if we have the field, in the fallback locale + if ($fallback) { + foreach ($this->fields as $field) { + if ($field->getName() == $name && $field->getLocale() == $this->getDefaultLocale()) { + return $field; + } + } + } + + // Third, see if we can create the field on the fly + if (!empty($definition)) { + return Field::factory($definition['type'], $name, $definition); + } + + // Alas, return an empty array + return []; + } + public function hasField(string $name): bool { return collect($this->fields)->contains('name', $name); diff --git a/src/Entity/Field.php b/src/Entity/Field.php index 9c0e6a50..7baa048d 100644 --- a/src/Entity/Field.php +++ b/src/Entity/Field.php @@ -122,15 +122,23 @@ class Field /** * @return Field */ - public static function factory(string $name = 'generic'): self + public static function factory(string $type = 'generic', string $name = '', array $definition = []): self { - $classname = '\\Bolt\\Entity\\Field\\' . ucwords($name) . 'Field'; + $classname = '\\Bolt\\Entity\\Field\\' . ucwords($type) . 'Field'; if (class_exists($classname)) { $field = new $classname(); } else { $field = new self(); } + if (!empty($name)) { + $field->setName($name); + } + + if (!empty($definition)) { + $field->setDefinition($type, $definition); + } + return $field; } diff --git a/src/Twig/LocaleExtension.php b/src/Twig/LocaleExtension.php index 89926734..048d712d 100644 --- a/src/Twig/LocaleExtension.php +++ b/src/Twig/LocaleExtension.php @@ -25,7 +25,7 @@ class LocaleExtension extends AbstractExtension public function __construct(string $locales, UrlGeneratorInterface $urlGenerator) { - $this->localeCodes = explode('|', $locales); + $this->localeCodes = collect(explode('|', $locales)); $this->urlGenerator = $urlGenerator; } @@ -39,6 +39,7 @@ class LocaleExtension extends AbstractExtension return [ new TwigFunction('locales', [$this, 'getLocales'], $env), + new TwigFunction('contentlocales', [$this, 'getContentLocales'], $env), new TwigFunction('locale', [$this, 'getLocale']), new TwigFunction('flag', [$this, 'flag'], $safe), ]; @@ -60,6 +61,18 @@ class LocaleExtension extends AbstractExtension return $this->locales; } + $this->locales = $this->localeHelper($env, $this->localeCodes); + + return $this->locales; + } + + public function getContentLocales(Twig_Environment $env, Collection $localeCodes) + { + return $this->localeHelper($env, $localeCodes); + } + + private function localeHelper(Twig_Environment $env, Collection $localeCodes) + { // Get the route and route params, to set the new localised link $globals = $env->getGlobals(); @@ -68,23 +81,31 @@ class LocaleExtension extends AbstractExtension $route = $request->attributes->get('_route'); $routeParams = $request->attributes->get('_route_params'); - $this->locales = new Collection(); - foreach ($this->localeCodes as $localeCode) { + $locales = new Collection(); + foreach ($localeCodes as $localeCode) { $locale = $this->localeInfo($localeCode); $routeParams['locale'] = $locale->get('code'); $locale->put('link', $this->urlGenerator->generate($route, $routeParams)); - $this->locales->push($locale); + $locales->push($locale); } - return $this->locales; + return $locales; } public function flag($localeCode) { $locale = $this->localeInfo($localeCode); - return $locale->get('flag'); + $html = sprintf( + '', + $locale->get('flag'), + $locale->get('name'), + $locale->get('localisedname'), + $locale->get('code') + ); + + return $html; } private function localeInfo($localeCode) diff --git a/templates/content/edit.html.twig b/templates/content/edit.html.twig index 0343c73a..a88f74c3 100644 --- a/templates/content/edit.html.twig +++ b/templates/content/edit.html.twig @@ -16,7 +16,8 @@ {% block vue_id 'editor' %} {% block topsection %} - {{ dump(record.definition) }} + {{ dump(locales()) }} + {{ dump(contentlocales(locales)) }} {{ dump(locales) }} {{ dump(currentlocale) }} {% endblock %} @@ -26,7 +27,7 @@