Ignore:
Timestamp:
Feb 24, 2006, 6:58:48 AM (18 years ago)
Author:
rvelices
Message:

calendar: added posted/created chronology

calendar: added a where are we bar (like: created/2005/august)

calendar: possibility to hide the All/Any buttons ($confcalendar_show_any)

calendar: possibility to display a single navigation bar instead of
several navigation bars ($confcalendar_multi_bar)

calendar: tried to simplify code and improve readability
(still requires a review)

File:
1 edited

Legend:

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

    r1056 r1057  
    2727include_once(PHPWG_ROOT_PATH.'include/calendar_base.class.php');
    2828
     29define ('CYEAR',  0);
     30define ('CMONTH', 1);
     31define ('CDAY',   2);
     32
    2933/**
    3034 * Monthly calendar style (composed of years/months and days)
     
    3842 * where not included here, true otherwise
    3943 */
    40 function generate_category_content($url_base, $view_type, &$requested)
    41 {
    42   global $lang;
     44function generate_category_content($url_base, $view_type)
     45{
     46  global $lang, $conf;
    4347
    4448  $this->url_base = $url_base;
    4549
    46   if ($view_type==CAL_VIEW_CALENDAR and count($requested)==0)
    47   {//case A: no year given - display all years+months
    48     if ($this->build_global_calendar($requested))
     50  if ($view_type==CAL_VIEW_CALENDAR)
     51  {
     52    if ( count($this->date_components)==0 )
     53    {//case A: no year given - display all years+months
     54      if ($this->build_global_calendar())
     55        return true;
     56    }
     57
     58    if ( count($this->date_components)==1 )
     59    {//case B: year given - display all days in given year
     60      if ($this->build_year_calendar())
     61      {
     62        if ( $conf['calendar_multi_bar'] )
     63          $this->build_nav_bar2(CYEAR, 'YEAR'); // years
     64        return true;
     65      }
     66    }
     67
     68    if ( count($this->date_components)==2 )
     69    {//case C: year+month given - display a nice month calendar
     70      $this->build_month_calendar();
     71      if ( $conf['calendar_multi_bar'] )
     72      {
     73        $this->build_nav_bar2(CYEAR, 'YEAR'); // years
     74      }
     75      if (count($this->date_components)>=1 and
     76          ( $conf['calendar_multi_bar'] or count($this->date_components)==1 ) )
     77      {
     78        $this->build_nav_bar2(CMONTH, 'MONTH', $lang['month']); // month
     79      }
    4980      return true;
    50   }
    51 
    52   if ($view_type==CAL_VIEW_CALENDAR and count($requested)==1)
    53   {//case B: year given - display all days in given year
    54     if ($this->build_year_calendar($requested))
    55     {
    56       $this->build_nav_bar2($view_type, $requested, 0, 'YEAR'); // years
    57       return true;
    58     }
    59   }
    60 
    61   if ($view_type==CAL_VIEW_CALENDAR and count($requested)==2)
    62   {//case C: year+month given - display a nice month calendar
    63     $this->build_month_calendar($requested);
    64     $this->build_nav_bar2(CAL_VIEW_CALENDAR, $requested, 0, 'YEAR'); // years
    65     if (count($requested)>0)
    66       $this->build_nav_bar2(CAL_VIEW_CALENDAR, $requested, 1, 'MONTH', $lang['month']); // month
    67     return true;
    68   }
    69 
    70   if ($view_type==CAL_VIEW_LIST or count($requested)==3)
    71   {
    72     $this->build_nav_bar2($view_type, $requested, 0, 'YEAR'); // years
    73     if (count($requested)>0)
    74       $this->build_nav_bar2($view_type, $requested, 1, 'MONTH', $lang['month']); // month
    75     if (count($requested)>1)
    76       $this->build_nav_bar2($view_type, $requested, 2, 'DAYOFMONTH' ); // days
     81    }
     82  }
     83
     84  if ($view_type==CAL_VIEW_LIST or count($this->date_components)==3)
     85  {
     86    if ( $conf['calendar_multi_bar'] or count($this->date_components)==0 )
     87    {
     88      $this->build_nav_bar2(CYEAR, 'YEAR'); // years
     89    }
     90    if ( count($this->date_components)>=1 and
     91        ( $conf['calendar_multi_bar'] or count($this->date_components)==1 )
     92       )
     93    {
     94      $this->build_nav_bar2(CMONTH, 'MONTH', $lang['month']); // month
     95    }
     96    if ( count($this->date_components)>=2 )
     97    {
     98      $this->build_nav_bar2(CDAY, 'DAYOFMONTH' ); // days
     99    }
    77100  }
    78101  return false;
     
    82105/**
    83106 * Returns a sql where subquery for the date field
    84  * @param array requested selected levels for this calendar
    85  * (e.g. 2005,11,5 for 5th of November 2005)
    86107 * @param int max_levels return the where up to this level
    87108 * (e.g. 2=only year and month)
    88109 * @return string
    89110 */
    90 function get_date_where($requested, $max_levels=3)
    91 {
    92   while (count($requested)>$max_levels)
    93   {
    94     array_pop($requested);
     111function get_date_where($max_levels=3)
     112{
     113  $date = $this->date_components;
     114  while (count($date)>$max_levels)
     115  {
     116    array_pop($date);
    95117  }
    96118  $res = '';
    97   if (isset($requested[0]) and $requested[0]!='any')
    98   {
    99     $b = $requested[0] . '-';
    100     $e = $requested[0] . '-';
    101     if (isset($requested[1]) and $requested[1]!='any')
    102     {
    103       $b .= $requested[1] . '-';
    104       $e .= $requested[1] . '-';
    105       if (isset($requested[2]) and $requested[2]!='any')
    106       {
    107         $b .= $requested[2];
    108         $e .= $requested[2];
     119  if (isset($date[CYEAR]) and $date[CYEAR]!='any')
     120  {
     121    $b = $date[CYEAR] . '-';
     122    $e = $date[CYEAR] . '-';
     123    if (isset($date[CMONTH]) and $date[CMONTH]!='any')
     124    {
     125      $b .= $date[CMONTH] . '-';
     126      $e .= $date[CMONTH] . '-';
     127      if (isset($date[CDAY]) and $date[CDAY]!='any')
     128      {
     129        $b .= $date[CDAY];
     130        $e .= $date[CDAY];
    109131      }
    110132      else
     
    118140      $b .= '01-01';
    119141      $e .= '12-31';
    120       if (isset($requested[1]) and $requested[1]!='any')
    121       {
    122         $res .= ' AND MONTH('.$this->date_field.')='.$requested[1];
    123       }
    124       if (isset($requested[2]) and $requested[2]!='any')
    125       {
    126         $res .= ' AND DAYOFMONTH('.$this->date_field.')='.$requested[2];
     142      if (isset($date[CMONTH]) and $date[CMONTH]!='any')
     143      {
     144        $res .= ' AND MONTH('.$this->date_field.')='.$date[CMONTH];
     145      }
     146      if (isset($date[2]) and $date[2]!='any')
     147      {
     148        $res .= ' AND DAYOFMONTH('.$this->date_field.')='.$date[CDAY];
    127149      }
    128150    }
     
    132154  {
    133155    $res = ' AND '.$this->date_field.' IS NOT NULL';
    134     if (isset($requested[1]) and $requested[1]!='any')
    135     {
    136       $res .= ' AND MONTH('.$this->date_field.')='.$requested[1];
    137     }
    138     if (isset($requested[2]) and $requested[2]!='any')
    139     {
    140       $res .= ' AND DAYOFMONTH('.$this->date_field.')='.$requested[2];
     156    if (isset($date[CMONTH]) and $date[CMONTH]!='any')
     157    {
     158      $res .= ' AND MONTH('.$this->date_field.')='.$date[CMONTH];
     159    }
     160    if (isset($date[CDAY]) and $date[CDAY]!='any')
     161    {
     162      $res .= ' AND DAYOFMONTH('.$this->date_field.')='.$date[CDAY];
    141163    }
    142164  }
     
    144166}
    145167
     168
     169function get_display_name()
     170{
     171  global $conf, $lang;
     172  $res = '';
     173  $url = $this->url_base;
     174  if ( isset($this->date_components[CYEAR]) )
     175  {
     176    $res .= $conf['level_separator'];
     177    $url .= $this->date_components[CYEAR].'-';
     178    $res .=
     179      '<a href="'.$url.'">'
     180      .$this->get_date_component_label($this->date_components[CYEAR])
     181      .'</a>';
     182  }
     183  if ( isset($this->date_components[CMONTH]) )
     184  {
     185    $res .= $conf['level_separator'];
     186    $url .= $this->date_components[CMONTH].'-';
     187    $res .=
     188      '<a href="'.$url.'">'
     189      .$this->get_date_component_label(
     190                     $this->date_components[CMONTH],
     191                     $lang['month']
     192                    )
     193      .'</a>';
     194  }
     195  if ( isset($this->date_components[CDAY]) )
     196  {
     197    $res .= $conf['level_separator'];
     198    $url .= $this->date_components[CDAY].'-';
     199    $res .=
     200      '<a href="'.$url.'">'
     201      .$this->get_date_component_label($this->date_components[CDAY])
     202      .'</a>';
     203  }
     204
     205  return $res;
     206}
     207
     208
    146209//--------------------------------------------------------- private members ---
    147 function build_nav_bar2($view_type, $requested, $level, $sql_func, $labels=null)
    148 {
    149   parent::build_nav_bar($view_type, $requested, $level, $sql_func, '', $labels);
    150 }
    151 
    152 function build_global_calendar(&$requested)
    153 {
    154   assert( count($requested) == 0 );
     210function build_nav_bar2($level, $sql_func, $labels=null)
     211{
     212  parent::build_nav_bar($level, $sql_func, '', $labels);
     213}
     214
     215function build_global_calendar()
     216{
     217  assert( count($this->date_components) == 0 );
    155218  $query='SELECT DISTINCT(DATE_FORMAT('.$this->date_field.',"%Y%m")) as period,
    156219            COUNT(id) as count';
    157220  $query.= $this->inner_sql;
    158   $query.= $this->get_date_where($requested);
     221  $query.= $this->get_date_where();
    159222  $query.= '
    160223  GROUP BY period';
     
    176239  {// only one year exists so bail out to year view
    177240    list($y) = array_keys($items);
    178     array_push($requested, $y );
     241    $this->date_components[CYEAR] = $y;
    179242    return false;
    180243  }
     
    183246  foreach ( $items as $year=>$year_data)
    184247  {
    185     $url_base = $this->url_base .'c-'.$year;
     248    $url_base = $this->url_base.$year;
    186249
    187250    $nav_bar = '<span class="calCalHead"><a href="'.$url_base.'">'.$year.'</a>';
     
    200263}
    201264
    202 function build_year_calendar(&$requested)
    203 {
    204   assert( count($requested) == 1 );
     265function build_year_calendar()
     266{
     267  assert( count($this->date_components) == 1 );
    205268  $query='SELECT DISTINCT(DATE_FORMAT('.$this->date_field.',"%m%d")) as period,
    206269            COUNT(id) as count';
    207270  $query.= $this->inner_sql;
    208   $query.= $this->get_date_where($requested);
     271  $query.= $this->get_date_where();
    209272  $query.= '
    210273  GROUP BY period';
     
    226289  { // only one month exists so bail out to month view
    227290    list($m) = array_keys($items);
    228     array_push($requested, $m );
     291    $this->date_components[CMONTH] = $m;
    229292    if (count($items[$m]['children'])==1)
    230293    { // or even to day view if everything occured in one day
    231294      list($d) = array_keys($items[$m]['children']);
    232       array_push($requested, $d);
     295      $this->date_components[CDAY] = $d;
    233296    }
    234297    return false;
     
    237300  foreach ( $items as $month=>$month_data)
    238301  {
    239     $url_base = $this->url_base.'c-'.$requested[0].'-'.$month;
     302    $url_base = $this->url_base.$this->date_components[CYEAR].'-'.$month;
    240303
    241304    $nav_bar = '<span class="calCalHead"><a href="'.$url_base.'">';
     
    256319}
    257320
    258 function build_month_calendar($requested)
     321function build_month_calendar()
    259322{
    260323  $query='SELECT DISTINCT(DATE_FORMAT('.$this->date_field.',"%d")) as period,
    261324            COUNT(id) as count';
    262325  $query.= $this->inner_sql;
    263   $query.= $this->get_date_where($requested, 2);
     326  $query.= $this->get_date_where($this->date_components);
    264327  $query.= '
    265328  GROUP BY period';
     
    278341  foreach ( $items as $day=>$nb_images)
    279342  {
    280     $url_base = $this->url_base.'c-'.$requested[0].'-'.$requested[1].'-'.$day;
    281     $requested[2]=$day;
     343    $url_base = $this->url_base.
     344          $this->date_components[CYEAR].'-'.
     345          $this->date_components[CMONTH].'-'.$day;
     346    $this->date_components[CDAY]=$day;
    282347    $query = '
    283348SELECT file,tn_ext,path, DAYOFWEEK('.$this->date_field.')-1 as dw';
    284349    $query.= $this->inner_sql;
    285     $query.= $this->get_date_where($requested);
     350    $query.= $this->get_date_where();
    286351    $query.= '
    287352  ORDER BY RAND()
    288353  LIMIT 0,1';
     354    unset ( $this->date_components[CDAY] );
    289355
    290356    $row = mysql_fetch_array(pwg_query($query));
Note: See TracChangeset for help on using the changeset viewer.