/**
  * Rebuild Calendar Cache
  *
  * @access	public
  * @param	bool		Whether to return or not
  * @return	void
  */
 public function calendarRebuildCache($return = 0)
 {
     /* INIT */
     $this->settings['calendar_limit'] = intval($this->settings['calendar_limit']) < 2 ? 1 : intval($this->settings['calendar_limit']);
     //--------------------------------------------
     // Grab an extra day for the TZ diff
     //--------------------------------------------
     $this->settings['calendar_limit']++;
     list($month, $day, $year) = explode(',', gmdate('n,j,Y', time()));
     $timenow = gmmktime(0, 0, 0, $month, 1, $year);
     $timethen = time() + intval($this->settings['calendar_limit']) * 86400;
     $seenids = array();
     $nowtime = time() - 86400;
     $birthdays = array();
     $calevents = array();
     $calendars = array();
     $a = explode(',', gmdate('Y,n,j,G,i,s', time()));
     $day = $a[2];
     $month = $a[1];
     $year = $a[0];
     $daysinmonth = date('t', time());
     //-----------------------------------------
     // Get 24hr before and 24hr after to make
     // sure we don't break any timezones
     //-----------------------------------------
     $last_day = $day - 1;
     $last_month = $month;
     $last_year = $year;
     $next_day = $day + 1;
     $next_month = $month;
     $next_year = $year;
     //-----------------------------------------
     // Calculate dates..
     //-----------------------------------------
     if ($last_day == 0) {
         $last_month -= 1;
         $last_day = gmdate('t', time());
     }
     if ($last_month == 0) {
         $last_month = 12;
         $last_year -= 1;
     }
     if ($next_day > gmdate('t', time())) {
         $next_month += 1;
         $next_day = 1;
     }
     if ($next_month == 13) {
         $next_month = 1;
         $next_year += 1;
     }
     //--------------------------------------------
     // Get classes
     //--------------------------------------------
     require_once IPSLib::getAppDir('calendar') . '/modules_public/calendar/calendars.php';
     $calendar = new public_calendar_calendar_calendars($this->registry);
     $calendar->makeRegistryShortcuts($this->registry);
     //--------------------------------------------
     // Get stuff
     //--------------------------------------------
     $this->DB->build(array('select' => 'c.*', 'from' => array('cal_calendars' => 'c'), 'order' => 'c.cal_position ASC', 'add_join' => array(array('select' => 'p.*', 'from' => array('permission_index' => 'p'), 'where' => "p.perm_type='calendar' AND perm_type_id=c.cal_id", 'type' => 'left'))));
     $this->DB->execute();
     while ($row = $this->DB->fetch()) {
         $row['_perm_read'] = $perms['perm_view'];
         $calendars[$row['cal_id']] = $row;
     }
     $calendar->calendarGetEventsSQL(0, 0, array('timenow' => $timenow, 'timethen' => $timethen));
     //--------------------------------------------
     // OK.. Go through days and check events
     //--------------------------------------------
     for ($i = 0; $i <= $this->settings['calendar_limit']; $i++) {
         list($_month, $tday, $year) = explode(',', gmdate('n,j,Y', $nowtime));
         $eventcache = $calendar->calendarGetDayEvents($_month, $tday, $year);
         foreach ($eventcache as $event) {
             if (!in_array($event['event_id'], $seenids)) {
                 if ($calendar->getInfoEvents($event, $_month, $tday, $year, 0)) {
                     if (!$event['event_approved']) {
                         continue;
                     }
                     unset($event['event_content'], $event['event_smilies']);
                     $event['_perm_read'] = $calendars[$event['event_calendar_id']]['perm_view'];
                     $calevents[$event['event_id']] = $event;
                 }
                 $seenids[$event['event_id']] = $event['event_id'];
             }
         }
         $nowtime += 86400;
     }
     //-----------------------------------------
     // Grab birthdays
     //-----------------------------------------
     $append_string = "";
     if (!date("L")) {
         if ($month == "2" and ($day == "28" or $day == "27")) {
             $append_string = " or( bday_month=2 AND bday_day=29 )";
         }
     }
     $this->DB->build(array('select' => 'member_id, members_seo_name, members_display_name, member_group_id, bday_day, bday_month, bday_year', 'from' => 'members', 'where' => "( bday_day={$last_day} AND bday_month={$last_month} )\n\t\t\t\t\t\t\t\t\t\t\t or ( bday_day={$day} AND bday_month={$month} )\n\t\t\t\t\t\t\t\t\t\t\t or ( bday_day={$next_day} AND bday_month={$next_month} ) {$append_string}"));
     $this->DB->execute();
     $birthdays = array();
     while ($r = $this->DB->fetch()) {
         $birthdays[$r['member_id']] = $r;
     }
     //--------------------------------------------
     // Update calendar array
     //--------------------------------------------
     $this->cache->setCache('calendar', $calevents, array('array' => 1, 'deletefirst' => 1));
     $this->cache->setCache('birthdays', $birthdays, array('array' => 1, 'deletefirst' => 1));
     if ($return) {
         $this->registry->output->global_message = $this->lang->words['c_recached'];
         $this->calendarsList();
     }
 }