From 8ee3e058152868945f2d521705b17065247d5970 Mon Sep 17 00:00:00 2001 From: Rafael Pivato Date: Sat, 1 Jun 2024 22:32:49 -0300 Subject: [PATCH] Showing a few slots for scheduling --- mahoee-scheduling/mahoee-scheduling.php | 10 ++- mahoee-scheduling/web/scheduling.css | 39 +++++++++ mahoee-scheduling/web/scheduling.js | 6 ++ mahoee-scheduling/web/shortcode.php | 100 ++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 mahoee-scheduling/web/scheduling.css create mode 100644 mahoee-scheduling/web/scheduling.js create mode 100644 mahoee-scheduling/web/shortcode.php diff --git a/mahoee-scheduling/mahoee-scheduling.php b/mahoee-scheduling/mahoee-scheduling.php index 9022010..af974fe 100644 --- a/mahoee-scheduling/mahoee-scheduling.php +++ b/mahoee-scheduling/mahoee-scheduling.php @@ -21,6 +21,7 @@ define('MAHOEE_SCHEDULING_PLUGIN_DIR', plugin_dir_path(__FILE__)); require_once MAHOEE_SCHEDULING_PLUGIN_DIR . 'admin/site-settings.php'; +require_once MAHOEE_SCHEDULING_PLUGIN_DIR . 'web/shortcode.php'; // Site admin menu function mahoee_scheduling_site_menu() @@ -35,4 +36,11 @@ function mahoee_scheduling_site_menu() 31 ); } -add_action('admin_menu', 'mahoee_scheduling_site_menu'); \ No newline at end of file +add_action('admin_menu', 'mahoee_scheduling_site_menu'); + +// Register the shortcode +function mahoee_scheduling_register_shortcode() +{ + add_shortcode('scheduling_block', 'mahoee_scheduling_shortcode_callback'); +} +add_action('init', 'mahoee_scheduling_register_shortcode'); \ No newline at end of file diff --git a/mahoee-scheduling/web/scheduling.css b/mahoee-scheduling/web/scheduling.css new file mode 100644 index 0000000..68e3733 --- /dev/null +++ b/mahoee-scheduling/web/scheduling.css @@ -0,0 +1,39 @@ +.mahoee-scheduling-block, +.mahoee-scheduling-block * { + box-sizing: border-box; + margin: 0; + padding: 0; +} + +.mahoee-scheduling-block { + display: flex; + justify-content: center; + padding: 10px; + gap: 10px; +} + +.mahoee-scheduling-block .option { + display: flex; + border: 2px solid #d2d2d2; + background-color: #e2e2e2; + border-radius: 10px; + flex-direction: column; + align-items: center; + width: 10rem; + padding: 10px; + gap: 5px; +} + +.mahoee-scheduling-block .option .weekday { + font-weight: bold; +} + +.mahoee-scheduling-block .option .date { + font-size: 0.8rem; +} + +.mahoee-scheduling-block .option .shift { + font-weight: bold; + font-size: 0.8rem; + text-transform: uppercase; +} diff --git a/mahoee-scheduling/web/scheduling.js b/mahoee-scheduling/web/scheduling.js new file mode 100644 index 0000000..31eb2b6 --- /dev/null +++ b/mahoee-scheduling/web/scheduling.js @@ -0,0 +1,6 @@ +jQuery(document).ready(function ($) { + $("#main-schedule-button").on("click", function () { + $("#scheduling-options").toggle(); + $("#no-days").toggle(); + }); +}); diff --git a/mahoee-scheduling/web/shortcode.php b/mahoee-scheduling/web/shortcode.php new file mode 100644 index 0000000..86f201f --- /dev/null +++ b/mahoee-scheduling/web/shortcode.php @@ -0,0 +1,100 @@ + 'Domingo', + 1 => 'Segunda-feira', + 2 => 'Terça-feira', + 3 => 'Quarta-feira', + 4 => 'Quinta-feira', + 5 => 'Sexta-feira', + 6 => 'Sábado', + ]; + $shifts_labels = [ + 0 => 'de manhã', + 1 => 'de tarde', + 2 => 'de noite', + ]; + + $min_random_skip = 2; + $max_random_skip = 5; + $pick_count = 3; + $required_slots = ($pick_count + 1) * $max_random_skip; + + // Determine current date and time + date_default_timezone_set($timezone ? $timezone : 'UTC'); + $now = new DateTime(); + $current_weekday = (int) $now->format('w'); + $current_time = (int) $now->format('G'); + + // Initialize slots array + $slots = []; + + // Determine the next slots considering the current time and shifts + $days_checked = 0; + while (count($slots) < $required_slots) { + $weekday = ($current_weekday + $days_checked) % 7; + if (in_array($weekday, $weekdays)) { + foreach ($shifts as $shift) { + if ($days_checked == 0 && $shift <= floor($current_time / 8)) { + // Skip past shifts for today + continue; + } + $date = clone $now; + $date->modify("+$days_checked day"); + $formatted_date = $date->format('Y-m-d'); + $slots[] = [$formatted_date, $weekdays_labels[$weekday], $shifts_labels[$shift]]; + } + } + $days_checked++; + } + + // Pick a few slots + $selected_slots = []; + $shifts_missing = array_unique(array_column($slots, 2)); + $current_index = 0; + $total_slots = count($slots); + while (count($selected_slots) < $pick_count) { + $current_slot = $slots[$current_index]; + $shift = $current_slot[2]; + if (empty($shifts_missing) || in_array($shift, $shifts_missing)) { + $selected_slots[] = $current_slot; + $shifts_missing = array_diff($shifts_missing, [$shift]); + } + $random_skip = rand($min_random_skip, $max_random_skip); + $current_index = ($current_index + $random_skip) % $total_slots; + } + + // Generate the HTML output + $output = '
'; + foreach ($selected_slots as $slot) { + list($date, $weekday_label, $shift_label) = $slot; + $output .= '
'; + $output .= '' . esc_html($weekday_label) . ''; + $output .= '' . esc_html($date) . ''; + $output .= '' . esc_html($shift_label) . ''; + $output .= '
'; + } + $output .= '
'; + + // Return output string + return $output; +} \ No newline at end of file