Forums / Cotonti / Extensions / [RELEASE] AutoALIAS <- simple way to autocreate aliases

Lombi
#1 2009-03-29 19:49
This is a great technique that I use to autofill the alias field from title. You can use it with the URL system that I just published.

PAGE.ADD.TPL

Put this before <form

<script language=javascript>
	function setAlias(objvalue){
		document.newpage.newpagealias.value = makesafe(objvalue);
	}

	function makesafe(text)
{
    text = text.toLowerCase();
    text = text.replace(/č/g,   "c");
    text = text.replace(/š/g,   "s");
    text = text.replace(/ž/g,   "z");
    text = text.replace(/ć/g,   "c");
    text = text.replace(/đ/g,   "d");
    text = text.replace(/&/g,   " and ");
    text = text.replace(/%/g,   " percent ");
    text = text.replace(/\W/g,  "_");

    while (text.indexOf("__") >= 0)
    {
        text = text.replace(/__/g, "_");
    }
    
    if ((text.length > 0) && (text.lastIndexOf("_") == (text.length - 1)))
    {
        text = text.substr(0, text.length - 1);
    }

    
    return text;
}
}
</script>

PAGE.EDIT.TPL

Put this before <form

<script language=javascript>
	function setAlias(objvalue){
		document.update.rpagealias.value = makesafe(objvalue);
	}

	function makesafe(text)
{
    text = text.toLowerCase();
    text = text.replace(/č/g,   "c");
    text = text.replace(/š/g,   "s");
    text = text.replace(/ž/g,   "z");
    text = text.replace(/ć/g,   "c");
    text = text.replace(/đ/g,   "d");
    text = text.replace(/&/g,   " and ");
    text = text.replace(/%/g,   " percent ");
    text = text.replace(/\W/g,  "_");

    while (text.indexOf("__") >= 0)
    {
        text = text.replace(/__/g, "_");
    }
    
    if ((text.length > 0) && (text.lastIndexOf("_") == (text.length - 1)))
    {
        text = text.substr(0, text.length - 1);
    }

    
    return text;
} 
}
</script>
<a href="http://www.domenlo.com">Surreal Art</a>
This post was edited by Lombi (2009-03-31 02:13, 15 years ago)
musiconly
#2 2009-03-29 19:53
This day started very nice :-D I'm gonna shutdown the site on Sunday XD
Lombi
#3 2009-03-29 21:47
Remember to change all special characters to their correct "normal" equivalents in your language!
<a href="http://www.domenlo.com">Surreal Art</a>
Brock
#4 2009-03-29 23:14
I love you #2.
Web Design Database - www.wddb.com
Kingsley
#5 2009-03-30 16:54
# Brock : I love you #2.

there's a lot of love here :p
Not that I mind.. Cotonti is a f***ing great system..


Ontopic:

niceeeeee..
Lombi
#6 2009-03-30 17:11
Thanks. The beauty of this over a php equivalent that hooks after you edit it is that you get to change the alias by yourself if you want after you make the title (gets created the second you move your pointer out of the title field)
<a href="http://www.domenlo.com">Surreal Art</a>
GHengeveld
#7 2009-03-30 17:13
There are better ways to replace illegal URI characters from a string. Please have a look here and here. Of course this isn't the prettiest option, but at least it should work for all languages. The best thing to use would be the php command:
$output = preg_replace("/[^A-Za-z0-9]/", "", $input);
Even though it will remove illegal chars instead of replace them.

Also, you could have done with a lot less code if you had used jQuery... Something like:
$("input[name='newpagealias']").val('Default text');
This post was edited by Koradhil (2009-03-30 17:31, 15 years ago)
dervan
#8 2009-03-30 18:48
# Koradhil : The best thing to use would be the php command:
$output = preg_replace("/[^A-Za-z0-9]/", "", $input);
Even though it will remove illegal chars instead of replace them.
and good practice would be to use "transliteration" feature which is supported in Cotonti, like this:
if ($lang != 'en' && is_array($sed_translit))
{
	$output = strtr($input, $sed_translit); // transliterate non-English characters
}
$output = preg_replace("/[^A-Za-z0-9]/", "", $output);

transliteration works when transition tables ($sed_translit, $sed_translitb) are defined in main.lang.php, see system/lang/ru/main.lang.php for example of these tables
Lombi
#9 2009-03-31 02:09
# Koradhil : There are better ways to replace illegal URI characters from a string. Please have a look here and here. Of course this isn't the prettiest option, but at least it should work for all languages.

Noone in their right mind would want to have htmlentity aliases. This was used on slovenian sites so the replaces are for slovenian characters which normalizes them. I'm aware that there may be fancier ways to do it, but it completely does the trick and it's fully client-based.

So no plugs or extra files needed.

The admin can put their own characters in there, if it's an english site it can stay as is.

$output = preg_replace("/[^A-Za-z0-9]/", "", $input);

Obviously noone would want that, since it needs one extra file or even a plugin.

---------------

Btw I noticed that two thirds of the code are for other cool features on the page add/edit page, so I'll remove them.
<a href="http://www.domenlo.com">Surreal Art</a>
musiconly
#10 2009-05-17 15:51
does this work on seditio? (it's not working for me)