PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/events-manager/classes/em-calendar.php

https://gitlab.com/Blueprint-Marketing/interoccupy.net
PHP | 381 lines | 300 code | 35 blank | 46 comment | 67 complexity | 2e64962eb5eab07e74e520622d6cb4c1 MD5 | raw file
  1. <?php
  2. class EM_Calendar extends EM_Object {
  3. function init(){
  4. //nothing to init anymore
  5. }
  6. function get( $args ){
  7. global $wpdb;
  8. $calendar_array = array();
  9. $calendar_array['cells'] = array();
  10. $original_args = $args;
  11. $args = self::get_default_search($args);
  12. $full = $args['full']; //For ZDE, don't delete pls
  13. $month = $args['month'];
  14. $year = $args['year'];
  15. $long_events = $args['long_events'];
  16. $week_starts_on_sunday = get_option('dbem_week_starts_sunday');
  17. $start_of_week = get_option('start_of_week');
  18. if( !(is_numeric($month) && $month <= 12 && $month > 0) ) {
  19. $month = date('m');
  20. }
  21. if( !( is_numeric($year) ) ){
  22. $year = date('Y');
  23. }
  24. // Get the first day of the month
  25. $month_start = mktime(0,0,0,$month, 1, $year);
  26. $calendar_array['month_start'] = $month_start;
  27. // Get friendly month name
  28. $month_name = date('M',$month_start);
  29. // Figure out which day of the week
  30. // the month starts on.
  31. $month_start_day = date('D', $month_start);
  32. switch($month_start_day){
  33. case "Sun": $offset = 0; break;
  34. case "Mon": $offset = 1; break;
  35. case "Tue": $offset = 2; break;
  36. case "Wed": $offset = 3; break;
  37. case "Thu": $offset = 4; break;
  38. case "Fri": $offset = 5; break;
  39. case "Sat": $offset = 6; break;
  40. }
  41. //We need to go back to the WP defined day when the week started, in case the event day is near the end
  42. $offset -= $start_of_week;
  43. if($offset<0)
  44. $offset += 7;
  45. // determine how many days are in the last month.
  46. $month_last = $month-1;
  47. $month_next = $month+1;
  48. $calendar_array['month_next'] = $month_next;
  49. $year_last = $year;
  50. $year_next = $year;
  51. $calendar_array['year_next'] = $year_next;
  52. if($month == 1) {
  53. $month_last = 12;
  54. $year_last = $year -1;
  55. }elseif($month == 12){
  56. $month_next = 1;
  57. $year_next = $year + 1;
  58. }
  59. $calendar_array['month_last'] = $month_last;
  60. $calendar_array['year_last'] = $year_last;
  61. $num_days_last = self::days_in_month($month_last, $year_last);
  62. // determine how many days are in the current month.
  63. $num_days_current = self::days_in_month($month, $year);
  64. // Build an array for the current days
  65. // in the month
  66. for($i = 1; $i <= $num_days_current; $i++){
  67. $num_days_array[] = mktime(0,0,0,$month, $i, $year);
  68. }
  69. // Build an array for the number of days
  70. // in last month
  71. for($i = 1; $i <= $num_days_last; $i++){
  72. $num_days_last_array[] = mktime(0,0,0,$month_last, $i, $year_last);
  73. }
  74. // If the $offset from the starting day of the
  75. // week happens to be Sunday, $offset would be 0,
  76. // so don't need an offset correction.
  77. if($offset > 0){
  78. $offset_correction = array_slice($num_days_last_array, -$offset, $offset);
  79. $new_count = array_merge($offset_correction, $num_days_array);
  80. $offset_count = count($offset_correction);
  81. } else { // The else statement is to prevent building the $offset array.
  82. $offset_count = 0;
  83. $new_count = $num_days_array;
  84. }
  85. // count how many days we have with the two
  86. // previous arrays merged together
  87. $current_num = count($new_count);
  88. // Since we will have 5 HTML table rows (TR)
  89. // with 7 table data entries (TD)
  90. // we need to fill in 35 TDs
  91. // so, we will have to figure out
  92. // how many days to appened to the end
  93. // of the final array to make it 35 days.
  94. if($current_num > 35){
  95. $num_weeks = 6;
  96. $outset = (42 - $current_num);
  97. } elseif($current_num < 35){
  98. $num_weeks = 5;
  99. $outset = (35 - $current_num);
  100. }
  101. if($current_num == 35){
  102. $num_weeks = 5;
  103. $outset = 0;
  104. }
  105. // Outset Correction
  106. for($i = 1; $i <= $outset; $i++){
  107. $new_count[] = mktime(0,0,0,$month_next, $i, $year_next);
  108. }
  109. // Now let's "chunk" the $all_days array
  110. // into weeks. Each week has 7 days
  111. // so we will array_chunk it into 7 days.
  112. $weeks = array_chunk($new_count, 7);
  113. //Get an array of arguments that don't include default valued args
  114. $link_args = self::get_link_args($args);
  115. $previous_url = "?ajaxCalendar=1&amp;mo={$month_last}&amp;yr={$year_last}&amp;{$link_args}";
  116. $next_url = "?ajaxCalendar=1&amp;mo={$month_next}&amp;yr={$year_next}&amp;{$link_args}";
  117. $weekdays = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  118. if(!empty($args['full'])) {
  119. if( get_option('dbem_full_calendar_abbreviated_weekdays') ) $weekdays = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  120. $day_initials_length = get_option('dbem_full_calendar_initials_length');
  121. } else {
  122. if ( get_option('dbem_small_calendar_abbreviated_weekdays') ) $weekdays = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  123. $day_initials_length = get_option('dbem_small_calendar_initials_length');
  124. }
  125. for( $n = 0; $n < $start_of_week; $n++ ) {
  126. $last_day = array_shift($weekdays);
  127. $weekdays[]= $last_day;
  128. }
  129. $days_initials_array = array();
  130. foreach($weekdays as $weekday) {
  131. $days_initials_array[] = self::translate_and_trim($weekday, $day_initials_length);
  132. }
  133. $calendar_array['links'] = array( 'previous_url'=>$previous_url, 'next_url'=>$next_url);
  134. $calendar_array['row_headers'] = $days_initials_array;
  135. // Now we break each key of the array
  136. // into a week and create a new table row for each
  137. // week with the days of that week in the table data
  138. $i = 0;
  139. $current_date = date('Y-m-d', current_time('timestamp'));
  140. $week_count = 0;
  141. foreach ( $weeks as $week ) {
  142. foreach ( $week as $d ) {
  143. $date = date('Y-m-d', $d);
  144. $calendar_array['cells'][$date] = array('date'=>$d); //set it up so we have the exact array of dates to be filled
  145. if ($i < $offset_count) { //if it is PREVIOUS month
  146. $calendar_array['cells'][$date]['type'] = 'pre';
  147. }
  148. if (($i >= $offset_count) && ($i < ($num_weeks * 7) - $outset)) { // if it is THIS month
  149. if ( $current_date == $date ){
  150. $calendar_array['cells'][$date]['type'] = 'today';
  151. }
  152. } elseif (($outset > 0)) { //if it is NEXT month
  153. if (($i >= ($num_weeks * 7) - $outset)) {
  154. $calendar_array['cells'][$date]['type'] = 'post';
  155. }
  156. }
  157. $i ++;
  158. }
  159. $week_count++;
  160. }
  161. // query the database for events in this time span
  162. if ($month == 1) {
  163. $month_pre=12;
  164. $month_post=2;
  165. $year_pre=$year-1;
  166. $year_post=$year;
  167. } elseif($month == 12) {
  168. $month_pre=11;
  169. $month_post=1;
  170. $year_pre=$year;
  171. $year_post=$year+1;
  172. } else {
  173. $month_pre=$month-1;
  174. $month_post=$month+1;
  175. $year_pre=$year;
  176. $year_post=$year;
  177. }
  178. $args['year'] = array($year_pre, $year_post);
  179. $args['month'] = array($month_pre, $month_post);
  180. $events = EM_Events::get($args);
  181. $event_format = get_option('dbem_full_calendar_event_format');
  182. $event_title_format = get_option('dbem_small_calendar_event_title_format');
  183. $event_title_separator_format = get_option('dbem_small_calendar_event_title_separator');
  184. $eventful_days= array();
  185. if($events){
  186. //Go through the events and slot them into the right d-m index
  187. foreach($events as $event) {
  188. $event = apply_filters('em_calendar_output_loop_start', $event);
  189. if( $long_events ){
  190. //If $long_events is set then show a date as eventful if there is an multi-day event which runs during that day
  191. $event_start_date = strtotime($event->start_date);
  192. $event_end_date = mktime(0,0,0,$month_post,date('t', $event_start_date),$year_post );
  193. if( $event_end_date == '' ) $event_end_date = $event_start_date;
  194. while( $event_start_date <= $event->end ){
  195. //Ensure date is within event dates, if so add to eventful days array
  196. $event_eventful_date = date('Y-m-d', $event_start_date);
  197. if( array_key_exists($event_eventful_date, $eventful_days) && is_array($eventful_days[$event_eventful_date]) ){
  198. $eventful_days[$event_eventful_date][] = $event;
  199. } else {
  200. $eventful_days[$event_eventful_date] = array($event);
  201. }
  202. $event_start_date += (86400); //add a day
  203. }
  204. }else{
  205. //Only show events on the day that they start
  206. if( isset($eventful_days[$event->event_start_date]) && is_array($eventful_days[$event->event_start_date]) ){
  207. $eventful_days[$event->event_start_date][] = $event;
  208. } else {
  209. $eventful_days[$event->event_start_date] = array($event);
  210. }
  211. }
  212. }
  213. }
  214. //generate a link argument string containing event search only
  215. $day_link_args = self::get_link_args( array_intersect_key($original_args, EM_Events::get_post_search($args, true) ));
  216. foreach($eventful_days as $day_key => $events) {
  217. if( array_key_exists($day_key, $calendar_array['cells']) ){
  218. //Get link title for this date
  219. $events_titles = array();
  220. foreach($events as $event) {
  221. if( !get_option('dbem_display_calendar_events_limit') || count($events_titles) < get_option('dbem_display_calendar_events_limit') ){
  222. $events_titles[] = $event->output($event_title_format);
  223. }else{
  224. $events_titles[] = get_option('dbem_display_calendar_events_limit_msg');
  225. break;
  226. }
  227. }
  228. $calendar_array['cells'][$day_key]['link_title'] = implode( $event_title_separator_format, $events_titles);
  229. //Get the link to this calendar day
  230. global $wp_rewrite;
  231. if( count($events) > 1 || !get_option('dbem_calendar_direct_links') ){
  232. if( get_option("dbem_events_page") > 0 ){
  233. $event_page_link = get_permalink(get_option("dbem_events_page")); //PAGE URI OF EM
  234. }else{
  235. if( $wp_rewrite->using_permalinks() ){
  236. $event_page_link = trailingslashit(home_url()).EM_POST_TYPE_EVENT_SLUG.'/'; //don't use EM_URI here, since ajax calls this before EM_URI is defined.
  237. }else{
  238. $event_page_link = trailingslashit(home_url()).'?post_type='.EM_POST_TYPE_EVENT; //don't use EM_URI here, since ajax calls this before EM_URI is defined.
  239. }
  240. }
  241. if( $wp_rewrite->using_permalinks() && !defined('EM_DISABLE_PERMALINKS') ){
  242. $calendar_array['cells'][$day_key]['link'] = trailingslashit($event_page_link).$day_key."/";
  243. if( !empty($day_link_args) ){
  244. $calendar_array['cells'][$day_key]['link'] .= '?'.$day_link_args;
  245. }
  246. }else{
  247. $joiner = (stristr($event_page_link, "?")) ? "&amp;" : "?";
  248. $calendar_array['cells'][$day_key]['link'] = $event_page_link.$joiner."calendar_day=".$day_key;
  249. if( !empty($day_link_args) ){
  250. $calendar_array['cells'][$day_key]['link'] .= '&amp;'.$day_link_args;
  251. }
  252. }
  253. }else{
  254. foreach($events as $EM_Event){
  255. $calendar_array['cells'][$day_key]['link'] = $EM_Event->get_permalink();
  256. }
  257. }
  258. //Add events to array
  259. $calendar_array['cells'][$day_key]['events'] = $events;
  260. }
  261. }
  262. return apply_filters('em_calendar_get',$calendar_array, $args);
  263. }
  264. function output($args = array(), $wrapper = true) {
  265. //Let month and year REQUEST override for non-JS users
  266. if( !empty($_REQUEST['mo']) || !empty($args['mo']) ){
  267. $args['month'] = ($_REQUEST['mo']) ? $_REQUEST['mo']:$args['mo'];
  268. }
  269. if( !empty($_REQUEST['yr']) || !empty($args['yr']) ){
  270. $args['year'] = (!empty($_REQUEST['yr'])) ? $_REQUEST['yr']:$args['yr'];
  271. }
  272. $calendar_array = self::get($args);
  273. $template = (!empty($args['full'])) ? 'templates/calendar-full.php':'templates/calendar-small.php';
  274. ob_start();
  275. em_locate_template($template, true, array('calendar'=>$calendar_array,'args'=>$args));
  276. if($wrapper){
  277. $calendar = '<div id="em-calendar-'.rand(100,200).'" class="em-calendar-wrapper">'.ob_get_clean().'</div>';
  278. }else{
  279. $calendar = ob_get_clean();
  280. }
  281. return apply_filters('em_calendar_output', $calendar, $args);
  282. }
  283. function days_in_month($month, $year) {
  284. return date('t', mktime(0,0,0,$month,1,$year));
  285. }
  286. function translate_and_trim($string, $length = 1) {
  287. if( $length > 0 ){
  288. if(function_exists('mb_substr')){ //fix for diacritic calendar names
  289. return mb_substr(__($string,'dbem'), 0, $length, 'UTF-8');
  290. }else{
  291. return substr(__($string,'dbem'), 0, $length);
  292. }
  293. }
  294. return __($string,'dbem');
  295. }
  296. /**
  297. * Helper function to create a link querystring from array which contains arguments with only values that aren't defuaults.
  298. */
  299. function get_link_args($args = array(), $html_entities=true){
  300. unset($args['month']); unset($args['year']);
  301. $default_args = self::get_default_search(array());
  302. foreach($default_args as $arg_key => $arg_value){
  303. if( !isset($args[$arg_key]) || $args[$arg_key] == $arg_value ){
  304. unset($args[$arg_key]);
  305. }
  306. }
  307. $qs_array = array();
  308. foreach($args as $key => $value){
  309. if(is_array($value)){
  310. $value = implode(',',$value);
  311. }
  312. $qs_array[] = "$key=".urlencode($value);
  313. }
  314. return ($html_entities) ? implode('&amp;', $qs_array) : implode('&', $qs_array);
  315. }
  316. function get_default_search($array=array()){
  317. //These defaults aren't for db queries, but flags for what to display in calendar output
  318. $defaults = array(
  319. 'full' => 0, //Will display a full calendar with event names
  320. 'long_events' => 0, //Events that last longer than a day
  321. 'scope' => 'future',
  322. 'status' => 1, //approved events only
  323. 'town' => false,
  324. 'state' => false,
  325. 'country' => false,
  326. 'region' => false,
  327. 'blog' => get_current_blog_id(),
  328. 'orderby' => get_option('dbem_display_calendar_orderby'),
  329. 'order' => get_option('dbem_display_calendar_order')
  330. );
  331. if(is_multisite()){
  332. global $bp;
  333. if( !is_main_site() && !array_key_exists('blog',$array) ){
  334. //not the main blog, force single blog search
  335. $array['blog'] = get_current_blog_id();
  336. }elseif( empty($array['blog']) && get_site_option('dbem_ms_global_events') ) {
  337. $array['blog'] = false;
  338. }
  339. }
  340. $atts = parent::get_default_search($defaults, $array);
  341. $atts['full'] = ($atts['full']==true) ? 1:0;
  342. $atts['long_events'] = ($atts['long_events']==true) ? 1:0;
  343. return apply_filters('em_calendar_get_default_search', $atts, $array, $defaults);
  344. }
  345. }
  346. add_action('init', array('EM_Calendar', 'init'));