b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (0.9.x) [ class tree: evocore ] [ index: evocore ] [ all elements ]

Source for file _class_calendar.php

Documentation is available at _class_calendar.php

  1. <?php
  2. /**
  3.  * Blog Calendar
  4.  *
  5.  * b2evolution - {@link http://b2evolution.net/}
  6.  * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
  7.  * @copyright (c)2003-2005 by Francois PLANQUE - {@link http://fplanque.net/}
  8.  *
  9.  * @package evocore
  10.  */
  11. if!defined('DB_USER') ) die'Please, do not access this page directly.' );
  12.  
  13. /**
  14.  * Calendar
  15.  *
  16.  * @package evocore
  17.  */
  18. class Calendar
  19. {
  20.     var $blog;
  21.     var $year$month;
  22.     var $specific;                    // WE ASKED FOR A SPECIFIC MONTH
  23.     
  24.     var $mode;                          // 'month' or 'year'
  25.     
  26.     var $where;
  27.     var $request;                        // SQL query string
  28.     var $result;                        // Result set
  29.     var $result_num_rows;        // Number of rows in result set
  30.  
  31.     var $displaycaption;
  32.     var $monthformat;
  33.     var $monthstart;
  34.     var $monthend;
  35.     var $linktomontharchive;
  36.     /**
  37.      * Where to do the navigation
  38.      * 
  39.      * 'caption' or 'tfoot';
  40.      *
  41.      * @var string 
  42.      */
  43.     var $navigation = 'caption';    
  44.  
  45.     var $tablestart;
  46.     var $tableend;
  47.  
  48.     var $rowstart;
  49.     var $rowend;
  50.  
  51.     var $headerdisplay;
  52.     var $headerrowstart;
  53.     var $headerrowend;
  54.     var $headercellstart;
  55.     var $headercellend;
  56.  
  57.     var $cellstart;
  58.     var $cellend;
  59.  
  60.     var $emptycellstart;
  61.     var $emptycellend;
  62.  
  63.     var $emptycellcontent;
  64.  
  65.     var $searchframe;
  66.  
  67.     var $browseyears;
  68.     
  69.     /*
  70.      * Calendar::Calendar(-)
  71.      *
  72.      * Constructor
  73.      */
  74.     function Calendar(
  75.         $blog 1,
  76.         $m '',
  77.         $show_statuses array(),
  78.         $timestamp_min '',        // Do not show posts before this timestamp
  79.         $timestamp_max 'now'  )    // Do not show posts after this timestamp
  80.     {
  81.         global $Settings;
  82.         
  83.         $this->blog = $blog;
  84.  
  85.         // Find out which month to display:
  86.         ifempty($m) )
  87.         {
  88.             $this->year = date('Y');
  89.             $this->month = date('m');
  90.             $this->mode = 'month';
  91.         }
  92.         else
  93.         {
  94.             $this->specific = true;
  95.             $this->year = substr($m04);
  96.             if (strlen($m6)
  97.             // no month provided
  98.                 $this->mode = 'year';
  99.                 if$this->year == date('Y') )
  100.                 // we display current year, month gets current
  101.                     $this->month = date('m');
  102.                 }
  103.                 else
  104.                 // highlight no month, when not current year
  105.                     $this->month = '';
  106.                 }
  107.             }
  108.             else
  109.             {
  110.                 $this->month = substr($m42);
  111.                 $this->mode = 'month';
  112.             }
  113.         }
  114.  
  115.         // CONSTRUCT THE WHERE CLAUSE:
  116.         /*
  117.          * ----------------------------------------------------
  118.          *  Restrict to the statuses we want to show:
  119.          * ----------------------------------------------------
  120.          */
  121.         $where ' AND '.statuses_where_clause$show_statuses );
  122.         $where_link ' AND ';
  123.  
  124.         // Restrict to timestamp limits:
  125.         if$timestamp_min == 'now' $timestamp_min time();
  126.         if!empty($timestamp_min) )
  127.         {    // Hide posts before
  128.             $date_min date('Y-m-d H:i:s'$timestamp_min ($Settings->get('time_difference'3600) );
  129.             $where .= $where_link.' post_issue_date >= \''.$date_min.'\'';
  130.             $where_link ' AND ';
  131.         }
  132.         if$timestamp_max == 'now' $timestamp_max time();
  133.         if!empty($timestamp_max) )
  134.         {    // Hide posts after
  135.             $date_max date('Y-m-d H:i:s'$timestamp_max ($Settings->get('time_difference'3600) );
  136.             $where .= $where_link.' post_issue_date <= \''.$date_max.'\'';
  137.             $where_link ' AND ';
  138.         }
  139.  
  140.         // Do we need to restrict categories:
  141.         if$blog )
  142.         {    // Blog #1 aggregates all
  143.             $where .= $where_link.' cat_blog_ID = '.$blog;
  144.             $where_link ' AND ';
  145.         }
  146.  
  147.         $this->where = $where;
  148.  
  149.  
  150.         // Default styling:
  151.         $this->displaycaption = 1;    // set this to 0 if you don't want to display the month name
  152.         $this->monthformat = 'F Y';
  153.         $this->linktomontharchive = true;  // month displayed as link to month' archive
  154.  
  155.         $this->tablestart = '<table class="bCalendarTable" cellspacing="0" summary="Monthly calendar with links to each day\'s posts">'."\n";
  156.         $this->tableend = '</table>';
  157.  
  158.         $this->monthstart = '<caption class="bCalendarCaption">';
  159.         $this->monthend = "</caption>\n";
  160.  
  161.         $this->rowstart = '<tr class="bCalendarRow">' "\n";
  162.         $this->rowend = "</tr>\n";
  163.  
  164.         $this->headerdisplay = 'D';     // D => 'Fri'; e => 'F', l (lowercase l) => 'Friday'
  165.         // These codes are twisted because they're the same as for date formats.
  166.         // set this to 0 or '' if you don't want to display the "Mon Tue Wed..." header
  167.         
  168.         $this->headerrowstart = '<thead><tr class="bCalendarRow">' "\n";
  169.         $this->headerrowend = "</tr></thead>\n";
  170.         $this->headercellstart = '<th class="bCalendarHeaderCell" abbr="[abbr]" scope="col" title="[abbr]">';    // please leave [abbr] there !
  171.         $this->headercellend = "</th>\n";
  172.  
  173.         $this->cellstart = '<td class="bCalendarCell">';
  174.         $this->cellend = "</td>\n";
  175.  
  176.         $this->emptycellstart = '<td class="bCalendarEmptyCell">';
  177.         $this->emptycellend = "</td>\n";
  178.         $this->emptycellcontent = '&nbsp;';
  179.  
  180.         $this->linkpostcellstart '<td class="bCalendarLinkPost">';
  181.         $this->linkposttodaycellstart '<td class="bCalendarLinkPostToday">';
  182.         $this->todaycellstart '<td id="bCalendarToday">';
  183.         $this->todaycellstartpost '<td id="bCalendarToday" class="bCalendarLinkPost">';
  184.  
  185.         $this->searchframe = 12;    // How many month will we search back for a post before we give up
  186.  
  187.         $this->browseyears = ($this->mode == 'year');  // browsing years from Calendar's caption
  188.         
  189.         /**#@+
  190.          * Display number of posts with days/months
  191.          *
  192.          * - set to '' (empty) to disable
  193.          * - %d gets replaced with the number of posts on that day/month
  194.          */
  195.         $this->postcount_month_cell '';                           // in table cell (behind day)
  196.         $this->postcount_month_cell_one '';                       //  -- " -- [for single post]
  197.         $this->postcount_month_atitle T_('%d posts');                         // in archive links title tag
  198.         $this->postcount_month_atitle_one T_('1 post');                      //  -- " -- [for single post]
  199.         #$this->postcount_year_cell = ' (%d)';                      // in table cell (behind abbr of month)
  200.         $this->postcount_year_cell   '';
  201.         $this->postcount_year_cell_one   '';
  202.         $this->postcount_year_atitle T_('%d posts');                             // in archive links title tag
  203.         $this->postcount_year_atitle_one T_('1 post');                         // in archive links title tag
  204.         /**#@-*/
  205.     }
  206.  
  207.     /*
  208.      * Calendar->set(-)
  209.      *
  210.      * set a variable
  211.      */
  212.     function set$var$value )
  213.     {
  214.         $this->$var $value;
  215.     }
  216.  
  217.     /**
  218.      * Calendar->display(-)
  219.      *
  220.      * display the calendar.
  221.      *
  222.      * @param string 
  223.      * @param string 
  224.      */
  225.     function display$file ''$params '' )    // Page to use for links
  226.     {
  227.         global $DB;
  228.         global $tableposts$tablepostcats$tablecategories;
  229.         global $weekday$weekday_abbrev$weekday_letter$month$month_abbrev;
  230.         global $start_of_week;
  231.         global $Settings;
  232.  
  233.         if$this->mode == 'month' )
  234.         {
  235.             $end_of_week (($start_of_week 77);
  236.     
  237.             // Find a month with posts
  238.             $searchmonth $this->month;
  239.             $searchyear $this->year;
  240.             for$i 0$i $this->searchframe$i++ )
  241.             {
  242.                 $arc_sql "SELECT COUNT(DISTINCT ID), YEAR(post_issue_date), MONTH(post_issue_date), DAYOFMONTH(post_issue_date) AS myday".
  243.                         " FROM ($tableposts INNER JOIN $tablepostcats ON ID = postcat_post_ID)".
  244.                         " INNER JOIN $tablecategories ON postcat_cat_ID = cat_ID".
  245.                         " WHERE MONTH(post_issue_date) = '$searchmonth' AND YEAR(post_issue_date) = '$searchyear".$this->where.
  246.                         " GROUP BY myday".
  247.                         " ORDER BY post_issue_date DESC";
  248.                 $arc_result $DB->get_results$arc_sqlARRAY_A );
  249.                 
  250.                 if$DB->num_rows )
  251.                 {    // OK we have a month with posts!
  252.                     foreach$arc_result as $arc_row )
  253.                     {
  254.                         $daysinmonthwithposts$arc_row['myday'] ] $arc_row['COUNT(DISTINCT ID)'];
  255.                     }
  256.                     $this->month = $searchmonth;
  257.                     $this->year = $searchyear;
  258.                     break// Don't search any further!
  259.                 }
  260.                 elseif ($this->specific)
  261.                 {    // No post, but we asked for a specific month to be displayed
  262.                     break// Don't search any further!
  263.                 }
  264.                 else
  265.                 {    // No, post, let's search in previous month!
  266.                     $searchmonth zeroise(intval($searchmonth)-1,2);
  267.                     if ($searchmonth == '00'{
  268.                         $searchmonth '12';
  269.                         $searchyear ''.(intval($searchyear)-1);
  270.                     }
  271.                 }
  272.             }
  273.     
  274.             // echo $this->month,'.',$this->year;
  275.     
  276.             $daysinmonth intval(date('t'mktime(000$this->month1$this->year)));
  277.             // echo 'days in month=', $daysinmonth;
  278.             $datestartofmonth $this->year.'-'.$this->month.'-01';
  279.             $dateendofmonth $this->year.'-'.$this->month.'-'.$daysinmonth;
  280.     
  281.             // caution: offset bug inside
  282.             $calendarblah get_weekstartend($datestartofmonth$start_of_week);
  283.             if (mysql2date('w'$datestartofmonth== $start_of_week{
  284.                 $calendarfirst $calendarblah['start'3600;     // adjust for daylight savings time
  285.             else {
  286.                 $calendarfirst $calendarblah['end'604799 3600;  // adjust for daylight savings time
  287.             }
  288.             //echo 'calendarfirst=', $calendarfirst;
  289.     
  290.             $calendarblah get_weekstartend($dateendofmonth$end_of_week);
  291.             if (mysql2date('w'$dateendofmonth== $end_of_week{
  292.                 $calendarlast $calendarblah['start'1;
  293.             else {
  294.                 $calendarlast $calendarblah['end'10000;
  295.             }
  296.             //echo 'calendarlast=', $calendarlast;
  297.     
  298.             // here the offset bug is corrected
  299.             if ((intval(date('d'$calendarfirst)) 1&& (intval(date('m'$calendarfirst)) == intval($this->month))) {
  300.                 $calendarfirst $calendarfirst 604800;
  301.             }
  302.     
  303.             // Create links to previous/next month
  304.             $previous_month_link '<a href="'.
  305.                 archive_link( ($this->month > 1$this->year : ($this->year - 1),    ($this->month > 1($this->month - 112''''false$file$params )
  306.                 .'" title="'.T_('previous month').'">&lt;</a>';
  307.     
  308.             $next_month_link '<a href="'.
  309.                 archive_link( ($this->month < 12$this->year : ($this->year + 1)($this->month < 12($this->month + 11''''false$file$params )
  310.                 .'" title="'.T_('next month').'">&gt;</a>';
  311.         }
  312.         else
  313.         // mode is 'year'
  314.             // Find months with posts
  315.             $arc_sql "SELECT COUNT(DISTINCT ID), MONTH(post_issue_date) AS mymonth ".
  316.                         "FROM ($tableposts INNER JOIN $tablepostcats ON ID = postcat_post_ID) ".
  317.                         "INNER JOIN $tablecategories ON postcat_cat_ID = cat_ID ".
  318.                         "WHERE YEAR(post_issue_date) = '$this->year".$this->where.
  319.                         " GROUP BY mymonth".
  320.                         " ORDER BY post_issue_date DESC";
  321.     
  322.             $arc_result $DB->get_results$arc_sqlARRAY_A );
  323.             
  324.             if$DB->num_rows )
  325.             {    // OK we have a month with posts!
  326.                 foreach$arc_result as $arc_row )
  327.                 {
  328.                     $monthswithposts$arc_row['mymonth'] ] $arc_row['COUNT(DISTINCT ID)'];
  329.                 }
  330.             }
  331.         }
  332.         
  333.         if$this->browseyears )
  334.         // create links to previous/next year
  335.             $previous_year_link '<a href="'.
  336.                 archive_link$this->year - 1($this->mode == 'month'$this->month : ''''''false$file$params )
  337.                 .'" title="'.T_('previous year').'">&lt;&lt;</a>&nbsp;&nbsp;';
  338.             $next_year_link '&nbsp;&nbsp;<a href="'.
  339.                 archive_link$this->year + 1($this->mode == 'month'$this->month : ''''''false$file$params )
  340.                 .'" title="'.T_('next year').'">&gt;&gt;</a>';
  341.         }
  342.  
  343.  
  344.         // ** display everything **
  345.  
  346.         echo $this->tablestart;
  347.  
  348.         // CAPTION :
  349.  
  350.         if$this->displaycaption )
  351.         {    // caption:
  352.             echo $this->monthstart;
  353.             
  354.             if$this->navigation == 'caption' )
  355.             {    // Link to previous year:
  356.                 echo isset$previous_year_link $previous_year_link '';
  357.             }
  358.             
  359.             if$this->mode == 'month' )
  360.             {    // MONTH CAPTION:
  361.  
  362.                 if$this->navigation == 'caption' )
  363.                 {    // Link to previous month:
  364.                     echo $previous_month_link.'&nbsp;&nbsp;';
  365.                 }
  366.  
  367.                 if$this->linktomontharchive )
  368.                 {    // chosen month with link to archives
  369.                     echo '<a href="'.archive_link$this->year$this->month''''false$file$params ).'" title="'.T_('go to month\'s archive').'">';
  370.                 }
  371.  
  372.                 echo date_i18n($this->monthformatmktime(000$this->month1$this->year));
  373.  
  374.                 if$this->linktomontharchive )
  375.                 {    // close link to month archive
  376.                     echo '</a>';
  377.                 }
  378.             
  379.                 if$this->navigation == 'caption' )
  380.                 {    // Link to next month:
  381.                     echo '&nbsp;&nbsp;'.$next_month_link;
  382.                 }
  383.             }
  384.             else
  385.             {    // YEAR CAPTION:
  386.                 echo date_i18n('Y'mktime(00011$this->year))// display year
  387.             }
  388.             
  389.             if$this->navigation == 'caption' )
  390.             {    // Link to next year:
  391.                 echo isset$next_year_link $next_year_link '';
  392.             }
  393.             
  394.             echo $this->monthend;
  395.         }
  396.  
  397.         // HEADER :
  398.  
  399.         if!empty($this->headerdisplay&& ($this->mode == 'month') )
  400.         {    // Weekdays:
  401.             echo $this->headerrowstart;
  402.  
  403.             for ($i $start_of_week$i ($start_of_week 7)$i $i 1)
  404.             {
  405.                 echo str_replace('[abbr]'T_($weekday[($i 7)])$this->headercellstart);
  406.                 switch$this->headerdisplay )
  407.                 {
  408.                     case 'e':
  409.                         // e => 'F'
  410.                         echo T_($weekday_letter[($i 7)]);
  411.                         break;
  412.                         
  413.                     case 'l':
  414.                         // l (lowercase l) => 'Friday'
  415.                         echo T_($weekday[($i 7)]);
  416.                         break;
  417.             
  418.                     default:    // Backward compatibility: any non emty value will display this
  419.                         // D => 'Fri'
  420.                         echo T_($weekday_abbrev[($i 7)]);
  421.                 }
  422.  
  423.                 echo $this->headercellend;
  424.             }
  425.             
  426.             echo $this->headerrowend;
  427.         }
  428.  
  429.         if$this->navigation == 'tfoot' )
  430.         {    // We want to display navigation in the table footer:
  431.             // TODO: YEAR MODE support
  432.             echo "<tfoot>\n";
  433.             echo "<tr>\n";
  434.             echo '<td colspan="'.(($this->mode == 'month''3' '2' ).'" id="prev">';
  435.             echo isset$previous_year_link $previous_year_link '';
  436.             echo isset$previous_month_link $previous_month_link '';
  437.             echo "</td>\n";
  438.             if$this->mode == 'month' echo '<td class="pad">&nbsp;</td>'."\n";
  439.             echo '<td colspan="'.(($this->mode == 'month''3' '2' ).'" id="next">';
  440.             echo isset$next_month_link $next_month_link '';
  441.             echo isset$next_year_link $next_year_link '';
  442.             echo "</td>\n";
  443.             echo "</tr>\n";
  444.             echo "</tfoot>\n";
  445.         }
  446.  
  447.         // REAL TABLE DATA :
  448.  
  449.         echo $this->rowstart;
  450.  
  451.         if$this->mode == 'year' )
  452.         {
  453.             for ($i 1$i 13$i $i 1)
  454.             {
  455.                 ifisset($monthswithposts$i ]) )
  456.                 {
  457.                     if$this->month == $i )
  458.                     {
  459.                         echo $this->todaycellstartpost;
  460.                     }
  461.                     else
  462.                     {
  463.                         echo $this->linkpostcellstart;
  464.                     }
  465.                     echo '<a href="';
  466.                     archive_link$this->year$i''''true$file$params );
  467.                     echo '"';
  468.                     if$monthswithposts$i && !empty($this->postcount_year_atitle) )
  469.                     // display postcount
  470.                         echo ' title="'.sprintf($this->postcount_year_atitle$monthswithposts$i ]).'"';
  471.                     }
  472.                     elseif!empty($this->postcount_year_atitle_one) )
  473.                     // display postcount for one post
  474.                         echo ' title="'.sprintf($this->postcount_year_atitle_one1).'"';
  475.                     }
  476.                     echo '>';
  477.                 }
  478.                 elseif$this->month == $i )
  479.                 // current month
  480.                     echo $this->todaycellstart;
  481.                 }
  482.                 else
  483.                 {
  484.                     echo $this->cellstart;
  485.                 }
  486.                 echo T_($month_abbrevzeroise($i2]);
  487.                 
  488.                 ifisset($monthswithposts$i ]) )
  489.                 // close anchor and show how many posts we have for this month
  490.                     if$monthswithposts$i && !empty($this->postcount_year_cell) )
  491.                     // display postcount
  492.                         printf($this->postcount_year_cell$monthswithposts$i ]);
  493.                     }
  494.                     elseif!empty($this->postcount_year_cell_one) )
  495.                     // display postcount for one post
  496.                         printf($this->postcount_year_cell_one1);
  497.                     }
  498.                     echo '</a>';
  499.                 }
  500.                 echo $this->cellend;
  501.                 if$i == || $i == )
  502.                 // new row
  503.                     echo $this->rowend.$this->rowstart;
  504.                 }
  505.             }
  506.         }
  507.         else // mode == 'month'
  508.         {    
  509.             $newrow 0;
  510.             $j 0;
  511.             $k 1;
  512.             
  513.             for($i $calendarfirst$i ($calendarlast 86400)$i $i 86400)
  514.             {    // loop day by day (86400 seconds = 24 hours)
  515.                 if ($newrow == 1)
  516.                 {    // We need to start a new row:
  517.                     if$k $daysinmonth )
  518.                     {    // Last day already displayed!
  519.                         break;
  520.                     }
  521.                     echo $this->rowend;
  522.                     echo $this->rowstart;
  523.                     $newrow 0;
  524.                 }
  525.     
  526.                 if (date('m'$i!= $this->month)
  527.                 {    // empty cell
  528.                     echo $this->emptycellstart;
  529.                     echo $this->emptycellcontent;
  530.                     echo $this->emptycellend;
  531.                 }
  532.                 else
  533.                 {    // This day is in this month
  534.                     $k $k 1;
  535.                     $calendartoday (date('Ymd',$i== date('Ymd'(time(($Settings->get('time_difference'3600))));
  536.     
  537.                     ifisset($daysinmonthwithpostsdate('j'$i]) )
  538.                     {
  539.                         if$calendartoday )
  540.                         {
  541.                             echo $this->todaycellstartpost;
  542.                         }
  543.                         else
  544.                         {
  545.                             echo $this->linkpostcellstart;
  546.                         }
  547.                         echo '<a href="';
  548.                         archive_link$this->year$this->monthdate('d',$i)''true$file$params );
  549.                         echo '"';
  550.                         if$daysinmonthwithpostsdate('j'$i&& !empty($this->postcount_month_atitle) )
  551.                         // display postcount
  552.                             echo ' title="'.sprintf($this->postcount_month_atitle$daysinmonthwithpostsdate('j'$i]).'"';
  553.                         }
  554.                         elseif!empty($this->postcount_month_atitle_one) )
  555.                         // display postcount for one post
  556.                             echo ' title="'.sprintf($this->postcount_month_atitle_one1).'"';
  557.                         }
  558.                         echo '>';
  559.                     }
  560.                     elseif ($calendartoday)
  561.                     {
  562.                         echo $this->todaycellstart;
  563.                     }
  564.                     else
  565.                     {
  566.                         echo $this->cellstart;
  567.                     }
  568.                     echo date('j',$i);
  569.                     ifisset($daysinmonthwithpostsdate('j'$i]) )
  570.                     {
  571.                         if$daysinmonthwithpostsdate('j'$i&& !empty($this->postcount_month_cell) )
  572.                         // display postcount
  573.                             printf($this->postcount_month_cell$daysinmonthwithpostsdate('j'$i]);
  574.                         }
  575.                         elseif!empty($this->postcount_month_cell_one) )
  576.                         // display postcount for one post
  577.                             printf($this->postcount_month_cell_one1);
  578.                         }
  579.                         echo '</a>';
  580.                     }
  581.                     echo $this->cellend;
  582.                 }
  583.                 $j $j 1;
  584.                 if ($j == 7)
  585.                 {    // This was the last day of week, we need to start a new row:
  586.                     $j 0;
  587.                     $newrow 1;
  588.                 }
  589.             // loop day by day
  590.         // mode == 'month'
  591.         
  592.         echo $this->rowend;
  593.         
  594.         echo $this->tableend;
  595.     }  // display(-)
  596.  
  597. }
  598.  
  599. ?>

Documentation generated on Tue, 20 May 2008 01:52:35 +0200 by phpDocumentor 1.4.2