Login  |  Register
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome, Guest. Please login or register.
Did you miss your activation email?

November 27, 2022, 01:51:55 AM

Login with username, password and session length
Members
  • Total Members: 3853
  • Latest: tty456
Stats
  • Total Posts: 193612
  • Total Topics: 21103
  • Online today: 64
  • Online ever: 3540
  • (September 02, 2022, 06:38:54 PM)
Users Online
Users: 2
Guests: 66
Total: 68

Author Topic: Greetings: Birthday, Holiday, Time of Day  (Read 18562 times)

0 Members and 1 Guest are viewing this topic.

JPDeni

  • Guest
Greetings: Birthday, Holiday, Time of Day
« on: October 22, 2006, 04:48:25 PM »
This isn't a code snippet to go into a block, but rather is a new function that you can use in any of your php blocks or articles or even if you alter the template files.

It returns a greeting to the person, which is one of the following:
"Happy birthday" if it's the person's birthday
a holiday greeting if it's a holiday
a welcome message if it's the member's first day on the site
an anniversary message if it's the anniversary of the member first registering
a time of day greeting if it's just a regular day
a generic greeting that should never be seen, but I added it just in case.  :)

You can have images with the greetings and you can even customize your greetings to the member's country, so that only Americans are wished a "Happy Fourth of July," for example, and Canadians can get Thanksgiving wishes on their Thanksgiving day in October, while Americans will get their greetings in November. (Sorry to be ethno-centric, but I'm not familiar enough with the specific holidays in other countries to be able to give other examples.)

There are several parts to this, but they're pretty straightforward.

Step one:
Copy the following into a new file and save the file as UserFunctions.php. This will go in your Sources directory.
Code: [Select]
<?php

function greeting()
{
  global 
$context$modSettings$user_settings$user_info,$options,$txt,$db_prefix;
  
$time time() + ($user_info['time_offset'] + $modSettings['time_offset']) * 3600;
  
$cur_date getdate($time);
  
$user_info['today_is_birthday'] = 0;
  
$image '';
  
$greeting '';
  isset(
$user_info['smiley_set']) ? $smiley $user_info['smiley_set'] : $smiley 'none';
  isset(
$options['country']) ? $country $options['country'] : $country 'all';
  
loadLanguage('Greetings');
  
$return '';
  
$greeting_array = array();

  if (isset(
$user_settings['birthdate']))
  {
    
$user_info['today_is_birthday'] = $cur_date['mon'] == substr($user_settings['birthdate'], 52) && $cur_date['mday'] == substr($user_settings['birthdate'], 8);
    if (
$user_info['today_is_birthday'])
    {
      if (isset(
$txt['birthday']['all']))
       
$greeting_array[] = 'birthday';
    }
  }
  
$today date('Y-m-d',$time);
  
$annual '0004-'.date('m-d',$time);
  
$query db_query(
      
"SELECT title
       FROM 
{$db_prefix}calendar_holidays
       WHERE eventDate = '
$today'
       OR eventDate = '
$annual'"__FILE____LINE__);
  if (
mysql_num_rows($query))
  {
    while (
$row mysql_fetch_assoc($query))
    {
      
$holiday str_replace(' ','',strtolower(preg_replace('/[^A-Z^a-z^0-9]+/',' ',$row['title']))); 
      if (isset(
$txt[$holiday][$country]) || isset($txt[$holiday]['all'] ))
        
$greeting_array[] = $holiday;
    }
  }
  
mysql_free_result($query);
  
$id $context['user']['id'];
  
$query db_query(" SELECT dateRegistered
FROM 
{$db_prefix}members 
WHERE ID_MEMBER = 
$id
LIMIT 1"
__FILE____LINE__);
  
$row mysql_fetch_assoc($query);  
  
mysql_free_result($query);
  if (
date('d m',$row['dateRegistered']) == date('d m',time()))
  {
    if (
date('Y',$row['dateRegistered']) == date('Y',time()))
    {
      if (isset(
$txt['firstday']))
        
$greeting_array[] = 'firstday';
    }
    elseif (isset(
$txt['anniversary']))
      
$greeting_array[] = 'anniversary';
  }
  if (
array_key_exists('0',$greeting_array))
  {
    
shuffle($greeting_array);
    if (isset(
$txt[$greeting_array[0]][$country]))
      
$greeting $txt[$greeting_array[0]][$country];
    elseif (isset(
$txt[$greeting_array[0]]['all']))
      
$greeting $txt[$greeting_array[0]]['all'];
    if (
$greeting '')
    {
      if (isset(
$txt['img'][$greeting_array[0]][$country]))
        
$image $txt['img'][$greeting_array[0]][$country];
      elseif (isset(
$txt['img'][$greeting_array[0]]['all']))
        
$image $txt['img'][$greeting_array[0]]['all'];
    }
  }
  if (
$greeting == '')
  {
    
$hour $cur_date['hours'];
    if (isset(
$txt['hour'][$hour]))
    {
      
$greeting $txt['hour'][$hour];
      if (isset(
$txt['img']['hour'][$hour]))
        
$image $txt['img']['hour'][$hour];
    }
  }

  if (
$greeting == '')
  {
    
$greeting $txt['generic'];
    if (isset(
$txt['img']['generic']))
      
$image $txt['img']['generic'];
  }
  if (
$image <> '' && $smiley <> 'none')
    
$return '<img src="' $modSettings['smileys_url'] . '/' $smiley '/'.$image.'" alt="'.$greeting.'" border="0" /> ';
  
$return .= $greeting;
  return 
$return;
}

?>


Step Two:
Create another new file, with the following, and save it as Greetings.english.php. You can also translate the file into whatever other languages are appropriate for your site. All of them go into the default/languages directory.

Code: [Select]
<?php

$txt
['generic'] = 'Hey there';

$txt['birthday']['all'] = 'Happy birthday';
$txt['img']['birthday']['all'] = 'birthday.gif';
$txt['anniversary']['all'] = 'Happy Anniversary';
$txt['img']['anniversary']['all'] = 'birthday.gif';
$txt['firstday']['all'] = 'Welcome';
$txt['img']['firstday']['all'] = 'rose.gif';


$txt['christmasday']['all'] = 'Merry Christmas';
$txt['christmasday']['uk'] = 'Happy Christmas';
$txt['img']['christmasday']['all'] = 'xmastree.gif';
$txt['newyearsday']['all'] = 'Happy New Year';
$txt['thanksgivingus']['us'] = 'Happy Thanksgiving';
$txt['img']['thanksgivingus']['us'] = 'turkey.gif';
$txt['thanksgivingcanada']['ca'] = 'Happy Thanksgiving';
$txt['img']['thanksgivingcanada']['ca'] = 'turkey.gif';

$txt['hour'][0]='Hi';
$txt['hour'][1]='Hi';
$txt['hour'][2]='Hi';
$txt['hour'][3]='Hi';
$txt['hour'][4]='Hi';
$txt['hour'][5]='Hi';
$txt['hour'][6]='Good morning';
$txt['img']['hour'][6]='sunrise.gif';
$txt['hour'][7]='Good morning';
$txt['hour'][8]='Good morning';
$txt['hour'][9]='Good morning';
$txt['hour'][10]='Good morning';
$txt['hour'][11]='Good morning';
$txt['hour'][12]='Good afternoon';
$txt['hour'][13]='Good afternoon';
$txt['hour'][14]='Good afternoon';
$txt['hour'][15]='Good afternoon';
$txt['hour'][16]='Good afternoon';
$txt['hour'][17]='Good afternoon';
$txt['hour'][18]='Good evening';
$txt['hour'][19]='Good evening';
$txt['hour'][20]='Good evening';
$txt['hour'][21]='Good evening';
$txt['hour'][22]='Good evening';
$txt['hour'][23]='Good evening';


?>



There are several things to notice here. To come up with the key value for the holiday text -- for example $txt['newyearsday']['all'] -- get the title value that's in the calendar and take out all spaces or punctuation and put the whole thing in lower case. So New Year's Day becomes newyearsday and Hallowe'en becomes halloween. If you have any other punctuation, take that out, too. You want to be left with just letters and numbers.

You don't have to put greetings in for every holiday on your calendar.

Most of the time, the second part of the key -- $txt['newyearsday']['all'] -- will be all. If you have a specific reason to limit it to only one country *and* you have a way for the members to define their country, you can use a country code instead. If you have the CountryFlag mod, you can use that. I intentionally used the same variable name.

Note that you can have one text for a specific country and another text for everyone else. In the case above, someone who is listed as being in the UK would see "Happy Christmas" on the 25th of December, but everyone else would see "Merry Christmas."

The other example is for Thanksgivings. The key values of ['thanksgivingus'] and ['thanksgivingcanada'] assume that there are entries in the holiday calendar like "Thanksgiving (US)" and "Thanksgiving (Canada)".

The names of the image files are identical to the names of the text files, except that the ['img'] is added as the first key. Again, you can have different image files for different countries. But just because you have a specific text for a given country doesn't mean you have to have a different image. In my Christmas example above, even though British folks would see different text, they would still see the same image. You don't have to have an image for every greeting.

You can even have different text and images for each hour of the day.

It doesn't matter what order the $txt array elements are in. I think it's easier to have the image references with the text so I don't get lost.

Step three:
Upload any graphics that you might have defined. They need to go into the smileys directories. If you have more than one set of smilies, you'll need to put your greeting graphics in all of them. I've attached the ones I referenced in my language file just so you have something to start with. These are from Mazeguy. I think it's preferable to have small graphics for these.

Step Four:
Use it!

In whatever place you want to add your greeting, whether it's in a block or an article or even one of the template files, first add

Code: [Select]
require_once['UserFunctions.php']
and in the exact place where you want your greeting to appear, use

Code: [Select]
greeting()
as if it were a variable. You can say something like

Code: [Select]
echo greeting(),', '.$context['user']['name'],'<br />';
which will give you something like
Happy birthday, Xarcell
or
Merry Christmas, G6
or
Good morning, Bloc
or whatever fits for the username and event.

I feel like I've probably over-explained this, so I'll just stop now and answer questions that others might have in case my explanation wasn't clear.

Edited to add:
I've done some editing of the code. Now it includes an "anniversary" day for the person's membership on the forum and if there is more than one event defined on a given day (holidays, birthday, anniversary), it prints out a random greeting each time the page is reloaded.
« Last Edit: November 07, 2006, 09:39:47 AM by JPDeni »

Offline G6Cad

  • Friends
  • *
  • Posts: 12643
    • FamiljeGodis
Re: Greetings: Birthday, Holiday, Time of Day
« Reply #1 on: October 23, 2006, 12:55:51 AM »
This was a fun thing to have JPDeni :)

Thank you for sharing :)

Think this goes better in Block code and snippets board to, so i move it there :)

JPDeni

  • Guest
Re: Greetings: Birthday, Holiday, Time of Day
« Reply #2 on: October 23, 2006, 06:18:34 AM »
That's fine. You're the organizer around here. ;D

akulion

  • Guest
Re: Greetings: Birthday, Holiday, Time of Day
« Reply #3 on: October 23, 2006, 06:50:42 AM »
nice :D thanks :up:



Great job


technodragon73

  • Guest
Re: Greetings: Birthday, Holiday, Time of Day
« Reply #4 on: October 23, 2006, 08:25:27 AM »
ok, I have tried several ways to do this.  I keep getting unable to load UserFunctions.php.  I have tried chmodding it and I know the syntax is correct for calling it
Code: [Select]
require_once($sourcedir.'/UserFunctions.php');

any help trying to figure out where/why it doesn't want to work?

JPDeni

  • Guest
Re: Greetings: Birthday, Holiday, Time of Day
« Reply #5 on: October 23, 2006, 08:40:23 AM »
Just use

Code: [Select]
require_once('UserFunctions.php');
If it's in your Sources directory, the script will find it.

technodragon73

  • Guest
Re: Greetings: Birthday, Holiday, Time of Day
« Reply #6 on: October 23, 2006, 08:54:37 AM »
thank you...that worked!

Though if I may offer a suggestion...

I noticed it worked better when i used
Code: [Select]
',greeting(),' instead.

JPDeni

  • Guest
Re: Greetings: Birthday, Holiday, Time of Day
« Reply #7 on: October 23, 2006, 09:02:40 AM »
Glad it worked. :)
Though if I may offer a suggestion...

I noticed it worked better when i used
Code: [Select]
',greeting(),' instead.
I don't understand. What's your whole line of code?

technodragon73

  • Guest
Re: Greetings: Birthday, Holiday, Time of Day
« Reply #8 on: October 23, 2006, 09:09:27 AM »
Code: [Select]
echo'
      <br />',greeting(),', <br />'.$context['user']['name'],'<br />';

JPDeni

  • Guest
Re: Greetings: Birthday, Holiday, Time of Day
« Reply #9 on: October 23, 2006, 09:19:07 AM »
Oh, yes. Since you already have something that's being echoed and you're having a linebreak before it. The code that I posted was straight out of a block that I made with my own userbox and it's the first line of the code, so I didn't have anything before the greeting. :)

I'm glad you mentioned that, though, because it puts it in a different context to help others understand how to use it.