Ignore:
Timestamp:
Feb 22, 2006, 2:00:39 AM (18 years ago)
Author:
rvelices
Message:

calendar redesign: monthly and weekly styles + list/calendar views for monthly

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions_calendar.inc.php

    r1047 r1050  
    66// | branch        : BSF (Best So Far)
    77// | file          : $RCSfile$
    8 // | last update   : $Date: 2006-02-12 16:52:16 -0500 (Sun, 12 Feb 2006) $
    9 // | last modifier : $Author: plg $
    10 // | revision      : $Revision: 1036 $
     8// | last update   : $Date: 2006-01-27 02:11:43 +0100 (ven, 27 jan 2006) $
     9// | last modifier : $Author: rvelices $
     10// | revision      : $Revision: 1014 $
    1111// +-----------------------------------------------------------------------+
    1212// | This program is free software; you can redistribute it and/or modify  |
     
    2525// +-----------------------------------------------------------------------+
    2626
    27 class BaseCalendarLevel
    28 {
    29   function BaseCalendarLevel($sql, $allow_any=true, $labels=null)
    30   {
    31     $this->sql = $sql;
    32     $this->allow_any = $allow_any;
    33     $this->labels = $labels;
    34   }
    35  
    36   function sql()
    37   {
    38     return $this->sql;
    39   }
    40   function sql_equal($item)
    41   {
    42     return $this->sql.'='.$item;
    43   }
    44   function allow_any()
    45   {
    46     return $this->allow_any;
    47   }
    48   function get_label($item)
    49   {
    50     if ( isset($this->labels[$item]) )
    51     {
    52       return $this->labels[$item];
    53     }
    54     return $item;
    55   }
    56 
    57   var $sql;
    58   var $allow_any;
    59   var $labels;
    60 }
    61 
    62 class YearMonthCalendarLevel extends BaseCalendarLevel
    63 {
    64   function YearMonthCalendarLevel()
    65   {
    66     global $conf;
    67     parent::BaseCalendarLevel('DATE_FORMAT('.$conf['calendar_datefield'].',"%Y%m")', false);
    68   }
    69 
    70   function sql_equal($item)
    71   {
    72     global $conf;
    73     $y = (int)($item/100);
    74     $m = (int)$item%100;
    75     // There seems to be much difference in performance between these:
    76     return $conf['calendar_datefield']." BETWEEN '$y-$m-01' AND '$y-$m-31'";
    77 /*    return '(YEAR('.$conf['calendar_datefield'].')='.$y.'
    78              AND MONTH('.$conf['calendar_datefield'].')='.$m.')';*/
    79 //    return parent::sql_equal($item);
    80   }
    81 
    82   function get_label($item)
    83   {
    84     global $lang;
    85     if ( preg_match( '/(\d{4})(\d{2})/', $item, $matches) )
    86     {
    87       return $lang['month'][(int)$matches[2]].' '.$matches[1];
    88     }
    89     return $item;
    90   }
    91 }
    92 
    93 // just to optimize MySql query so that it uses the index
    94 class YearCalendarLevel extends BaseCalendarLevel
    95 {
    96   function YearCalendarLevel($sql, $allow_any=true)
    97   {
    98     parent::BaseCalendarLevel($sql, $allow_any);
    99   }
    100 
    101   function sql_equal($item)
    102   {
    103     global $conf;
    104     return $conf['calendar_datefield']." BETWEEN '$item-01-01' AND '$item-12-31'";
    105   }
    106 }
    107 
    108 /**
    109  * Parses $param and returns an array of calendar levels
    110  * @param requested array of requested items for each calendar level
    111  * @param cal_type is the requested calendar type
    112  */
    113 function get_calendar_params($param, &$requested, &$cal_type)
    114 {
    115   global $conf, $lang;
    116   $requested = explode('-', $param);
    117   $cal_struct = array();
    118   if ($requested[0]=='ywd')
    119   {
    120     array_push($cal_struct, new YearCalendarLevel(
    121         'YEAR('.$conf['calendar_datefield'].')'  ) );
    122     array_push($cal_struct, new BaseCalendarLevel(
    123         'WEEK('.$conf['calendar_datefield'].')+1' ) );
    124     array_push($cal_struct, new BaseCalendarLevel(
    125         'DAYOFWEEK('.$conf['calendar_datefield'].')-1', true, $lang['day'] ) );
    126     $cal_type=array_shift($requested);
    127   }
    128   else if ($requested[0]=='md')
    129   {
    130     array_push($cal_struct, new YearMonthCalendarLevel() );
    131     array_push($cal_struct, new BaseCalendarLevel(
    132         'DAY('.$conf['calendar_datefield'].')'  ) );
    133     $cal_type=array_shift($requested);
    134   }
    135   else
    136   {
    137     array_push($cal_struct, new YearCalendarLevel(
    138         'YEAR('.$conf['calendar_datefield'].')'  ) );
    139     array_push($cal_struct, new BaseCalendarLevel(
    140         'MONTH('.$conf['calendar_datefield'].')', true, $lang['month'] ) );
    141     array_push($cal_struct, new BaseCalendarLevel(
    142         'DAY('.$conf['calendar_datefield'].')'  ) );
    143 
    144     if ($requested[0]=='ymd')
    145     {
    146       $cal_type=array_shift($requested);
    147     }
    148     else
    149     {
    150       $cal_type='';
    151     }
    152   }
    153  
    154   // perform a sanity check on $requested
    155   while (count($requested)>count($cal_struct))
    156   {
    157     array_pop($requested);
    158   }
    159  
    160   $any_count = 0;
    161   for ($i=0; $i<count($requested); $i++)
    162   {
    163     if ($requested[$i]=='any')
    164     {
    165       if (! $cal_struct[$i]->allow_any() )
    166       {
    167         while ($i<count($requested))
    168         {
    169           array_pop( $requested );
    170         }
    171         break;
    172       }
    173       $any_count++;
    174     }
    175     elseif ( empty($requested[$i]) )
    176     {
    177       while ($i<count($requested))
    178       {
    179         array_pop( $requested );
    180       }
    181     }
    182   }
    183   if ($any_count==count($cal_struct))
    184   {
    185     array_pop($requested);
    186   }
    187   return $cal_struct;
    188 }
    189 
    190 
     27define('CAL_VIEW_LIST','l');
     28define('CAL_VIEW_CALENDAR','c');
    19129
    19230function initialize_calendar()
     
    19432  global $page, $conf, $user, $template;
    19533
     34//------------------ initialize the condition on items to take into account ---
     35  $inner_sql = ' FROM ' . IMAGES_TABLE;
    19636  if ( !isset($page['cat']) or is_numeric($page['cat']) )
    19737  { // we will regenerate the items by including subcats elements
    19838    $page['cat_nb_images']=0;
    19939    $page['items']=array();
     40    $inner_sql .= '
     41INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
    20042    if ( is_numeric($page['cat']) )
    20143    {
    20244      $sub_ids = get_subcat_ids(array($page['cat']));
    203       $sub_ids = array_diff($sub_ids, 
     45      $sub_ids = array_diff($sub_ids,
    20446                            explode(',', $user['forbidden_categories']) );
    20547      if (empty($sub_ids))
     
    20749        return; // nothing to do
    20850      }
    209       $category_restriction .= ' IN ('.implode(',',$sub_ids).')';
     51      $inner_sql .= '
     52WHERE category_id IN ('.implode(',',$sub_ids).')';
    21053    }
    21154    else
    21255    {
    213       $category_restriction = ' NOT IN ('.$user['forbidden_categories'].')';
     56      $inner_sql .= '
     57WHERE category_id NOT IN ('.$user['forbidden_categories'].')';
    21458    }
    21559  }
     
    22064      return; // nothing to do
    22165    }
    222   }
    223 
     66    $inner_sql .= '
     67WHERE id IN (' . implode(',',$page['items']) .')';
     68  }
     69
     70//-------------------------------------- initialize the calendar parameters ---
    22471  pwg_debug('start initialize_calendar');
    225  
    226   $cal_struct = get_calendar_params($_GET['calendar'], $requested, $cal_type);
    227 
    228   //echo ('<pre>'. var_export($cal_struct, true) . '</pre>');
     72  $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', ),
     77    );
     78
     79  $requested = explode('-', $_GET['calendar']);
     80  $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']);
     86      $calendar = new Calendar();
     87      array_shift($requested);
     88      break;
     89    }
     90  }
     91  if ( !isset($calendar) )
     92  {
     93    foreach( $cal_styles as $cal_style)
     94    {
     95      if (''==$cal_style['default_link'])
     96        break;
     97    }
     98    include( PHPWG_ROOT_PATH.'include/'.$cal_style['include']);
     99    $calendar = new Calendar();
     100  }
     101
     102  $view_type=CAL_VIEW_LIST;
     103  if ($requested[0]==CAL_VIEW_LIST)
     104  {
     105    array_shift($requested);
     106  }
     107  elseif ($requested[0]==CAL_VIEW_CALENDAR)
     108  {
     109    if ($cal_style['view_calendar'])
     110    {
     111      $view_type=CAL_VIEW_CALENDAR;
     112    }
     113    array_shift($requested);
     114  }
     115  // perform a sanity check on $requested
     116  while (count($requested)>3)
     117  {
     118    array_pop($requested);
     119  }
     120
     121  $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)
     127      {// we dont allow any in calendar view
     128        while ($i<count($requested))
     129        {
     130          array_pop( $requested );
     131        }
     132        break;
     133      }
     134      $any_count++;
     135    }
     136    elseif ( $requested[$i]=='' )
     137    {
     138      while ($i<count($requested))
     139      {
     140        array_pop( $requested );
     141      }
     142    }
     143  }
     144  if ($any_count==3)
     145  {
     146    array_pop($requested);
     147  }
     148
     149  $calendar->initialize($conf['calendar_datefield'], $inner_sql);
    229150  //echo ('<pre>'. var_export($requested, true) . '</pre>');
    230  
     151  //echo ('<pre>'. var_export($calendar, true) . '</pre>');
     152
    231153  $category_calling = false;
    232154  if (basename($_SERVER["PHP_SELF"]) == 'category.php')
     
    234156    $category_calling = true;
    235157  }
    236  
     158
     159  $must_show_list = true;
    237160  if ($category_calling)
    238161  {
     
    243166    $url_base .= 'calendar=';
    244167    $url_base = PHPWG_ROOT_PATH.'category.php'.$url_base;
    245    
    246     // Build navigation bar for calendar styles
    247     $nav_bar = 'Styles: ';
    248     foreach ( array('ymd','md','ywd') as $type)
    249     {
    250       if ( $type==$cal_type or ($cal_type=='' and $type=='ymd') )
    251       {
    252         $nav_bar .= $type.' ';
     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'] );
     175      $must_show_list = false;
     176    }
     177
     178    if ($cal_style['view_calendar'])
     179    { // Build bar for view modes (List/Calendar)
     180      $views = array(
     181        array(CAL_VIEW_LIST, l10n('List') ),
     182        array(CAL_VIEW_CALENDAR, l10n('calendar') ),
     183        );
     184      $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> ';
     193        }
     194        else
     195        {
     196          $v = $v.' ';
     197        }
     198        $views_bar .= $v . ' ';
     199      }
     200      $template->assign_block_vars('calendar.views', array(
     201        'BAR'=>$views_bar
     202        ));
     203    }
     204
     205    // Build bar for calendar styles (Monthly, Weekly)
     206    $styles_bar = '';
     207    foreach ( $cal_styles as $style)
     208    {
     209      if ($cal_style['link']!=$style['link'])
     210      {
     211        $url = $url_base.$style['default_link'];
     212        $url .= $view_type;
     213        if (isset($requested[0]))
     214        {
     215          $url .= '-' . $requested[0];
     216        }
     217        $styles_bar .= '<a href="'. $url . '">'.$style['name'].'</a> ';
    253218      }
    254219      else
    255220      {
    256         $nav_bar .= '<a href="'. $url_base.$type . '">'.$type.'</a> ';
    257       }
    258     }
    259     $template->assign_block_vars( 'calendar.navbar', 
    260            array( 'BAR' => $nav_bar)
    261            );
    262            
    263     $url_base .= $cal_type;
    264     if ($cal_type!='')
    265     {
    266       $url_base .= '-';
    267     }
    268    
    269    
    270     $prev_level_query='
    271 AND '.$conf['calendar_datefield'].' IS NOT NULL ';
    272 
    273     for ($i=0; $i<count($cal_struct); $i++)
    274     {
    275       $crt_cal_level = $cal_struct[$i];
    276       $query = '
    277 SELECT DISTINCT('.$crt_cal_level->sql().') AS period, COUNT(id) as count
    278 FROM '.IMAGES_TABLE;
    279       if ( isset($category_restriction) )
    280       {
    281         $query.= '
    282 INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
    283 WHERE category_id' . $category_restriction;
    284       }
    285       else
    286       {
    287         $query.= '
    288 WHERE id IN (' . implode(',',$page['items']) .')';
    289       }
    290       $query.= $prev_level_query;
    291       $query.= '
    292 GROUP BY period';
    293 
    294       $level_items=array();
    295       $result = pwg_query($query);
    296       $total_pics = 0;
    297       while ($row = mysql_fetch_array($result))
    298       {
    299         $level_items[$row['period']] = (int)$row['count'];
    300         $total_pics += $row['count'];
    301       }
    302       //echo ('<pre>'. var_export($level_items, true) . '</pre>');
    303 
    304       if ( $requested[$i] == 'any' and ! $crt_cal_level->allow_any() )
    305       {
    306         unset($requested[$i]);
    307       }
    308      
    309       // --- Build the navigation bar
    310       if ( $crt_cal_level->allow_any() )
    311       {
    312         $level_items['any'] = $total_pics;
    313       }
    314       $nav_bar='';
    315       foreach ($level_items as $item => $nb_images)
    316       {
    317         $label = $crt_cal_level->get_label($item);
    318         if ( $item==$requested[$i] )
    319         {
    320           $nav_bar .= ' <span class="dateSelected">';
    321           $nav_bar .= $label;
    322           $nav_bar.= '</span>';
    323         }
    324         else
    325         {
    326           $url = $url_base . $item;
    327           $nav_bar .= '<a href="'.$url.'">';
    328           $nav_bar .= $label;
    329           $nav_bar .= '</a>';
    330         }
    331         $nav_bar .= ' ';
    332       }
    333       $template->assign_block_vars( 'calendar.navbar', 
    334            array( 'BAR' => $nav_bar)
    335            );
    336 
    337       if ( !isset($requested[$i]) )
    338         break;
    339       if ($requested[$i]!='any')
    340       {
    341         $prev_level_query.= ' AND '.$crt_cal_level->sql_equal($requested[$i]);
    342       }
    343       $url_base .= $requested[$i].'-';
    344     } // end for each calendar level
    345 
    346 
    347     if ( $i < count($cal_struct) )
    348     {
    349       $template->assign_block_vars('thumbnails', array());
    350       $template->assign_block_vars('thumbnails.line', array());
    351       foreach ($level_items as $level_item => $nb_pics)
    352       {
    353         if ($level_item=='any')
    354           continue;
    355         $query = '
    356 SELECT file,tn_ext,'.$conf['calendar_datefield'].',path
    357 FROM '.IMAGES_TABLE;
    358         if ( isset($category_restriction) )
    359         {
    360           $query.= '
    361 INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
    362 WHERE category_id' . $category_restriction;
    363         }
    364         else
    365         {
    366           $query.= '
    367 WHERE id IN (' . implode(',',$page['items']) .')';
    368         }
    369         $query.= $prev_level_query;
    370         $query.= ' AND '.$crt_cal_level->sql_equal($level_item);
    371         $query.= '
    372 ORDER BY RAND()
    373 LIMIT 0,1';
    374         $row = mysql_fetch_array(pwg_query($query));
    375        
    376         $thumbnail_src = get_thumbnail_src($row['path'], @$row['tn_ext']);
    377         $thumbnail_title = $crt_cal_level->get_label($level_item);
    378         $name = $thumbnail_title .' ('.$nb_pics.')';
    379        
    380         $template->assign_block_vars(
    381           'thumbnails.line.thumbnail',
    382           array(
    383             'IMAGE'=>$thumbnail_src,
    384             'IMAGE_ALT'=>$row['file'],
    385             'IMAGE_TITLE'=>$thumbnail_title,
    386             'U_IMG_LINK'=>$url_base.$level_item
    387            )
    388          );
    389         $template->assign_block_vars(
    390           'thumbnails.line.thumbnail.category_name',
    391           array(
    392             'NAME' => $name
    393             )
    394           );
    395       }
    396       unset( $page['thumbnails_include'] ); // maybe move everything to a new include file ?
    397       pwg_debug('end initialize_calendar for thumbs');
    398       return;
    399     }
    400   }
    401  
    402   if (!$category_calling or $i==count($cal_struct) )
    403   {
    404     $query = 'SELECT DISTINCT(id) FROM '.IMAGES_TABLE;
    405     if ( isset($category_restriction) )
    406     {
    407       $query.= '
    408 INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
    409 WHERE category_id' . $category_restriction;
    410     }
    411     else
    412     {
    413       $query.= '
    414 WHERE id IN ('.implode(',',$page['items']).')';
    415     }
    416     $query.= '
    417 AND '.$conf['calendar_datefield'].' IS NOT NULL ';
    418 
    419     for ($i=0; $i<count($cal_struct); $i++)
    420     {
    421       assert( isset($requested[$i]) ); // otherwise we should not be here
    422       if ($requested[$i]!='any')
    423       {
    424       $query.= '
    425 AND '. $cal_struct[$i]->sql_equal($requested[$i]);
    426       }
    427      
    428     }
    429    
     221        $styles_bar .=  $style['name'].' ';
     222      }
     223    }
     224    $template->assign_block_vars( 'calendar.styles',
     225               array( 'BAR' => $styles_bar)
     226               );
     227  } // end category calling
     228
     229  if ($must_show_list)
     230  {
     231    $query = 'SELECT DISTINCT(id)';
     232    $query .= $calendar->inner_sql;
     233    $query .= $calendar->get_date_where($requested);
     234
    430235    $page['items'] = array_from_query($query, 'id');
    431236    $page['cat_nb_images'] = count($page['items']);
Note: See TracChangeset for help on using the changeset viewer.