Skip to content

Commit

Permalink
Merge pull request #1129 from Victoire/feature/add-permalink
Browse files Browse the repository at this point in the history
Feature/add permalink
  • Loading branch information
lenybernard authored Jun 3, 2019
2 parents b4fc68b + 01d80ae commit 116b109
Show file tree
Hide file tree
Showing 25 changed files with 278 additions and 31 deletions.
1 change: 1 addition & 0 deletions Bundle/BlogBundle/Builder/BlogReferenceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function buildReference(View $view, EntityManager $em)
$viewReference->setName($view->getName());
$viewReference->setViewId($view->getId());
$viewReference->setSlug($view->getSlug());
$viewReference->setPermalink($view->getPermalink());
$viewReference->setViewNamespace($em->getClassMetadata(get_class($view))->name);
if ($parent = $view->getParent()) {
$viewReference->setParent(ViewReferenceHelper::generateViewReferenceId($parent));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function buildReference(View $businessPage, EntityManager $em)
$businessPageReference->setViewId($businessPage->getId());
$businessPageReference->setTemplateId($businessPage->getTemplate()->getId());
$businessPageReference->setSlug($businessPage->getSlug());
$businessPageReference->setPermalink($businessPage->getPermalink());
$businessPageReference->setEntityId($businessPage->getBusinessEntity()->getId());
$businessPageReference->setEntityNamespace($em->getClassMetadata(get_class($businessPage->getBusinessEntity()))->name);
$businessPageReference->setViewNamespace($em->getClassMetadata(get_class($businessPage))->name);
Expand Down
13 changes: 13 additions & 0 deletions Bundle/CoreBundle/Entity/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Knp\DoctrineBehaviors\Model\Translatable\Translatable;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Victoire\Bundle\BusinessPageBundle\Entity\VirtualBusinessPage;
use Victoire\Bundle\CoreBundle\Validator\Constraints as Assert;
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;

Expand All @@ -25,6 +26,7 @@
* )
* @ORM\Table("vic_view")
* @ORM\HasLifecycleCallbacks
* @Assert\UniquePermalink()
*/
abstract class View
{
Expand Down Expand Up @@ -766,6 +768,17 @@ public function setSlug($slug, $locale = null)
$this->mergeNewTranslations();
}

public function getPermalink()
{
return PropertyAccess::createPropertyAccessor()->getValue($this->translate(null, false), 'getPermalink');
}

public function setPermalink($permalink, $locale = null)
{
$this->translate($locale, false)->setPermalink($permalink);
$this->mergeNewTranslations();
}

/**
* @return array
*/
Expand Down
4 changes: 4 additions & 0 deletions Bundle/CoreBundle/Entity/WebViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ public function isPublished();
public function isHomepage();

public function setHomepage($homepage);

public function getPermalink();

public function setPermalink($permalink);
}
6 changes: 6 additions & 0 deletions Bundle/CoreBundle/Form/ViewType.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'label' => 'form.page.type.slug.label',
'field_type' => UrlvalidatedType::class,
];
$translationOptions['fields']['permalink'] = [
'label' => 'form.page.type.permalink.label',
'field_type' => UrlvalidatedType::class,
'parentUrl' => '',
'allow_empty' => true,
];
}
$form->add('translations', TranslationsType::class, $translationOptions);
}
Expand Down
10 changes: 7 additions & 3 deletions Bundle/CoreBundle/Helper/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ public function buildUrl(WebViewInterface $view)
if (!(method_exists($view, 'isHomepage') && $view->isHomepage())) {
$slug = [$view->getSlug()];
}

//get the slug of the parents
$url = $this->getParentSlugs($view, $slug);
// if permalink is set
if ($view->getPermalink() != null && $view->getPermalink() != '') {
$url = [$view->getPermalink()];
} else {
//get the slug of the parents
$url = $this->getParentSlugs($view, $slug);
}

//reorder the list of slugs
$url = array_reverse($url);
Expand Down
10 changes: 10 additions & 0 deletions Bundle/CoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@ services:
tags:
- { name: doctrine.event_subscriber, connection: default }

# ================== Validator ================== #
Victoire\Bundle\CoreBundle\Validator\UniquePermalinkValidator:
arguments:
- "@victoire_view_reference.repository"
- "@request_stack"
- "@victoire_core.url_builder"
tags:
- { name: 'validator.constraint_validator', alias: 'victoire.validator.view.permalink_unique' }

# ================== MENU ================== #

victoire_core.admin_menu_builder:
class: Victoire\Bundle\CoreBundle\Menu\MenuBuilder
arguments: [ '@knp_menu.factory', '@security.authorization_checker' ]
Expand Down
11 changes: 11 additions & 0 deletions Bundle/CoreBundle/Resources/translations/validators.en.xliff
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" target-language="en" datatype="plaintext" original="not.available">
<body>
<trans-unit id="564432933783321f38899" resname="victoire.url.alreadyInUse">
<source>victoire.url.alreadyInUse</source>
<target>The url is already in use.</target>
</trans-unit>
</body>
</file>
</xliff>
11 changes: 11 additions & 0 deletions Bundle/CoreBundle/Resources/translations/validators.es.xliff
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" target-language="es" datatype="plaintext" original="not.available">
<body>
<trans-unit id="564432933783321f38899" resname="victoire.url.alreadyInUse">
<source>victoire.url.alreadyInUse</source>
<target>La url ya está en uso.</target>
</trans-unit>
</body>
</file>
</xliff>
11 changes: 11 additions & 0 deletions Bundle/CoreBundle/Resources/translations/validators.fr.xliff
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" target-language="fr" datatype="plaintext" original="not.available">
<body>
<trans-unit id="564432933783321f38899" resname="victoire.url.alreadyInUse">
<source>victoire.url.alreadyInUse</source>
<target>L'url est déjà utilisée.</target>
</trans-unit>
</body>
</file>
</xliff>
22 changes: 22 additions & 0 deletions Bundle/CoreBundle/Validator/Constraints/UniquePermalink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Victoire\Bundle\CoreBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Victoire\Bundle\CoreBundle\Validator\UniquePermalinkValidator;

/**
* @Annotation
*/
class UniquePermalink extends Constraint
{
public function validatedBy()
{
return UniquePermalinkValidator::class;
}

public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}
51 changes: 51 additions & 0 deletions Bundle/CoreBundle/Validator/UniquePermalinkValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Victoire\Bundle\CoreBundle\Validator;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface;
use Victoire\Bundle\CoreBundle\Helper\UrlBuilder;
use Victoire\Bundle\ViewReferenceBundle\Connector\ViewReferenceRepository;

class UniquePermalinkValidator extends ConstraintValidator
{
private $viewReferenceRepository;
private $requestStack;
private $urlBuilder;

/**
* UniquePermalinkValidator constructor.
*
* @param ViewReferenceRepository $viewReferenceRepository
* @param RequestStack $requestStack
* @param UrlBuilder $urlBuilder
*/
public function __construct(ViewReferenceRepository $viewReferenceRepository, RequestStack $requestStack, UrlBuilder $urlBuilder)
{
$this->viewReferenceRepository = $viewReferenceRepository;
$this->requestStack = $requestStack;
$this->urlBuilder = $urlBuilder;
}

/**
* @param View $view
* @param Constraint $constraint
*/
public function validate($view, Constraint $constraint)
{
if ($view instanceof WebViewInterface && ($url = $this->urlBuilder->buildUrl($view)) != '') {
$viewReference = $this->viewReferenceRepository->getReferenceByUrl(
$url,
$this->requestStack->getCurrentRequest()->getLocale()
);

if ($viewReference && $viewReference->getViewId() != $view->getId()) {
$this->context->buildViolation('victoire.url.alreadyInUse')
->atPath('permalink')
->addViolation();
}
}
}
}
4 changes: 4 additions & 0 deletions Bundle/FormBundle/Form/Type/UrlvalidatedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public function buildView(FormView $view, FormInterface $form, array $options)
}

$parentUrl = $page->getParent() ? $page->getParent()->getUrl() : '';
if (isset($options['parentUrl'])) {
$parentUrl = $options['parentUrl'];
}
$url = $this->router->generate('victoire_core_page_show', ['url' => $parentUrl, '_locale' => $locale], Router::ABSOLUTE_URL);
$view->vars['base_url'] = $url;

Expand All @@ -49,6 +52,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefined('parentUrl');
parent::configureOptions($resolver);
}

Expand Down
2 changes: 1 addition & 1 deletion Bundle/FormBundle/Resources/public/js/slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function displaySlugIcons(inputSlug, slug, allowEmpty){
}
}else{
if (allowEmpty && slug != 'undefined') {
correctSlugIcon.removeClass('v-hidden');
correctSlugIcon.addClass('v-hidden');
notCorrectSlugIcon.addClass('v-hidden');
} else {
correctSlugIcon.addClass('v-hidden');
Expand Down
20 changes: 11 additions & 9 deletions Bundle/FormBundle/Resources/views/Form/fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -723,15 +723,17 @@
}}
{% endfor %}
{% else %}
<span class="vic-help-{{ block('vic_error_type') }}">
{% for error in errors %}
{% set _errorMessage =
error.messagePluralization is null
? error.messageTemplate|trans(error.messageParameters, 'validators')
: error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators')
%}<span title="{{ _errorMessage|striptags }}">{{ _errorMessage|raw }}</span> <br>
{% endfor %}
</span>
{% embed "VictoireUIBundle:Bricks:alert.html.twig" with { theme: "danger" } %}
{% block body %}
{% for error in errors %}
{% set _errorMessage =
error.messagePluralization is null
? error.messageTemplate|trans(error.messageParameters, 'validators')
: error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators')
%}<span title="{{ _errorMessage|striptags }}">{{ _errorMessage|raw }}</span> <br>
{% endfor %}
{% endblock body %}
{% endembed %}
{% endif %}
{% endif %}
{% endif %}
Expand Down
33 changes: 33 additions & 0 deletions Bundle/I18nBundle/Entity/ViewTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ class ViewTranslation
*/
protected $name;

/**
* @var string
*
* @Assert\NotBlank()
* @ORM\Column(name="permalink", type="string", length=255, nullable=true)
* @Serializer\Groups({"search"})
*/
protected $permalink;

/**
* @var string
*
Expand Down Expand Up @@ -72,6 +81,30 @@ public function setName($name)
return $this;
}

/**
* Get permalink.
*
* @return string
*/
public function getPermalink()
{
return $this->permalink;
}

/**
* Set permalink.
*
* @param string $permalink
*
* @return View
*/
public function setPermalink($permalink)
{
$this->permalink = $permalink;

return $this;
}

/**
* Set slug.
*
Expand Down
1 change: 1 addition & 0 deletions Bundle/PageBundle/Builder/PageReferenceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function buildReference(View $view, EntityManager $em)
$viewReference->setName($view->getName());
$viewReference->setViewId($view->getId());
$viewReference->setSlug($view->isHomepage() ? '' : $view->getSlug());
$viewReference->setPermalink($view->getPermalink());
$viewReference->setViewNamespace(ClassUtils::getClass($view));
if ($parent = $view->getParent()) {
$parent->translate($view->getCurrentLocale());
Expand Down
8 changes: 6 additions & 2 deletions Bundle/PageBundle/Resources/translations/victoire.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@
<source>modal.form.template.settings.title</source>
<target><![CDATA[Template settings <strong>%templateName%</strong>]]></target>
</trans-unit>
<trans-unit id="58893b99eaf9e" resname="form.page.type.slug.label">
<trans-unit id="58893b544f9e" resname="form.page.type.slug.label">
<source>form.page.type.slug.label</source>
<target>URL</target>
<target>Slug</target>
</trans-unit>
<trans-unit id="58893b99eaf9e" resname="form.page.type.permalink.label">
<source>form.page.type.permalink.label</source>
<target>Permalink</target>
</trans-unit>
<trans-unit id="58893b99eb008" resname="form.page.type.status.label">
<source>form.page.type.status.label</source>
Expand Down
6 changes: 5 additions & 1 deletion Bundle/PageBundle/Resources/translations/victoire.es.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@
</trans-unit>
<trans-unit id="58893b99ecb6c" resname="form.page.type.slug.label">
<source>form.page.type.slug.label</source>
<target>URL</target>
<target>Slug</target>
</trans-unit>
<trans-unit id="5889d329ecb6c" resname="form.page.type.permalink.label">
<source>form.page.type.permalink.label</source>
<target>Permalink</target>
</trans-unit>
<trans-unit id="58893b99ecb92" resname="form.page.type.status.label">
<source>form.page.type.status.label</source>
Expand Down
6 changes: 5 additions & 1 deletion Bundle/PageBundle/Resources/translations/victoire.fr.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
</trans-unit>
<trans-unit id="58893b99ed74d" resname="form.page.type.slug.label">
<source>form.page.type.slug.label</source>
<target>URL</target>
<target>Slug</target>
</trans-unit>
<trans-unit id="58893322ed74d" resname="form.page.type.permalink.label">
<source>form.page.type.permalink.label</source>
<target>Permalink</target>
</trans-unit>
<trans-unit id="58893b99ed756" resname="form.page.type.status.label">
<source>form.page.type.status.label</source>
Expand Down
3 changes: 3 additions & 0 deletions Bundle/PageBundle/Resources/views/Page/settings.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<div class="vic-tab-pane vic-active" id="basics">

<form method="POST" action="{{ path('victoire_core_page_settings_post', {'id': page.id} ) }}">

{{ form_errors(form) }}

<div class="v-grid">
<div class="v-col v-col--md-6">
{{ form_row(form.status) }}
Expand Down
Loading

0 comments on commit 116b109

Please sign in to comment.