marty.net 1001 Postcards Love Love Love Meep Meep ZenZodiac DailyQuotation

YOU ARE HERE:
Date Hooks

HOME

CPSHOP|

|

|

EMAIL MARTY
  marty.net message board
  « Date Hooks »
Welcome, Guest. Please Login or Register.
12/15/17 at 06:22:59
Home Help Search Login Register
   marty.net message board
   About cpshop
   Tutorials
   Date Hooks
(Moderator: Marty)
Pages: 1 Reply Notify of replies Send the topic Print 
   Author  Topic: Date Hooks  (Read 1828 times)
Hex Angel - Patrick
Gold Dragon
****

Posts: 500



Our nature lies in movement; complete calm is death.
   

WWW
Date Hooks
« on: 10/14/07 at 21:09:16 »
Reply with quote Modify message

[EDIT: Fully functional]

This code in the next post activates "date hooks". It's part of my cpdig code, but it could be integrated into cpshop's gethooks() routine.

Here's some reference on how to use the code.


Code:
# call with cpdig_datehook($hookname)
#  $hookname is, well, the name of the hook
#
#  This routine will print the contents of every hook file defined for the current server date
#  Optional header and footer files can be used to wrap the hooks being printed
#
#  Hook format in your template:
# <|datehook:hookname|>
#
#  Create 'datehooks' directory in your cgi-bin and put your date hook files there
#  Also create a sub-directory in 'datehooks' called 'datecache' where your hook output will be efficiently cached on a daily basis
#  
#
#  Hook filename syntax:
# cpshop.cgi.date.yymmdd.YYMMDD.hookid.hookname.txt
#  WHERE:
#      cpshop.cgi :: name of the cpshop script for which the hook is executed
#      ddmmyy :: start date for hook to be active, eg: 070922
#      DDMMYY :: end date for hook to be active, eg: 070923
#            for dates, use '00' to mean ANY day, month or year eg:
#                  cpshop.cgi.000922.000922.RussianBomb.onthisday.txt             is activated every year on September 22.
#                  cpshop.cgi.080922.000922.RussianBomb.onthisday.txt             is activated every year BEGINNING WITH 2008 on September 22.
#                  cphsop.cgi.000001.000001.NewMonth.onthisday.txt             is activated on the first day of every month.
#                  cpshop.cgi.001100.001224.Christmas.holidays.txt             is activated for every day of November and the first 24 days of December.
#   hookid :: unique string allows multiple hook instances for the same dates EG:
#                        cpshop.cgi.000922.000922.RussianBomb.onthisday.txt
#                        cpshop.cgi.000922.000922.LincolnEmancipates.onthisday.txt
#                        cpshop.cgi.000922.000922.NixonSubpoenae.onthisday.txt
#
# Header & Footer files: if a date hook is activated for the current day,
# then cpdig_datehook() looks for header and footer files to wrap around the hook code. EG:
#        cpshop.cgi.date.header.onthisday.txt
#        cpshop.cgi.date.footer.onthisday.txt
#
« Last Edit: 10/20/07 at 13:40:27 by Hex Angel - Patrick » Logged

CPSHOP
www.PeaceAndLoveShop.com Namaste
BLOG
www.TheNomadicSpirit.com Inspired by travel
www.GodBeautyPerfectionLove.com Inspired by spirit
www.MyMuseMoviesMusicAndBooks.com Inspired by the arts
Hex Angel - Patrick
Gold Dragon
****

Posts: 500



Our nature lies in movement; complete calm is death.
   

WWW
Re: Date Hooks
« Reply #1 on: 10/15/07 at 00:09:38 »
Reply with quote Modify message

And here's the actual code.


Code:
sub cpdig_datehook {

     my $hookname = $_[0];
     my $hookcode = "";
     my $filepath = "";
     
     
     my $dir = ${script_dir} . "datehooks/";

     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
     $mon += 1;
     $year -= 100;
     if ($mday < 10) { $mday = "0" . $mday; }
     if ($mon < 10) { $mon = "0" . $mon; }
     if ($year < 10) { $year = "0" . $year; }
     
     my $cache_file = $dir . "datecache/" .  ${main_script} . ".date." . $year . $mon . $mday . "." . $hookname . ".txt";
     use Time::Local;
     
     # if we've cached today's date, display the file contents
     if (-e $cache_file)  
     {
           if (-s $cache_file)
           {
                 open(DATECACHE,$cache_file) || die;
                 local $/;
                 $hookcode = <DATECACHE>;
                 close(DATECACHE);
           } else {
                 $hookcode = "";
           }
     } else {
           # find datehook files for today's date
           opendir(DIRHANDLE, $dir) || die "Cannot opendir $dir: $!";
           
           foreach $file (readdir(DIRHANDLE)) {
                       
                 if ( $file =~ m/${main_script}\.date\.(\d{2})(\d{2})(\d{2})\.(\d{2})(\d{2})(\d{2})\..*?\.$hookname.txt/) {
                             # convert wildcards to today's date
                       $first_day = ($3 ne "00") ? $3 : $mday;
                       $first_month = ($2 ne "00") ? $2 : $mon;
                       $first_year = ($1 ne "00") ? $1 : $year;
                       $last_day = ($6 ne "00") ? $6 : $mday;
                       $last_month = ($5 ne "00") ? $5 : $mon;
                       $last_year = ($4 ne "00") ? $4 : $year;
                       my $year_added = FALSE;

                             # When the last_... values are 00 we've got potential month/year end spanning issues when other first_... values are larger than their last_... counterparts
                             # EG 001201.000121 is obviously intended to span from december 1, 2007 to january 21, 2008
                             # Add a month or year if the first_ day/month is larger than the last_day/month
                             # this will catch most issues
                       if ( $4 == 0) {
                             $last_month += ($6 < $3) ? 1 : 0;
                             if ($last_month eq 13) {
                                   $last_month = 1;
                                   if ($5 == 0) {
                                         $last_year += ($5 < $2) ? 1 : 0;
                                         if ($last_year < 10) { $last_year = "0" . $last_year; }
                                         $year_added = TRUE;
                                   }
                             }
                             if ($last_month < 10) { $last_month = "0" . $last_month; }
                       }
                       if ($5 == 0 && $year_added == FALSE) {
                             $last_year += ($5 < $2) ? 1 : 0;
                             if ($last_year < 10) { $last_year = "0" . $last_year; }
                       }
                                   
                       if ( ( timelocal(0,0,0,$first_day, $first_month-1, $first_year+100 ) le  timelocal(0,0,0,$mday,$mon-1,$year+100) ) &&
                            ( timelocal(0,0,0,$last_day, $last_month-1, $last_year+100 ) ge  timelocal(0,0,0,$mday,$mon-1,$year+100) ) )
                       {
                             $filepath = $dir . '/' . $file;
                             open(HOOK,$filepath);
                             { local $/; $hookcode .= <HOOK>; }
                             close(HOOK);
                             
                       }
                 }
           }
           
           closedir(DIRHANDLE);
           
           if ($hookcode ne "") {
                 $filepath = $dir . ${main_script} . ".date.header." . $hookname . ".txt";
                 if (-s $filepath)
                 {
                       open(DATEHEADER,$filepath);
                       local $/;
                       $hookcode = <DATEHEADER> . $hookcode;
                       close(DATEHEADER);
                 }
                                               
                 $filepath = $dir . ${main_script} . ".date.footer." . $hookname . ".txt";
                 if (-e $filepath)
                 {
                       open(DATEFOOTER,$filepath);
                       local $/;
                       $hookcode .= <DATEFOOTER>;
                       close(DATEFOOTER);
                 }
                                                                 
           }
           
           open(DATECACHE, '>', $cache_file); # || die "cannot open output file ($cache_file)";
           local $/;
           print { DATECACHE } $hookcode;
           close(DATECACHE);
           
     }
                 
     return $hookcode;

}
« Last Edit: 10/20/07 at 13:44:44 by Hex Angel - Patrick » Logged

CPSHOP
www.PeaceAndLoveShop.com Namaste
BLOG
www.TheNomadicSpirit.com Inspired by travel
www.GodBeautyPerfectionLove.com Inspired by spirit
www.MyMuseMoviesMusicAndBooks.com Inspired by the arts
Hex Angel - Patrick
Gold Dragon
****

Posts: 500



Our nature lies in movement; complete calm is death.
   

WWW
Re: Date Hooks
« Reply #2 on: 10/19/07 at 10:27:23 »
Reply with quote Modify message

Oh, if you want to see this at work:

http://www.criticalgraphics.com

Look in the sidebar for "On This Day" or "Upcoming Events".  There should be something in one or both of those most days.

p.
Logged

CPSHOP
www.PeaceAndLoveShop.com Namaste
BLOG
www.TheNomadicSpirit.com Inspired by travel
www.GodBeautyPerfectionLove.com Inspired by spirit
www.MyMuseMoviesMusicAndBooks.com Inspired by the arts
hazeleyes7
Gold Dragon
****

Gender: female
Posts: 212



Labs Rock!
   

WWW E-Mail
Re: Date Hooks
« Reply #3 on: 10/19/07 at 13:08:11 »
Reply with quote Modify message

I looked into various sections of your shop and I don't see either "On This Day" or "Upcoming Events".  
Logged

https://shannonksteffen.com
http://www.dogwiregifts.com
http://www.8pawsup.com
http://www.angelcove.us
http://www.catwiregifts.com
Hex Angel - Patrick
Gold Dragon
****

Posts: 500



Our nature lies in movement; complete calm is death.
   

WWW
Re: Date Hooks
« Reply #4 on: 10/20/07 at 01:09:35 »
Reply with quote Modify message

Ooops.  Hangin' browser means multiple posts.
« Last Edit: 10/20/07 at 01:27:47 by Hex Angel - Patrick » Logged

CPSHOP
www.PeaceAndLoveShop.com Namaste
BLOG
www.TheNomadicSpirit.com Inspired by travel
www.GodBeautyPerfectionLove.com Inspired by spirit
www.MyMuseMoviesMusicAndBooks.com Inspired by the arts
Hex Angel - Patrick
Gold Dragon
****

Posts: 500



Our nature lies in movement; complete calm is death.
   

WWW
Re: Date Hooks
« Reply #5 on: 10/20/07 at 01:15:32 »
Reply with quote Modify message

Ooops. Hanging browser means multiple posts...
« Last Edit: 10/20/07 at 01:28:22 by Hex Angel - Patrick » Logged

CPSHOP
www.PeaceAndLoveShop.com Namaste
BLOG
www.TheNomadicSpirit.com Inspired by travel
www.GodBeautyPerfectionLove.com Inspired by spirit
www.MyMuseMoviesMusicAndBooks.com Inspired by the arts
Hex Angel - Patrick
Gold Dragon
****

Posts: 500



Our nature lies in movement; complete calm is death.
   

WWW
Re: Date Hooks
« Reply #6 on: 10/20/07 at 01:20:00 »
Reply with quote Modify message

Funky.

There's an upcoming event: halloween, down the left sidebar after "Browse Designs".  It's appearing on the main page and every page containing thumbnails (sections or products) and the product pages.

No "on this day"s until closer to the end of October.

If you still don't see it, let me know.

Cheers,

p.
Logged

CPSHOP
www.PeaceAndLoveShop.com Namaste
BLOG
www.TheNomadicSpirit.com Inspired by travel
www.GodBeautyPerfectionLove.com Inspired by spirit
www.MyMuseMoviesMusicAndBooks.com Inspired by the arts
Pages: 1 Reply Notify of replies Send the topic Print 
Jump to:


marty.net message board (http://www.marty.net/cgi/marty/board/messageboard)

Powered by YaBB 1 Gold - Release (Yet Another Bulletin Board)
Copyright 2000-2001, X-Null. All Rights Reserved.