Announcement

#1 2010-08-09 21:19:42

LucMorizur
Member
Vienne (Isère) - France
2009-04-30
168

Include username in a text

Hi;

this feature being asked for quite often I think, here is a way to include username in a text (category description, [extension by plg] PWG Stuffs block...) :
copy following code into your Personnal Plugin thanks to LocalFiles Editor

Code:

// js_username_prefilter_trigger
// Adds a JavaScript variable named USERNAME in the header template,
// if this Smarty variable is set.
//
// no variables, no return
//
add_event_handler('init', 'js_username_prefilter_trigger');

function js_username_prefilter_trigger() {
  global $template;
  $template->set_prefilter('header', 'js_username_prefilter');
}

function js_username_prefilter($content, &$smarty) {
  return preg_replace(
    '/<\/head>/',
    '</head> <script type="text/javascript"> var USERNAME = "{if isset($USERNAME)}{$USERNAME}{/if}"; </script>',
    $content
  );
}

Once the Personnal Plugin activated in the plugins list, perform a compiled templates purge : using Piwigo 2.1.x, Admin > Tools > Maintenance > Purge compiled templates .

Now a text will be able to display the user name thanks to the bold part of following text :

Currently testing.<br />
<script type="text/javascript">
if (USERNAME != '') document.write('Hi, ' + USERNAME + '!<br />');
</script>

And here we are!

With this example, that I put in a PWG Stuffs block, one can see :

Currently testing.
And here we are!

when no user is currently identified;
and:

Currently testing.
Hi, Luc!
And here we are!

when user "Luc" is currently identified.

Demo:

as guest ;
identified as user "Piwigo" (thanks to [extension by LucMorizur] Event Cats (pardon for hidden advertising ;-) !)).
Do create other users to check that it works properly!

:-)


Our gallery : Le Site à Nous (a silly name in french, but here I don't care ;-) ! )
An event, a new gallery ? Plugin Event Cats
My test gallery : Tests Piwigo de Luc
Thanksalot for this beautiful project.

Offline

 

#2 2010-08-10 15:07:27

LucMorizur
Member
Vienne (Isère) - France
2009-04-30
168

Re: Include username in a text

Of course, P@t has given a better solution :-/ ... :

Just add the following code into your Personnal Plugin :

add_event_handler('loc_end_page_tail', 'js_username_replace');

function js_username_replace()
{
  global $template, $user;

  if (!defined('IN_ADMIN')) $template->append('footer_elements', '
    <script type="text/javascript" >
    var regex = /USERNAME/g;
    document.body.innerHTML = document.body.innerHTML.replace(regex,"'.$user['username'].'");
    </script >');
}

Now you just need to write the following into your text:

Hi USERNAME!

To the attention of intersted people, P@t also precises that using prefilters is useless to add elements in header.tpl, as smarty variable $head_elements can be used, just the same as $footer_elements in above code.

Thanks P@t :-) !

Last edited by LucMorizur (2012-05-13 21:11:04)


Our gallery : Le Site à Nous (a silly name in french, but here I don't care ;-) ! )
An event, a new gallery ? Plugin Event Cats
My test gallery : Tests Piwigo de Luc
Thanksalot for this beautiful project.

Offline

 

Board footer

Powered by FluxBB

github twitter facebook google+ newsletter Donate Piwigo.org © 2002-2019 · Contact