FormatUser() Method

Ticket:221 (implemented)

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.
 



The code


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


  1.      /**
  2.      * Formatter for usernames.
  3.      *
  4.      * Renders usernames as links only when needed, avoiding the creation of dozens
  5.      * missing page links for users without userpage. Makes other options configurable
  6.      * (like truncating long hostnames or disabling link formatting).
  7.      *
  8.      * @author      {@link http://www.wikkawiki.org/DarTar Dario Taraborelli}
  9.      * @version     0.1
  10.      *
  11.      * @param   string  $user   required: name of user or hostname retrieved from the DB;
  12.      * @param   string  $link   optional: enables/disables linking to userpage;
  13.      * @param   string  $maxhostlength  optional: max length for hostname, hostnames longer
  14.      *                              than this will be truncated with an ellipsis;
  15.      * @param   string  $ellipsis   optional: character to be use at the end of truncated hosts;
  16.      * @return string   $formatted_user: formatted username.
  17.      */
  18.     function FormatUser ($user, $link='1', $maxhostlength='10', $ellipsis='…')
  19.     {
  20.         if (strlen($user)>0)
  21.         {
  22.             // check if user is registered
  23.             if ($this->LoadUser($user))
  24.             {
  25.                 // check if userpage exists and if linking is enabled
  26.                 $formatted_user = ($this->ExistsPage($user) && ($link == 1))? $this->Link($user) : $user;
  27.             }
  28.             else
  29.             {
  30.                 // truncate long host names
  31.                 $user = (strlen($user) > $maxhostlength) ? '<span title="'.$user.'">'.substr($user, 0, $maxhostlength).$ellipsis.'</span>' : $user;
  32.                 $formatted_user = '<tt>'.$user.'</tt>';
  33.             }
  34.         }
  35.         else
  36.         {
  37.         // page has empty user field
  38.         $formatted_user = 'anonymous';
  39.         }
  40.         return $formatted_user;
  41.     }



CategoryDevelopmentCore
There are 2 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki