Changeset 1053


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.

Location:
trunk/include
Files:
4 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?>
  • trunk/include/calendar_monthly.class.php

    r1051 r1053  
    7474      $this->build_nav_bar2($view_type, $requested, 1, 'MONTH', $lang['month']); // month
    7575    if (count($requested)>1)
    76       $this->build_nav_bar2($view_type, $requested, 2, 'DAY' ); // days
     76      $this->build_nav_bar2($view_type, $requested, 2, 'DAYOFWEEK' ); // days
    7777  }
    7878  return false;
     
    124124      if (isset($requested[2]) and $requested[2]!='any')
    125125      {
    126         $res .= ' AND DAY('.$this->date_field.')='.$requested[2];
     126        $res .= ' AND DAYOFMONTH('.$this->date_field.')='.$requested[2];
    127127      }
    128128    }
     
    138138    if (isset($requested[2]) and $requested[2]!='any')
    139139    {
    140       $res .= ' AND DAY('.$this->date_field.')='.$requested[2];
     140      $res .= ' AND DAYOFMONTH('.$this->date_field.')='.$requested[2];
    141141    }
    142142  }
  • trunk/include/category_default.inc.php

    r1047 r1053  
    8080    $thumbnail_title .= ' : '.$row['filesize'].' KB';
    8181  }
     82 
    8283  // url link on picture.php page
    83   $url_link = PHPWG_ROOT_PATH.'picture.php?';
    84   if ( isset($page['cat']) )
     84  $url_link = PHPWG_ROOT_PATH.'picture.php?image_id='.$row['id'];
     85
     86  if (isset($page['cat']))
    8587  {
    86     $url_link .= 'cat='.$page['cat'].'&amp;';
     88    $url_link.= 'cat='.$page['cat'].'&amp;';
     89
     90    if ($page['cat'] == 'search')
     91    {
     92      $url_link.= '&amp;search='.$_GET['search'];
     93    }
     94    else if ($page['cat'] == 'list')
     95    {
     96      $url_link.= '&amp;list='.$_GET['list'];
     97    }
    8798  }
    88   $url_link.= 'image_id='.$row['id'];
    89   if ($page['cat'] == 'search')
    90   {
    91     $url_link.= '&amp;search='.$_GET['search'];
    92   }
    93   else if ($page['cat'] == 'list')
    94   {
    95     $url_link.= '&amp;list='.$_GET['list'];
    96   }
    97   if ( isset($_GET['calendar']) )
     99 
     100  if (isset($_GET['calendar']))
    98101  {
    99102    $url_link.= '&amp;calendar='.$_GET['calendar'];
  • trunk/include/functions_calendar.inc.php

    r1051 r1053  
    2525// +-----------------------------------------------------------------------+
    2626
    27 define('CAL_VIEW_LIST','l');
    28 define('CAL_VIEW_CALENDAR','c');
     27define('CAL_VIEW_LIST',     'l');
     28define('CAL_VIEW_CALENDAR', 'c');
    2929
    3030function initialize_calendar()
     
    3434//------------------ initialize the condition on items to take into account ---
    3535  $inner_sql = ' FROM ' . IMAGES_TABLE;
    36   if ( !isset($page['cat']) or is_numeric($page['cat']) )
     36 
     37  if (!isset($page['cat']) or is_numeric($page['cat']))
    3738  { // we will regenerate the items by including subcats elements
    38     $page['cat_nb_images']=0;
    39     $page['items']=array();
     39    $page['cat_nb_images'] = 0;
     40    $page['items'] = array();
    4041    $inner_sql .= '
    4142INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
    42     if ( is_numeric($page['cat']) )
    43     {
    44       $sub_ids = get_subcat_ids(array($page['cat']));
    45       $sub_ids = array_diff($sub_ids,
    46                             explode(',', $user['forbidden_categories']) );
     43   
     44    if (isset($page['cat']) and is_numeric($page['cat']))
     45    {
     46      $sub_ids = array_diff(
     47        get_subcat_ids(array($page['cat'])),
     48        explode(',', $user['forbidden_categories'])
     49        );
     50     
    4751      if (empty($sub_ids))
    4852      {
     
    7074//-------------------------------------- initialize the calendar parameters ---
    7175  pwg_debug('start initialize_calendar');
     76 
    7277  $cal_styles = array(
    73     array('link'=>'m', 'default_link'=>'', 'name'=>l10n('Monthly'),
    74           'include'=>'calendar_monthly.class.php', 'view_calendar'=>true ),
    75     array('link'=>'w', 'default_link'=>'w-', 'name'=>l10n('Weekly'),
    76           'include'=>'calendar_weekly.class.php', ),
     78    // Weekly style
     79    array(
     80      'link'           => 'm',
     81      'default_link'   => '',
     82      'name'           => l10n('Monthly'),
     83      'include'        => 'calendar_monthly.class.php',
     84      'view_calendar'  => true,
     85      ),
     86    // Monthly style
     87    array(
     88      'link'           => 'w',
     89      'default_link'   => 'w-',
     90      'name'           => l10n('Weekly'),
     91      'include'        => 'calendar_weekly.class.php',
     92      ),
    7793    );
    7894
    7995  $requested = explode('-', $_GET['calendar']);
    8096  $calendar = null;
    81   foreach( $cal_styles as $cal_style)
    82   {
    83     if ($requested[0]==$cal_style['link'])
    84     {
    85       include( PHPWG_ROOT_PATH.'include/'.$cal_style['include']);
     97  foreach ($cal_styles as $cal_style)
     98  {
     99    if ($requested[0] == $cal_style['link'])
     100    {
     101      include(PHPWG_ROOT_PATH.'include/'.$cal_style['include']);
    86102      $calendar = new Calendar();
    87103      array_shift($requested);
     
    89105    }
    90106  }
    91   if ( !isset($calendar) )
    92   {
    93     foreach( $cal_styles as $cal_style)
    94     {
    95       if (''==$cal_style['default_link'])
     107 
     108  if (!isset($calendar))
     109  {
     110    foreach($cal_styles as $cal_style)
     111    {
     112      if ('' == $cal_style['default_link'])
     113      {
    96114        break;
     115      }
    97116    }
    98117    include( PHPWG_ROOT_PATH.'include/'.$cal_style['include']);
     
    100119  }
    101120
    102   $view_type=CAL_VIEW_LIST;
    103   if ($requested[0]==CAL_VIEW_LIST)
     121  $view_type = CAL_VIEW_LIST;
     122  if ($requested[0] == CAL_VIEW_LIST)
    104123  {
    105124    array_shift($requested);
    106125  }
    107   elseif ($requested[0]==CAL_VIEW_CALENDAR)
     126  elseif ($requested[0] == CAL_VIEW_CALENDAR)
    108127  {
    109128    if ($cal_style['view_calendar'])
    110129    {
    111       $view_type=CAL_VIEW_CALENDAR;
     130      $view_type = CAL_VIEW_CALENDAR;
    112131    }
    113132    array_shift($requested);
    114133  }
    115134  // perform a sanity check on $requested
    116   while (count($requested)>3)
     135  while (count($requested) > 3)
    117136  {
    118137    array_pop($requested);
     
    120139
    121140  $any_count = 0;
    122   for ($i=0; $i<count($requested); $i++)
    123   {
    124     if ($requested[$i]=='any')
    125     {
    126       if ($view_type==CAL_VIEW_CALENDAR)
     141  for ($i = 0; $i < count($requested); $i++)
     142  {
     143    if ($requested[$i] == 'any')
     144    {
     145      if ($view_type == CAL_VIEW_CALENDAR)
    127146      {// we dont allow any in calendar view
    128         while ($i<count($requested))
    129         {
    130           array_pop( $requested );
     147        while ($i < count($requested))
     148        {
     149          array_pop($requested);
    131150        }
    132151        break;
     
    134153      $any_count++;
    135154    }
    136     elseif ( $requested[$i]=='' )
    137     {
    138       while ($i<count($requested))
    139       {
    140         array_pop( $requested );
    141       }
    142     }
    143   }
    144   if ($any_count==3)
     155    elseif ($requested[$i] == '')
     156    {
     157      while ($i < count($requested))
     158      {
     159        array_pop($requested);
     160      }
     161    }
     162  }
     163  if ($any_count == 3)
    145164  {
    146165    array_pop($requested);
     
    151170  //echo ('<pre>'. var_export($calendar, true) . '</pre>');
    152171
    153   $category_calling = false;
     172  // TODO: what makes the list view required?
     173  $must_show_list = true;
     174 
    154175  if (basename($_SERVER["PHP_SELF"]) == 'category.php')
    155176  {
    156     $category_calling = true;
    157   }
    158 
    159   $must_show_list = true;
    160   if ($category_calling)
    161   {
    162177    $template->assign_block_vars('calendar', array());
    163178
    164     $url_base = get_query_string_diff(array('start','calendar'));
    165     $url_base .= empty($url_base) ? '?' : '&';
    166     $url_base .= 'calendar=';
    167     $url_base = PHPWG_ROOT_PATH.'category.php'.$url_base;
    168 
    169     if ( $calendar->generate_category_content(
    170               $url_base.$cal_style['default_link'], $view_type, $requested) )
    171     {
    172       unset( $page['thumbnails_include'] );
    173       unset( $page['items'] );
    174       unset( $page['cat_nb_images'] );
     179    $url_base =
     180      PHPWG_ROOT_PATH.'category.php'
     181      .get_query_string_diff(array('start', 'calendar'))
     182      .(empty($url_base) ? '?' : '&')
     183      .'calendar='
     184      ;
     185
     186    if ($calendar->generate_category_content(
     187          $url_base.$cal_style['default_link'],
     188          $view_type,
     189          $requested
     190          )
     191       )
     192    {
     193      unset(
     194        $page['thumbnails_include'],
     195        $page['items'],
     196        $page['cat_nb_images']
     197        );
     198     
    175199      $must_show_list = false;
    176200    }
    177201
    178202    if ($cal_style['view_calendar'])
    179     { // Build bar for view modes (List/Calendar)
     203    { // Build bar for views (List/Calendar)
    180204      $views = array(
    181         array(CAL_VIEW_LIST, l10n('List') ),
    182         array(CAL_VIEW_CALENDAR, l10n('calendar') ),
    183         );
     205        // list view
     206        array(
     207          'type'  => CAL_VIEW_LIST,
     208          'label' => l10n('List')
     209          ),
     210        // calendar view
     211        array(
     212          'type'  => CAL_VIEW_CALENDAR,
     213          'label' => l10n('calendar')
     214          ),
     215        );
     216     
    184217      $views_bar = '';
    185       foreach( $views as $view )
    186       {
    187         $v = $view[1];
    188         if ( $view_type!=$view[0] )
    189         {
    190           $url = $url_base.$cal_style['default_link'].$view[0].'-';
    191           $url .= implode('-', $requested);
    192           $v = '<a href="'.$url.'">'.$v.'</a> ';
     218
     219      foreach ($views as $view)
     220      {
     221        if ($view_type != $view['type'])
     222        {
     223          $views_bar.=
     224            '<a href="'
     225            .$url_base.$cal_style['default_link'].$view['type'].'-'
     226            .implode('-', $requested)
     227            .'">'.$view['label'].'</a> ';
    193228        }
    194229        else
    195230        {
    196           $v = $v.' ';
    197         }
    198         $views_bar .= $v . ' ';
    199       }
    200       $template->assign_block_vars('calendar.views', array(
    201         'BAR'=>$views_bar
    202         ));
     231          $views_bar.= $view['label'].' ';
     232        }
     233       
     234        $views_bar.= ' ';
     235      }
     236     
     237      $template->assign_block_vars(
     238        'calendar.views',
     239        array(
     240          'BAR' => $views_bar,
     241          )
     242        );
    203243    }
    204244
    205245    // Build bar for calendar styles (Monthly, Weekly)
    206246    $styles_bar = '';
    207     foreach ( $cal_styles as $style)
    208     {
    209       if ($cal_style['link']!=$style['link'])
     247    foreach ($cal_styles as $style)
     248    {
     249      if ($cal_style['link'] != $style['link'])
    210250      {
    211251        $url = $url_base.$style['default_link'];
     
    222262      }
    223263    }
    224     $template->assign_block_vars( 'calendar.styles',
    225                array( 'BAR' => $styles_bar)
    226                );
     264    $template->assign_block_vars(
     265      'calendar.styles',
     266      array(
     267        'BAR' => $styles_bar,
     268        )
     269      );
    227270  } // end category calling
    228271
     
    240283    {
    241284      $order_by = str_replace(
    242                 'ORDER BY ',
    243                 'ORDER BY '.$calendar->date_field.',', $conf['order_by']
    244                );
     285        'ORDER BY ',
     286        'ORDER BY '.$calendar->date_field.',', $conf['order_by']
     287        );
    245288      $query .= $order_by;
    246289    }
    247290
    248     $page['items'] = array_from_query($query, 'id');
    249     $page['cat_nb_images'] = count($page['items']);
     291    $page['items']              = array_from_query($query, 'id');
     292    $page['cat_nb_images']      = count($page['items']);
    250293    $page['thumbnails_include'] = 'include/category_default.inc.php';
    251294  }
Note: See TracChangeset for help on using the changeset viewer.