[ Index ]

PHP Cross Reference of MyBB 1.8.38

title

Body

[close]

/inc/ -> functions_calendar.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  /**
  12   * Build a mini calendar for a specific month
  13   *
  14   * @param array $calendar The calendar array for the calendar
  15   * @param int $month The month of the year
  16   * @param int $year The year
  17   * @param array $events_cache Optional events cache for this calendar
  18   * @return string The built mini calendar
  19   */
  20  function build_mini_calendar($calendar, $month, $year, &$events_cache)
  21  {
  22      global $events_cache, $mybb, $templates, $theme, $monthnames;
  23  
  24      // Incoming month/year?
  25      if(!$year || $year > my_date("Y")+5)
  26      {
  27          $year = my_date("Y");
  28      }
  29  
  30      // Then the month
  31      if($month < 1 || $month > 12)
  32      {
  33          $month = my_date("n");
  34      }
  35  
  36      $weekdays = fetch_weekday_structure($calendar['startofweek']);
  37  
  38      $calendar_permissions = get_calendar_permissions($calendar['cid']);
  39  
  40      $month_link = get_calendar_link($calendar['cid'], $year, $month);
  41  
  42      $next_month = get_next_month($month, $year);
  43      $prev_month = get_prev_month($month, $year);
  44  
  45      $month_start_weekday = gmdate("w", gmmktime(0, 0, 0, $month, $calendar['startofweek']+1, $year));
  46  
  47      $prev_month_days = gmdate("t", gmmktime(0, 0, 0, $prev_month['month'], 1, $prev_month['year']));
  48      if($month_start_weekday != $weekdays[0] || $calendar['startofweek'] != 0)
  49      {
  50          $prev_days = $day = gmdate("t", gmmktime(0, 0, 0, $prev_month['month'], 1, $prev_month['year']));
  51          $day -= array_search(($month_start_weekday), $weekdays);
  52          $day += $calendar['startofweek']+1;
  53          if($day > $prev_month_days+1)
  54          {
  55              // Go one week back
  56              $day -= 7;
  57          }
  58          $calendar_month = $prev_month['month'];
  59          $calendar_year = $prev_month['year'];
  60      }
  61      else
  62      {
  63          $day = $calendar['startofweek']+1;
  64          $calendar_month = $month;
  65          $calendar_year = $year;
  66      }
  67  
  68      // So now we fetch events for this month
  69      $start_timestamp = gmmktime(0, 0, 0, $calendar_month, $day, $year);
  70      $num_days = gmdate("t", gmmktime(0, 0, 0, $month, 1, $year));
  71      $end_timestamp = gmmktime(23, 59, 59, $month, $num_days, $year);
  72  
  73      if(!$events_cache)
  74      {
  75          $events_cache = get_events($calendar, $start_timestamp, $end_timestamp, $calendar_permissions['canmoderateevents']);
  76      }
  77  
  78      $today = my_date("dnY");
  79  
  80      // Build weekday headers
  81      $weekday_headers = '';
  82      foreach($weekdays as $weekday)
  83      {
  84          $weekday_name = fetch_weekday_name($weekday, true);
  85          eval("\$weekday_headers .= \"".$templates->get("calendar_mini_weekdayheader")."\";");
  86      }
  87  
  88      $in_month = 0;
  89      $day_bits = $calendar_rows = '';
  90      for($row = 0; $row < 6; ++$row) // Iterate weeks (each week gets a row)
  91      {
  92          foreach($weekdays as $weekday_id => $weekday)
  93          {
  94              // Current month always starts on 1st row
  95              if($row == 0 && $day == $calendar['startofweek']+1)
  96              {
  97                  $in_month = 1;
  98                  $calendar_month = $month;
  99                  $calendar_year = $year;
 100              }
 101              else if($calendar_month == $prev_month['month'] && $day > $prev_month_days)
 102              {
 103                  $day = 1;
 104                  $in_month = 1;
 105                  $calendar_month = $month;
 106                  $calendar_year = $year;
 107              }
 108              else if($day > $num_days && $calendar_month != $prev_month['month'])
 109              {
 110                  $in_month = 0;
 111                  $calendar_month = $next_month['month'];
 112                  $calendar_year = $next_month['year'];
 113                  $day = 1;
 114                  if($calendar_month == $month)
 115                  {
 116                      $in_month = 1;
 117                  }
 118              }
 119  
 120              if($weekday_id == 0)
 121              {
 122                  $week_stamp = gmmktime(0, 0, 0, $calendar_month, $day, $calendar_year);
 123                  $week_link = get_calendar_week_link($calendar['cid'], $week_stamp);
 124              }
 125  
 126              if($weekday_id == 0 && $calendar_month == $next_month['month'])
 127              {
 128                  break;
 129              }
 130  
 131              $link_to_day = false;
 132              // Any events on this specific day?
 133              if(!empty($events_cache["$day-$calendar_month-$calendar_year"]))
 134              {
 135                  $link_to_day = true;
 136              }
 137  
 138              // Is the current day
 139              if($day.$calendar_month.$year == $today && $month == $calendar_month)
 140              {
 141                  $day_class = "trow_sep";
 142              }
 143              // Not in this month
 144              else if($in_month == 0)
 145              {
 146                  $day_class = "trow1";
 147              }
 148              // Just a normal day in this month
 149              else
 150              {
 151                  $day_class = "trow2";
 152              }
 153              if($link_to_day)
 154              {
 155                  $calendar['link'] = get_calendar_link($calendar['cid'], $calendar_year, $calendar_month, $day);
 156                  eval("\$day_link = \"".$templates->get("calendar_mini_weekrow_day_link")."\";");
 157              }
 158              else
 159              {
 160                  $day_link = $day;
 161              }
 162              eval("\$day_bits .= \"".$templates->get("calendar_mini_weekrow_day")."\";");
 163              ++$day;
 164          }
 165          if($day_bits)
 166          {
 167              eval("\$calendar_rows .= \"".$templates->get("calendar_mini_weekrow")."\";");
 168          }
 169          $day_bits = "";
 170      }
 171      eval("\$mini_calendar = \"".$templates->get("calendar_mini")."\";");
 172      return $mini_calendar;
 173  }
 174  
 175  /**
 176   * Cache available calendars in to memory or return the cached calendars
 177   *
 178   * @return array Cached calendars
 179   */
 180  function cache_calendars()
 181  {
 182      global $db;
 183      static $calendar_cache;
 184  
 185      if(is_array($calendar_cache))
 186      {
 187          return $calendar_cache;
 188      }
 189  
 190      $query = $db->simple_select("calendars", "*", "", array("order_by" => "disporder", "order_dir" => "asc"));
 191      while($calendar = $db->fetch_array($query))
 192      {
 193          $calendar_cache[$calendar['cid']] = $calendar;
 194      }
 195      return $calendar_cache;
 196  }
 197  
 198  /**
 199   * Fetch the calendar permissions for the current user for one or more calendars
 200   *
 201   * @param int $cid Optional calendar ID. If none specified, permissions for all calendars are returned
 202   * @return array Array of permissions
 203   */
 204  function get_calendar_permissions($cid=0)
 205  {
 206      global $db, $mybb;
 207      static $calendar_permissions;
 208  
 209      $calendars = cache_calendars();
 210  
 211      $group_permissions = array(
 212          "canviewcalendar" => $mybb->usergroup['canviewcalendar'],
 213          "canaddevents" => $mybb->usergroup['canaddevents'],
 214          "canbypasseventmod" => $mybb->usergroup['canbypasseventmod'],
 215          "canmoderateevents" => $mybb->usergroup['canmoderateevents']
 216      );
 217  
 218      if(!is_array($calendars))
 219      {
 220          return $group_permissions;
 221      }
 222  
 223      $gid = $mybb->user['usergroup'];
 224  
 225      if(isset($mybb->user['additionalgroups']))
 226      {
 227          $gid .= ",".$mybb->user['additionalgroups'];
 228      }
 229  
 230      if(!is_array($calendar_permissions))
 231      {
 232          $calendar_permissions = array();
 233          $query = $db->simple_select("calendarpermissions", "*");
 234          while($permission = $db->fetch_array($query))
 235          {
 236              $calendar_permissions[$permission['cid']][$permission['gid']] = $permission;
 237          }
 238  
 239          // Add in our usergroup permissions (if custom ones are set, these aren't added)
 240          if(is_array($calendar_permissions))
 241          {
 242              foreach($calendar_permissions as $calendar => $permission)
 243              {
 244                  if(is_array($calendar_permissions[$calendar][$mybb->user['usergroup']]))
 245                  {
 246                      // Already has permissions set
 247                      continue;
 248                  }
 249  
 250                  // Use the group permissions!
 251                  $calendar_permissions[$calendar][$mybb->user['usergroup']] = $group_permissions;
 252                  $calendar_permissions[$calendar][$mybb->user['usergroup']]['cid'] = $calendar;
 253                  $calendar_permissions[$calendar][$mybb->user['usergroup']]['gid'] = $mybb->user['usergroup'];
 254              }
 255          }
 256      }
 257  
 258      if($cid > 0)
 259      {
 260          if(isset($calendar_permissions[$cid]))
 261          {
 262              $permissions = fetch_calendar_permissions($cid, $gid, $calendar_permissions[$cid]);
 263          }
 264          if(empty($permissions))
 265          {
 266              $permissions = $group_permissions;
 267          }
 268      }
 269      else
 270      {
 271          foreach($calendars as $calendar)
 272          {
 273              if(isset($calendar_permissions[$calendar['cid']]))
 274              {
 275                  $permissions[$calendar['cid']] = fetch_calendar_permissions($calendar['cid'], $gid, $calendar_permissions[$calendar['cid']]);
 276              }
 277              if(empty($permissions[$calendar['cid']]))
 278              {
 279                  $permissions[$calendar['cid']] = $group_permissions;
 280              }
 281          }
 282      }
 283      return $permissions;
 284  }
 285  
 286  /**
 287   * Fetch the calendar permissions
 288   *
 289   * @param int $cid Calendar ID
 290   * @param string $gid User group ID, comma seperated
 291   * @param array Array of permissions for this calendar and group
 292   * @return array|void Array of current permissions or nothing if an error occured
 293   */
 294  function fetch_calendar_permissions($cid, $gid, $calendar_permissions)
 295  {
 296      $groups = explode(",", $gid);
 297  
 298      if(!is_array($calendar_permissions))
 299      {
 300          return;
 301      }
 302  
 303      $current_permissions = array();
 304  
 305      foreach($groups as $gid)
 306      {
 307          // If this calendar has permissions set for this group
 308          if($calendar_permissions[$gid])
 309          {
 310              $level_permissions = $calendar_permissions[$gid];
 311              foreach($level_permissions as $permission => $access)
 312              {
 313                  if($access >= $current_permissions[$permission] || ($access == "yes" && $current_permissions[$permission] == "no") || !$current_permissions[$permission])
 314                  {
 315                      $current_permissions[$permission] = $access;
 316                  }
 317              }
 318          }
 319      }
 320  
 321      if(count($current_permissions) == 0)
 322      {
 323          return;
 324      }
 325      return $current_permissions;
 326  }
 327  
 328  /**
 329   * Build a calendar select list to jump between calendars
 330   *
 331   * @param int $selected The selected calendar ID
 332   * @return string The calendar select
 333   */
 334  function build_calendar_jump($selected=0)
 335  {
 336      global $db, $mybb, $templates, $lang, $gobutton;
 337  
 338      $calendar_permissions = get_calendar_permissions();
 339  
 340      $calendars = cache_calendars();
 341  
 342      if(!is_array($calendars))
 343      {
 344          return;
 345      }
 346  
 347      $jump_options = '';
 348  
 349      foreach($calendars as $calendar)
 350      {
 351          if($calendar_permissions[$calendar['cid']]['canviewcalendar'] == 0)
 352          {
 353              continue;
 354          }
 355          $calendar['name'] = htmlspecialchars_uni($calendar['name']);
 356          $sel = "";
 357          if($selected == $calendar['cid'] || ($selected == 0 && $calendar['disporder'] == 1))
 358          {
 359              $sel = "selected=\"selected\"";
 360          }
 361  
 362          eval("\$jump_options .= \"".$templates->get("calendar_jump_option")."\";");
 363      }
 364  
 365      eval("\$calendar_jump = \"".$templates->get("calendar_jump")."\";");
 366      return $calendar_jump;
 367  }
 368  
 369  /**
 370   * Fetch the next calendar month from a specified month/year
 371   *
 372   * @param int $month The month
 373   * @param int $year The year
 374   * @return array Array of the next month and next year
 375   */
 376  function get_next_month($month, $year)
 377  {
 378      global $monthnames;
 379  
 380      if($month == 12)
 381      {
 382          $nextmonth = 1;
 383          $nextyear = $year+1;
 384      }
 385      else
 386      {
 387          $nextmonth = $month+1;
 388          $nextyear = $year;
 389      }
 390  
 391      return array("month" => $nextmonth, "year" => $nextyear, "name" => $monthnames[$nextmonth]);
 392  }
 393  
 394  /**
 395   * Fetch the previous calendar month from a specified month/year
 396   *
 397   * @param int $month The month
 398   * @param int $year The year
 399   * @return array Array of the previous month and previous year
 400   */
 401  function get_prev_month($month, $year)
 402  {
 403      global $monthnames;
 404  
 405      if($month == 1)
 406      {
 407          $prevmonth = 12;
 408          $prevyear = $year-1;
 409      }
 410      else
 411      {
 412          $prevmonth = $month-1;
 413          $prevyear = $year;
 414      }
 415  
 416      return array("month" => $prevmonth, "year" => $prevyear, "name" => $monthnames[$prevmonth]);
 417  }
 418  
 419  /**
 420   * Fetch the events for a specific calendar and date range
 421   *
 422   * @param int $calendar The calendar ID
 423   * @param int $start Start time stamp
 424   * @param int $end End time stmap
 425   * @param int $unapproved 1 to fetch unapproved events too
 426   * @param int $private The user ID to fetch private events for (0 fetches none)
 427   * @return array Array of events
 428   */
 429  function get_events($calendar, $start, $end, $unapproved=0, $private=1)
 430  {
 431      global $db, $mybb;
 432  
 433      // We take in to account timezones here - we either add 14 hours or subtract 12 hours from our GMT time ranges
 434      $start -= 12*3600;
 435      $end += 14*3600;
 436  
 437      $visible_where = '';
 438      if($unapproved != 1)
 439      {
 440          $visible_where = " AND e.visible='1'";
 441      }
 442  
 443      $events_cache = array();
 444      $query = $db->query("
 445          SELECT u.*, e.*
 446          FROM ".TABLE_PREFIX."events e
 447          LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=e.uid)
 448          WHERE e.cid='{$calendar['cid']}' {$visible_where} AND ((e.endtime>={$start} AND e.starttime<={$end}) OR (e.endtime=0 AND e.starttime>={$start} AND e.starttime<={$end})) AND ((e.uid='{$mybb->user['uid']}' AND private='1') OR private!='1')
 449          ORDER BY endtime DESC
 450      ");
 451      while($event = $db->fetch_array($query))
 452      {
 453          if($event['ignoretimezone'] == 0)
 454          {
 455              $offset = (float)$event['timezone'];
 456          }
 457          else
 458          {
 459              $offset = (float)$mybb->user['timezone'];
 460          }
 461          $event['starttime_user'] = $event['starttime']+($offset*3600);
 462  
 463          // Single day event
 464          if($event['endtime'] == 0)
 465          {
 466              $event_date = gmdate("j-n-Y", $event['starttime_user']);
 467              $events_cache[$event_date][] = $event;
 468          }
 469          // Ranged event
 470          else
 471          {
 472              $event_date = explode("-", gmdate("j-n-Y", $event['starttime_user']));
 473              $event['endtime_user'] = $event['endtime']+($offset*3600);
 474              $event['weekday_start'] = $calendar['startofweek'];
 475  
 476              $start_day = gmmktime(0, 0, 0, $event_date[1], $event_date[0], $event_date[2]);
 477  
 478              $event['repeats'] = my_unserialize($event['repeats']);
 479  
 480              // Event does not repeat - just goes over a few days
 481              if($event['repeats']['repeats'] == 0)
 482              {
 483                  if($start_day < $start)
 484                  {
 485                      $range_start = gmmktime(0, 0, 0, gmdate("n", $start), gmdate("j", $start), gmdate("Y", $start));
 486                  }
 487                  else
 488                  {
 489                      $range_start = $start_day;
 490                  }
 491              }
 492              else
 493              {
 494                  $range_start = fetch_next_occurance($event, array("start" => $start, "end" => $end), $start_day, true);
 495              }
 496              $first = "";
 497              $event_date = explode("-", gmdate("j-n-Y", $range_start));
 498  
 499              // Get rid of hour/minutes because sometimes they cause the events to stretch into the next day
 500              $range_end = gmmktime(23, 59, 59, gmdate("n", $event['endtime_user']), gmdate("j", $event['endtime_user']), gmdate("Y", $event['endtime_user']));
 501              while($range_start < $range_end)
 502              {
 503                  // Outside the dates we care about, break! (No unnecessary looping here!)
 504                  if($range_start > $end || !$range_start)
 505                  {
 506                      break;
 507                  }
 508                  if($range_start >= $start)
 509                  {
 510                      $day_date = gmdate("j-n-Y", $range_start);
 511                      if($first && $day_date != "{$first}")
 512                      {
 513                          $events_cache[$day_date][] = &$events_cache["{$first}"][$count];
 514                      }
 515                      else if(!$first)
 516                      {
 517                          if(!isset($events_cache[$day_date]) || !is_array($events_cache[$day_date]))
 518                          {
 519                              $events_cache[$day_date] = array();
 520                          }
 521                          $count = count($events_cache[$day_date]);
 522                          $first = $day_date;
 523                          $events_cache[$day_date][] = $event;
 524                      }
 525                  }
 526                  if($event['repeats']['repeats'] == 0)
 527                  {
 528                      $range_start += 86400;
 529                  }
 530                  else
 531                  {
 532                      $range_start = fetch_next_occurance($event, array("start" => $start, "end" => $end), $range_start);
 533                  }
 534              }
 535          }
 536      }
 537      return $events_cache;
 538  }
 539  
 540  /**
 541   * Fetch the birthdays for one or more months or a specific day
 542   *
 543   * @param int|array $months Integer of the month or array of months
 544   * @param int $day Day of the specific month (if only one month specified above)
 545   * @return array Array of birthdays
 546   */
 547  function get_birthdays($months, $day=0)
 548  {
 549      global $db;
 550  
 551      $year = my_date("Y");
 552      $feb_fix = 0;
 553  
 554      if(!is_array($months))
 555      {
 556          $months = array($months);
 557      }
 558  
 559      foreach($months as $month)
 560      {
 561          if($day)
 562          {
 563              $day_where = "{$day}-{$month}";
 564          }
 565          else
 566          {
 567              $day_where = "%-{$month}";
 568          }
 569          if($month == 3 && ($day == 1 || !$day) && my_date("L", gmmktime(0, 0, 0, $month, 1, $year)) != 1)
 570          {
 571              $where[] = "birthday LIKE '29-2%' OR birthday='29-2'";
 572              $feb_fix = 1;
 573          }
 574          $where[] = "birthday LIKE '{$day_where}-%' OR birthday LIKE '{$day_where}'";
 575      }
 576  
 577      $where = implode(" OR ", $where);
 578  
 579      $bdays = array();
 580  
 581      $query = $db->simple_select("users", "uid, username, birthday, birthdayprivacy, usergroup, displaygroup", $where);
 582      while($user = $db->fetch_array($query))
 583      {
 584          $bday = explode("-", $user['birthday']);
 585          if($bday[2] && $bday[2] < $year)
 586          {
 587              $user['age'] = $year - $bday[2];
 588          }
 589          if($feb_fix == 1 && $bday[0] == 29 && $bday[1] == 2)
 590          {
 591              $bdays["1-3"][] = $user;
 592          }
 593          else
 594          {
 595              $bdays["$bday[0]-$bday[1]"][] = $user;
 596          }
 597      }
 598      if($day)
 599      {
 600          if(!isset($bdays["$day-$month"]))
 601          {
 602              return array();
 603          }
 604          return $bdays["$day-$month"];
 605      }
 606      return $bdays;
 607  }
 608  
 609  /**
 610   * Fetch an ordered list of weekdays depended on a specified starting day
 611   *
 612   * @param int $week_start The weekday we want to start the week with
 613   * @return array Ordered list of weekdays dependant on start of week
 614   */
 615  function fetch_weekday_structure($week_start)
 616  {
 617      switch($week_start)
 618      {
 619          case "1":
 620              $weekdays = array(1,2,3,4,5,6,0);
 621              break;
 622          case "2":
 623              $weekdays = array(2,3,4,5,6,0,1);
 624              break;
 625          case "3":
 626              $weekdays = array(3,4,5,6,0,1,2);
 627              break;
 628          case "4":
 629              $weekdays = array(4,5,6,0,1,2,3);
 630              break;
 631          case "5":
 632              $weekdays = array(5,6,0,1,2,3,4);
 633              break;
 634          case "6":
 635              $weekdays = array(6,0,1,2,3,4,5);
 636              break;
 637          default:
 638              $weekdays = array(0,1,2,3,4,5,6);
 639              break;
 640      }
 641      return $weekdays;
 642  }
 643  
 644  /**
 645   * Fetch a weekday name based on a number
 646   *
 647   * @param int $weekday The weekday number
 648   * @param boolean $short True to fetch the short name ('S'), false to fetch full name
 649   * @return string The weekday name
 650   */
 651  function fetch_weekday_name($weekday, $short=false)
 652  {
 653      global $lang;
 654      switch($weekday)
 655      {
 656          case 1:
 657              $weekday_name = $lang->monday;
 658              $short_weekday_name = $lang->short_monday;
 659              break;
 660          case 2:
 661              $weekday_name = $lang->tuesday;
 662              $short_weekday_name = $lang->short_tuesday;
 663              break;
 664          case 3:
 665              $weekday_name = $lang->wednesday;
 666              $short_weekday_name = $lang->short_wednesday;
 667              break;
 668          case 4:
 669              $weekday_name = $lang->thursday;
 670              $short_weekday_name = $lang->short_thursday;
 671              break;
 672          case 5:
 673              $weekday_name = $lang->friday;
 674              $short_weekday_name = $lang->short_friday;
 675              break;
 676          case 6:
 677              $weekday_name = $lang->saturday;
 678              $short_weekday_name = $lang->short_saturday;
 679              break;
 680          case 0:
 681              $weekday_name = $lang->sunday;
 682              $short_weekday_name = $lang->short_sunday;
 683              break;
 684      }
 685  
 686      if($short == true)
 687      {
 688          return $short_weekday_name;
 689      }
 690      else
 691      {
 692          return $weekday_name;
 693      }
 694  }
 695  
 696  /**
 697   * Fetches the next occurance for a repeating event.
 698   *
 699   * @param array $event The event array
 700   * @param array $range The range of start/end timestamps
 701   * @param int $last_occurance The last occurance of this event
 702   * @param boolean $first True if this is our first iteration of this function (Does some special optimised calculations on false)
 703   * @return int The next occurance timestamp
 704   */
 705  function fetch_next_occurance($event, $range, $last_occurance, $first=false)
 706  {
 707      $new_time = $last_occurance;
 708  
 709      $repeats = $event['repeats'];
 710  
 711      $start_day = explode("-", gmdate("j-n-Y", $event['starttime_user']));
 712      $start_date = gmmktime(0, 0, 0, $start_day[1], $start_day[0], $start_day[2]);
 713  
 714      if($repeats['repeats'] == 0)
 715      {
 716          $new_time += 86400;
 717      }
 718      // Repeats daily
 719      else if($repeats['repeats'] == 1)
 720      {
 721          // If this isn't the first time we've called this function then we can just tack on the time since $last_occurance
 722          if($first == false)
 723          {
 724              $new_time += 86400*$repeats['days'];
 725          }
 726          else
 727          {
 728              // Need to count it out
 729              if($range['start'] > $event['starttime'])
 730              {
 731                  $days_since = ceil(($range['start']-$start_date)/86400);
 732                  $occurances = floor($days_since/$repeats['days']);
 733                  $next_date = $occurances*$repeats['days'];
 734                  $new_time = $event['starttime']+(86400*$next_date);
 735              }
 736              else
 737              {
 738                  $new_time = $start_date;
 739              }
 740          }
 741      }
 742      // Repeats on weekdays only
 743      else if($repeats['repeats'] == 2)
 744      {
 745          if($first == false)
 746          {
 747              $last_dow = gmdate("w", $last_occurance);
 748              // Last day of week = friday, +3 gives monday
 749              if($last_dow == 5)
 750              {
 751                  $new_time += 86400*3;
 752              }
 753              // Still in week, add a day
 754              else
 755              {
 756                  $new_time += 86400;
 757              }
 758          }
 759          // First loop with start date
 760          else
 761          {
 762              if($range['start'] < $event['starttime'])
 763              {
 764                  $start = $event['starttime'];
 765              }
 766              else
 767              {
 768                  $start = $range['start'];
 769              }
 770              $first_dow = gmdate("w", $start);
 771              if($first_dow == 6)
 772              {
 773                  $new_time = $start + (86400*2);
 774              }
 775              else if($first_dow == 0)
 776              {
 777                  $new_time = $start + 86400;
 778              }
 779              else
 780              {
 781                  $new_time = $start;
 782              }
 783          }
 784      }
 785      // Repeats weekly
 786      else if($repeats['repeats'] == 3)
 787      {
 788          $weekdays = fetch_weekday_structure($event['weekday_start']);
 789          $last_dow = gmdate("w", $last_occurance);
 790          if($first == true)
 791          {
 792              $last_dow = -1;
 793              $start_day = gmdate('w', $last_occurance);
 794              if(in_array($start_day, $weekdays))
 795              {
 796                  $next_dow = 0;
 797              }
 798          }
 799          else
 800          {
 801              foreach($repeats['days'] as $weekday)
 802              {
 803                  if($weekday > $last_dow)
 804                  {
 805                      $next_dow = $weekday;
 806                      break;
 807                  }
 808              }
 809          }
 810          if(!isset($next_dow))
 811          {
 812              // Fetch first weekday
 813              $first = $repeats['days'][0]*86400;
 814              $new_time += $first;
 815              // Increase x weeks
 816              $new_time += (7-$last_dow)*86400;
 817              $new_time += (($repeats['weeks']-1)*604800);
 818          }
 819          else
 820          {
 821              // Next day of week exists
 822              if($last_dow > 0)
 823              {
 824                  $day_diff = $next_dow-$last_dow;
 825              }
 826              else
 827              {
 828                  $day_diff = $next_dow;
 829              }
 830              $new_time += $day_diff*86400;
 831          }
 832      }
 833      // Repeats monthly
 834      else if($repeats['repeats'] == 4)
 835      {
 836          $last_month = gmdate("n", $last_occurance);
 837          $last_year = gmdate("Y", $last_occurance);
 838          $last_day = gmdate("j", $last_occurance);
 839          $last_num_days = gmdate("t", $last_occurance);
 840  
 841          // X of every Y months
 842          if($repeats['day'])
 843          {
 844              if($first == true)
 845              {
 846                  if($last_day <= $repeats['day'])
 847                  {
 848                      $new_time = gmmktime(0, 0, 0, $last_month, $repeats['day'], $last_year);
 849                  }
 850                  else
 851                  {
 852                      $new_time = gmmktime(0, 0, 0, $last_month+1, $repeats['day'], $last_year);
 853                      if($new_time > $event['endtime'])
 854                      {
 855                          return false;
 856                      }
 857                  }
 858              }
 859              else
 860              {
 861                  $new_time = gmmktime(0, 0, 0, $last_month+$repeats['months'], $repeats['day'], $last_year);
 862              }
 863          }
 864          // The 1st/etc (weekday) of every X months
 865          else
 866          {
 867              if($first == true)
 868              {
 869                  $new_time = fetch_weekday_monthly_repetition($repeats, $last_month, $last_year);
 870                  if($new_time < $last_occurance)
 871                  {
 872                      $new_time = fetch_weekday_monthly_repetition($repeats, $last_month+1, $last_year);
 873                  }
 874              }
 875              else
 876              {
 877                  $new_time = fetch_weekday_monthly_repetition($repeats, $last_month+$repeats['months'], $last_year);
 878              }
 879          }
 880      }
 881      // Repeats yearly
 882      else if($repeats['repeats'] == 5)
 883      {
 884          $last_year = gmdate("Y", $last_occurance);
 885  
 886          // Repeats on (day) of (month) every (years)
 887          if($repeats['day'])
 888          {
 889              if($first == true)
 890              {
 891                  $new_time = gmmktime(0, 0, 0, $repeats['month'], $repeats['day'], $last_year);
 892                  if($new_time < $last_occurance)
 893                  {
 894                      $new_time = gmmktime(0, 0, 0, $repeats['month'], $repeats['day'], $last_year+1);
 895                  }
 896              }
 897              else
 898              {
 899                  $new_time = gmmktime(0, 0, 0, $repeats['month'], $repeats['day'], $last_year+$repeats['years']);
 900              }
 901          }
 902          // The 1st/etc (weekday) of (month) every (years)
 903          else
 904          {
 905              if($first == true)
 906              {
 907                  $new_time = fetch_weekday_monthly_repetition($repeats, $repeats['month'], $last_year);
 908                  if($new_time < $last_occurance)
 909                  {
 910                      $new_time = fetch_weekday_monthly_repetition($repeats, $repeats['month'], $last_year+1);
 911                  }
 912              }
 913              else
 914              {
 915                  $new_time = fetch_weekday_monthly_repetition($repeats, $repeats['month'], $last_year+$repeats['years']);
 916              }
 917          }
 918      }
 919      return $new_time;
 920  }
 921  
 922  /**
 923   * Fetch a friendly repetition value for a specific event (Repeats every x months etc)
 924   *
 925   * @param array $event The array of the event
 926   * @return string The friendly repetition string
 927   */
 928  function fetch_friendly_repetition($event)
 929  {
 930      global $lang;
 931  
 932      $monthnames = array(
 933          "offset",
 934          $lang->month_1,
 935          $lang->month_2,
 936          $lang->month_3,
 937          $lang->month_4,
 938          $lang->month_5,
 939          $lang->month_6,
 940          $lang->month_7,
 941          $lang->month_8,
 942          $lang->month_9,
 943          $lang->month_10,
 944          $lang->month_11,
 945          $lang->month_12
 946      );
 947  
 948      if(!is_array($event['repeats']))
 949      {
 950          $event['repeats'] = my_unserialize($event['repeats']);
 951          if(!is_array($event['repeats']))
 952          {
 953              return false;
 954          }
 955      }
 956  
 957      $repeats = $event['repeats'];
 958  
 959      switch($repeats)
 960      {
 961          case 1:
 962              if($repeats['days'] <= 1)
 963              {
 964                  return $lang->repeats_every_day;
 965              }
 966              return $lang->sprintf($lang->repeats_every_x_days, $event['repeats']['days']);
 967              break;
 968          case 2:
 969              return $lang->repeats_on_weekdays;
 970              break;
 971          case 3:
 972              if($event['repeats']['days'] || count($event['repeats']['days']) == 7)
 973              {
 974                  $weekdays  = null;
 975                  foreach($event['repeats']['days'] as $id => $weekday)
 976                  {
 977                      $weekday_name = fetch_weekday_name($weekday);
 978                      if($event['repeats']['days'][$id+1] && $weekday)
 979                      {
 980                          $weekdays .= $lang->comma;
 981                      }
 982                      else if(!$event['repeats']['days'][$id+1] && $weekday)
 983                      {
 984                          $weekdays .= " {$lang->and} ";
 985                      }
 986                      $weekdays .= $weekday_name;
 987                  }
 988              }
 989              if($event['repeats']['weeks'] == 1)
 990              {
 991                  if($weekdays)
 992                  {
 993                      return $lang->sprintf($lang->every_week_on_days, $weekdays);
 994                  }
 995                  else
 996                  {
 997                      return $lang->sprintf($lang->every_week);
 998                  }
 999              }
1000              else
1001              {
1002                  if($weekdays)
1003                  {
1004                      return $lang->sprintf($lang->every_x_weeks_on_days, $event['repeats']['weeks'], $weekdays);
1005                  }
1006                  else
1007                  {
1008                      return $lang->sprintf($lang->every_x_weeks, $event['repeats']['weeks']);
1009                  }
1010              }
1011              break;
1012          case 4:
1013              if($event['repeats']['day'])
1014              {
1015                  if($event['repeats']['months'] == 1)
1016                  {
1017                      return $lang->sprintf($lang->every_month_on_day, $event['repeats']['day']);
1018                  }
1019                  else
1020                  {
1021                      return $lang->sprintf($lang->every_x_months_on_day, $event['repeats']['day'], $event['repeats']['months']);
1022                  }
1023              }
1024              else
1025              {
1026                  $weekday_name = fetch_weekday_name($event['repeats']['weekday']);
1027                  $occurance = "weekday_occurance_".$event['repeats']['occurance'];
1028                  $occurance = $lang->$occurance;
1029                  if($event['repeats']['months'] == 1)
1030                  {
1031                      return $lang->sprintf($lang->every_month_on_weekday, $occurance, $weekday_name);
1032                  }
1033                  else
1034                  {
1035                      return $lang->sprintf($lang->every_x_months_on_weekday, $occurance, $weekday_name, $event['repeats']['months']);
1036                  }
1037              }
1038              break;
1039          case 5:
1040              $month = $monthnames[$event['repeats']['month']];
1041              if($event['repeats']['day'])
1042              {
1043                  if($event['repeats']['years'] == 1)
1044                  {
1045                      return $lang->sprintf($lang->every_year_on_day, $event['repeats']['day'], $month);
1046                  }
1047                  else
1048                  {
1049                      return $lang->sprintf($lang->every_x_years_on_day, $event['repeats']['day'], $month, $event['repeats']['years']);
1050                  }
1051              }
1052              else
1053              {
1054                  $weekday_name = fetch_weekday_name($event['repeats']['weekday']);
1055                  $occurance = "weekday_occurance_".$event['repeats']['occurance'];
1056                  $occurance = $lang->$occurance;
1057                  if($event['repeats']['years'] == 1)
1058                  {
1059                      return $lang->sprintf($lang->every_year_on_weekday, $occurance, $weekday_name, $month);
1060                  }
1061                  else
1062                  {
1063                      return $lang->sprintf($lang->every_x_year_on_weekday, $occurance, $weekday_name, $month, $event['repeats']['years']);
1064                  }
1065              }
1066              break;
1067      }
1068  }
1069  
1070  /**
1071   * Fetch a timestamp for "the first/second etc weekday" for a month.
1072   *
1073   * @param array $repeats The repetition array from the event
1074   * @param int $month The month of the year
1075   * @param int $year The year
1076   * @return int The UNIX timestamp
1077   */
1078  function fetch_weekday_monthly_repetition($repeats, $month, $year)
1079  {
1080      $first_last = gmmktime(0, 0, 0, $month, 1, $year);
1081      $first_dow = gmdate("w", $first_last);
1082      $day = 1+($repeats['weekday']-$first_dow);
1083      if($day < 1)
1084      {
1085          $day += 7;
1086      }
1087      if($repeats['occurance'] != "last")
1088      {
1089          $day += ($repeats['occurance']-1)*7;
1090      }
1091      else
1092      {
1093          $last_dow = gmdate("w", gmmktime(0, 0, 0, $month, gmdate("t", $first_last), $year));
1094          $day = (gmdate("t", $first_last)-$last_dow)+$repeats['weekday'];
1095          if($day > gmdate("t", $first_dow))
1096          {
1097              $day -= 7;
1098          }
1099      }
1100      return gmmktime(0, 0, 0, $month, $day, $year);
1101  }


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref