[ Index ]

PHP Cross Reference of MyBB 1.8.28

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  
 553      if(!is_array($months))
 554      {
 555          $months = array($months);
 556      }
 557  
 558      foreach($months as $month)
 559      {
 560          if($day)
 561          {
 562              $day_where = "{$day}-{$month}";
 563          }
 564          else
 565          {
 566              $day_where = "%-{$month}";
 567          }
 568          if($month == 3 && ($day == 1 || !$day) && my_date("L", gmmktime(0, 0, 0, $month, 1, $year)) != 1)
 569          {
 570              $where[] = "birthday LIKE '29-2%' OR birthday='29-2'";
 571              $feb_fix = 1;
 572          }
 573          $where[] = "birthday LIKE '{$day_where}-%' OR birthday LIKE '{$day_where}'";
 574      }
 575  
 576      $where = implode(" OR ", $where);
 577  
 578      $bdays = array();
 579  
 580      $query = $db->simple_select("users", "uid, username, birthday, birthdayprivacy, usergroup, displaygroup", $where);
 581      while($user = $db->fetch_array($query))
 582      {
 583          $bday = explode("-", $user['birthday']);
 584          if($bday[2] && $bday[2] < $year)
 585          {
 586              $user['age'] = $year - $bday[2];
 587          }
 588          if($feb_fix == 1 && $bday[0] == 29 && $bday[1] == 2)
 589          {
 590              $bdays["1-3"][] = $user;
 591          }
 592          else
 593          {
 594              $bdays["$bday[0]-$bday[1]"][] = $user;
 595          }
 596      }
 597      if($day)
 598      {
 599          if(!isset($bdays["$day-$month"]))
 600          {
 601              return array();
 602          }
 603          return $bdays["$day-$month"];
 604      }
 605      return $bdays;
 606  }
 607  
 608  /**
 609   * Fetch an ordered list of weekdays depended on a specified starting day
 610   *
 611   * @param int $week_start The weekday we want to start the week with
 612   * @return array Ordered list of weekdays dependant on start of week
 613   */
 614  function fetch_weekday_structure($week_start)
 615  {
 616      switch($week_start)
 617      {
 618          case "1":
 619              $weekdays = array(1,2,3,4,5,6,0);
 620              break;
 621          case "2":
 622              $weekdays = array(2,3,4,5,6,0,1);
 623              break;
 624          case "3":
 625              $weekdays = array(3,4,5,6,0,1,2);
 626              break;
 627          case "4":
 628              $weekdays = array(4,5,6,0,1,2,3);
 629              break;
 630          case "5":
 631              $weekdays = array(5,6,0,1,2,3,4);
 632              break;
 633          case "6":
 634              $weekdays = array(6,0,1,2,3,4,5);
 635              break;
 636          default:
 637              $weekdays = array(0,1,2,3,4,5,6);
 638              break;
 639      }
 640      return $weekdays;
 641  }
 642  
 643  /**
 644   * Fetch a weekday name based on a number
 645   *
 646   * @param int $weekday The weekday number
 647   * @param boolean $short True to fetch the short name ('S'), false to fetch full name
 648   * @return string The weekday name
 649   */
 650  function fetch_weekday_name($weekday, $short=false)
 651  {
 652      global $lang;
 653      switch($weekday)
 654      {
 655          case 1:
 656              $weekday_name = $lang->monday;
 657              $short_weekday_name = $lang->short_monday;
 658              break;
 659          case 2:
 660              $weekday_name = $lang->tuesday;
 661              $short_weekday_name = $lang->short_tuesday;
 662              break;
 663          case 3:
 664              $weekday_name = $lang->wednesday;
 665              $short_weekday_name = $lang->short_wednesday;
 666              break;
 667          case 4:
 668              $weekday_name = $lang->thursday;
 669              $short_weekday_name = $lang->short_thursday;
 670              break;
 671          case 5:
 672              $weekday_name = $lang->friday;
 673              $short_weekday_name = $lang->short_friday;
 674              break;
 675          case 6:
 676              $weekday_name = $lang->saturday;
 677              $short_weekday_name = $lang->short_saturday;
 678              break;
 679          case 0:
 680              $weekday_name = $lang->sunday;
 681              $short_weekday_name = $lang->short_sunday;
 682              break;
 683      }
 684  
 685      if($short == true)
 686      {
 687          return $short_weekday_name;
 688      }
 689      else
 690      {
 691          return $weekday_name;
 692      }
 693  }
 694  
 695  /**
 696   * Fetches the next occurance for a repeating event.
 697   *
 698   * @param array $event The event array
 699   * @param array $range The range of start/end timestamps
 700   * @param int $last_occurance The last occurance of this event
 701   * @param boolean $first True if this is our first iteration of this function (Does some special optimised calculations on false)
 702   * @return int The next occurance timestamp
 703   */
 704  function fetch_next_occurance($event, $range, $last_occurance, $first=false)
 705  {
 706      $new_time = $last_occurance;
 707  
 708      $repeats = $event['repeats'];
 709  
 710      $start_day = explode("-", gmdate("j-n-Y", $event['starttime_user']));
 711      $start_date = gmmktime(0, 0, 0, $start_day[1], $start_day[0], $start_day[2]);
 712  
 713      if($repeats['repeats'] == 0)
 714      {
 715          $new_time += 86400;
 716      }
 717      // Repeats daily
 718      else if($repeats['repeats'] == 1)
 719      {
 720          // If this isn't the first time we've called this function then we can just tack on the time since $last_occurance
 721          if($first == false)
 722          {
 723              $new_time += 86400*$repeats['days'];
 724          }
 725          else
 726          {
 727              // Need to count it out
 728              if($range['start'] > $event['starttime'])
 729              {
 730                  $days_since = ceil(($range['start']-$start_date)/86400);
 731                  $occurances = floor($days_since/$repeats['days']);
 732                  $next_date = $occurances*$repeats['days'];
 733                  $new_time = $event['starttime']+(86400*$next_date);
 734              }
 735              else
 736              {
 737                  $new_time = $start_date;
 738              }
 739          }
 740      }
 741      // Repeats on weekdays only
 742      else if($repeats['repeats'] == 2)
 743      {
 744          if($first == false)
 745          {
 746              $last_dow = gmdate("w", $last_occurance);
 747              // Last day of week = friday, +3 gives monday
 748              if($last_dow == 5)
 749              {
 750                  $new_time += 86400*3;
 751              }
 752              // Still in week, add a day
 753              else
 754              {
 755                  $new_time += 86400;
 756              }
 757          }
 758          // First loop with start date
 759          else
 760          {
 761              if($range['start'] < $event['starttime'])
 762              {
 763                  $start = $event['starttime'];
 764              }
 765              else
 766              {
 767                  $start = $range['start'];
 768              }
 769              $first_dow = gmdate("w", $start);
 770              if($first_dow == 6)
 771              {
 772                  $new_time = $start + (86400*2);
 773              }
 774              else if($first_dow == 0)
 775              {
 776                  $new_time = $start + 86400;
 777              }
 778              else
 779              {
 780                  $new_time = $start;
 781              }
 782          }
 783      }
 784      // Repeats weekly
 785      else if($repeats['repeats'] == 3)
 786      {
 787          $weekdays = fetch_weekday_structure($event['weekday_start']);
 788          $last_dow = gmdate("w", $last_occurance);
 789          if($first == true)
 790          {
 791              $last_dow = -1;
 792              $start_day = gmdate('w', $last_occurance);
 793              if(in_array($start_day, $weekdays))
 794              {
 795                  $next_dow = 0;
 796              }
 797          }
 798          else
 799          {
 800              foreach($repeats['days'] as $weekday)
 801              {
 802                  if($weekday > $last_dow)
 803                  {
 804                      $next_dow = $weekday;
 805                      break;
 806                  }
 807              }
 808          }
 809          if(!isset($next_dow))
 810          {
 811              // Fetch first weekday
 812              $first = $repeats['days'][0]*86400;
 813              $new_time += $first;
 814              // Increase x weeks
 815              $new_time += (7-$last_dow)*86400;
 816              $new_time += (($repeats['weeks']-1)*604800);
 817          }
 818          else
 819          {
 820              // Next day of week exists
 821              if($last_dow > 0)
 822              {
 823                  $day_diff = $next_dow-$last_dow;
 824              }
 825              else
 826              {
 827                  $day_diff = $next_dow;
 828              }
 829              $new_time += $day_diff*86400;
 830          }
 831      }
 832      // Repeats monthly
 833      else if($repeats['repeats'] == 4)
 834      {
 835          $last_month = gmdate("n", $last_occurance);
 836          $last_year = gmdate("Y", $last_occurance);
 837          $last_day = gmdate("j", $last_occurance);
 838          $last_num_days = gmdate("t", $last_occurance);
 839  
 840          // X of every Y months
 841          if($repeats['day'])
 842          {
 843              if($first == true)
 844              {
 845                  if($last_day <= $repeats['day'])
 846                  {
 847                      $new_time = gmmktime(0, 0, 0, $last_month, $repeats['day'], $last_year);
 848                  }
 849                  else
 850                  {
 851                      $new_time = gmmktime(0, 0, 0, $last_month+1, $repeats['day'], $last_year);
 852                      if($new_time > $event['endtime'])
 853                      {
 854                          return false;
 855                      }
 856                  }
 857              }
 858              else
 859              {
 860                  $new_time = gmmktime(0, 0, 0, $last_month+$repeats['months'], $repeats['day'], $last_year);
 861              }
 862          }
 863          // The 1st/etc (weekday) of every X months
 864          else
 865          {
 866              if($first == true)
 867              {
 868                  $new_time = fetch_weekday_monthly_repetition($repeats, $last_month, $last_year);
 869                  if($new_time < $last_occurance)
 870                  {
 871                      $new_time = fetch_weekday_monthly_repetition($repeats, $last_month+1, $last_year);
 872                  }
 873              }
 874              else
 875              {
 876                  $new_time = fetch_weekday_monthly_repetition($repeats, $last_month+$repeats['months'], $last_year);
 877              }
 878          }
 879      }
 880      // Repeats yearly
 881      else if($repeats['repeats'] == 5)
 882      {
 883          $last_year = gmdate("Y", $last_occurance);
 884  
 885          // Repeats on (day) of (month) every (years)
 886          if($repeats['day'])
 887          {
 888              if($first == true)
 889              {
 890                  $new_time = gmmktime(0, 0, 0, $repeats['month'], $repeats['day'], $last_year);
 891                  if($new_time < $last_occurance)
 892                  {
 893                      $new_time = gmmktime(0, 0, 0, $repeats['month'], $repeats['day'], $last_year+1);
 894                  }
 895              }
 896              else
 897              {
 898                  $new_time = gmmktime(0, 0, 0, $repeats['month'], $repeats['day'], $last_year+$repeats['years']);
 899              }
 900          }
 901          // The 1st/etc (weekday) of (month) every (years)
 902          else
 903          {
 904              if($first == true)
 905              {
 906                  $new_time = fetch_weekday_monthly_repetition($repeats, $repeats['month'], $last_year);
 907                  if($new_time < $last_occurance)
 908                  {
 909                      $new_time = fetch_weekday_monthly_repetition($repeats, $repeats['month'], $last_year+1);
 910                  }
 911              }
 912              else
 913              {
 914                  $new_time = fetch_weekday_monthly_repetition($repeats, $repeats['month'], $last_year+$repeats['years']);
 915              }
 916          }
 917      }
 918      return $new_time;
 919  }
 920  
 921  /**
 922   * Fetch a friendly repetition value for a specific event (Repeats every x months etc)
 923   *
 924   * @param array $event The array of the event
 925   * @return string The friendly repetition string
 926   */
 927  function fetch_friendly_repetition($event)
 928  {
 929      global $lang;
 930  
 931      $monthnames = array(
 932          "offset",
 933          $lang->month_1,
 934          $lang->month_2,
 935          $lang->month_3,
 936          $lang->month_4,
 937          $lang->month_5,
 938          $lang->month_6,
 939          $lang->month_7,
 940          $lang->month_8,
 941          $lang->month_9,
 942          $lang->month_10,
 943          $lang->month_11,
 944          $lang->month_12
 945      );
 946  
 947      if(!is_array($event['repeats']))
 948      {
 949          $event['repeats'] = my_unserialize($event['repeats']);
 950          if(!is_array($event['repeats']))
 951          {
 952              return false;
 953          }
 954      }
 955  
 956      $repeats = $event['repeats'];
 957  
 958      switch($repeats)
 959      {
 960          case 1:
 961              if($repeats['days'] <= 1)
 962              {
 963                  return $lang->repeats_every_day;
 964              }
 965              return $lang->sprintf($lang->repeats_every_x_days, $event['repeats']['days']);
 966              break;
 967          case 2:
 968              return $lang->repeats_on_weekdays;
 969              break;
 970          case 3:
 971              if($event['repeats']['days'] || count($event['repeats']['days']) == 7)
 972              {
 973                  $weekdays  = null;
 974                  foreach($event['repeats']['days'] as $id => $weekday)
 975                  {
 976                      $weekday_name = fetch_weekday_name($weekday);
 977                      if($event['repeats']['days'][$id+1] && $weekday)
 978                      {
 979                          $weekdays .= $lang->comma;
 980                      }
 981                      else if(!$event['repeats']['days'][$id+1] && $weekday)
 982                      {
 983                          $weekdays .= " {$lang->and} ";
 984                      }
 985                      $weekdays .= $weekday_name;
 986                  }
 987              }
 988              if($event['repeats']['weeks'] == 1)
 989              {
 990                  if($weekdays)
 991                  {
 992                      return $lang->sprintf($lang->every_week_on_days, $weekdays);
 993                  }
 994                  else
 995                  {
 996                      return $lang->sprintf($lang->every_week);
 997                  }
 998              }
 999              else
1000              {
1001                  if($weekdays)
1002                  {
1003                      return $lang->sprintf($lang->every_x_weeks_on_days, $event['repeats']['weeks'], $weekdays);
1004                  }
1005                  else
1006                  {
1007                      return $lang->sprintf($lang->every_x_weeks, $event['repeats']['weeks']);
1008                  }
1009              }
1010              break;
1011          case 4:
1012              if($event['repeats']['day'])
1013              {
1014                  if($event['repeats']['months'] == 1)
1015                  {
1016                      return $lang->sprintf($lang->every_month_on_day, $event['repeats']['day']);
1017                  }
1018                  else
1019                  {
1020                      return $lang->sprintf($lang->every_x_months_on_day, $event['repeats']['day'], $event['repeats']['months']);
1021                  }
1022              }
1023              else
1024              {
1025                  $weekday_name = fetch_weekday_name($event['repeats']['weekday']);
1026                  $occurance = "weekday_occurance_".$event['repeats']['occurance'];
1027                  $occurance = $lang->$occurance;
1028                  if($event['repeats']['months'] == 1)
1029                  {
1030                      return $lang->sprintf($lang->every_month_on_weekday, $occurance, $weekday_name);
1031                  }
1032                  else
1033                  {
1034                      return $lang->sprintf($lang->every_x_months_on_weekday, $occurance, $weekday_name, $event['repeats']['months']);
1035                  }
1036              }
1037              break;
1038          case 5:
1039              $month = $monthnames[$event['repeats']['month']];
1040              if($event['repeats']['day'])
1041              {
1042                  if($event['repeats']['years'] == 1)
1043                  {
1044                      return $lang->sprintf($lang->every_year_on_day, $event['repeats']['day'], $month);
1045                  }
1046                  else
1047                  {
1048                      return $lang->sprintf($lang->every_x_years_on_day, $event['repeats']['day'], $month, $event['repeats']['years']);
1049                  }
1050              }
1051              else
1052              {
1053                  $weekday_name = fetch_weekday_name($event['repeats']['weekday']);
1054                  $occurance = "weekday_occurance_".$event['repeats']['occurance'];
1055                  $occurance = $lang->$occurance;
1056                  if($event['repeats']['years'] == 1)
1057                  {
1058                      return $lang->sprintf($lang->every_year_on_weekday, $occurance, $weekday_name, $month);
1059                  }
1060                  else
1061                  {
1062                      return $lang->sprintf($lang->every_x_year_on_weekday, $occurance, $weekday_name, $month, $event['repeats']['years']);
1063                  }
1064              }
1065              break;
1066      }
1067  }
1068  
1069  /**
1070   * Fetch a timestamp for "the first/second etc weekday" for a month.
1071   *
1072   * @param array $repeats The repetition array from the event
1073   * @param int $month The month of the year
1074   * @param int $year The year
1075   * @return int The UNIX timestamp
1076   */
1077  function fetch_weekday_monthly_repetition($repeats, $month, $year)
1078  {
1079      $first_last = gmmktime(0, 0, 0, $month, 1, $year);
1080      $first_dow = gmdate("w", $first_last);
1081      $day = 1+($repeats['weekday']-$first_dow);
1082      if($day < 1)
1083      {
1084          $day += 7;
1085      }
1086      if($repeats['occurance'] != "last")
1087      {
1088          $day += ($repeats['occurance']-1)*7;
1089      }
1090      else
1091      {
1092          $last_dow = gmdate("w", gmmktime(0, 0, 0, $month, gmdate("t", $first_last), $year));
1093          $day = (gmdate("t", $first_last)-$last_dow)+$repeats['weekday'];
1094          if($day > gmdate("t", $first_dow))
1095          {
1096              $day -= 7;
1097          }
1098      }
1099      return gmmktime(0, 0, 0, $month, $day, $year);
1100  }


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