Changeset 25507 for trunk/include


Ignore:
Timestamp:
Nov 17, 2013, 4:03:32 PM (10 years ago)
Author:
mistic100
Message:

feature 2999 : documentation of include/functions_calendar.inc.php and Calendar classes

Location:
trunk/include
Files:
5 edited

Legend:

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

    r23618 r25507  
    2323
    2424/**
     25 * @package functions\calendar
     26 */
     27
     28
     29/**
    2530 * Base class for monthly and weekly calendar styles
    2631 */
    27 class CalendarBase
     32abstract class CalendarBase
    2833{
    29   // db column on which this calendar works
     34  /** db column on which this calendar works */
    3035  var $date_field;
    31   // used for queries (INNER JOIN or normal)
     36  /** used for queries (INNER JOIN or normal) */
    3237  var $inner_sql;
    33   //
     38  /** used to store db fields */
    3439  var $calendar_levels;
    3540
    3641  /**
    37    * Initialize the calendar
    38    * @param string inner_sql used for queries (INNER JOIN or normal)
     42   * Generate navigation bars for category page.
     43   *
     44   * @return boolean false indicates that thumbnails where not included
     45   */
     46  abstract function generate_category_content();
     47
     48  /**
     49   * Returns a sql WHERE subquery for the date field.
     50   *
     51   * @param int $max_levels (e.g. 2=only year and month)
     52   * @return string
     53   */
     54  abstract function get_date_where($max_levels);
     55
     56  /**
     57   * Initialize the calendar.
     58   *
     59   * @param string $inner_sql
    3960   */
    4061  function initialize($inner_sql)
     
    5273  }
    5374
     75  /**
     76   * Returns the calendar title (with HTML).
     77   *
     78   * @return string
     79   */
    5480  function get_display_name()
    5581  {
     
    83109  }
    84110
    85 //--------------------------------------------------------- private members ---
    86   /**
    87    * Returns a display name for a date component optionally using labels
    88   */
    89   function get_date_component_label($level, $date_component)
     111  /**
     112   * Returns a display name for a date component optionally using labels.
     113   *
     114   * @return string
     115   */
     116  protected function get_date_component_label($level, $date_component)
    90117  {
    91118    $label = $date_component;
     
    102129
    103130  /**
    104    * Gets a nice display name for a date to be shown in previos/next links.
    105    */
    106   function get_date_nice_name($date)
     131   * Gets a nice display name for a date to be shown in previous/next links
     132   *
     133   * @param string $date
     134   * @return string
     135   */
     136  protected function get_date_nice_name($date)
    107137  {
    108138    $date_components = explode('-', $date);
     
    126156   * Creates a calendar navigation bar.
    127157   *
    128    * @param array date_components
    129    * @param array items - hash of items to put in the bar (e.g. 2005,2006)
    130    * @param bool show_any - adds any link to the end of the bar
    131    * @param bool show_empty - shows all labels even those without items
    132    * @param array labels - optional labels for items (e.g. Jan,Feb,...)
    133    * @return string the navigation bar
    134    */
    135   function get_nav_bar_from_items($date_components, $items,
     158   * @param array $date_components
     159   * @param array $items - hash of items to put in the bar (e.g. 2005,2006)
     160   * @param bool $show_any - adds any link to the end of the bar
     161   * @param bool $show_empty - shows all labels even those without items
     162   * @param array $labels - optional labels for items (e.g. Jan,Feb,...)
     163   * @return string
     164   */
     165  protected function get_nav_bar_from_items($date_components, $items,
    136166                                  $show_any,
    137167                                  $show_empty=false, $labels=null)
     
    204234   * Creates a calendar navigation bar for a given level.
    205235   *
    206    * @param int level - the level (0-year,1-month/week,2-day)
    207    * @return void
    208    */
    209   function build_nav_bar($level, $labels=null)
     236   * @param int $level - 0-year, 1-month/week, 2-day
     237   */
     238  protected function build_nav_bar($level, $labels=null)
    210239  {
    211240    global $template, $conf, $page;
     
    262291   * the currently choosen date.
    263292   */
    264   function build_next_prev()
     293  protected function build_next_prev()
    265294  {
    266295    global $template, $page;
     
    344373  }
    345374}
     375
    346376?>
  • trunk/include/calendar_monthly.class.php

    r25018 r25507  
    2222// +-----------------------------------------------------------------------+
    2323
     24/**
     25 * @package functions\calendar
     26 */
     27
    2428include_once(PHPWG_ROOT_PATH.'include/calendar_base.class.php');
    2529
    26 define ('CYEAR',  0);
    27 define ('CMONTH', 1);
    28 define ('CDAY',   2);
     30/** level of year view */
     31define('CYEAR',  0);
     32/** level of month view */
     33define('CMONTH', 1);
     34/** level of day view */
     35define('CDAY',   2);
     36
    2937
    3038/**
     
    3341class Calendar extends CalendarBase
    3442{
    35 
    36   /**
    37    * Initialize the calendar
    38    * @param string inner_sql used for queries (INNER JOIN or normal)
     43  /**
     44   * Initialize the calendar.
     45   * @param string $inner_sql
    3946   */
    4047  function initialize($inner_sql)
     
    5865  }
    5966
    60 /**
    61  * Generate navigation bars for category page
    62  * @return boolean false to indicate that thumbnails
    63  * where not included here, true otherwise
    64  */
    65 function generate_category_content()
    66 {
    67   global $conf, $page;
    68 
    69   $view_type = $page['chronology_view'];
    70   if ($view_type==CAL_VIEW_CALENDAR)
    71   {
    72     global $template;
    73     $tpl_var = array();
    74     if ( count($page['chronology_date'])==0 )
    75     {//case A: no year given - display all years+months
    76       if ($this->build_global_calendar($tpl_var))
    77       {
    78         $template->assign('chronology_calendar', $tpl_var);
     67  /**
     68   * Generate navigation bars for category page.
     69   *
     70   * @return boolean false indicates that thumbnails where not included
     71   */
     72  function generate_category_content()
     73  {
     74    global $conf, $page;
     75
     76    $view_type = $page['chronology_view'];
     77    if ($view_type==CAL_VIEW_CALENDAR)
     78    {
     79      global $template;
     80      $tpl_var = array();
     81      if ( count($page['chronology_date'])==0 )
     82      {//case A: no year given - display all years+months
     83        if ($this->build_global_calendar($tpl_var))
     84        {
     85          $template->assign('chronology_calendar', $tpl_var);
     86          return true;
     87        }
     88      }
     89
     90      if ( count($page['chronology_date'])==1 )
     91      {//case B: year given - display all days in given year
     92        if ($this->build_year_calendar($tpl_var))
     93        {
     94          $template->assign('chronology_calendar', $tpl_var);
     95          $this->build_nav_bar(CYEAR); // years
     96          return true;
     97        }
     98      }
     99
     100      if ( count($page['chronology_date'])==2 )
     101      {//case C: year+month given - display a nice month calendar
     102        if ( $this->build_month_calendar($tpl_var) )
     103        {
     104          $template->assign('chronology_calendar', $tpl_var);
     105        }
     106        $this->build_next_prev();
    79107        return true;
    80108      }
    81109    }
    82110
    83     if ( count($page['chronology_date'])==1 )
    84     {//case B: year given - display all days in given year
    85       if ($this->build_year_calendar($tpl_var))
    86       {
    87         $template->assign('chronology_calendar', $tpl_var);
     111    if ($view_type==CAL_VIEW_LIST or count($page['chronology_date'])==3)
     112    {
     113      if ( count($page['chronology_date'])==0 )
     114      {
    88115        $this->build_nav_bar(CYEAR); // years
    89         return true;
    90       }
    91     }
    92 
    93     if ( count($page['chronology_date'])==2 )
    94     {//case C: year+month given - display a nice month calendar
    95       if ( $this->build_month_calendar($tpl_var) )
    96       {
    97         $template->assign('chronology_calendar', $tpl_var);
     116      }
     117      if ( count($page['chronology_date'])==1)
     118      {
     119        $this->build_nav_bar(CMONTH); // month
     120      }
     121      if ( count($page['chronology_date'])==2 )
     122      {
     123        $day_labels = range( 1, $this->get_all_days_in_month(
     124                $page['chronology_date'][CYEAR] ,$page['chronology_date'][CMONTH] ) );
     125        array_unshift($day_labels, 0);
     126        unset( $day_labels[0] );
     127        $this->build_nav_bar( CDAY, $day_labels ); // days
    98128      }
    99129      $this->build_next_prev();
    100       return true;
    101     }
    102   }
    103 
    104   if ($view_type==CAL_VIEW_LIST or count($page['chronology_date'])==3)
    105   {
    106     if ( count($page['chronology_date'])==0 )
    107     {
    108       $this->build_nav_bar(CYEAR); // years
    109     }
    110     if ( count($page['chronology_date'])==1)
    111     {
    112       $this->build_nav_bar(CMONTH); // month
    113     }
    114     if ( count($page['chronology_date'])==2 )
    115     {
    116       $day_labels = range( 1, $this->get_all_days_in_month(
    117               $page['chronology_date'][CYEAR] ,$page['chronology_date'][CMONTH] ) );
    118       array_unshift($day_labels, 0);
    119       unset( $day_labels[0] );
    120       $this->build_nav_bar( CDAY, $day_labels ); // days
    121     }
    122     $this->build_next_prev();
    123   }
    124   return false;
    125 }
    126 
    127 
    128 /**
    129  * Returns a sql where subquery for the date field
    130  * @param int max_levels return the where up to this level
    131  * (e.g. 2=only year and month)
    132  * @return string
    133  */
    134 function get_date_where($max_levels=3)
    135 {
    136   global $page;
    137 
    138   $date = $page['chronology_date'];
    139   while (count($date)>$max_levels)
    140   {
    141     array_pop($date);
    142   }
    143   $res = '';
    144   if (isset($date[CYEAR]) and $date[CYEAR]!=='any')
    145   {
    146     $b = $date[CYEAR] . '-';
    147     $e = $date[CYEAR] . '-';
    148     if (isset($date[CMONTH]) and $date[CMONTH]!=='any')
    149     {
    150       $b .= sprintf('%02d-', $date[CMONTH]);
    151       $e .= sprintf('%02d-', $date[CMONTH]);
     130    }
     131    return false;
     132  }
     133
     134  /**
     135   * Returns a sql WHERE subquery for the date field.
     136   *
     137   * @param int $max_levels (e.g. 2=only year and month)
     138   * @return string
     139   */
     140  function get_date_where($max_levels=3)
     141  {
     142    global $page;
     143
     144    $date = $page['chronology_date'];
     145    while (count($date)>$max_levels)
     146    {
     147      array_pop($date);
     148    }
     149    $res = '';
     150    if (isset($date[CYEAR]) and $date[CYEAR]!=='any')
     151    {
     152      $b = $date[CYEAR] . '-';
     153      $e = $date[CYEAR] . '-';
     154      if (isset($date[CMONTH]) and $date[CMONTH]!=='any')
     155      {
     156        $b .= sprintf('%02d-', $date[CMONTH]);
     157        $e .= sprintf('%02d-', $date[CMONTH]);
     158        if (isset($date[CDAY]) and $date[CDAY]!=='any')
     159        {
     160          $b .= sprintf('%02d', $date[CDAY]);
     161          $e .= sprintf('%02d', $date[CDAY]);
     162        }
     163        else
     164        {
     165          $b .= '01';
     166          $e .= $this->get_all_days_in_month($date[CYEAR], $date[CMONTH]);
     167        }
     168      }
     169      else
     170      {
     171        $b .= '01-01';
     172        $e .= '12-31';
     173        if (isset($date[CMONTH]) and $date[CMONTH]!=='any')
     174        {
     175          $res .= ' AND '.$this->calendar_levels[CMONTH]['sql'].'='.$date[CMONTH];
     176        }
     177        if (isset($date[CDAY]) and $date[CDAY]!=='any')
     178        {
     179          $res .= ' AND '.$this->calendar_levels[CDAY]['sql'].'='.$date[CDAY];
     180        }
     181      }
     182      $res = " AND $this->date_field BETWEEN '$b' AND '$e 23:59:59'" . $res;
     183    }
     184    else
     185    {
     186      $res = ' AND '.$this->date_field.' IS NOT NULL';
     187      if (isset($date[CMONTH]) and $date[CMONTH]!=='any')
     188      {
     189        $res .= ' AND '.$this->calendar_levels[CMONTH]['sql'].'='.$date[CMONTH];
     190      }
    152191      if (isset($date[CDAY]) and $date[CDAY]!=='any')
    153192      {
    154         $b .= sprintf('%02d', $date[CDAY]);
    155         $e .= sprintf('%02d', $date[CDAY]);
    156       }
    157       else
    158       {
    159         $b .= '01';
    160         $e .= $this->get_all_days_in_month($date[CYEAR], $date[CMONTH]);
    161       }
     193        $res .= ' AND '.$this->calendar_levels[CDAY]['sql'].'='.$date[CDAY];
     194      }
     195    }
     196    return $res;
     197  }
     198
     199  /**
     200   * Returns an array with all the days in a given month.
     201   *
     202   * @param int $year
     203   * @param int $month
     204   * @return int[]
     205   */
     206  protected function get_all_days_in_month($year, $month)
     207  {
     208    $md= array(1=>31,28,31,30,31,30,31,31,30,31,30,31);
     209
     210    if ( is_numeric($year) and $month==2)
     211    {
     212      $nb_days = $md[2];
     213      if ( ($year%4==0)  and ( ($year%100!=0) or ($year%400!=0) ) )
     214      {
     215        $nb_days++;
     216      }
     217    }
     218    elseif ( is_numeric($month) )
     219    {
     220      $nb_days = $md[ $month ];
    162221    }
    163222    else
    164223    {
    165       $b .= '01-01';
    166       $e .= '12-31';
    167       if (isset($date[CMONTH]) and $date[CMONTH]!=='any')
    168       {
    169         $res .= ' AND '.$this->calendar_levels[CMONTH]['sql'].'='.$date[CMONTH];
    170       }
    171       if (isset($date[CDAY]) and $date[CDAY]!=='any')
    172       {
    173         $res .= ' AND '.$this->calendar_levels[CDAY]['sql'].'='.$date[CDAY];
    174       }
    175     }
    176     $res = " AND $this->date_field BETWEEN '$b' AND '$e 23:59:59'" . $res;
    177   }
    178   else
    179   {
    180     $res = ' AND '.$this->date_field.' IS NOT NULL';
    181     if (isset($date[CMONTH]) and $date[CMONTH]!=='any')
    182     {
    183       $res .= ' AND '.$this->calendar_levels[CMONTH]['sql'].'='.$date[CMONTH];
    184     }
    185     if (isset($date[CDAY]) and $date[CDAY]!=='any')
    186     {
    187       $res .= ' AND '.$this->calendar_levels[CDAY]['sql'].'='.$date[CDAY];
    188     }
    189   }
    190   return $res;
    191 }
    192 
    193 
    194 
    195 //--------------------------------------------------------- private members ---
    196 
    197 // returns an array with all the days in a given month
    198 function get_all_days_in_month($year, $month)
    199 {
    200   $md= array(1=>31,28,31,30,31,30,31,31,30,31,30,31);
    201 
    202   if ( is_numeric($year) and $month==2)
    203   {
    204     $nb_days = $md[2];
    205     if ( ($year%4==0)  and ( ($year%100!=0) or ($year%400!=0) ) )
    206     {
    207       $nb_days++;
    208     }
    209   }
    210   elseif ( is_numeric($month) )
    211   {
    212     $nb_days = $md[ $month ];
    213   }
    214   else
    215   {
    216     $nb_days = 31;
    217   }
    218   return $nb_days;
    219 }
    220 
    221 function build_global_calendar(&$tpl_var)
    222 {
    223   global $page;
    224 
    225   assert( count($page['chronology_date']) == 0 );
    226   $query='
    227 SELECT '.pwg_db_get_date_YYYYMM($this->date_field).' as period,
    228   COUNT(distinct id) as count';
    229   $query.= $this->inner_sql;
    230   $query.= $this->get_date_where();
    231   $query.= '
    232   GROUP BY period
    233   ORDER BY '.pwg_db_get_year($this->date_field).' DESC, '.pwg_db_get_month($this->date_field).' ASC';
    234 
    235   $result = pwg_query($query);
    236   $items=array();
    237   while ($row = pwg_db_fetch_assoc($result))
    238   {
    239     $y = substr($row['period'], 0, 4);
    240     $m = (int)substr($row['period'], 4, 2);
    241     if ( ! isset($items[$y]) )
    242     {
    243       $items[$y] = array('nb_images'=>0, 'children'=>array() );
    244     }
    245     $items[$y]['children'][$m] = $row['count'];
    246     $items[$y]['nb_images'] += $row['count'];
    247   }
    248   //echo ('<pre>'. var_export($items, true) . '</pre>');
    249   if (count($items)==1)
    250   {// only one year exists so bail out to year view
    251     list($y) = array_keys($items);
    252     $page['chronology_date'][CYEAR] = $y;
    253     return false;
    254   }
    255 
    256   global $lang;
    257   foreach ( $items as $year=>$year_data)
    258   {
    259     $chronology_date = array( $year );
    260     $url = duplicate_index_url( array('chronology_date'=>$chronology_date) );
    261 
    262     $nav_bar = $this->get_nav_bar_from_items( $chronology_date,
    263             $year_data['children'], false, false, $lang['month'] );
    264 
    265     $tpl_var['calendar_bars'][] =
    266       array(
    267         'U_HEAD'  => $url,
    268         'NB_IMAGES' => $year_data['nb_images'],
    269         'HEAD_LABEL' => $year,
    270         'items' => $nav_bar,
    271       );
    272   }
    273   return true;
    274 }
    275 
    276 function build_year_calendar(&$tpl_var)
    277 {
    278   global $page;
    279 
    280   assert( count($page['chronology_date']) == 1 );
    281   $query='SELECT '.pwg_db_get_date_MMDD($this->date_field).' as period,
    282             COUNT(DISTINCT id) as count';
    283   $query.= $this->inner_sql;
    284   $query.= $this->get_date_where();
    285   $query.= '
    286   GROUP BY period
    287   ORDER BY period ASC';
    288 
    289   $result = pwg_query($query);
    290   $items=array();
    291   while ($row = pwg_db_fetch_assoc($result))
    292   {
    293     $m = (int)substr($row['period'], 0, 2);
    294     $d = substr($row['period'], 2, 2);
    295     if ( ! isset($items[$m]) )
    296     {
    297       $items[$m] = array('nb_images'=>0, 'children'=>array() );
    298     }
    299     $items[$m]['children'][$d] = $row['count'];
    300     $items[$m]['nb_images'] += $row['count'];
    301   }
    302   if (count($items)==1)
    303   { // only one month exists so bail out to month view
    304     list($m) = array_keys($items);
    305     $page['chronology_date'][CMONTH] = $m;
    306     return false;
    307   }
    308   global $lang;
    309   foreach ( $items as $month=>$month_data)
    310   {
    311     $chronology_date = array( $page['chronology_date'][CYEAR], $month );
    312     $url = duplicate_index_url( array('chronology_date'=>$chronology_date) );
    313 
    314     $nav_bar = $this->get_nav_bar_from_items( $chronology_date,
    315                      $month_data['children'], false );
    316 
    317     $tpl_var['calendar_bars'][] =
    318       array(
    319         'U_HEAD'  => $url,
    320         'NB_IMAGES' => $month_data['nb_images'],
    321         'HEAD_LABEL' => $lang['month'][$month],
    322         'items' => $nav_bar,
    323       );
    324   }
    325   return true;
    326 
    327 }
    328 
    329 function build_month_calendar(&$tpl_var)
    330 {
    331   global $page, $lang, $conf;
    332 
    333   $query='SELECT '.pwg_db_get_dayofmonth($this->date_field).' as period,
    334             COUNT(DISTINCT id) as count';
    335   $query.= $this->inner_sql;
    336   $query.= $this->get_date_where();
    337   $query.= '
    338   GROUP BY period
    339   ORDER BY period ASC';
    340 
    341   $items=array();
    342   $result = pwg_query($query);
    343   while ($row = pwg_db_fetch_assoc($result))
    344   {
    345     $d = (int)$row['period'];
    346     $items[$d] = array('nb_images'=>$row['count']);
    347   }
    348 
    349   foreach ( $items as $day=>$data)
    350   {
    351     $page['chronology_date'][CDAY]=$day;
    352     $query = '
    353 SELECT id, file,representative_ext,path,width,height,rotation, '.pwg_db_get_dayofweek($this->date_field).'-1 as dow';
     224      $nb_days = 31;
     225    }
     226    return $nb_days;
     227  }
     228
     229  /**
     230   * Build global calendar and assign the result in _$tpl_var_
     231   *
     232   * @param array $tpl_var
     233   * @return bool
     234   */
     235  protected function build_global_calendar(&$tpl_var)
     236  {
     237    global $page;
     238
     239    assert( count($page['chronology_date']) == 0 );
     240    $query='
     241  SELECT '.pwg_db_get_date_YYYYMM($this->date_field).' as period,
     242    COUNT(distinct id) as count';
    354243    $query.= $this->inner_sql;
    355244    $query.= $this->get_date_where();
    356245    $query.= '
    357   ORDER BY '.DB_RANDOM_FUNCTION.'()
    358   LIMIT 1';
    359     unset ( $page['chronology_date'][CDAY] );
    360 
    361     $row = pwg_db_fetch_assoc(pwg_query($query));
    362     $derivative = new DerivativeImage(IMG_SQUARE, new SrcImage($row));
    363     $items[$day]['derivative'] = $derivative;
    364     $items[$day]['file'] = $row['file'];
    365     $items[$day]['dow'] = $row['dow'];
    366   }
    367 
    368   if ( !empty($items) )
    369   {
    370     list($known_day) = array_keys($items);
    371     $known_dow = $items[$known_day]['dow'];
    372     $first_day_dow = ($known_dow-($known_day-1))%7;
    373     if ($first_day_dow<0)
    374     {
    375       $first_day_dow += 7;
    376     }
    377     //first_day_dow = week day corresponding to the first day of this month
    378     $wday_labels = $lang['day'];
    379 
    380     if ('monday' == $conf['week_starts_on'])
    381     {
    382       if ($first_day_dow==0)
    383       {
    384         $first_day_dow = 6;
    385       }
    386       else
    387       {
    388         $first_day_dow -= 1;
    389       }
    390 
    391       $wday_labels[] = array_shift($wday_labels);
    392     }
    393 
    394     list($cell_width, $cell_height) = ImageStdParams::get_by_type(IMG_SQUARE)->sizing->ideal_size;
    395     if ($cell_width>120)
    396     {
    397       $cell_width = $cell_height = 120;
    398     }
    399 
    400     $tpl_weeks    = array();
    401     $tpl_crt_week = array();
    402 
    403     //fill the empty days in the week before first day of this month
    404     for ($i=0; $i<$first_day_dow; $i++)
    405     {
    406       $tpl_crt_week[] = array();
    407     }
    408 
    409     for ( $day = 1;
    410           $day <= $this->get_all_days_in_month(
    411             $page['chronology_date'][CYEAR], $page['chronology_date'][CMONTH]
     246    GROUP BY period
     247    ORDER BY '.pwg_db_get_year($this->date_field).' DESC, '.pwg_db_get_month($this->date_field).' ASC';
     248
     249    $result = pwg_query($query);
     250    $items=array();
     251    while ($row = pwg_db_fetch_assoc($result))
     252    {
     253      $y = substr($row['period'], 0, 4);
     254      $m = (int)substr($row['period'], 4, 2);
     255      if ( ! isset($items[$y]) )
     256      {
     257        $items[$y] = array('nb_images'=>0, 'children'=>array() );
     258      }
     259      $items[$y]['children'][$m] = $row['count'];
     260      $items[$y]['nb_images'] += $row['count'];
     261    }
     262    //echo ('<pre>'. var_export($items, true) . '</pre>');
     263    if (count($items)==1)
     264    {// only one year exists so bail out to year view
     265      list($y) = array_keys($items);
     266      $page['chronology_date'][CYEAR] = $y;
     267      return false;
     268    }
     269
     270    global $lang;
     271    foreach ( $items as $year=>$year_data)
     272    {
     273      $chronology_date = array( $year );
     274      $url = duplicate_index_url( array('chronology_date'=>$chronology_date) );
     275
     276      $nav_bar = $this->get_nav_bar_from_items( $chronology_date,
     277              $year_data['children'], false, false, $lang['month'] );
     278
     279      $tpl_var['calendar_bars'][] =
     280        array(
     281          'U_HEAD'  => $url,
     282          'NB_IMAGES' => $year_data['nb_images'],
     283          'HEAD_LABEL' => $year,
     284          'items' => $nav_bar,
     285        );
     286    }
     287
     288    return true;
     289  }
     290
     291  /**
     292   * Build year calendar and assign the result in _$tpl_var_
     293   *
     294   * @param array $tpl_var
     295   * @return bool
     296   */
     297  protected function build_year_calendar(&$tpl_var)
     298  {
     299    global $page;
     300
     301    assert( count($page['chronology_date']) == 1 );
     302    $query='SELECT '.pwg_db_get_date_MMDD($this->date_field).' as period,
     303              COUNT(DISTINCT id) as count';
     304    $query.= $this->inner_sql;
     305    $query.= $this->get_date_where();
     306    $query.= '
     307    GROUP BY period
     308    ORDER BY period ASC';
     309
     310    $result = pwg_query($query);
     311    $items=array();
     312    while ($row = pwg_db_fetch_assoc($result))
     313    {
     314      $m = (int)substr($row['period'], 0, 2);
     315      $d = substr($row['period'], 2, 2);
     316      if ( ! isset($items[$m]) )
     317      {
     318        $items[$m] = array('nb_images'=>0, 'children'=>array() );
     319      }
     320      $items[$m]['children'][$d] = $row['count'];
     321      $items[$m]['nb_images'] += $row['count'];
     322    }
     323    if (count($items)==1)
     324    { // only one month exists so bail out to month view
     325      list($m) = array_keys($items);
     326      $page['chronology_date'][CMONTH] = $m;
     327      return false;
     328    }
     329    global $lang;
     330    foreach ( $items as $month=>$month_data)
     331    {
     332      $chronology_date = array( $page['chronology_date'][CYEAR], $month );
     333      $url = duplicate_index_url( array('chronology_date'=>$chronology_date) );
     334
     335      $nav_bar = $this->get_nav_bar_from_items( $chronology_date,
     336                       $month_data['children'], false );
     337
     338      $tpl_var['calendar_bars'][] =
     339        array(
     340          'U_HEAD'  => $url,
     341          'NB_IMAGES' => $month_data['nb_images'],
     342          'HEAD_LABEL' => $lang['month'][$month],
     343          'items' => $nav_bar,
     344        );
     345    }
     346
     347    return true;
     348  }
     349
     350  /**
     351   * Build month calendar and assign the result in _$tpl_var_
     352   *
     353   * @param array $tpl_var
     354   * @return bool
     355   */
     356  protected function build_month_calendar(&$tpl_var)
     357  {
     358    global $page, $lang, $conf;
     359
     360    $query='SELECT '.pwg_db_get_dayofmonth($this->date_field).' as period,
     361              COUNT(DISTINCT id) as count';
     362    $query.= $this->inner_sql;
     363    $query.= $this->get_date_where();
     364    $query.= '
     365    GROUP BY period
     366    ORDER BY period ASC';
     367
     368    $items=array();
     369    $result = pwg_query($query);
     370    while ($row = pwg_db_fetch_assoc($result))
     371    {
     372      $d = (int)$row['period'];
     373      $items[$d] = array('nb_images'=>$row['count']);
     374    }
     375
     376    foreach ( $items as $day=>$data)
     377    {
     378      $page['chronology_date'][CDAY]=$day;
     379      $query = '
     380  SELECT id, file,representative_ext,path,width,height,rotation, '.pwg_db_get_dayofweek($this->date_field).'-1 as dow';
     381      $query.= $this->inner_sql;
     382      $query.= $this->get_date_where();
     383      $query.= '
     384    ORDER BY '.DB_RANDOM_FUNCTION.'()
     385    LIMIT 1';
     386      unset ( $page['chronology_date'][CDAY] );
     387
     388      $row = pwg_db_fetch_assoc(pwg_query($query));
     389      $derivative = new DerivativeImage(IMG_SQUARE, new SrcImage($row));
     390      $items[$day]['derivative'] = $derivative;
     391      $items[$day]['file'] = $row['file'];
     392      $items[$day]['dow'] = $row['dow'];
     393    }
     394
     395    if ( !empty($items) )
     396    {
     397      list($known_day) = array_keys($items);
     398      $known_dow = $items[$known_day]['dow'];
     399      $first_day_dow = ($known_dow-($known_day-1))%7;
     400      if ($first_day_dow<0)
     401      {
     402        $first_day_dow += 7;
     403      }
     404      //first_day_dow = week day corresponding to the first day of this month
     405      $wday_labels = $lang['day'];
     406
     407      if ('monday' == $conf['week_starts_on'])
     408      {
     409        if ($first_day_dow==0)
     410        {
     411          $first_day_dow = 6;
     412        }
     413        else
     414        {
     415          $first_day_dow -= 1;
     416        }
     417
     418        $wday_labels[] = array_shift($wday_labels);
     419      }
     420
     421      list($cell_width, $cell_height) = ImageStdParams::get_by_type(IMG_SQUARE)->sizing->ideal_size;
     422      if ($cell_width>120)
     423      {
     424        $cell_width = $cell_height = 120;
     425      }
     426
     427      $tpl_weeks    = array();
     428      $tpl_crt_week = array();
     429
     430      //fill the empty days in the week before first day of this month
     431      for ($i=0; $i<$first_day_dow; $i++)
     432      {
     433        $tpl_crt_week[] = array();
     434      }
     435
     436      for ( $day = 1;
     437            $day <= $this->get_all_days_in_month(
     438              $page['chronology_date'][CYEAR], $page['chronology_date'][CMONTH]
     439                );
     440            $day++)
     441      {
     442        $dow = ($first_day_dow + $day-1)%7;
     443        if ($dow==0 and $day!=1)
     444        {
     445          $tpl_weeks[]    = $tpl_crt_week; // add finished week to week list
     446          $tpl_crt_week   = array(); // start new week
     447        }
     448
     449        if ( !isset($items[$day]) )
     450        {// empty day
     451          $tpl_crt_week[]   =
     452            array(
     453                'DAY' => $day
    412454              );
    413           $day++)
    414     {
    415       $dow = ($first_day_dow + $day-1)%7;
    416       if ($dow==0 and $day!=1)
    417       {
    418         $tpl_weeks[]    = $tpl_crt_week; // add finished week to week list
    419         $tpl_crt_week   = array(); // start new week
    420       }
    421 
    422       if ( !isset($items[$day]) )
    423       {// empty day
    424         $tpl_crt_week[]   =
    425           array(
    426               'DAY' => $day
    427             );
    428       }
    429       else
    430       {
    431         list($tn_width,$tn_height) = $items[$day]['derivative']->get_size();
    432 
    433         // now need to fit the thumbnail of size tn_size within
    434         // a cell of size cell_size by playing with CSS position (left/top)
    435         // and the width and height of <img>.
    436         $ratio_w = $tn_width/$cell_width;
    437         $ratio_h = $tn_height/$cell_height;
    438 
    439         $pos_top=$pos_left=0;
    440         $css_style = '';
    441 
    442         if ( $ratio_w>1 and $ratio_h>1)
    443         {// cell completely smaller than the thumbnail so we will let the browser
    444          // resize the thumbnail
    445           if ($ratio_w > $ratio_h )
    446           {// thumbnail ratio compared to cell -> wide format
    447             $css_style = 'height:'.$cell_height.'px;';
    448             $browser_img_width = $cell_height*$tn_width/$tn_height;
    449             $pos_left = ($browser_img_width-$cell_width)/2;
     455        }
     456        else
     457        {
     458          list($tn_width,$tn_height) = $items[$day]['derivative']->get_size();
     459
     460          // now need to fit the thumbnail of size tn_size within
     461          // a cell of size cell_size by playing with CSS position (left/top)
     462          // and the width and height of <img>.
     463          $ratio_w = $tn_width/$cell_width;
     464          $ratio_h = $tn_height/$cell_height;
     465
     466          $pos_top=$pos_left=0;
     467          $css_style = '';
     468
     469          if ( $ratio_w>1 and $ratio_h>1)
     470          {// cell completely smaller than the thumbnail so we will let the browser
     471           // resize the thumbnail
     472            if ($ratio_w > $ratio_h )
     473            {// thumbnail ratio compared to cell -> wide format
     474              $css_style = 'height:'.$cell_height.'px;';
     475              $browser_img_width = $cell_height*$tn_width/$tn_height;
     476              $pos_left = ($browser_img_width-$cell_width)/2;
     477            }
     478            else
     479            {
     480              $css_style = 'width:'.$cell_width.'px;';
     481              $browser_img_height = $cell_width*$tn_height/$tn_width;
     482              $pos_top = ($browser_img_height-$cell_height)/2;
     483            }
    450484          }
    451485          else
    452486          {
    453             $css_style = 'width:'.$cell_width.'px;';
    454             $browser_img_height = $cell_width*$tn_height/$tn_width;
    455             $pos_top = ($browser_img_height-$cell_height)/2;
     487            $pos_left = ($tn_width-$cell_width)/2;
     488            $pos_top = ($tn_height-$cell_height)/2;
    456489          }
    457         }
    458         else
    459         {
    460           $pos_left = ($tn_width-$cell_width)/2;
    461           $pos_top = ($tn_height-$cell_height)/2;
    462         }
    463 
    464         if ( round($pos_left)!=0)
    465         {
    466           $css_style.='left:'.round(-$pos_left).'px;';
    467         }
    468         if ( round($pos_top)!=0)
    469         {
    470           $css_style.='top:'.round(-$pos_top).'px;';
    471         }
    472         $url = duplicate_index_url(
     490
     491          if ( round($pos_left)!=0)
     492          {
     493            $css_style.='left:'.round(-$pos_left).'px;';
     494          }
     495          if ( round($pos_top)!=0)
     496          {
     497            $css_style.='top:'.round(-$pos_top).'px;';
     498          }
     499          $url = duplicate_index_url(
     500              array(
     501                'chronology_date' =>
     502                  array(
     503                    $page['chronology_date'][CYEAR],
     504                    $page['chronology_date'][CMONTH],
     505                    $day
     506                  )
     507              )
     508            );
     509
     510          $tpl_crt_week[]   =
    473511            array(
    474               'chronology_date' =>
    475                 array(
    476                   $page['chronology_date'][CYEAR],
    477                   $page['chronology_date'][CMONTH],
    478                   $day
    479                 )
    480             )
    481           );
    482 
    483         $tpl_crt_week[]   =
     512                'DAY'         => $day,
     513                'DOW'         => $dow,
     514                'NB_ELEMENTS' => $items[$day]['nb_images'],
     515                'IMAGE'       => $items[$day]['derivative']->get_url(),
     516                'U_IMG_LINK'  => $url,
     517                'IMAGE_STYLE' => $css_style,
     518                'IMAGE_ALT'   => $items[$day]['file'],
     519              );
     520        }
     521      }
     522      //fill the empty days in the week after the last day of this month
     523      while ( $dow<6 )
     524      {
     525        $tpl_crt_week[] = array();
     526        $dow++;
     527      }
     528      $tpl_weeks[]    = $tpl_crt_week;
     529
     530      $tpl_var['month_view'] =
    484531          array(
    485               'DAY'         => $day,
    486               'DOW'         => $dow,
    487               'NB_ELEMENTS' => $items[$day]['nb_images'],
    488               'IMAGE'       => $items[$day]['derivative']->get_url(),
    489               'U_IMG_LINK'  => $url,
    490               'IMAGE_STYLE' => $css_style,
    491               'IMAGE_ALT'   => $items[$day]['file'],
     532             'CELL_WIDTH'   => $cell_width,
     533             'CELL_HEIGHT' => $cell_height,
     534             'wday_labels' => $wday_labels,
     535             'weeks' => $tpl_weeks,
    492536            );
    493       }
    494     }
    495     //fill the empty days in the week after the last day of this month
    496     while ( $dow<6 )
    497     {
    498       $tpl_crt_week[] = array();
    499       $dow++;
    500     }
    501     $tpl_weeks[]    = $tpl_crt_week;
    502 
    503     $tpl_var['month_view'] =
    504         array(
    505            'CELL_WIDTH'   => $cell_width,
    506            'CELL_HEIGHT' => $cell_height,
    507            'wday_labels' => $wday_labels,
    508            'weeks' => $tpl_weeks,
    509           );
    510   }
    511 
    512   return true;
     537    }
     538
     539    return true;
     540  }
    513541}
    514542
    515 }
    516543?>
  • trunk/include/calendar_weekly.class.php

    r25018 r25507  
    2222// +-----------------------------------------------------------------------+
    2323
     24/**
     25 * @package functions\calendar
     26 */
     27
    2428include_once(PHPWG_ROOT_PATH.'include/calendar_base.class.php');
    2529
    26 define ('CYEAR', 0);
    27 define ('CWEEK', 1);
    28 define ('CDAY',  2);
     30/** level of year view */
     31define('CYEAR', 0);
     32/** level of week view */
     33define('CWEEK', 1);
     34/** level of day view */
     35define('CDAY',  2);
     36
    2937
    3038/**
     
    3341class Calendar extends CalendarBase
    3442{
    35 
    3643  /**
    3744   * Initialize the calendar
    38    * @param string inner_sql used for queries (INNER JOIN or normal)
     45   * @param string $inner_sql
    3946   */
    4047  function initialize($inner_sql)
     
    7380  }
    7481
    75 /**
    76  * Generate navigation bars for category page
    77  * @return boolean false to indicate that thumbnails where not included here
    78  */
    79 function generate_category_content()
    80 {
    81   global $conf, $page;
     82  /**
     83   * Generate navigation bars for category page.
     84   *
     85   * @return boolean false indicates that thumbnails where not included
     86   */
     87  function generate_category_content()
     88  {
     89    global $conf, $page;
    8290
    83   if ( count($page['chronology_date'])==0 )
    84   {
    85     $this->build_nav_bar(CYEAR); // years
    86   }
    87   if ( count($page['chronology_date'])==1 )
    88   {
    89     $this->build_nav_bar(CWEEK, array()); // week nav bar 1-53
    90   }
    91   if ( count($page['chronology_date'])==2 )
    92   {
    93     $this->build_nav_bar(CDAY); // days nav bar Mon-Sun
    94   }
    95   $this->build_next_prev();
    96   return false;
    97 }
    98 
    99 
    100 /**
    101  * Returns a sql where subquery for the date field
    102  * @param int max_levels return the where up to this level
    103  * (e.g. 2=only year and week in year)
    104  * @return string
    105  */
    106 function get_date_where($max_levels=3)
    107 {
    108   global $page;
    109   $date = $page['chronology_date'];
    110   while (count($date)>$max_levels)
    111   {
    112     array_pop($date);
    113   }
    114   $res = '';
    115   if (isset($date[CYEAR]) and $date[CYEAR]!=='any')
    116   {
    117     $y = $date[CYEAR];
    118     $res = " AND $this->date_field BETWEEN '$y-01-01' AND '$y-12-31 23:59:59'";
     91    if ( count($page['chronology_date'])==0 )
     92    {
     93      $this->build_nav_bar(CYEAR); // years
     94    }
     95    if ( count($page['chronology_date'])==1 )
     96    {
     97      $this->build_nav_bar(CWEEK, array()); // week nav bar 1-53
     98    }
     99    if ( count($page['chronology_date'])==2 )
     100    {
     101      $this->build_nav_bar(CDAY); // days nav bar Mon-Sun
     102    }
     103    $this->build_next_prev();
     104    return false;
    119105  }
    120106
    121   if (isset($date[CWEEK]) and $date[CWEEK]!=='any')
     107  /**
     108   * Returns a sql WHERE subquery for the date field.
     109   *
     110   * @param int $max_levels (e.g. 2=only year and month)
     111   * @return string
     112   */
     113  function get_date_where($max_levels=3)
    122114  {
    123     $res .= ' AND '.$this->calendar_levels[CWEEK]['sql'].'='.$date[CWEEK];
     115    global $page;
     116    $date = $page['chronology_date'];
     117    while (count($date)>$max_levels)
     118    {
     119      array_pop($date);
     120    }
     121    $res = '';
     122    if (isset($date[CYEAR]) and $date[CYEAR]!=='any')
     123    {
     124      $y = $date[CYEAR];
     125      $res = " AND $this->date_field BETWEEN '$y-01-01' AND '$y-12-31 23:59:59'";
     126    }
     127
     128    if (isset($date[CWEEK]) and $date[CWEEK]!=='any')
     129    {
     130      $res .= ' AND '.$this->calendar_levels[CWEEK]['sql'].'='.$date[CWEEK];
     131    }
     132    if (isset($date[CDAY]) and $date[CDAY]!=='any')
     133    {
     134      $res .= ' AND '.$this->calendar_levels[CDAY]['sql'].'='.$date[CDAY];
     135    }
     136    if (empty($res))
     137    {
     138      $res = ' AND '.$this->date_field.' IS NOT NULL';
     139    }
     140    return $res;
    124141  }
    125   if (isset($date[CDAY]) and $date[CDAY]!=='any')
    126   {
    127     $res .= ' AND '.$this->calendar_levels[CDAY]['sql'].'='.$date[CDAY];
    128   }
    129   if (empty($res))
    130   {
    131     $res = ' AND '.$this->date_field.' IS NOT NULL';
    132   }
    133   return $res;
    134 }
    135 
    136142}
    137143
  • trunk/include/functions.inc.php

    r25459 r25507  
    936936 *
    937937 * @param array $element_info element information from db (at least 'path')
    938  * @return strinf
     938 * @return string
    939939 */
    940940function get_element_path($element_info)
  • trunk/include/functions_calendar.inc.php

    r25018 r25507  
    2222// +-----------------------------------------------------------------------+
    2323
     24/**
     25 * @package functions\calendar
     26 */
     27
     28/** URL keyword for list view */
    2429define('CAL_VIEW_LIST',     'list');
     30/** URL keyword for calendar view */
    2531define('CAL_VIEW_CALENDAR', 'calendar');
    2632
     33
     34/**
     35 * Initialize _$page_ and _$template_ vars for calendar view.
     36 */
    2737function initialize_calendar()
    2838{
     
    125135  $cal_style = $page['chronology_style'];
    126136  include(PHPWG_ROOT_PATH.'include/'. $styles[$cal_style]['include']);
    127   $calendar = new Calendar();
     137  // TODO : class name overlap, rename them in CalendarMonth and CalendarWeek
     138  $calendar = new Calendar();
    128139
    129140  // Retrieve view
     
    290301  pwg_debug('end initialize_calendar');
    291302}
     303
    292304?>
Note: See TracChangeset for help on using the changeset viewer.