/**
  * 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 month calendar and assign the result in _$tpl_var_
    *
    * @param array $tpl_var
    * @return bool
    */
   protected function build_month_calendar(&$tpl_var)
   {
       global $page, $lang, $conf;
       $query = 'SELECT ' . pwg_db_get_dayofmonth($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 period ASC';
       $items = array();
       $result = pwg_query($query);
       while ($row = pwg_db_fetch_assoc($result)) {
           $d = (int) $row['period'];
           $items[$d] = array('nb_images' => $row['count']);
       }
       foreach ($items as $day => $data) {
           $page['chronology_date'][CDAY] = $day;
           $query = '
 SELECT id, file,representative_ext,path,width,height,rotation, ' . pwg_db_get_dayofweek($this->date_field) . '-1 as dow';
           $query .= $this->inner_sql;
           $query .= $this->get_date_where();
           $query .= '
   ORDER BY ' . DB_RANDOM_FUNCTION . '()
   LIMIT 1';
           unset($page['chronology_date'][CDAY]);
           $row = pwg_db_fetch_assoc(pwg_query($query));
           $derivative = new DerivativeImage(IMG_SQUARE, new SrcImage($row));
           $items[$day]['derivative'] = $derivative;
           $items[$day]['file'] = $row['file'];
           $items[$day]['dow'] = $row['dow'];
       }
       if (!empty($items)) {
           list($known_day) = array_keys($items);
           $known_dow = $items[$known_day]['dow'];
           $first_day_dow = ($known_dow - ($known_day - 1)) % 7;
           if ($first_day_dow < 0) {
               $first_day_dow += 7;
           }
           //first_day_dow = week day corresponding to the first day of this month
           $wday_labels = $lang['day'];
           if ('monday' == $conf['week_starts_on']) {
               if ($first_day_dow == 0) {
                   $first_day_dow = 6;
               } else {
                   $first_day_dow -= 1;
               }
               $wday_labels[] = array_shift($wday_labels);
           }
           list($cell_width, $cell_height) = ImageStdParams::get_by_type(IMG_SQUARE)->sizing->ideal_size;
           $tpl_weeks = array();
           $tpl_crt_week = array();
           //fill the empty days in the week before first day of this month
           for ($i = 0; $i < $first_day_dow; $i++) {
               $tpl_crt_week[] = array();
           }
           for ($day = 1; $day <= $this->get_all_days_in_month($page['chronology_date'][CYEAR], $page['chronology_date'][CMONTH]); $day++) {
               $dow = ($first_day_dow + $day - 1) % 7;
               if ($dow == 0 and $day != 1) {
                   $tpl_weeks[] = $tpl_crt_week;
                   // add finished week to week list
                   $tpl_crt_week = array();
                   // start new week
               }
               if (!isset($items[$day])) {
                   // empty day
                   $tpl_crt_week[] = array('DAY' => $day);
               } else {
                   $url = duplicate_index_url(array('chronology_date' => array($page['chronology_date'][CYEAR], $page['chronology_date'][CMONTH], $day)));
                   $tpl_crt_week[] = array('DAY' => $day, 'DOW' => $dow, 'NB_ELEMENTS' => $items[$day]['nb_images'], 'IMAGE' => $items[$day]['derivative']->get_url(), 'U_IMG_LINK' => $url, 'IMAGE_ALT' => $items[$day]['file']);
               }
           }
           //fill the empty days in the week after the last day of this month
           while ($dow < 6) {
               $tpl_crt_week[] = array();
               $dow++;
           }
           $tpl_weeks[] = $tpl_crt_week;
           $tpl_var['month_view'] = array('CELL_WIDTH' => $cell_width, 'CELL_HEIGHT' => $cell_height, 'wday_labels' => $wday_labels, 'weeks' => $tpl_weeks);
       }
       return true;
   }