PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/common/calendar.php

https://bitbucket.org/darktrojan/cms
PHP | 170 lines | 147 code | 23 blank | 0 comment | 20 complexity | cd2f6098171e609d9b3165c6b17ab6c7 MD5 | raw file
  1. <?
  2. define('CAN_EDIT', $prefs['calendar.edit']);
  3. $m = idate('n');
  4. $y = idate('Y');
  5. if (preg_match(
  6. '/\/(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\w*-([0-9]{4})\b/i',
  7. $_SERVER['REQUEST_URI'], $parts
  8. )) {
  9. $months = array(
  10. 'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4, 'may' => 5, 'jun' => 6,
  11. 'jul' => 7, 'aug' => 8, 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12
  12. );
  13. $m = $months[strtolower($parts[1])];
  14. $y = (int)($parts[2]);
  15. unset($parts);
  16. }
  17. $d = mktime(0, 0, 0, $m, 1, $y);
  18. $firstShown = mktime(0, 0, 0, idate('m') - 1, 1, idate('Y'));
  19. $lastShown = mktime(0, 0, 0, idate('m'), 1, idate('Y') + 1);
  20. if ($d < $firstShown) {
  21. header('Location: '.CALENDAR_URL);
  22. exit;
  23. } elseif ($d > $firstShown) {
  24. $prev = mktime(0, 0, 0, $m - 1, 1, $y);
  25. }
  26. if ($d > $lastShown) {
  27. header('Location: '.CALENDAR_URL);
  28. exit;
  29. } elseif ($d < $lastShown) {
  30. $next = mktime(0, 0, 0, $m + 1, 1, $y);
  31. }
  32. unset($firstShown, $lastShown);
  33. $start = mktime(0, 0, 0, $m, 1, $y);
  34. $t_start = idate('t', $start);
  35. $w_start = idate('w', $start);
  36. $i_start = mktime(0, 0, 0, idate('m', $start), 1 - $w_start, idate('Y', $start));
  37. $end = mktime(0, 0, 0, $m + 1, 0, $y);
  38. $w_end = idate('w', $end);
  39. $i_end = mktime(0, 0, 0, idate('m', $start), $t_start + 7 - $w_end, idate('Y', $start));
  40. $pagetitle = date('F Y', $d);
  41. if ($prev)
  42. $extraHeadTags[] = '<link rel="prev" href="'.CALENDAR_URL.'/'.date('F-Y', $prev).'" />';
  43. if ($next)
  44. $extraHeadTags[] = '<link rel="next" href="'.CALENDAR_URL.'/'.date('F-Y', $next).'" />';
  45. $extraHeadTags[] = '<link rel="stylesheet" href="/common/tools/darkbox.css" type="text/css" />';
  46. $extraFootTags[] = '<script src="/common/tools/extend.js" type="text/javascript"></script>';
  47. $extraFootTags[] = '<script src="/common/tools/xhr.js" type="text/javascript"></script>';
  48. $extraFootTags[] = '<script src="/common/tools/contentbox.js" type="text/javascript"></script>';
  49. $extraFootTags[] = '<script src="/common/calendar.js" type="text/javascript"></script>';
  50. if (CAN_EDIT) {
  51. $extraFootTags[] = '<script src="/common/tools/date.js" type="text/javascript"></script>';
  52. $extraFootTags[] = '<script src="/common/tools/dragdrop.js" type="text/javascript"></script>';
  53. $extraFootTags[] = '<script src="/admin/inline/calendar.js" type="text/javascript"></script>';
  54. }
  55. require_once 'header.inc';
  56. if (CAN_EDIT) {
  57. echo '<div class="editbuttonsurround">';
  58. echo '<button type="button" class="editbutton" onclick="newEvent();" style="top: -30px">';
  59. echo '<span class="icon icon_add">Add Event</span>';
  60. echo '</button>';
  61. echo '</div>';
  62. }
  63. echo '<div id="calendarheader">';
  64. if ($prev) {
  65. echo '<a href="'.CALENDAR_URL.'/'.date('F-Y', $prev).'" rel="prev">'.
  66. date('F Y', $prev).'</a>';
  67. }
  68. if ($next) {
  69. echo '<a href="'.CALENDAR_URL.'/'.date('F-Y', $next).'" rel="next">'.
  70. date('F Y', $next).'</a>';
  71. }
  72. echo '<h1>'.date('F Y', $d).'</h1>';
  73. echo '</div>';
  74. $events = array();
  75. foreach (Events::SelectEventsByDate($i_start, $i_end) as $event) {
  76. $eventStart = strtotime($event['start']);
  77. $eventEnd = strtotime($event['end']);
  78. $eventDate = mktime(0, 0, 0, idate('m', $eventStart), idate('d', $eventStart), idate('Y', $eventStart));
  79. do {
  80. if (!isset($events[$eventDate])) {
  81. $events[$eventDate] = array();
  82. }
  83. $events[$eventDate][] = $event;
  84. $eventDate = mktime(0, 0, 0, idate('m', $eventDate), idate('d', $eventDate) + 1, idate('Y', $eventDate));
  85. } while ($eventDate < $eventEnd);
  86. }
  87. unset($event, $eventStart, $eventEnd, $eventDate);
  88. echo '<table cellspacing="0" cellpadding="0" id="calendar" data-first="'.
  89. date('Y-m-d', mktime(0, 0, 0, $m, 1 - $w_start, $y)).'">';
  90. echo '<thead>'.
  91. '<tr>'.
  92. '<th>Sunday</th>'.
  93. '<th>Monday</th>'.
  94. '<th>Tuesday</th>'.
  95. '<th>Wednesday</th>'.
  96. '<th>Thursday</th>'.
  97. '<th>Friday</th>'.
  98. '<th>Saturday</th>'.
  99. '</tr>'.
  100. '</thead>';
  101. echo '<tfoot>'.
  102. '<tr>'.
  103. '<th>Sunday</th>'.
  104. '<th>Monday</th>'.
  105. '<th>Tuesday</th>'.
  106. '<th>Wednesday</th>'.
  107. '<th>Thursday</th>'.
  108. '<th>Friday</th>'.
  109. '<th>Saturday</th>'.
  110. '</tr>'.
  111. '</tfoot>';
  112. echo '<tbody>';
  113. for ($i = 1 - $w_start; $i <= $t_start + 6 - $w_end; $i++) {
  114. $today = mktime(0, 0, 0, $m, $i, $y);
  115. if (idate('w', $today) == 0) {
  116. echo '<tr>';
  117. }
  118. $classname = 'date';
  119. if ($i <= 0 || $i > $t_start) {
  120. $classname .= ' differentmonth';
  121. } elseif (date('Y-m-d', $today) == date('Y-m-d')) {
  122. $classname .= ' today';
  123. }
  124. echo '<td class="'.$classname.'">';
  125. echo '<div class="dateinner">';
  126. echo '<div class="datemarker">'.date('j', $today).'</div>';
  127. if (isset($events[$today])) {
  128. echo '<ul class="events">';
  129. foreach ($events[$today] as $event) {
  130. printf(
  131. '<li><a href="%s" data-id="%u">%s</a></li>',
  132. CALENDAR_URL.'/events/'.substr($event['start'], 0, 10).'/'.$event['uri'],
  133. $event['id'],
  134. htmlSafe($event['title'])
  135. );
  136. }
  137. echo '</ul>';
  138. }
  139. echo '</div>';
  140. echo '</td>';
  141. if (idate('w', $today) == 6) {
  142. echo '</tr>';
  143. }
  144. }
  145. echo '</tbody>';
  146. echo '</table>';
  147. require_once 'footer.inc';