Ignore:
Timestamp:
Feb 23, 2006, 5:53:11 PM (18 years ago)
Author:
plg
Message:

modification: DAY() MySQL function replaced by DAYOFMONTH() to improve
backward compatibility (this function was added in MySQL 4.1)

bug fixed: with chronology mode, PWG displays thumbnails on main page if
even if no category (which will soon be called "section") is set. This was
producing warnings on category.php from include/category_default.inc.php.

refactoring: on include/calendar_base.class.php and
include/functions_calendar.inc.php. Unix file format, coding guidelines,
etc. While trying to understand the code, I've made some presentation
modification to clarify variable names and so on.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/calendar_base.class.php

    r1050 r1053  
    3030class CalendarBase
    3131{
    32 // db column on which this calendar works
    33 var $date_field;
    34 // used for queries (INNER JOIN or normal)
    35 var $inner_sql;
    36 // base url used when generating html links
    37 var $url_base;
     32  // db column on which this calendar works
     33  var $date_field;
     34  // used for queries (INNER JOIN or normal)
     35  var $inner_sql;
     36  // base url used when generating html links
     37  var $url_base;
    3838
    39 function get_date_where()
    40 {
    41   die("get_date_where not extended");
    42 }
     39  function get_date_where()
     40  {
     41    die("get_date_where not extended");
     42  }
    4343
    44 /**
    45  * Initialize the calendar
    46  * @param string date_field db column on which this calendar works
    47  * @param string inner_sql used for queries (INNER JOIN or normal)
    48  */
    49 function initialize($date_field, $inner_sql)
    50 {
    51   $this->date_field = $date_field;
    52   $this->inner_sql = $inner_sql;
    53 }
     44  /**
     45   * Initialize the calendar
     46   * @param string date_field db column on which this calendar works
     47   * @param string inner_sql used for queries (INNER JOIN or normal)
     48   */
     49  function initialize($date_field, $inner_sql)
     50  {
     51    $this->date_field = $date_field;
     52    $this->inner_sql = $inner_sql;
     53  }
    5454
    5555//--------------------------------------------------------- private members ---
    56 /**
    57  * Creates a calendar navigation bar.
    58  * @param string url_base - links start with this root
    59  * @param array items - hash of items to put in the bar (e.g. 2005,2006)
    60  * @param array selected_item - item currently selected (e.g. 2005)
    61  * @param string class_prefix - html class attribute prefix for span elements
    62  * @param bool allow_any - adds any to the end of the bar
    63  * @param array labels - optional labels for items (e.g. Jan,Feb,...)
    64  * @return string the navigation bar
    65  */
    66 function get_nav_bar_from_items($url_base, $items, $selected_item,
    67                                 $class_prefix, $allow_any, $labels=null)
    68 {
    69   $nav_bar='';
    70   foreach ($items as $item => $nb_images)
     56
     57  /**
     58   * Creates a calendar navigation bar.
     59   *
     60   * @param string url_base - links start with this root
     61   * @param array items - hash of items to put in the bar (e.g. 2005,2006)
     62   * @param array selected_item - item currently selected (e.g. 2005)
     63   * @param string class_prefix - html class attribute prefix for span elements
     64   * @param bool allow_any - adds any to the end of the bar
     65   * @param array labels - optional labels for items (e.g. Jan,Feb,...)
     66   * @return string the navigation bar
     67   */
     68  function get_nav_bar_from_items($url_base, $items, $selected_item,
     69                                  $class_prefix, $allow_any, $labels=null)
    7170  {
    72     $label = $item;
    73     if (isset($labels[$item]))
     71    $nav_bar = '';
     72     
     73    foreach ($items as $item => $nb_images)
    7474    {
    75       $label = $labels[$item];
     75      $label = $item;
     76      if (isset($labels[$item]))
     77      {
     78        $label = $labels[$item];
     79      }
     80      if (isset($selected_item) and $item == $selected_item)
     81      {
     82        $nav_bar .= '<span class="'.$class_prefix.'Sel">';
     83        $nav_bar .= $label;
     84      }
     85      else
     86      {
     87        $nav_bar .= '<span class="'.$class_prefix.'">';
     88        $url = $url_base . $item;
     89        $nav_bar .= '<a href="'.$url.'">';
     90        $nav_bar .= $label;
     91        $nav_bar .= '</a>';
     92      }
     93      if ($nb_images > 0)
     94      {
     95        $nav_bar .= '('.$nb_images.')';
     96      }
     97      $nav_bar.= '</span>';
    7698    }
    77     if ( isset($selected_item) and $item==$selected_item )
     99
     100    if ($allow_any and count($items) > 1)
    78101    {
    79       $nav_bar .= '<span class="'.$class_prefix.'Sel">';
    80       $nav_bar .= $label;
     102      $label = l10n('calendar_any');
     103      if (isset($selected_item) and 'any' == $selected_item)
     104      {
     105        $nav_bar .= '<span class="'.$class_prefix.'Sel">';
     106        $nav_bar .= $label;
     107      }
     108      else
     109      {
     110        $nav_bar .= '<span class="'.$class_prefix.'">';
     111        $url = $url_base . 'any';
     112        $nav_bar .= '<a href="'.$url.'">';
     113        $nav_bar .= $label;
     114        $nav_bar .= '</a>';
     115      }
     116      $nav_bar.= '</span>';
    81117    }
    82     else
    83     {
    84       $nav_bar .= '<span class="'.$class_prefix.'">';
    85       $url = $url_base . $item;
    86       $nav_bar .= '<a href="'.$url.'">';
    87       $nav_bar .= $label;
    88       $nav_bar .= '</a>';
    89     }
    90     if ($nb_images>0)
    91     {
    92       $nav_bar .= '('.$nb_images.')';
    93     }
    94     $nav_bar.= '</span>';
     118    return $nav_bar;
    95119  }
    96120
    97   if ($allow_any and count($items)>1 )
     121  /**
     122   * Creates a calendar navigation bar for a given level.
     123   *
     124   * @param string view_type - list or calendar (e.g. 'l' or 'c')
     125   * @param array requested - array of current selected elements (e.g. 2005,10)
     126   * @param string sql_func - YEAR/MONTH/DAY/WEEK/DAYOFWEEK ...
     127   * @param string sql_offset - (e.g. +1 for WEEK - first in year is 1)
     128   * @param array labels - optional labels to show in the navigation bar
     129   * @return void
     130   */
     131  function build_nav_bar($view_type, $requested, $level, $sql_func,
     132                         $sql_offset='', $labels=null)
    98133  {
    99     $label = l10n('calendar_any');
    100     if ( isset($selected_item) and 'any'==$selected_item )
     134    global $template;
     135   
     136    $query = '
     137SELECT DISTINCT('.$sql_func.'('.$this->date_field.')'.$sql_offset
     138      .') as period';
     139    $query.= $this->inner_sql;
     140    $query.= $this->get_date_where($requested, $level);
     141    $query.= '
     142  GROUP BY period
     143;';
     144
     145    $level_items = array();
     146    $result = pwg_query($query);
     147    while ($row = mysql_fetch_array($result))
    101148    {
    102       $nav_bar .= '<span class="'.$class_prefix.'Sel">';
    103       $nav_bar .= $label;
     149      $level_items[$row['period']] = 0;
    104150    }
    105     else
     151
     152    $url_base = $this->url_base;
     153    $url_base .= $view_type.'-';
     154    for ($i=0; $i<$level; $i++)
    106155    {
    107       $nav_bar .= '<span class="'.$class_prefix.'">';
    108       $url = $url_base . 'any';
    109       $nav_bar .= '<a href="'.$url.'">';
    110       $nav_bar .= $label;
    111       $nav_bar .= '</a>';
     156      if (isset($requested[$i]))
     157      {
     158        $url_base .= $requested[$i].'-';
     159      }
    112160    }
    113     $nav_bar.= '</span>';
     161
     162    $nav_bar = $this->get_nav_bar_from_items(
     163      $url_base,
     164      $level_items,
     165      $requested[$level],
     166      'cal',
     167      true,
     168      $labels
     169      );
     170
     171    $template->assign_block_vars(
     172      'calendar.navbar',
     173      array(
     174        'BAR' => $nav_bar
     175        )
     176      );
    114177  }
    115   return $nav_bar;
    116178}
    117 
    118 
    119 /**
    120  * Creates a calendar navigation bar for a given level.
    121  * @param string view_type - list or calendar (e.g. 'l' or 'c')
    122  * @param array requested - array of current selected elements (e.g. 2005,10)
    123  * @param string sql_func - YEAR/MONTH/DAY/WEEK/DAYOFWEEK ...
    124  * @param string sql_offset - (e.g. +1 for WEEK - first in year is 1)
    125  * @param array labels - optional labels to show in the navigation bar
    126  * @return void
    127  */
    128 function build_nav_bar($view_type, $requested, $level, $sql_func,
    129                        $sql_offset='', $labels=null)
    130 {
    131   global $template;
    132   $query = 'SELECT DISTINCT('.$sql_func.'('.$this->date_field.')'.$sql_offset
    133             .') as period';
    134   $query.= $this->inner_sql;
    135   $query.= $this->get_date_where($requested, $level);
    136   $query.= '
    137   GROUP BY period';
    138 
    139   $level_items=array();
    140   $result = pwg_query($query);
    141   while ($row = mysql_fetch_array($result))
    142   {
    143     $level_items[$row['period']] = 0;
    144   }
    145 
    146   $url_base = $this->url_base;
    147   $url_base .= $view_type.'-';
    148   for ($i=0; $i<$level; $i++)
    149   {
    150     if (isset($requested[$i]))
    151     {
    152       $url_base .= $requested[$i].'-';
    153     }
    154   }
    155 
    156   $nav_bar = $this->get_nav_bar_from_items( $url_base, $level_items,
    157               $requested[$level], 'cal', true, $labels);
    158 
    159   $template->assign_block_vars( 'calendar.navbar',
    160          array( 'BAR' => $nav_bar)
    161          );
    162 }
    163 }
    164 
    165179?>
Note: See TracChangeset for help on using the changeset viewer.