diff --git a/admin/add_broadcast_page.php b/admin/add_broadcast_page.php index cd8b4df..02dd9a4 100644 --- a/admin/add_broadcast_page.php +++ b/admin/add_broadcast_page.php @@ -30,8 +30,8 @@ function create_broadcast_post() ); } add_action('init', 'create_broadcast_post'); - add_action('add_meta_boxes', 'live_meta_box_add'); + function live_meta_box_add() { //give name of your input field @@ -47,7 +47,7 @@ function orari_dirette_input($post) $broadcast = term_exists('Podcast', 'category', 0); $old_term = term_exists($post->post_title, 'category', $broadcast); ?> - +
@@ -77,9 +77,11 @@ function orari_dirette_input($post)
@@ -93,6 +95,8 @@ function orari_dirette_input($post)
@@ -129,9 +133,9 @@ function orari_repliche_input($post)
@@ -166,12 +170,7 @@ function orari_dirette_save($post_id) if (!current_user_can('edit_post', $post_id)) { return; } - /* - echo '
';
-    print_r($_POST);
-    echo '
'; - exit(); - */ + // now we can actually save the data $allowed = array( 'a' => array( // on allow a tags @@ -199,17 +198,39 @@ function orari_dirette_save($post_id) wp_insert_term($_POST['post_title'], 'category', $args); } update_post_meta($post_id, 'taxonomy_id', wp_kses($_POST['taxonomy_id'], $allowed)); + for ($x=0;$x<7;$x++) { - update_post_meta($post_id, 'on_air_day_'.$x, wp_kses($_POST['on_air_day_'.$x], $allowed)); + $onair_day_data = wp_kses($_POST['on_air_day_'.$x], $allowed); + $rerun_day_data = wp_kses($_POST['rerun_day_'.$x], $allowed); + $onair_start_date_data = wp_kses($_POST['on_air_start_date_'.$x], $allowed); + $rerun_start_date_data = wp_kses($_POST['rerun_start_date_'.$x], $allowed); + + if (!empty($onair_day_data) && !empty($onair_start_date_data)) { + check_and_correct_start_date_flag($onair_start_date_data, $onair_day_data); + } + if (!empty($rerun_day_data) && !empty($rerun_start_date_data)) { + check_and_correct_start_date_flag($rerun_start_date_data, $rerun_day_data); + } + update_post_meta($post_id, 'on_air_day_'.$x, $onair_day_data); update_post_meta($post_id, 'on_air_start_'.$x, wp_kses($_POST['on_air_start_'.$x], $allowed)); update_post_meta($post_id, 'on_air_end_'.$x, wp_kses($_POST['on_air_end_'.$x], $allowed)); update_post_meta($post_id, 'on_air_cadence_'.$x, wp_kses($_POST['on_air_cadence_'.$x], $allowed)); - update_post_meta($post_id, 'on_air_start_date_'.$x, wp_kses($_POST['on_air_start_date_'.$x], $allowed)); - update_post_meta($post_id, 'rerun_day_'.$x, wp_kses($_POST['rerun_day_'.$x], $allowed)); + update_post_meta($post_id, 'rerun_day_'.$x, $rerun_day_data); update_post_meta($post_id, 'rerun_start_'.$x, wp_kses($_POST['rerun_start_'.$x], $allowed)); update_post_meta($post_id, 'rerun_end_'.$x, wp_kses($_POST['rerun_end_'.$x], $allowed)); - update_post_meta($post_id, 'rerun_cadence_'.$x, wp_kses($_POST['rerun_end_'.$x], $allowed)); - update_post_meta($post_id, 'rerun_start_date_'.$x, wp_kses($_POST['rerun_start_date_'.$x], $allowed)); + update_post_meta($post_id, 'rerun_cadence_'.$x, wp_kses($_POST['rerun_cadence_'.$x], $allowed)); + update_post_meta($post_id, 'on_air_start_date_'.$x, $onair_start_date_data); + update_post_meta($post_id, 'rerun_start_date_'.$x, $rerun_start_date_data); } } } + +function check_and_correct_start_date_flag(&$start_date_ptr, $day) { + $start_date_dt = new DateTime($start_date_ptr); + $weekday_number = $start_date_dt->format("N"); + if ($day != $weekday_number) { + $day_diff = $day - $weekday_number; + $start_date_dt->modify($day_diff." days"); + $start_date_ptr = $start_date_dt->format("Y-m-d"); + }; +} diff --git a/admin/add_onchange_listener.js b/admin/add_onchange_listener.js new file mode 100644 index 0000000..bcf0b65 --- /dev/null +++ b/admin/add_onchange_listener.js @@ -0,0 +1,43 @@ +// change, consistently with day field, the date field +function add_change_day_handler(elem, day_field_id, date_field_id) { + elem.addEventListener('change', function(e) { + const setted_weekday = document.getElementById(day_field_id).value; + const date_input = document.getElementById(date_field_id); + if (date_input.value.length !== 0){ + const date = new Date(date_input.value); + let weekday = date.getDay(); + weekday = (weekday == 0) ? 7 : weekday; + if (weekday != setted_weekday ) { + let weekday_diff = setted_weekday - weekday; + date.setDate(date.getDate() + weekday_diff); + date_input.value = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate(); + } + } + }); +} + +// change, consistently with date field, the day field +function add_change_date_handler(elem, day_field_id, date_field_id) { + elem.addEventListener('change', function(e) { + const setted_date = document.getElementById(date_field_id).value; + const day_input = document.getElementById(day_field_id); + let setted_weekday = day_input.value; + let date_weekday = new Date(setted_date).getDay(); + date_weekday = (date_weekday == 0) ? 7 : date_weekday; + if (date_weekday != setted_weekday) { + day_input.value = date_weekday; + } + }); +} + +for (var i = 0; i < 7 ; i++) { + let onair_day_input = document.getElementById("on_air_day_" + i); + let rerun_day_input = document.getElementById("rerun_day_" + i); + let onair_date_input = document.getElementById("on_air_start_date_" + i); + let rerun_date_input = document.getElementById("rerun_start_date_" + i); + add_change_day_handler(onair_day_input, "on_air_day_" + i, "on_air_start_date_" + i); + add_change_day_handler(rerun_day_input, "rerun_day_" + i, "rerun_start_date_" + i); + add_change_date_handler(onair_date_input, "on_air_day_" + i, "on_air_start_date_" + i); + add_change_date_handler(rerun_date_input, "rerun_day_" + i, "rerun_start_date_" + i); + +} \ No newline at end of file diff --git a/shortcodes/palinsesto.php b/shortcodes/palinsesto.php index ac17b08..a5acd87 100644 --- a/shortcodes/palinsesto.php +++ b/shortcodes/palinsesto.php @@ -1,18 +1,20 @@ setTime(0,0,0); + $start_date = new DateTime($start_date_s); $return_value; - switch($cadence) { - case 1: + switch($cadence_i) { + case 1: //settimanale $return_value = true; break; - case 2: - case 4: - $today = new DateTime(); - $date = new DateTime($start_date); - $modulo = 7 * $cadence; - $date_difference = date_diff($today, $date); + case 2: //ogni 2 settimane + case 3: + case 4: //ogni 4 settimane + $modulo = 7 * $cadence_i; + $date_difference = date_diff($today, $start_date); $days_difference = $date_difference->days; if (($days_difference % $modulo) < 7) { $return_value = true; @@ -20,11 +22,32 @@ function have_to_run_this_week($start_date, $cadence) } $return_value = false; break; + case 5: //una volta al mese + if (which_day_of_the_month($start_date) == which_day_of_the_month($today)) { + $return_value = true; + break; + } + $return_value = false; + break; } return $return_value; } +function which_day_of_the_month($datetime) +{ + $weekday_literal = $datetime->format("l"); + $weekday_iterator = clone($datetime); + $weekday_iterator->modify("first $weekday_literal of this month"); + $count = 1; + + while($datetime != $weekday_iterator && $count < 5) { + $weekday_iterator->modify("next $weekday_literal"); + $count++; + } + return $count; +} + function show_palinsesto($args) { $week_days = ['Lunedì','Martedì','Mercoledì','Giovedì','Venerdì','Sabato','Domenica']; @@ -76,12 +99,17 @@ function show_palinsesto($args) $rerun_end = get_post_meta($post->ID, 'rerun_end_'.$d, true); $rerun_cadence = get_post_meta($post->ID, 'rerun_cadence_'.$d, true); $rerun_start_date = get_post_meta($post->ID, 'rerun_start_date_'.$d, true); - - if (empty($rerun_cadence) || empty($rerun_start_date)) { - $rerun_cadence = "1"; //default settimanale - } - + +/* + echo '
';
+    print_r($post->ID . " - " . $rerun_day ." - " . $rerun_cadence);
+    echo '
'; +*/ if (!empty($rerun_day)&&!empty($rerun_start)&&!empty($rerun_end)) { + + if (empty($rerun_cadence) || empty($rerun_start_date)) { + $rerun_cadence = "1"; //default settimanale + } if (have_to_run_this_week($rerun_start_date, (int)$rerun_cadence)) { $palinsesto[(int)$rerun_day][(string)$rerun_start] = @@ -102,17 +130,19 @@ function show_palinsesto($args) ksort($palinsesto); -// echo '
';
-//  print_r($palinsesto);
-//  echo '
'; + // echo '
';
+  // print_r($palinsesto);
+  // echo '
'; $today = date("N"); for ($x=1;$x<=7;$x++) { - /*echo '
';
+        /*
+        echo '
';
         echo("$today,$x");
-        echo '
';*/ + echo '
'; + */ if ($x == $today) $tabs .= "
  • ".$week_days[$x-1]."
  • "; @@ -139,6 +169,7 @@ function show_palinsesto($args) $content .= ''; } ?> +
      diff --git a/shortcodes/whatplaynow.php b/shortcodes/whatplaynow.php index 6ec2041..7d589d1 100644 --- a/shortcodes/whatplaynow.php +++ b/shortcodes/whatplaynow.php @@ -9,7 +9,7 @@ a settimana nello stesso giorno. function what_play_now($args) { - global $wpdb; + global $wpdb; /* Oggi espresso come $day (giorno della settimana 1-7 a partire da lunedì) e $now oggetto DateTime completo */ $day = date("N"); $now = new DateTime("NOW"); @@ -69,7 +69,6 @@ function what_play_now($args) echo ""; */ - foreach ($results as $t) { $tocheck[0] = $i; $tocheck[1] = $t->post_id;