Wiki source for FormatUserMethod


Show raw source

===== ##""FormatUser()""## Method=====
++Ticket:221++ (implemented)

>>**See also:**
~-InfoHandler
~-PageAdminAction
~-UserAdmin
>>This is the development page for the ##""FormatUser()""## method, a small utility that allows rendering of user- and hostnames when they are retrieved from the database and printed.

This method renders usernames as links only when needed (when the corresponding userpage exists), avoiding the creation of dozens missing page links for users without userpage. It also makes other options configurable like truncating long hostnames or disabling link formatting.::c::


=== The code ===

The following code should be added to ##libs/Wakka.class.php## in the ##""//USER""## section.


%%(php;802)
/**
* Formatter for usernames.
*
* Renders usernames as links only when needed, avoiding the creation of dozens
* missing page links for users without userpage. Makes other options configurable
* (like truncating long hostnames or disabling link formatting).
*
* @author {@link http://www.wikkawiki.org/DarTar Dario Taraborelli}
* @version 0.1
*
* @param string $user required: name of user or hostname retrieved from the DB;
* @param string $link optional: enables/disables linking to userpage;
* @param string $maxhostlength optional: max length for hostname, hostnames longer
* than this will be truncated with an ellipsis;
* @param string $ellipsis optional: character to be use at the end of truncated hosts;
* @return string $formatted_user: formatted username.
*/
function FormatUser ($user, $link='1', $maxhostlength='10', $ellipsis='…')
{
if (strlen($user)>0)
{
// check if user is registered
if ($this->LoadUser($user))
{
// check if userpage exists and if linking is enabled
$formatted_user = ($this->ExistsPage($user) && ($link == 1))? $this->Link($user) : $user;
}
else
{
// truncate long host names
$user = (strlen($user) > $maxhostlength) ? '<span title="'.$user.'">'.substr($user, 0, $maxhostlength).$ellipsis.'</span>' : $user;
$formatted_user = '<tt>'.$user.'</tt>';
}
}
else
{
// page has empty user field
$formatted_user = 'anonymous';
}
return $formatted_user;
}

%%

----
CategoryDevelopmentCore
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki