/**
  * planner_prepare - prepare the planner view
  *
  * - sets global environment variables
  * - initializes class member variables used in multiple planner related functions
  * - generates header lines for the planner view (month, calendar week, days)
  */
 function planner_prepare($no_header = False)
 {
     // set some globals
     //
     if (!$no_header) {
         unset($GLOBALS['phpgw_info']['flags']['noheader']);
         unset($GLOBALS['phpgw_info']['flags']['nonavbar']);
         if ($this->always_app_header) {
             $GLOBALS['phpgw_info']['flags']['app_header'] = $GLOBALS['phpgw_info']['apps']['calendar']['title'] . ' - ' . lang('Group Planner');
         }
         //$GLOBALS['phpgw']->common->phpgw_header();
         $this->header();
     }
     // intervals_per_day can be configured in preferences now :-)
     //
     if (!$this->bo->prefs['calendar']['planner_intervals_per_day']) {
         $GLOBALS['phpgw']->preferences->read();
         $GLOBALS['phpgw']->preferences->add('calendar', 'planner_intervals_per_day', 3);
         $GLOBALS['phpgw']->preferences->save_repository();
         $this->bo->prefs['calendar']['planner_intervals_per_day'] = 3;
     }
     $intervals_per_day = $this->bo->prefs['calendar']['planner_intervals_per_day'];
     $this->bo->save_sessiondata();
     // need to save $this->bo->save_owner
     // set title for table and rows of planner view
     //
     if ($this->bo->sortby == 'category') {
         $title = lang('Category');
     } else {
         $title = lang('User');
         $this->set_planner_group_members();
     }
     // create/initialize variables directly used for HTML code generation
     //
     $this->planner_html = CreateObject('calendar.html');
     $this->planner_header = array();
     $this->planner_rows = array();
     // generate header lines with days and associated months
     //
     $hdr =& $this->planner_header;
     $hdr[0]['0'] = $title;
     $hdr[0]['.0'] = 'rowspan="3"';
     $this->planner_days = 0;
     // reset
     $m = $this->bo->month;
     $y = $this->bo->year;
     $this->bo->read_holidays($y);
     for ($i = 1; $i <= $this->bo->num_months; $i++, $m++) {
         if ($m == 13) {
             $m = 1;
             $y++;
             // "wrap-around" into new year
             $this->bo->read_holidays($y);
         }
         $days = phpgwapi_datetime::days_in_month($m, $y);
         $d = mktime(0, 0, 0, $m, 1, $y);
         $month = lang(date('F', $d)) . strftime(' %Y', $d);
         $cssclass = $m % 2 ? 'row_on' : 'row_off';
         $cols = $days * $intervals_per_day;
         $hdr[0]['.' . $i] = 'classr="' . $cssclass . '" colspan="' . $cols . '" align ="center"';
         $prev_month = sprintf('%04d%02d01', $y - ($m == 1), $m > 1 ? $m - 1 : 12);
         $next_month = sprintf('%04d%02d01', $y + ($m == 12), $m < 12 ? $m + 1 : 1);
         $prev_link = $GLOBALS['phpgw']->link('/index.php', array('menuaction' => 'calendar.uicalendar.planner', 'date' => $prev_month));
         $next_link = $GLOBALS['phpgw']->link('/index.php', array('menuaction' => 'calendar.uicalendar.planner', 'date' => $next_month));
         $hdr[0][$i] = "<b><a href=\"{$prev_link}\">&lt;&lt;</a> &nbsp {$month} &nbsp <a href=\"{$next_link}\">&gt;&gt;</a></b>";
         $add_owner = array();
         // if no add-rights on the showed cal use own cal
         if ((!isset($this->bo->save_owner) || !$this->bo->save_owner) && !$this->bo->check_perms(PHPGW_ACL_ADD) || !$this->bo->check_perms(PHPGW_ACL_ADD, 0, isset($this->bo->save_owner) ? $this->bo->save_owner : '')) {
             $add_owner = array('owner' => $this->bo->contacts->is_contact($GLOBALS['phpgw_info']['user']['account_id']));
         } else {
             $add_owner = array('owner' => isset($this->bo->save_owner) && $this->bo->save_owner ? $this->bo->save_owner : $this->bo->contacts->is_contact($GLOBALS['phpgw_info']['user']['account_id']));
         }
         for ($d = 1; $d <= $days; $d++) {
             $dayname = substr(lang(date('D', mktime(0, 0, 0, $m, $d, $y))), 0, 2);
             $index = $d + $this->planner_days;
             $hdr[2]['.' . $index] = 'colspan="' . $intervals_per_day . '" align="center"';
             // highlight today, saturday, sunday and holidays
             //
             $dow = phpgwapi_datetime::day_of_week($y, $m, $d);
             $date = sprintf("%04d%02d%02d", $y, $m, $d);
             if ($date == date('Ymd')) {
                 $cssclass = 'cal_today';
             } elseif (isset($this->bo->cached_holidays[$date]) && $this->bo->cached_holidays[$date]) {
                 $cssclass = $this->bo->holiday_class;
                 $hdr[2]['.' . $index] .= ' title="' . $this->bo->cached_holidays[$date][0]['name'] . '"';
             } elseif ($dow == 0 || $dow == 6) {
                 $cssclass = 'th';
             }
             $hdr[2]['.' . $index] .= " class=\"{$cssclass}\"";
             $hdr[2][$index] = '<a href="' . $this->planner_html->link('/index.php', array('menuaction' => 'calendar.uicalendar.add', 'date' => $date) + $add_owner) . '">' . $dayname . '<br />' . $d . '</a>';
         }
         $this->planner_days += $days;
     }
     // create/initialize member variables describing the time interval to be displayed
     //
     $this->planner_end_month = $m - 1;
     $this->planner_end_year = $y;
     $this->planner_days_in_end_month = phpgwapi_datetime::days_in_month($this->planner_end_month, $this->planner_end_year);
     $this->planner_firstday = intval(date('Ymd', mktime(0, 0, 0, $this->bo->month, 1, $this->bo->year)));
     $this->planner_lastday = intval(date('Ymd', mktime(0, 0, 0, $this->planner_end_month, $this->planner_days_in_end_month, $this->planner_end_year)));
     // generate line with calendar weeks in observed interval
     //
     $d = mktime(0, 0, 0, $this->bo->month, 1, $this->bo->year);
     $w = date('W', $d);
     if ($w == 'W') {
         $w = 1 + intval(date('z', $d) / 7);
         // a bit simplistic
     }
     $offset = (7 - date("w", $d) + 1) % 7;
     $offset = $offset == 0 ? 7 : $offset;
     $cssclass = $w % 2 ? 'th' : 'row_on';
     $hdr[1]['.' . $w] = 'class="' . $cssclass . '" colspan="' . $intervals_per_day * $offset . '" align="left"';
     $hdr[1][$w] = '';
     if ($offset >= 3) {
         $hdr[1][$w] .= '<font size="-2"> ' . lang('week') . ' ' . $w . ' </font>';
     }
     $days_left = $this->planner_days - $offset;
     $colspan = 7 * $intervals_per_day;
     while ($days_left > 0) {
         $colspan = $days_left < 7 ? $days_left * $intervals_per_day : $colspan;
         $d += 604800;
         // 7 days whith 24 hours (1h == 3600 seconds) each
         $w = date('W', $d);
         if ($w == 'W') {
             $w = 1 + intval(date('z', $d) / 7);
             // a bit simplistic
         }
         $w += isset($hdr[1][$w]) ? 1 : 0;
         // bug in "date('W')" ?
         $cssclass = $w % 2 ? 'th' : 'row_on';
         $hdr[1]['.' . $w] = 'cssclass="' . $cssclass . '" colspan="' . $colspan . '" align="left"';
         $hdr[1][$w] = '';
         if ($days_left >= 3) {
             $hdr[1][$w] .= '<font size="-2"> ' . lang('week') . ' ' . $w . ' </font>';
         }
         $days_left -= 7;
     }
     return $hdr;
 }