/views/event-calendar.php

https://github.com/RyanFlannagan/Wordpress-Meetup-Plugin- · PHP · 120 lines · 92 code · 25 blank · 3 comment · 23 complexity · 632685548dd40f313b15461ac983f811 MD5 · raw file

  1. <?php
  2. $group_ul_contents = "";
  3. foreach ($groups as $group) {
  4. $group_ul_contents .= $this->element(
  5. 'li',
  6. $this->element('a', $group->name, array('href' => $group->link, 'style' => 'border-left: 20px solid ' . $group->color)),
  7. array()
  8. );
  9. }
  10. echo $this->element('ul', $group_ul_contents, array('id' => 'wp-meetup-groups'));
  11. //print_r($events);
  12. $events_by_date = array();
  13. foreach ($events as $event) {
  14. $event_time = $event->time + $event->utc_offset;
  15. $event_date = mktime(0, 0, 0, date('n', $event_time), date('j', $event_time), date('Y', $event_time));
  16. $date_key = date('Y-m-d', $event_date);
  17. if (!array_key_exists($date_key, $events_by_date))
  18. $events_by_date[$date_key] = array();
  19. $events_by_date[$date_key][] = $event;
  20. }
  21. unset($events);
  22. //pr($events_by_date);
  23. if (count($events_by_date) > 0) {
  24. $today = mktime(0, 0, 0, date('n'), date('j'), date('Y'));
  25. $div_contents = "";
  26. for ($m = 0; $m < $number_of_months; $m++) { // 'm' counts the months, 'i' counts the weeks, 'j' counts the days in that week
  27. $current_month = date('n') + $m;
  28. $first_of_the_month = mktime(0, 0, 0, $current_month, 1, date('Y'));
  29. $date_start = mktime(0, 0, 0, $current_month, 1-date('w', $first_of_the_month), date('Y')); // monday of the first week of the month (which may not lie in the same month)
  30. $end_date = mktime(0, 0, 0, $current_month+1, -1, date('Y')); // last day of the current month
  31. $theadrow_contents = '';
  32. foreach (array('Su', 'M', 'T', 'W', 'Th', 'F', 'Sa') as $day) {
  33. $theadrow_contents .= $this->element('th', $day);
  34. }
  35. $thead_contents = $this->element('tr', $theadrow_contents);
  36. $tbody_contents = '';
  37. $current_date = $date_start;
  38. $i = 0;
  39. while ($current_date < $end_date) {
  40. $tr_contents = '';
  41. for ($j = 0; $j < 7; $j++) {
  42. $current_date = strtotime('+' . (($i*7)+$j) . 'days', $date_start);
  43. $td_classes = array();
  44. if (date('w', $current_date) == 0 || date('w', $current_date) == 6) { // sunday ('w'=0) and saturday ('w'=6) are the weekend
  45. $td_classes[] = 'weekend';
  46. }
  47. if ($current_date == $today)
  48. $td_classes[] = 'today';
  49. $td_contents = date('j', $current_date);
  50. $date_key = date('Y-m-d', $current_date);
  51. if (array_key_exists($date_key, $events_by_date)) {
  52. $ul_contents = "";
  53. foreach ($events_by_date[$date_key] as $event) {
  54. $event_status = time() < $event->time + $event->utc_offset ? 'upcoming' : 'past';
  55. $anchor_attributes = array('href' => get_permalink($event->post->ID));
  56. if ($event_status == 'upcoming')
  57. $anchor_attributes['style'] = 'background-color:' . $event->group->color;
  58. if ($event->post->post_status == 'publish') {
  59. $ul_contents .= $this->element('li',
  60. $this->element('a',
  61. $this->element('span', date("g:i A", $event->time + $event->utc_offset)) . $event->name,
  62. $anchor_attributes
  63. ),
  64. array('class' => ($event_status) . " " . $event->group->url_name)
  65. );
  66. } else {
  67. $ul_contents .= $this->element('li',
  68. $this->element('span', date("g:i A", $event->time + $event->utc_offset)) . $event->name,
  69. array('class' => ($event_status) . " " . $event->group->url_name)
  70. );
  71. }
  72. }
  73. $td_contents .= $this->element('ul', $ul_contents);
  74. } else {
  75. $td_contents .= $this->element('ul',$this->element('li',$this->element('span',"&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp ")));
  76. }
  77. if ((date('n', $current_date) % 12) == ($current_month % 12)) { // evaluating mod 12 fixes bug of months in later years all being 'out of range'
  78. $tr_contents .= $this->element('td', $td_contents, array('class' => implode(' ', $td_classes)));
  79. } else {
  80. $tr_contents .= $this->element('td', "", array('class' => 'out-of-range'));
  81. }
  82. }
  83. $tbody_contents .= $this->element('tr', $tr_contents);
  84. $i++;
  85. }
  86. $div_contents .= $this->element('h2', date('F Y', $first_of_the_month));
  87. // if current_month is this month (given by date('n')) then table_class is 'current-month'; otherwise the table_class is 'next-month'
  88. $table_class = $current_month - date('n') == 0 ? "current-month" : "next-month";
  89. $div_contents .= $this->element('table', $this->element('thead', $thead_contents) . $this->element('tbody', $tbody_contents), array('class' => $table_class,'cellpadding' => 0, 'cellspacing' => 0));
  90. }
  91. echo $this->element('div', $div_contents, array('id' => 'wp-meetup-calendar'));
  92. } else {
  93. echo $this->element('p', "No events listed.");
  94. }
  95. ?>