eventsmanager

custom section-dates.php hides events when using conditional placeholder

Nov 10th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.65 KB | None | 0 0
  1. <?php
  2. /*
  3. This template is the main meat of the calendar, most of the heavy lifting is done here to show dates according to settings.
  4.  
  5. 2022-07 - It's recommended to avoid tweaking this file, as more work will inevitably be done in this part of the calendar logic for the coming months. Use CSS/JS or PHP Hooks whenever possible for the time being.
  6. */
  7. /* @var array $args The $args passed onto the calendar template via EM_Calendar */
  8. /* @var array $calendar The $calendar array of data passed on by EM_Calendar */
  9. ?>
  10. <section class="em-cal-body em-cal-days <?php echo esc_attr(implode(' ', $calendar['css']['dates_classes'])); ?>">
  11. <?php
  12. $cal_count = count($calendar['cells']); //to prevent an extra tr
  13. $col_count = $tot_count = 1; //this counts collumns in the $calendar_array['cells'] array
  14. $col_max = count($calendar['row_headers']); //each time this collumn number is reached, we create a new collumn, the number of cells should divide evenly by the number of row_headers
  15. // go through day cells
  16. $events = $multiday_slots = $multiday_slots_freed = array();
  17. foreach($calendar['cells'] as $date => $cell_data ){
  18. $class = ( !empty($cell_data['events']) && count($cell_data['events']) > 0 ) ? 'eventful':'eventless';
  19. if(!empty($cell_data['type'])){
  20. $class .= "-".$cell_data['type'];
  21. }
  22. //In some cases (particularly when long events are set to show here) long events and all day events are not shown in the right order. In these cases,
  23. //if you want to sort events cronologically on each day, including all day events at top and long events within the right times, add define('EM_CALENDAR_SORTTIME', true); to your wp-config.php file
  24. //if( defined('EM_CALENDAR_SORTTIME') && EM_CALENDAR_SORTTIME ) ksort($cell_data['events']); //indexes are timestamps
  25. ?>
  26. <div class="<?php echo esc_attr($class); ?> em-cal-day em-cal-col-<?php echo $col_count; ?>">
  27. <?php if( !empty($cell_data['events']) && count($cell_data['events']) > 0 ): ?>
  28. <div class="em-cal-day-date colored" data-calendar-date="<?php echo $cell_data['date']; ?>">
  29. <a href="<?php echo esc_url($cell_data['link']); ?>" title="<?php echo esc_attr($cell_data['link_title']); ?>"><?php echo esc_html(date('j',$cell_data['date'])); ?></a>
  30. <?php if( $args['limit'] && $cell_data['events_count'] > $args['limit'] && get_option('dbem_display_calendar_events_limit_msg') != '' ): ?>
  31. <div class="limited-icon size-small size-medium">+</div>
  32. <?php endif; ?>
  33. </div>
  34. <?php
  35. // ouptut event data
  36. $single_day_events = $allday_events = $colors = array();
  37. $multiday_slots_content = $multiday_slots;
  38. foreach($multiday_slots_content as $k => $v ){
  39. $url = '';
  40. if( !empty($events[$v]) ) {
  41. $EM_Event = $events[$v];
  42. $url = $EM_Event->get_permalink();
  43. }
  44. $multiday_slots_content[$k] = '<div class="em-cal-event multiday" data-event-url="'. esc_url($url) .'" data-event-id="'.absint($v).'"></div>';
  45. } //placeholder
  46. foreach( $cell_data['events'] as $EM_Event ){ /* @var EM_Event $EM_Event */
  47. $events[$EM_Event->event_id] = $EM_Event;
  48. $classes = array();
  49. $event_allday = $event_multiday = $event_multiday_continuation = false;
  50. // get primary color of event
  51. $EM_Category = $EM_Event->get_categories()->get_first();
  52. if( $EM_Category ) {
  53. $colors[] = $EM_Category->get_color();
  54. }
  55. // check multi-day events first
  56. if( !empty($args['long_events']) && $EM_Event->event_start_date != $EM_Event->event_end_date ){
  57. $event_multiday = true;
  58. $event_text = '';
  59. // multi-day event classes
  60. if( $EM_Event->event_start_date === $date || $col_count === 1 ){
  61. $days_left_in_week = $col_max - ($col_count - 1);
  62. $EM_DateTime = new EM_DateTime($date, $EM_Event->end()->getTimezone());
  63. $days_left_in_event = $EM_DateTime->diff( $EM_Event->end() )->days + 1;
  64. //$days_left_in_event = floor(($EM_Event->end()->getTimestamp() - $cell_data['date']) / DAY_IN_SECONDS);
  65. if( $EM_Event->event_start_date === $date ){
  66. // first day of event
  67. $event_text = $EM_Event->output( get_option('dbem_calendar_large_pill_format') );
  68. $classes[] = 'has-start';
  69. // is end date at and of this week?
  70. if( $days_left_in_event < $days_left_in_week ){
  71. $classes[] = 'has-end days-'. $days_left_in_event;
  72. }else{
  73. $classes[] = 'days-'. $days_left_in_week;
  74. }
  75. }elseif( $col_count === 1 ){
  76. // event continues onto following week, so decide when it ends or if it rolls over to next week
  77. $event_multiday_continuation = true;
  78. $event_text = $EM_Event->output( get_option('dbem_calendar_large_pill_format') );
  79. if( $days_left_in_event < $days_left_in_week ){
  80. // spans a few more days and ends
  81. $classes[] = 'has-end';
  82. $classes[] = 'days-'.$days_left_in_event;
  83. }else{
  84. // spans whole week
  85. $classes[] = 'days-'.$days_left_in_week;
  86. }
  87. }
  88. // generate event multiday content
  89. ob_start();
  90. ?>
  91. <div class="em-cal-event multiday <?php echo esc_attr(implode(' ', $classes)); ?>" style="<?php echo esc_attr($EM_Event->get_colors(true)); ?>" data-event-url="<?php echo esc_url($EM_Event->get_permalink()); ?>" data-event-id="<?php echo esc_attr($EM_Event->event_id); ?>">
  92. <?php if( !empty($event_text) ): ?><div><?php echo $event_text; ?></div><?php endif; ?>
  93. </div>
  94. <?php
  95. $event_content = ob_get_clean();
  96. // take event content and slot it into cell array, whether into reserved spot or to an unreserved spot
  97. if( $col_count == 1 ) {
  98. // fill up the slots which will be empty on first column
  99. $days_key = $days_left_in_event - 1;
  100. while( !empty($multiday_slots_content[$days_key]) ){
  101. $days_key += 0.00001; // adding a large decimal to prevent large scale calendars
  102. }
  103. $multiday_slots_content[$days_key] = $event_content;
  104. $multiday_slots[$days_key] = $EM_Event->event_id;
  105. }else{
  106. // check if reserved
  107. if( in_array( $EM_Event->event_id, $multiday_slots ) ){
  108. // already reserved, so add to content slot
  109. foreach( $multiday_slots as $k => $v ){
  110. if( $EM_Event->event_id === $v ){
  111. $multiday_slots_content[$k] = $event_content;
  112. }
  113. }
  114. }else{
  115. $multiday_slot_found = false;
  116. foreach( $multiday_slots as $k => $v ){
  117. if( empty($v) ){
  118. $multiday_slots_content[$k] = $event_content;
  119. $multiday_slots[$k] = $EM_Event->event_id;
  120. $multiday_slot_found = true;
  121. }
  122. }
  123. if( !$multiday_slot_found ){
  124. $multiday_slots_content[] = $event_content;
  125. $multiday_slots[] = $EM_Event->event_id;
  126. }
  127. }
  128. }
  129. }
  130. if( $EM_Event->event_end_date === $date ){
  131. $multiday_slots_freed[] = $EM_Event->event_id;
  132. }
  133. }elseif ( $EM_Event->event_start_date === $date ){
  134. // regular single-day event
  135. ob_start();
  136. $content = $EM_Event->output( get_option('dbem_calendar_large_pill_format') );
  137. if ( !empty($content) ){
  138. ?>
  139. <div class="em-cal-event" style="<?php echo esc_attr($EM_Event->get_colors(true)); ?>" data-event-url="<?php echo esc_url($EM_Event->get_permalink()); ?>" data-event-id="<?php echo esc_attr($EM_Event->event_id); ?>">
  140. <div><?php echo $EM_Event->output( get_option('dbem_calendar_large_pill_format') ); ?></div>
  141. </div>
  142. <?php
  143. }
  144. $single_day_events[] = ob_get_clean();
  145. }
  146. }
  147. // considerations for start of week
  148. if( $col_count == 1 ){
  149. // reorder slot lists built further up, and re-index them so they're numeric from 0
  150. krsort($multiday_slots);
  151. krsort($multiday_slots_content);
  152. $multiday_slots_content = array_values($multiday_slots_content);
  153. $multiday_slots = array_values($multiday_slots); // this will be re-used for the rest of the week
  154. }
  155. // output result
  156. $cell_events = array_merge($multiday_slots_content, $allday_events, $single_day_events);
  157. echo implode($cell_events);
  158.  
  159. // free up slots after content has been output
  160. foreach( $multiday_slots_freed as $event_id ){
  161. foreach( $multiday_slots as $k => $v ){
  162. if( $v == $event_id ){
  163. $multiday_slots[$k] = '';
  164. }
  165. }
  166. }
  167. $multiday_slots_freed = array();
  168. // remove empty slots at end of multiday
  169. foreach( array_reverse($multiday_slots, true) as $k => $v ){
  170. if( empty($v) ){
  171. unset($multiday_slots[$k]);
  172. }else{
  173. break;
  174. }
  175. }
  176. if( !empty($colors) ){
  177. ?>
  178. <span class="date-day-colors" data-colors="<?php echo esc_attr(json_encode($colors)); ?>"></span>
  179. <?php
  180. }
  181. ?>
  182. <?php if( $args['limit'] && $cell_data['events_count'] > $args['limit'] && get_option('dbem_display_calendar_events_limit_msg') != '' ): ?>
  183. <div class="em-cal-day-limit"><a href="<?php echo esc_url($cell_data['link']); ?>" class="button">
  184. <?php echo str_replace('%COUNT%', $cell_data['events_count'] - $args['limit'], get_option('dbem_display_calendar_events_limit_msg')); ?></a>
  185. </div>
  186. <?php endif; ?>
  187.  
  188. <?php else:?>
  189. <div class="em-cal-day-date">
  190. <span><?php echo esc_html(date('j',$cell_data['date'])); ?></span>
  191. </div>
  192. <?php endif; ?>
  193. </div>
  194. <?php
  195. //create a new row once we reach the end of a table collumn
  196. $col_count= ($col_count == $col_max ) ? 1 : $col_count+1;
  197. if ($col_count == 1 && $tot_count < $cal_count) {
  198. $multiday_slots = array();
  199. }
  200. $tot_count ++;
  201. }
  202. ?>
  203. </section>
Add Comment
Please, Sign In to add comment