web/wp-content/plugins/event-calendar/template-functions.php.old
branchwordpress
changeset 127 3a1f99a92698
parent 126 9b7d841b551a
child 128 5a07ca65fe4d
equal deleted inserted replaced
126:9b7d841b551a 127:3a1f99a92698
     1 <?php
       
     2 /*
       
     3 Copyright (c) 2005-2008, Alex Tingle.  $Revision: 287 $
       
     4 
       
     5 This program is free software; you can redistribute it and/or
       
     6 modify it under the terms of the GNU General Public License
       
     7 as published by the Free Software Foundation; either version 2
       
     8 of the License, or (at your option) any later version.
       
     9 
       
    10 This program is distributed in the hope that it will be useful,
       
    11 but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 GNU General Public License for more details.
       
    14 
       
    15 You should have received a copy of the GNU General Public License
       
    16 along with this program; if not, write to the Free Software
       
    17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    18 */
       
    19 
       
    20 
       
    21 /** Report an error if EventCalendar not yet installed. */
       
    22 function ec3_check_installed($title)
       
    23 {
       
    24   global $ec3;
       
    25   if(!$ec3->event_category)
       
    26   {?>
       
    27     <div style="background-color:black; color:red; border:2px solid red; padding:1em">
       
    28      <div style="font-size:large"><?php echo $title; ?></div>
       
    29      <?php _e('You must choose an event category.','ec3'); ?>
       
    30      <a style="color:red; text-decoration:underline" href="<?php echo
       
    31        get_option('home');?>/wp-admin/options-general.php?page=ec3_admin">
       
    32       <?php _e('Go to Event Calendar Options','ec3'); ?>
       
    33      </a>
       
    34     </div>
       
    35    <?php
       
    36   }
       
    37   return $ec3->event_category;
       
    38 }
       
    39 
       
    40 
       
    41 /** Calculate the header (days of week). */
       
    42 function ec3_util_thead()
       
    43 {
       
    44   global
       
    45     $ec3,
       
    46     $weekday,
       
    47     $weekday_abbrev,
       
    48     $weekday_initial;
       
    49 
       
    50   $result="<thead><tr>\n";
       
    51 
       
    52   $start_of_week =intval( get_option('start_of_week') );
       
    53   for($i=0; $i<7; $i++)
       
    54   {
       
    55     $full_day_name=$weekday[ ($i+$start_of_week) % 7 ];
       
    56     if(3==$ec3->day_length)
       
    57         $display_day_name=$weekday_abbrev[$full_day_name];
       
    58     elseif($ec3->day_length<3)
       
    59         $display_day_name=$weekday_initial[$full_day_name];
       
    60     else
       
    61         $display_day_name=$full_day_name;
       
    62     $result.="\t<th abbr='$full_day_name' scope='col' title='$full_day_name'>"
       
    63            . "$display_day_name</th>\n";
       
    64   }
       
    65 
       
    66   $result.="</tr></thead>\n";
       
    67   return $result;
       
    68 }
       
    69 
       
    70 
       
    71 /** Echos the event calendar navigation controls. */
       
    72 function ec3_get_calendar_nav($date,$num_months)
       
    73 {
       
    74   global $ec3;
       
    75   echo "<table class='nav'><tbody><tr>\n";
       
    76 
       
    77   // Previous
       
    78   $prev=$date->prev_month();
       
    79   echo "\t<td id='prev'><a id='ec3_prev' href='" . $prev->month_link() . "'"
       
    80      . '>&laquo;&nbsp;' . $prev->month_abbrev() . "</a></td>\n";
       
    81 
       
    82   echo "\t<td><img id='ec3_spinner' style='display:none' src='" 
       
    83      . $ec3->myfiles . "/ec_load.gif' alt='spinner' />\n";
       
    84   // iCalendar link.
       
    85   $webcal=get_option('home') . "/?ec3_ical";
       
    86   // Macintosh always understands webcal:// protocol.
       
    87   // It's hard to guess on other platforms, so stick to http://
       
    88   if(strstr($_SERVER['HTTP_USER_AGENT'],'Mac OS X'))
       
    89       $webcal=preg_replace('/^http:/','webcal:',$webcal);
       
    90   echo "\t    <a id='ec3_publish' href='$webcal'"
       
    91      . " title='" . __('Subscribe to iCalendar.','ec3') ."'>\n"
       
    92      . "\t     <img src='$ec3->myfiles/publish.gif' alt='iCalendar' />\n"
       
    93      . "\t    </a>\n";
       
    94   echo "\t</td>\n";
       
    95 
       
    96   // Next
       
    97   $next=$date->plus_months($num_months);
       
    98   echo "\t<td id='next'><a id='ec3_next' href='" . $next->month_link() . "'"
       
    99      . '>' . $next->month_abbrev() . "&nbsp;&raquo;</a></td>\n";
       
   100 
       
   101   echo "</tr></tbody></table>\n";
       
   102 }
       
   103 
       
   104 
       
   105 /** Generates an array of all 'ec3_Day's between the start of
       
   106  *  begin_month & end_month. Indexed by day_id.
       
   107  *  month_id is in the form: ec3_<year_num>_<month_num> */
       
   108 function ec3_util_calendar_days($begin_month_id,$end_month_id)
       
   109 {
       
   110   $begin_date=date('Y-m-d 00:00:00',ec3_dayid2php($begin_month_id));
       
   111   $end_date  =date('Y-m-d 00:00:00',ec3_dayid2php($end_month_id));
       
   112   global $ec3, $wpdb;
       
   113 
       
   114   $sql=
       
   115     "SELECT DISTINCT
       
   116        id,
       
   117        post_title,
       
   118        GREATEST(start,'$begin_date') AS start_date,
       
   119        LEAST(end,'$end_date') AS end_date,
       
   120        allday,
       
   121        1 AS is_event
       
   122      FROM $wpdb->posts,$ec3->schedule
       
   123      WHERE post_status='publish'
       
   124        AND post_type='post'
       
   125        AND post_id=id
       
   126        AND end>='$begin_date'
       
   127        AND start<'$end_date'";
       
   128   if(!$ec3->show_only_events)
       
   129   {
       
   130     // We are interested in normal posts, as well as events.
       
   131     $sql="( $sql ) UNION
       
   132      ( SELECT DISTINCT
       
   133          id,
       
   134          post_title,
       
   135          post_date AS start_date,
       
   136          post_date AS end_date,
       
   137          0 AS allday,
       
   138          0 AS is_event
       
   139        FROM $wpdb->posts
       
   140        WHERE post_status='publish'
       
   141          AND post_type='post'
       
   142          AND post_date>='$begin_date'
       
   143          AND post_date<'$end_date'
       
   144          AND post_date<NOW()
       
   145      )";
       
   146   }
       
   147   $sql.=' ORDER BY id, allday DESC, start_date, is_event DESC';
       
   148   $calendar_entries = $wpdb->get_results($sql);
       
   149 
       
   150   $calendar_days = array(); // result
       
   151   if(!$calendar_entries)
       
   152       return $calendar_days;
       
   153 
       
   154   // In advanced mode, we don't want to show events as blog posts in the cal.
       
   155   $ignore_post_ids=array();
       
   156   if($ec3->advanced && !$ec3->show_only_events)
       
   157   {
       
   158     foreach($calendar_entries as $ent)
       
   159       if($ent->is_event)
       
   160         $ignore_post_ids[] = $ent->id;
       
   161   }
       
   162 
       
   163   $current_post_id=0;
       
   164   $current_day_id ='';
       
   165   $time_format=get_option('time_format');
       
   166   $allday=str_replace(' ','&#160;',__('all day','ec3')); // #160==nbsp
       
   167   foreach($calendar_entries as $ent)
       
   168   {
       
   169     if(!$ent->is_event && in_array($ent->id,$ignore_post_ids))
       
   170         continue;
       
   171     if($current_post_id!=$ent->id)
       
   172     {
       
   173       $current_post_id=$ent->id;
       
   174       $current_day_id='';
       
   175     }
       
   176     $date=ec3_mysql2date($ent->start_date);
       
   177     $end_date=ec3_mysql2date($ent->end_date);
       
   178     while(true)
       
   179     {
       
   180       $day_id=$date->day_id();
       
   181       if($current_day_id==$day_id)
       
   182           break;
       
   183       $current_day_id=$day_id;
       
   184       if(empty($calendar_days[$day_id]))
       
   185           $calendar_days[$day_id] = new ec3_Day();
       
   186 
       
   187       if($ent->allday)
       
   188           $time=$allday;
       
   189       else
       
   190           $time=mysql2date($time_format,$ent->start_date);
       
   191       //?? Should only record start time on FIRST day.
       
   192       $calendar_days[$day_id]->add_post($ent->post_title,$time,$ent->is_event);
       
   193       if($date->to_unixdate()==$end_date->to_unixdate())
       
   194         break;
       
   195       $date->increment_day();
       
   196     }
       
   197   }
       
   198   return $calendar_days;
       
   199 }
       
   200 
       
   201 /** Echos one event calendar month table. */
       
   202 function ec3_get_calendar_month($date,$calendar_days,$thead)
       
   203 {
       
   204   global $ec3;
       
   205   //
       
   206   // Table start.
       
   207   $title=
       
   208     sprintf(__('View posts for %1$s %2$s'),$date->month_name(),$date->year_num);
       
   209   echo "<table id='" . $date->month_id() . "'>\n<caption>"
       
   210     . '<a href="' . $date->month_link() . '" title="' . $title . '">'
       
   211     . $date->month_name() . ' ' . $date->year_num . "</a></caption>\n";
       
   212   echo $thead;
       
   213 
       
   214   //
       
   215   // Table body
       
   216   echo "<tbody>\n\t<tr>";
       
   217 
       
   218   $days_in_month =$date->days_in_month();
       
   219   $week_day=( $date->week_day() + 7 - intval(get_option('start_of_week')) ) % 7;
       
   220   $col =0;
       
   221   
       
   222   while(True)
       
   223   {
       
   224     if($col>6)
       
   225     {
       
   226       echo "</tr>\n\t<tr>";
       
   227       $col=0;
       
   228     }
       
   229     if($col<$week_day)
       
   230     {
       
   231       // insert padding
       
   232       $pad=$week_day-$col;
       
   233       echo "<td colspan='$pad' class='pad'>&nbsp;</td>";
       
   234       $col=$week_day;
       
   235     }
       
   236     // insert day
       
   237     $day_id = $date->day_id();
       
   238     echo "<td id='$day_id'";
       
   239 
       
   240     if(array_key_exists($day_id,$calendar_days))
       
   241     {
       
   242       echo ' class="ec3_postday';
       
   243       if($calendar_days[$day_id]->is_event)
       
   244           echo ' ec3_eventday';
       
   245       echo '">';
       
   246       echo '<a href="' . $date->day_link()
       
   247          . '" title="' . $calendar_days[$day_id]->get_titles() . '"';
       
   248       if($calendar_days[$day_id]->is_event)
       
   249           echo ' class="eventday"';
       
   250       echo ">$date->day_num</a>";
       
   251     }
       
   252     else
       
   253     {
       
   254       echo '>' . $date->day_num;
       
   255     }
       
   256 
       
   257     echo '</td>';
       
   258 
       
   259     $col++;
       
   260     $date->increment_day();
       
   261     if(1==$date->day_num)
       
   262         break;
       
   263     $week_day=($week_day+1) % 7;
       
   264   }
       
   265   // insert padding
       
   266   $pad=7-$col;
       
   267   if($pad>1)
       
   268       echo "<td colspan='$pad' class='pad' style='vertical-align:bottom'>"
       
   269       . "<a href='http://blog.firetree.net/?ec3_version=$ec3->version'"
       
   270       . " title='Event Calendar $ec3->version'"
       
   271       . ($ec3->hide_logo? " style='display:none'>": ">")
       
   272       . "<span class='ec3_ec'><span>EC</span></span></a></td>";
       
   273   elseif($pad)
       
   274       echo "<td colspan='$pad' class='pad'>&nbsp;</td>";
       
   275 
       
   276   echo "</tr>\n</tbody>\n</table>";
       
   277 }
       
   278 
       
   279 
       
   280 /** Template function. Call this from your template to insert the
       
   281  *  Event Calendar. */
       
   282 function ec3_get_calendar()
       
   283 {
       
   284   if(!ec3_check_installed(__('Event Calendar','ec3')))
       
   285     return;
       
   286   global $ec3;
       
   287 
       
   288   // Can't cope with more than one calendar on the same page. Everything has
       
   289   // a unique ID, so it can't be duplicated.
       
   290   // Simple fix for problem: Just ignore all calls after the first.
       
   291   $ec3->call_count++;
       
   292   if($ec3->call_count>1)
       
   293   {
       
   294     echo "<!-- You can only have one Event Calendar on each page. -->\n";
       
   295     return;
       
   296   }
       
   297 
       
   298   echo "<div id='wp-calendar'>\n";
       
   299 
       
   300   $this_month = new ec3_Date();
       
   301 
       
   302   // Display navigation panel.
       
   303   if(0==$ec3->navigation)
       
   304     ec3_get_calendar_nav($this_month,$ec3->num_months);
       
   305   
       
   306   // Get entries
       
   307   $end_month=$this_month->plus_months($ec3->num_months);
       
   308   $calendar_days =
       
   309     ec3_util_calendar_days(
       
   310       $this_month->month_id(),
       
   311       $end_month->month_id()
       
   312     );
       
   313 
       
   314   // Display months.
       
   315   $thead=ec3_util_thead();
       
   316   for($i=0; $i<$ec3->num_months; $i++)
       
   317   {
       
   318     $next_month=$this_month->next_month();
       
   319     ec3_get_calendar_month($this_month,$calendar_days,$thead);
       
   320     $this_month=$next_month;
       
   321   }
       
   322 
       
   323   // Display navigation panel.
       
   324   if(1==$ec3->navigation)
       
   325     ec3_get_calendar_nav(new ec3_Date(),$ec3->num_months);
       
   326 
       
   327   echo "</div>\n";
       
   328 
       
   329   if(!$ec3->disable_popups)
       
   330     echo "\t<script type='text/javascript' src='"
       
   331     .    $ec3->myfiles . "/popup.js'></script>\n";
       
   332 }
       
   333 
       
   334 
       
   335 /** Substitutes placeholders like '%key%' in $format with 'value' from $data
       
   336  *  array. */
       
   337 function ec3_format_str($format,$data)
       
   338 {
       
   339   foreach($data as $k=>$v)
       
   340       $format=str_replace("%$k%",$v,$format);
       
   341   return $format;
       
   342 }
       
   343 
       
   344 
       
   345 define('EC3_DEFAULT_TEMPLATE_EVENT','<a href="%LINK%">%TITLE% (%TIME%)</a>');
       
   346 define('EC3_DEFAULT_TEMPLATE_DAY',  '%DATE%:');
       
   347 define('EC3_DEFAULT_DATE_FORMAT',   'j F');
       
   348 define('EC3_DEFAULT_TEMPLATE_MONTH','');
       
   349 define('EC3_DEFAULT_MONTH_FORMAT',  'F Y');
       
   350 
       
   351 /** Template function. Call this from your template to insert a list of
       
   352  *  forthcoming events. Available template variables are:
       
   353  *   - template_day: %DATE% %SINCE% (only with Time Since plugin)
       
   354  *   - template_event: %DATE% %TIME% %LINK% %TITLE% %AUTHOR%
       
   355  */
       
   356 function ec3_get_events(
       
   357   $limit,
       
   358   $template_event=EC3_DEFAULT_TEMPLATE_EVENT,
       
   359   $template_day  =EC3_DEFAULT_TEMPLATE_DAY,
       
   360   $date_format   =EC3_DEFAULT_DATE_FORMAT,
       
   361   $template_month=EC3_DEFAULT_TEMPLATE_MONTH,
       
   362   $month_format  =EC3_DEFAULT_MONTH_FORMAT)
       
   363 {
       
   364   if(!ec3_check_installed(__('Upcoming Events','ec3')))
       
   365     return;
       
   366   global $ec3,$wpdb,$wp_version;
       
   367 
       
   368   // Parse $limit:
       
   369   //  NUMBER      - limits number of posts
       
   370   //  NUMBER days - next NUMBER of days
       
   371   if(empty($limit))
       
   372   {
       
   373     $limit_numposts='LIMIT 5';
       
   374   }
       
   375   elseif(preg_match('/^ *([0-9]+) *d(ays?)?/',$limit,$matches))
       
   376   {
       
   377     $secs=intval($matches[1])*24*3600;
       
   378     $and_before="AND start<='".ec3_strftime('%Y-%m-%d',time()+$secs)."'";
       
   379   }
       
   380   elseif(intval($limit)<1)
       
   381   {
       
   382     $limit_numposts='LIMIT 5';
       
   383   }
       
   384   else
       
   385   {
       
   386     $limit_numposts='LIMIT '.intval($limit);
       
   387   }
       
   388   
       
   389   if(!$date_format)
       
   390       $date_format=get_option('date_format');
       
   391 
       
   392   // Find the upcoming events.
       
   393   $calendar_entries = $wpdb->get_results(
       
   394     "SELECT DISTINCT
       
   395        p.id AS id,
       
   396        post_title,
       
   397        start,
       
   398        u.$ec3->wp_user_nicename AS author,
       
   399        allday
       
   400      FROM $ec3->schedule s
       
   401      LEFT JOIN $wpdb->posts p ON s.post_id=p.id
       
   402      LEFT JOIN $wpdb->users u ON p.post_author = u.id
       
   403      WHERE p.post_status='publish'
       
   404        AND end>='$ec3->today' $and_before
       
   405      ORDER BY start $limit_numposts"
       
   406   );
       
   407 
       
   408   echo "<ul class='ec3_events'>";
       
   409   echo "<!-- Generated by Event Calendar v$ec3->version -->\n";
       
   410   if($calendar_entries)
       
   411   {
       
   412     $time_format=get_option('time_format');
       
   413     $current_month=false;
       
   414     $current_date=false;
       
   415     $data=array();
       
   416     foreach($calendar_entries as $entry)
       
   417     {
       
   418       // To use %SINCE%, you need Dunstan's 'Time Since' plugin.
       
   419       if(function_exists('time_since'))
       
   420           $data['SINCE']=time_since( time(), ec3_to_time($entry->start) );
       
   421 
       
   422       // Month changed?
       
   423       $data['MONTH']=mysql2date($month_format,$entry->start);
       
   424       if((!$current_month || $current_month!=$data['MONTH']) && $template_month)
       
   425       {
       
   426         if($current_date)
       
   427             echo "</ul></li>\n";
       
   428         if($current_month)
       
   429             echo "</ul></li>\n";
       
   430         echo "<li class='ec3_list ec3_list_month'>"
       
   431         .    ec3_format_str($template_month,$data)."\n<ul>\n";
       
   432         $current_month=$data['MONTH'];
       
   433         $current_date=false;
       
   434       }
       
   435 
       
   436       // Date changed?
       
   437       $data['DATE'] =mysql2date($date_format, $entry->start);
       
   438       if((!$current_date || $current_date!=$data['DATE']) && $template_day)
       
   439       {
       
   440         if($current_date)
       
   441             echo "</ul></li>\n";
       
   442         echo "<li class='ec3_list ec3_list_day'>"
       
   443         .    ec3_format_str($template_day,$data)."\n<ul>\n";
       
   444         $current_date=$data['DATE'];
       
   445       }
       
   446 
       
   447       if($entry->allday)
       
   448           $data['TIME']=__('all day','ec3');
       
   449       else
       
   450           $data['TIME']=mysql2date($time_format,$entry->start);
       
   451 
       
   452       $data['TITLE'] =
       
   453         htmlentities(
       
   454           stripslashes(strip_tags($entry->post_title)),
       
   455           ENT_QUOTES,get_option('blog_charset')
       
   456         );
       
   457       $data['LINK']  =get_permalink($entry->id);
       
   458       $data['AUTHOR']=
       
   459         htmlentities($entry->author,ENT_QUOTES,get_option('blog_charset'));
       
   460       echo " <li>".ec3_format_str($template_event,$data)."</li>\n";
       
   461     }
       
   462     if($current_date)
       
   463         echo "</ul></li>\n";
       
   464     if($current_month)
       
   465         echo "</ul></li>\n";
       
   466   }
       
   467   else
       
   468   {
       
   469     echo "<li>".__('No events.','ec3')."</li>\n";
       
   470   }
       
   471   echo "</ul>\n";
       
   472 }
       
   473 
       
   474 define('EC3_DEFAULT_FORMAT_SINGLE','<tr><td colspan="3">%s</td></tr>');
       
   475 define('EC3_DEFAULT_FORMAT_RANGE','<tr><td class="ec3_start">%1$s</td>'
       
   476  . '<td class="ec3_to">%3$s</td><td class="ec3_end">%2$s</td></tr>');
       
   477 define('EC3_DEFAULT_FORMAT_WRAPPER','<table class="ec3_schedule">%s</table>');
       
   478 
       
   479 /** Formats the schedule for the current post.
       
   480  *  Returns the HTML fragment as a string. */
       
   481 function ec3_get_schedule(
       
   482   $format_single =EC3_DEFAULT_FORMAT_SINGLE,
       
   483   $format_range  =EC3_DEFAULT_FORMAT_RANGE,
       
   484   $format_wrapper=EC3_DEFAULT_FORMAT_WRAPPER
       
   485 )
       
   486 {
       
   487   global $ec3,$post;
       
   488   // Should have been set by ec3_filter_the_posts()
       
   489   if(!$post || !$post->ec3_schedule)
       
   490       return '';
       
   491   $result='';
       
   492   $date_format=get_option('date_format');
       
   493   $time_format=get_option('time_format');
       
   494   $current=false;
       
   495   foreach($post->ec3_schedule as $s)
       
   496   {
       
   497     $date_start=mysql2date($date_format,$s->start);
       
   498     $date_end  =mysql2date($date_format,$s->end);
       
   499     $time_start=mysql2date($time_format,$s->start);
       
   500     $time_end  =mysql2date($time_format,$s->end);
       
   501 
       
   502     if($s->allday)
       
   503     {
       
   504       if($date_start!=$date_end)
       
   505       {
       
   506         $result.=sprintf($format_range,$date_start,$date_end,__('to','ec3'));
       
   507       }
       
   508       elseif($date_start!=$current)
       
   509       {
       
   510         $current=$date_start;
       
   511         $result.=sprintf($format_single,$date_start);
       
   512       }
       
   513     }
       
   514     else
       
   515     {
       
   516       if($date_start!=$date_end)
       
   517       {
       
   518         $current=$date_start;
       
   519         $result.=sprintf($format_range,
       
   520           "$date_start $time_start","$date_end $time_end",__('to','ec3'));
       
   521       }
       
   522       else
       
   523       {
       
   524         if($date_start!=$current)
       
   525         {
       
   526           $current=$date_start;
       
   527           $result.=sprintf($format_single,$date_start);
       
   528         }
       
   529         if($time_start==$time_end)
       
   530           $result.=sprintf($format_single,$time_start);
       
   531         else
       
   532           $result.=sprintf($format_range,$time_start,$time_end,__('to','ec3'));
       
   533       }
       
   534     }
       
   535   }
       
   536   return sprintf($format_wrapper,$result);
       
   537 }
       
   538 
       
   539 
       
   540 /** Echos the schedule for the current post. */
       
   541 function ec3_the_schedule(
       
   542   $format_single =EC3_DEFAULT_FORMAT_SINGLE,
       
   543   $format_range  =EC3_DEFAULT_FORMAT_RANGE,
       
   544   $format_wrapper=EC3_DEFAULT_FORMAT_WRAPPER
       
   545 )
       
   546 {
       
   547   echo ec3_get_schedule($format_single,$format_range,$format_wrapper);
       
   548 }
       
   549 
       
   550 ?>