Skip to content

Commit

Permalink
Allow to set anchor for uri in Admin (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBDev authored and benjamin-hubert committed Jun 30, 2016
1 parent f768cb3 commit a05ccfa
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 22 deletions.
21 changes: 14 additions & 7 deletions Controller/Admin/CRUD/ItemAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,19 @@ public function checkUri($object = null)
$form = $this->getForm();
$request = $this->getRequest();
$uriType = $request->request->get('uri_type');
if (empty($uriType) || !in_array($uriType, [Item::URI_TYPE_EXTERNAL, Item::URI_TYPE_INTERNAL])) {

if (!Item::uriTypeExists($uriType)) {
$form->get('uri')->addError(new FormError(
'Le type d\'url est invalide !'
));

return;
}

if ($uriType === Item::URI_TYPE_ANCHOR) {
return;
}

if ($uriType === Item::URI_TYPE_EXTERNAL) {
$uri = $form->get('uri')->getData();
if (preg_match('/^https?:\/\//', $uri) === 0) {
Expand All @@ -232,13 +237,15 @@ public function preValidate($object = null)

public function postUpdate($object = null)
{
if ($object !== null && $object->getUri() !== null) {
$container = $this->getConfigurationPool()->getContainer();
$checker = $container->get('alpixel_menu.utils.url_checker');
if ($object !== null) {
$url = $object->getUri();
if ($checker->check($url) === URLChecker::URL_PROBLEM) {
$session = $container->get('session');
$session->getFlashBag()->add('warning', 'Cependant une erreur semble être apparue quand nous avons tenté d\'analyser la page "'.$url.'". Vous devriez vérifier que le lien spécifié n\'affiche aucune erreur.');
if ($url !== null) {
$container = $this->getConfigurationPool()->getContainer();
$checker = $container->get('alpixel_menu.utils.url_checker');
if ($checker->check($url) === URLChecker::URL_PROBLEM) {
$session = $container->get('session');
$session->getFlashBag()->add('warning', 'Cependant une erreur semble être apparue quand nous avons tenté d\'analyser la page "'.$url.'". Vous devriez vérifier que le lien spécifié n\'affiche aucune erreur.');
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions Entity/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
class Item implements ItemInterface
{
const URI_TYPE_ANCHOR = 'anchor';
const URI_TYPE_EXTERNAL = 'external';
const URI_TYPE_INTERNAL = 'internal';

Expand Down Expand Up @@ -303,4 +304,18 @@ public function setSlug($slug)

return $this;
}

public static function getUriType()
{
return [
self::URI_TYPE_ANCHOR,
self::URI_TYPE_EXTERNAL,
self::URI_TYPE_INTERNAL,
];
}

public static function uriTypeExists($type)
{
return in_array($type, self::getUriType());
}
}
38 changes: 26 additions & 12 deletions Resources/public/js/item_uri_input.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
function init()
{
var currentUri = $('#input-group-addon-uri + input').val();
var type = 'external';
if (currentUri != null && currentUri.indexOf('/') === 0) {
type = 'internal';
var type = 'anchor';

if (currentUri != null) {
if (currentUri.indexOf('/') === 0) {
type = 'internal';
} else if (currentUri.indexOf('http') === 0) {
type = 'external';
}
}

setIconUriType(type);
Expand All @@ -20,23 +25,32 @@

// Force the / on start of string for internal link
function forceValidInternalLink() {
var currentUri = $('#input-group-addon-uri + input').val();
if (getUriType() === 'internal' && currentUri != null && currentUri.indexOf('/') !== 0) {
$('#input-group-addon-uri + input').val('/'+currentUri);
}
else if (getUriType() === 'external' && currentUri.indexOf('/') === 0) {
currentUri = currentUri.substring(1, currentUri.length);
$('#input-group-addon-uri + input').val(currentUri);
var inputUri = $('#input-group-addon-uri + input');
var currentUri = inputUri.val();
if (currentUri != null) {
if (getUriType() === 'internal' && currentUri.indexOf('/') !== 0) {
inputUri.val('/'+currentUri);
} else if (getUriType() === 'external' && currentUri.indexOf('/') === 0) {
currentUri = currentUri.substring(1, currentUri.length);
inputUri.val(currentUri);
} else if (getUriType() === 'anchor' && currentUri.indexOf('#') !== 0) {
inputUri.val('#'+currentUri);
}
}
}

function setIconUriType(type) {
var iconElement = $('#input-group-addon-uri');

switch (type) {
case 'anchor':
iconElement.html('<i class="fa fa-anchor"></i>');
break;
case 'external':
$('#input-group-addon-uri').html('<i class="fa fa-link"></i>');
iconElement.html('<i class="fa fa-link"></i>');
break
case 'internal':
$('#input-group-addon-uri').html(BASE_URL);
iconElement.html(BASE_URL);
forceValidInternalLink();
break
}
Expand Down
1 change: 1 addition & 0 deletions Resources/views/Form/item_uri.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<div class="row">
<div class="col-xs-1">
<select name="uri_type">
<option value="{{ constant('\\Alpixel\\Bundle\\MenuBundle\\Entity\\Item::URI_TYPE_ANCHOR') }}">Ancre</option>
<option value="{{ constant('\\Alpixel\\Bundle\\MenuBundle\\Entity\\Item::URI_TYPE_INTERNAL') }}">Interne</option>
<option value="{{ constant('\\Alpixel\\Bundle\\MenuBundle\\Entity\\Item::URI_TYPE_EXTERNAL') }}">Externe</option>
</select>
Expand Down
4 changes: 2 additions & 2 deletions Twig/DisplayLinkExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public function menuLink(\Twig_Environment $twig, $link)
{
$globals = $twig->getGlobals();
$app = $globals['app'];
$env = $app->getEnvironment();
$env = $app->getDebug();

if ($env === 'dev' && strpos($link, 'http') === false) {
if ($env && strpos($link, 'http') === false) {
$dev = ($link[0] === '/') ? 'app_dev.php' : 'app_dev.php/';
$link = $dev.$link;
}
Expand Down
6 changes: 5 additions & 1 deletion Validator/Constraints/RouteExistsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
class RouteExistsValidator extends ConstraintValidator
{
private $checker;
private $session;

public function __construct(URLChecker $checker)
{
Expand All @@ -18,6 +17,11 @@ public function __construct(URLChecker $checker)

public function validate($value, Constraint $constraint)
{
// If the first occurrence of $value is the # character it's an anchor any check is done
if (strpos($value, '#') === 0) {
return;
}

$match = true;
$code = $this->checker->check($value);
if ($code === URLChecker::URL_NOT_FOUND) {
Expand Down

0 comments on commit a05ccfa

Please sign in to comment.