/**
  * Initialize the calendar
  * @param string $inner_sql
  */
 function initialize($inner_sql)
 {
     parent::initialize($inner_sql);
     global $lang, $conf;
     $week_no_labels = array();
     for ($i = 1; $i <= 53; $i++) {
         $week_no_labels[$i] = l10n('Week %d', $i);
         //$week_no_labels[$i] = $i;
     }
     $this->calendar_levels = array(array('sql' => pwg_db_get_year($this->date_field), 'labels' => null), array('sql' => pwg_db_get_week($this->date_field) . '+1', 'labels' => $week_no_labels), array('sql' => pwg_db_get_dayofweek($this->date_field) . '-1', 'labels' => $lang['day']));
     //Comment next lines for week starting on Sunday or if MySQL version<4.0.17
     //WEEK(date,5) = "0-53 - Week 1=the first week with a Monday in this year"
     if ('monday' == $conf['week_starts_on']) {
         $this->calendar_levels[CWEEK]['sql'] = pwg_db_get_week($this->date_field, 5) . '+1';
         $this->calendar_levels[CDAY]['sql'] = pwg_db_get_weekday($this->date_field);
         $this->calendar_levels[CDAY]['labels'][] = array_shift($this->calendar_levels[CDAY]['labels']);
     }
 }
   /**
    * Build global calendar and assign the result in _$tpl_var_
    *
    * @param array $tpl_var
    * @return bool
    */
   protected function build_global_calendar(&$tpl_var)
   {
       global $page;
       assert(count($page['chronology_date']) == 0);
       $query = '
 SELECT ' . pwg_db_get_date_YYYYMM($this->date_field) . ' as period,
   COUNT(distinct id) as count';
       $query .= $this->inner_sql;
       $query .= $this->get_date_where();
       $query .= '
   GROUP BY period
   ORDER BY ' . pwg_db_get_year($this->date_field) . ' DESC, ' . pwg_db_get_month($this->date_field) . ' ASC';
       $result = pwg_query($query);
       $items = array();
       while ($row = pwg_db_fetch_assoc($result)) {
           $y = substr($row['period'], 0, 4);
           $m = (int) substr($row['period'], 4, 2);
           if (!isset($items[$y])) {
               $items[$y] = array('nb_images' => 0, 'children' => array());
           }
           $items[$y]['children'][$m] = $row['count'];
           $items[$y]['nb_images'] += $row['count'];
       }
       //echo ('<pre>'. var_export($items, true) . '</pre>');
       if (count($items) == 1) {
           // only one year exists so bail out to year view
           list($y) = array_keys($items);
           $page['chronology_date'][CYEAR] = $y;
           return false;
       }
       global $lang;
       foreach ($items as $year => $year_data) {
           $chronology_date = array($year);
           $url = duplicate_index_url(array('chronology_date' => $chronology_date));
           $nav_bar = $this->get_nav_bar_from_items($chronology_date, $year_data['children'], false, false, $lang['month']);
           $tpl_var['calendar_bars'][] = array('U_HEAD' => $url, 'NB_IMAGES' => $year_data['nb_images'], 'HEAD_LABEL' => $year, 'items' => $nav_bar);
       }
       return true;
   }