[Code snippet] Full user name and link by log-in

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[Code snippet] Full user name and link by log-in

Roman Muntyanu
Sorry for spam, just can't resist sharing my first small victory in programming XWiki :)
XWiki 2.0 Markup-based code-snippet for displaying user's full name with link to profile by log-in.

{{velocity}}
#macro(displayFullNameByLogin $login)
    #set($userDocName = "XWiki."+$login)
    #set($userDoc = $xwiki.getDocument($userDocName))
    #if (!$userDoc)
        $login (does not exist)
    #else
        #set($userObject = $userDoc.getObject("XWiki.XWikiUsers"))
        #if(!$userObject)
          User Sheet
          This stylesheet must be applied on a document containing a XWiki.XWikiUsers object.
        #else
          [[$xwiki.getUserName($userDoc.fullName, false)>>$userDoc.fullName]]
        #end
    #end
#end
{{/velocity}}

Usage
{{velocity}}#displayFullNameByLogin("rmuntyan"){{/velocity}}

Regards,  
 Roman
Reply | Threaded
Open this post in threaded view
|

Re: [Code snippet] Full user name and link by log-in

Thomas Mortagne
Administrator
You should share that kind of things on http://code.xwiki.org ;)

On Thu, Apr 1, 2010 at 18:21, coldserenity <[hidden email]> wrote:

>
> Sorry for spam, just can't resist sharing my first small victory in
> programming XWiki :)
> XWiki 2.0 Markup-based code-snippet for displaying user's full name with
> link to profile by log-in.
>
> {{velocity}}
> #macro(displayFullNameByLogin $login)
>    #set($userDocName = "XWiki."+$login)
>    #set($userDoc = $xwiki.getDocument($userDocName))
>    #if (!$userDoc)
>        $login (does not exist)
>    #else
>        #set($userObject = $userDoc.getObject("XWiki.XWikiUsers"))
>        #if(!$userObject)
>          User Sheet
>          This stylesheet must be applied on a document containing a
> XWiki.XWikiUsers object.
>        #else
>          [[$xwiki.getUserName($userDoc.fullName,
> false)>>$userDoc.fullName]]
>        #end
>    #end
> #end
> {{/velocity}}
>
> Usage
> {{velocity}}#displayFullNameByLogin("rmuntyan"){{/velocity}}
>
> Regards,
>  Roman
> --
> View this message in context: http://n2.nabble.com/Code-snippet-Full-user-name-and-link-by-log-in-tp4838148p4838148.html
> Sent from the XWiki- Users mailing list archive at Nabble.com.
> _______________________________________________
> users mailing list
> [hidden email]
> http://lists.xwiki.org/mailman/listinfo/users
>



--
Thomas Mortagne
_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: [Code snippet] Full user name and link by log-in

vmassol
Administrator

On Apr 1, 2010, at 6:49 PM, Thomas Mortagne wrote:

> You should share that kind of things on http://code.xwiki.org ;)

err wait I think it can improved first :)

> On Thu, Apr 1, 2010 at 18:21, coldserenity <[hidden email]> wrote:
>>
>> Sorry for spam, just can't resist sharing my first small victory in
>> programming XWiki :)
>> XWiki 2.0 Markup-based code-snippet for displaying user's full name with
>> link to profile by log-in.
>>
>> {{velocity}}
>> #macro(displayFullNameByLogin $login)
>>    #set($userDocName = "XWiki."+$login)
>>    #set($userDoc = $xwiki.getDocument($userDocName))
>>    #if (!$userDoc)

This will probably not work since getDocument, contrary to its name, create a document if it doesn't exist... :)

$userDoc.isNew() would work.

There might be a better way to do this but I'll let others offer suggestions... There might even be an api to get what you wanted to do, in one line.

Thanks
-Vincent

>>        $login (does not exist)
>>    #else
>>        #set($userObject = $userDoc.getObject("XWiki.XWikiUsers"))
>>        #if(!$userObject)
>>          User Sheet
>>          This stylesheet must be applied on a document containing a
>> XWiki.XWikiUsers object.
>>        #else
>>          [[$xwiki.getUserName($userDoc.fullName,
>> false)>>$userDoc.fullName]]
>>        #end
>>    #end
>> #end
>> {{/velocity}}
>>
>> Usage
>> {{velocity}}#displayFullNameByLogin("rmuntyan"){{/velocity}}
>>
>> Regards,
>>  Roman
>> --
_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: [Code snippet] Full user name and link by log-in

Roman Muntyanu
Thanks for the info, updated version of the script looks like this
{{velocity}}
#macro(displayFullNameByLogin $login)
    #set($userDocName = "XWiki."+$login)
    {{html}}$xwiki.getUserName($userDocName, true){{/html}}
#end
{{/velocity}}

usage is the same (works as it meant to work: displays Full Name with URL to user's profile if user exists and login otherwise).
Reply | Threaded
Open this post in threaded view
|

Re: [Code snippet] Full user name and link by log-in

vmassol
Administrator

On Apr 2, 2010, at 9:07 AM, coldserenity wrote:

>
> Thanks for the info, updated version of the script looks like this
> {{velocity}}
> #macro(displayFullNameByLogin $login)
>    #set($userDocName = "XWiki."+$login)
>    {{html}}$xwiki.getUserName($userDocName, true){{/html}}
> #end
> {{/velocity}}
>
> usage is the same (works as it meant to work: displays Full Name with URL to
> user's profile if user exists and login otherwise).

Cool, much better indeed :)

Now the next step is to transform this into a wiki macro so that you can write:

{{username name="myname"/}}

The tutorial for doing this is here:
http://platform.xwiki.org/xwiki/bin/view/DevGuide/WikiMacroTutorial

Once you've done this users will also be able to use your macro within the WYSIWYG editor as a normal, standard macro.

-Vincent
_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: [Code snippet] Full user name and link by log-in

Roman Muntyanu
Transformation into XWiki macro went with ease :)
A lot less code in the pages now.

I did an enhancement on the actual snippet itself, now it's capable of accepting comma-separated lists of log-ins, transforming them into comma-separated list of [full-user-name-link-to-user-page].

{{velocity}}
#macro(displayFullNameByLogin $logins)
  ## split incoming string by zero-or-more number of
  ##   ’ ’ (space), ’\t’ (tab) and ’,’ (comma)
  ## symbols. This way there will be less empty elements in the resulting array
  ## Useful links:
  ##  http://platform.xwiki.org/xwiki/bin/view/DevGuide/Scripting#HBindings
  ##  http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/XWikiContext.html
  ##  http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/util/Util.html
  #set($userLoginsArray = $xcontext.util.split(’/[,| |\t]+/’, $logins))
{{html}}
  #foreach( $login in $userLoginsArray )
      #if ("$!login" == "" )
        ## ignore null or empty (see http://wiki.apache.org/velocity/CheckingForNull)
      #else
        #set($userDocName = "XWiki."+$login)
        ## check for last iteration before appending ", "
        ## http://velocity.apache.org/engine/devel/user-guide.html#Loops
        $xwiki.getUserName($userDocName, true)#if( $velocityHasNext ), #end
      #end
  #end
{{/html}}
#end
{{/velocity}}

Thanks for the hints! :)