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

Lombi
#1 29. März 2009, 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>

Dieser Beitrag wurde von Lombi (am 31. März 2009, 02:13, vor 15 Jahre) bearbeitet
musiconly
#2 29. März 2009, 19:53
This day started very nice :-D I'm gonna shutdown the site on Sunday XD
Lombi
#3 29. März 2009, 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 29. März 2009, 23:14
I love you #2.
Web Design Database - www.wddb.com
Kingsley
#5 30. März 2009, 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 30. März 2009, 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 30. März 2009, 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');

Dieser Beitrag wurde von Koradhil (am 30. März 2009, 17:31, vor 15 Jahre) bearbeitet
dervan
#8 30. März 2009, 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 31. März 2009, 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 17. Mai 2009, 15:51
does this work on seditio? (it's not working for me)