1

I want to modify my booking calendar widget in such a way that I can prevent people from booking at the last minute. I would like to make it in such a way that bookings are only allowed at least 3 days in advance. the code is shown as below (my guess is that it has something to do with datepicker, but I could be wrong):

class Calendar_widget extends WP_Widget {

    function Calendar_widget(){
        parent::__construct( 'calendar_widget', 'Calendar Widget' );
    }

    function do_with_time_stamp_something(&$arrayOfTimeStamps, $timeStamp){
// var_dump($timeStamp);
    if(!empty($timeStamp)){
        $date_link = date( "F.j.Y", $timeStamp );
        array_push($arrayOfTimeStamps, array(
        get_site_url()."/event_category/event/?date=".$date_link,
        date( "m/d/Y", $timeStamp )
    ));
    }

}

    function widget($args, $instance) {
        extract($args);
        $title = apply_filters('widget_title', $instance['title']);

$i=0;
global $wpdb;
$data   =   array();



$wpdb->query("
        SELECT $wpdb->postmeta.`meta_value`
        FROM $wpdb->postmeta

    INNER JOIN $wpdb->posts
    ON $wpdb->postmeta.post_id=$wpdb->posts.ID

    WHERE (`meta_key` = '".SHORTNAME."_event_date'
    OR `meta_key` = '".SHORTNAME."_multiple_weeks_array'
    OR `meta_key` = '".SHORTNAME."_multiple_weeks_array_1') and $wpdb->posts.post_status = 'publish' and $wpdb->posts.post_type = 'event'
");

foreach($wpdb->last_result as $v){

    $meta_value = maybe_unserialize($v->meta_value);

    if(is_array($meta_value)){
        foreach ($meta_value as $timeStamp) {
            // do_with_time_stamp_something($data, $timeStamp);
            if(!empty($timeStamp)){
        $date_link = date( "F.j.Y", $timeStamp );
        array_push($data, array(
        get_site_url()."/event_category/event/?date=".$date_link,
        date( "m/d/Y", $timeStamp )
    ));
    }
        }
    } else {
        // var_dump($meta_value);
        if(!empty($meta_value)){
            // var_dump($meta_value);
            $timeStamp = strtotime($meta_value);
            $date_link = date( "F.j.Y", $timeStamp );
            array_push($data, false);
        }
        // do_with_time_stamp_something($data, $meta_value);
        if(!empty($timeStamp)){

        $date_link = date( "F.j.Y", $timeStamp );
        array_push($data, array(
        get_site_url()."/event_category/event/?date=".$date_link,
        date( "m/d/Y", $timeStamp )
    ));
    }   
    }
}

?>
<?php 
if(isset($_GET['date'])){
    // var_dump($_GET['date']);die;
    $date = $_GET['date'];
    $a = strtotime( $date );
    $final = date( "m/d/Y", $a );
    // echo '---';
    // var_dump($final);die;
} else {
    $final = '';
}
?>
<div class="block_datepicker">
<div id="datepicker" class="calendar">
    <script>
        jQuery(document).ready(function($){
            var events = [
                <?php foreach ($data as $value){ ?>
                    { Title: '<?php echo"$value[0]"; ?>', Date: new Date('<?php echo"$value[1]"; ?>') },
                <?php } ?>
            ];

            $("#datepicker").datepicker({
                defaultDate: new Date ('<?php echo $final; ?>'),
                beforeShowDay: function(date) {
                    var result = [true, '', null];
                    var matching = $.grep(events, function(event) {
                        return event.Date.valueOf() === date.valueOf();
                    });
                    // console.log(matching);
                    if (matching.length) {
                        result = [true, 'highlight', null];
                    }
                    return result;
                },
                onSelect: function(dateText) {
                    var calendar = dateText;
                    $('.about_event[eventdata!="'+calendar+'"]').parents('.events').hide();
                    // $('.about_event[eventdata=="03/28/2014"]').css('display', 'none');
                    // $('.about_event').each(function(){
                    //  if($(this).attr('eventdata') != dateText){
                    //      $(this).parents('.events').hide();
                    //  }
                    //  else{
                    //      $(this).parents('.events').show();
                    //  }
                    // })
                    // console.log(cont);
                    var date,
                        selectedDate = new Date(dateText),
                        i = 0,
                        event = null;

                    while (i < events.length && !event) {
                        date = events[i].Date;

                        if (selectedDate.valueOf() === date.valueOf()) {
                            event = events[i];
                            //console.log(event);
                        }
                        i++;
                    }
                    if (event) {
                        //LIKE in OLD TIME
                        window.location.href = event.Title;
                    }
                }
            });
        }); 
    </script>
</div>
</div>
<?php
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        // $instance['phone'] = strip_tags($new_instance['phone']);
        // $instance['email'] = strip_tags($new_instance['email']);
        return $instance;
    }

    function form($instance) {
        if(isset($instance['title'])){
            $title = esc_attr($instance['title']);
        }
        // if(isset($instance['phone'])){
     //     $phone = esc_attr($instance['phone']);
        // }
        // if(isset($instance['email'])){
     //     $email = esc_attr($instance['email']);
        // }
        ?>
            <!-- <div class="contact_us"> -->
                <!-- <p> -->
                    <!-- <label for="<?php //echo $this->get_field_id('title'); ?>"><?php //_e('Title:'); ?></label> -->
                    <!-- <input id="<?php //echo $this->get_field_id('title'); ?>" name="<?php //echo $this->get_field_name('title'); ?>" type="text" value="<?php //if(isset($title)) echo $title; ?>" /> -->
                <!-- </p> -->
                <!-- <p> -->
                    <!-- <label for="<?php //echo $this->get_field_id('phone'); ?>"><?php //_e('Phone:'); ?></label> -->
                    <!-- <input id="<?php //echo $this->get_field_id('phone'); ?>" name="<?php //echo $this->get_field_name('phone'); ?>" type="text" value="<?php //if(isset($phone)) echo $phone; ?>" /> -->
                <!-- </p> -->
                <!-- <p> -->
                    <!-- <label for="<?php //echo $this->get_field_id('email'); ?>"><?php //_e('Email:'); ?></label> -->
                    <!-- <input id="<?php //echo $this->get_field_id('email'); ?>" name="<?php //echo $this->get_field_name('email'); ?>" type="text" value="<?php //if(isset($email)) echo $email; ?>" /> -->
                <!-- </p> -->
            <!-- </div> -->
        <?php
    }

}

?>

Hi, I think it might have been the php file. The code is below:

<script type="text/javascript">
function show_or_hide(){
    cb = document.getElementById("banner-widget-use");
    if (cb.checked){
        document.getElementById('fake').disabled = true;
    }
    else{
        document.getElementById('fake').disabled = false;
    }
    cd = document.getElementById("second-day-use");
    if (cd.checked){
        document.getElementById('fake_day').disabled = true;
    }
    else{
        document.getElementById('fake_day').disabled = false;
    }

    cd1 = document.getElementById("second-day-use_1");
    if (cd1.checked){
        document.getElementById('fake_day_1').disabled = true;
    }
    else{
        document.getElementById('fake_day_1').disabled = false;
    }

}

ID, SHORTNAME."_event_place", true ); $event_date = get_post_meta( $post->ID, SHORTNAME."_event_date", true ); if( $event_date != "" ){ $date = $event_date; } else{ $date = ''; } $time = get_post_meta( $post->ID, SHORTNAME."_event_time", true ); $array_start = get_post_meta( $post->ID, SHORTNAME."_event_date_array_start", true ); if( $array_start != "" ){ $start_date = $array_start; } else{ $start_date = ''; } $array_end = get_post_meta( $post->ID, SHORTNAME."_event_date_array_end", true ); if( $array_end != "" ){ $end_date = $array_end; } else{ $end_date = ''; } $date_day = get_post_meta( $post->ID, SHORTNAME."_event_multiple_week_day", true ); $multiple_from = get_post_meta( $post->ID, SHORTNAME."_event_multitime_time_begin", true ); $date_day_1 = get_post_meta( $post->ID, SHORTNAME."_event_multiple_week_day_1", true ); $multiple_from_1 = get_post_meta( $post->ID, SHORTNAME."_event_multitime_time_begin_1", true ); $use_multidate_event = get_post_meta($post->ID, SHORTNAME."_use_multidate_event", true); $use_second_event_day = get_post_meta($post->ID, SHORTNAME."_use_second_event_day", true); $time_price = get_post_meta($post->ID, SHORTNAME."_time_price", true); $time_price_child = get_post_meta($post->ID, SHORTNAME."_time_price_child", true); $time_price_other = get_post_meta($post->ID, SHORTNAME."_time_price_other", true); $due_balance = get_post_meta($post->ID, SHORTNAME."_due_balance", true); $max_count = get_post_meta($post->ID, SHORTNAME."_max_count", true); $reserved = get_post_meta($post->ID, SHORTNAME."_reserved", true); $date_day_next = array(); $multiple_from_next = array(); $date_day_1_next = array(); $multiple_from_1_next = array(); $use_multidate_event_next = array(); $use_second_event_day_next = array(); $time_price_next = array(); $time_price_child_next = array(); $time_price_other_next = array(); $due_balance_next = array(); $max_count_next = array(); $reserved_next = array(); for($i = 1; $i ID, SHORTNAME."_event_multiple_week_day_next".$i, true ); $multiple_from_next[$i] = get_post_meta( $post->ID, SHORTNAME."_event_multitime_time_begin_next".$i, true ); // $multiple_till_next[$i] = get_post_meta( $post->ID, SHORTNAME."_event_multitime_time_end_next".$i, true ); $date_day_1_next[$i] = get_post_meta( $post->ID, SHORTNAME."_event_multiple_week_day_1_next".$i, true ); $multiple_from_1_next[$i] = get_post_meta( $post->ID, SHORTNAME."_event_multitime_time_begin_1_next".$i, true ); // $multiple_till_1_next[$i] = get_post_meta( $post->ID, SHORTNAME."_event_multitime_time_end_1_next".$i, true ); $use_multidate_event_next[$i] = get_post_meta($post->ID, SHORTNAME."_use_multidate_event_next".$i, true); $use_second_event_day_next[$i] = get_post_meta($post->ID, SHORTNAME."_use_second_event_day_next".$i, true); $time_price_next[$i] = get_post_meta($post->ID, SHORTNAME."_time_price_next".$i, true); $time_price_child_next[$i] = get_post_meta($post->ID, SHORTNAME."_time_price_child_next".$i, true); $time_price_other_next[$i] = get_post_meta($post->ID, SHORTNAME."_time_price_other_next".$i, true); $due_balance_next[$i] = get_post_meta($post->ID, SHORTNAME."_due_balance_next".$i, true); $max_count_next[$i] = get_post_meta($post->ID, SHORTNAME."_max_count_next".$i, true); $reserved_next[$i] = get_post_meta($post->ID, SHORTNAME."_reserved_next".$i, true); } $week_days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); ?> jQuery(function($){ $( "#event-date" ).datepicker({dateFormat: 'MM dd, yy'}); $( "#event-date-array-start" ).datepicker({dateFormat: 'MM dd, yy'}); $( "#event-date-array-end" ).datepicker({dateFormat: 'MM dd, yy'}); $( "#event-date-array-start_1" ).datepicker({dateFormat: 'MM dd, yy'}); $( "#event-date-array-end_1" ).datepicker({dateFormat: 'MM dd, yy'}); if ($('#banner-widget-use').is(':checked')) { $(".multidate").show(); $('.standart_event').hide(); } else { $(".multidate").hide(); $('.standart_event').show(); } $('#banner-widget-use').click(function() { $( ".multidate" ).slideToggle(this.checked); $('.standart_event').slideToggle(this.checked); $( ".add_time" ).slideToggle(this.checked); $( ".delete_time" ).slideToggle(this.checked); }); if ($('#second-day-use').is(':checked')) { $(".second_day").show(); $('.add_time').show(); $('.delete_time').show(); } else { $(".second_day").hide(); $('.add_time').hide(); $('.delete_time').hide(); } $('#second-day-use').click(function() { }); }); _event_place" id="event-place" value="" style="width:100%;"> _event_date" id="event-date" value="" style="width:100%;"> _event_time" id="event-time" value="" style="width:100%;"> '. __( "Recurring Weekly?", SHORTNAME).'
'; echo ''; } else{ echo ' '. __( "Recurring Weekly?", SHORTNAME).'
'; echo ''; } ?>
_event_date_array_start" id="event-date-array-start" value="" style="width:100%;" />
_event_date_array_end" id="event-date-array-end" value="" style="width:100%;" /> _event_multiple_week_day" name="_event_multiple_week_day"> $day_name) { if($day_id == $date_day){ ?> " selected> "> _event_multitime_time_begin" value="" > _time_price" value="" > _time_price_child" value="" > _time_price_other" value="" > _due_balance" value="" > _max_count" value="" > _reserved" value="" > '. __( "And", SHORTNAME).'
'; echo ''; } elseif($use_second_event_day=="no"){ echo ' '. __( "And", SHORTNAME).'
'; echo ''; } else{ echo ' '. __( "And", SHORTNAME).'
'; echo ''; } ?> _event_multiple_week_day_1" name="_event_multiple_week_day_1"> $day_name) { if($day_id == $date_day_next1){ ?> " selected> "> _event_multitime_time_begin_1" value="" > _time_price" value="" > _time_price_child" value="" > _time_price_other" value="" > _due_balance" value="" > _max_count" value="" > _reserved" value="" > And " id="second-day-use_1" value="yes" checked="checked" >
    <?php if(!empty($multiple_from_1_next1)){ ?>
    <div class="second_day">
        <label style="display:block"><?php _e('Every', SHORTNAME ); ?><br/ >
            <select id="<?php echo SHORTNAME; ?>_event_multiple_week_day_1_next1" name="<?php echo SHORTNAME; ?>_event_multiple_week_day_1_next1">
                <?php foreach ($week_days as $day_id => $day_name) {
                    if($day_id == $date_day_next1){ ?>
                        <option value="<?php echo $day_id; ?>" selected><?php echo $day_name; ?></option>
                    <?php }
                    else{ ?>
                        <option value="<?php echo $day_id; ?>"><?php echo $day_name; ?></option>
                    <?php }
                } ?>
            </select>
        </label>
        <label style="display:block"><?php _e('From', SHORTNAME); ?><br/ ><input type="text" name="<?php echo SHORTNAME; ?>_event_multitime_time_begin_1_next1" value="<?php echo $multiple_from_1_next1; ?>" ></label>

    </div>
    <?php } ?>

    <?php $y = 1; ?>
    <?php for($i = 1; $i <= 100; $i++){
        $block = get_post_meta( $post->ID, SHORTNAME.'_event_multitime_time_begin_1_next'.$i);  
        if(isset($block[0]) && !empty($block[0])){
        ?>
        <div class="second_day">
        <label style="display:block"><?php _e('Every', SHORTNAME ); ?><br/ >
            <select id="<?php echo SHORTNAME; ?>_event_multiple_week_day_1_next<?php echo $i; ?>" name="<?php echo SHORTNAME; ?>_event_multiple_week_day_1_next<?php echo $i; ?>">
                <?php foreach ($week_days as $day_id => $day_name) {
                    $ddn = $date_day_1_next[$i];
                    if($day_id == $ddn){ ?>
                        <option value="<?php echo $day_id; ?>" selected><?php echo $day_name; ?></option>
                    <?php }
                    else{ ?>
                        <option value="<?php echo $day_id; ?>"><?php echo $day_name; ?></option>
                    <?php }
                } ?>
            </select>
        </label>
        <?php 
        $mfn = $multiple_from_1_next[$i];
        $use_day = $use_second_event_day_next[$i];
        $price = $time_price_next[$i];
        $price_child = $time_price_child_next[$i];
        $price_other = $time_price_other_next[$i];
        $due_balance = $due_balance_next[$i];
        $max_cap = $max_count_next[$i];
        if(isset($reserved_next[$i]) && !empty($reserved_next[$i])){
            $reserve = $reserved_next[$i];
        }
        else{
            $reserve = 0;   
        }

    ?>
        <label style="display:block"><?php _e('From', SHORTNAME); ?><br/ ><input type="text" name="<?php echo SHORTNAME; ?>_event_multitime_time_begin_1_next<?php echo $y; ?>" value="<?php echo $mfn; ?>" ></label>
        <label style="display:block"><?php _e('Price Adult', SHORTNAME); ?><br/ ><input type="text" name="<?php echo SHORTNAME; ?>_time_price_next<?php echo $y; ?>" value="<?php echo $price; ?>" ></label>
        <label style="display:block"><?php _e('Price Child', SHORTNAME); ?><br/ ><input type="text" name="<?php echo SHORTNAME; ?>_time_price_child_next<?php echo $y; ?>" value="<?php echo $price_child; ?>" ></label>
        <label style="display:block"><?php _e('Price Other', SHORTNAME); ?><br/ ><input type="text" name="<?php echo SHORTNAME; ?>_time_price_other_next<?php echo $y; ?>" value="<?php echo $price_other; ?>" ></label>
        <label style="display:block"><?php _e('Minimum Deposit (%)', SHORTNAME); ?><br/ ><input type="text" name="<?php echo SHORTNAME; ?>_due_balance_next<?php echo $y; ?>" value="<?php echo $due_balance; ?>" ></label>
        <label style="display:block"><?php _e('Maximum Capacity', SHORTNAME); ?><br/ ><input type="text" name="<?php echo SHORTNAME; ?>_max_count_next<?php echo $y; ?>" value="<?php echo $max_cap; ?>" ></label>
        <input type="hidden" name="<?php echo SHORTNAME; ?>_reserved_next<?php echo $y; ?>" value="<?php echo $reserve; ?>" >
        <label for="wpcitytour_use_multidate_event" style="margin-right:10px;">And</label>
        <input type="checkbox" num="<?php echo $i; ?>" name="wpcitytour_use_second_event_day_next<?php echo $y; ?>" id="second-day-use_1" value="yes" checked="checked" ><br>
    </div>
    <?php
    $y++;
    }
}?>

</div>
<script type="text/javascript">
    jQuery(document).ready(function($){

        $('.second_day').last().find('input').last().removeAttr('checked');

        $(':checkbox').live('change', function() {

            if($(this).is(":checked")) {

                var id = $('.second_day').length;
                $('.second_day').last().after('<div class="second_day">Every<br /><select id="wpcitytour_event_multiple_week_day_1_next'+id+'" name="wpcitytour_event_multiple_week_day_1_next'+id+'"><option value="0">Sunday</option><option value="1">Monday</option><option value="2">Tuesday</option><option value="3">Wednesday</option><option value="4">Thursday</option><option value="5">Friday</option><option value="6">Saturday</option></select><label style="display:block">From<br/><input type="text" name="wpcitytour_event_multitime_time_begin_1_next'+id+'" value="" ></label><br/><label style="display:block">Price Adult<br/ ><input type="text" name="wpcitytour_time_price_next'+id+'" value="" ></label><br /><label style="display:block">Price Child<br/ ><input type="text" name="wpcitytour_time_price_child_next'+id+'" value="" ></label><br /><label style="display:block">Price Senior / Student<br/ ><input type="text" name="wpcitytour_time_price_other_next'+id+'" value="" ></label><br /><label style="display:block">Maximum Capacity<br/ ><input type="text" name="wpcitytour_max_count_next'+id+'" value="" ></label><br /><input type="hidden" name="wpcitytour_reserved_next'+id+'" value="0" ><label for="wpcitytour_use_multidate_event'+id+'" style="margin-right:10px;">And</label><input type="checkbox" name="wpcitytour_use_second_event_day'+id+'" id="*emphasized text*second-day-use" value="yes"><br><input type="hidden" id="fake" name="wpcitytour_use_multidate_event'+id+'" value="no" disabled="disabled">');
                return false;

           } else{

                var number = $(this).attr('num');
                var post_id = $('#post_ID').val();
                var send_data = {
                    post_id: post_id,
                    number: number,
                    action: "delete_time"
                };

                $.ajax({
                    type: 'POST',
                    url: ajaxurl,
                    dataType: 'json',
                    data: send_data,
                    success:function(result){
                        if(result.error == 0){

                        }
                    }
                });

                if($(this).parents().hasClass('second_day')){
                    $(this).parents('.second_day').next().find('input').val('');
                    $(this).parents('.second_day').next().addClass('trash').removeClass( "second_day" );
                    $('.trash').first().hide();
                }
                else{
                    $('.second_day').last().find('input').val('');
                    $('.second_day').last().addClass('trash').removeClass( "second_day" );
                    $('.trash').first().hide();
                }

                return false;
           }
        });

            return false;
        });

</script>
  • compare the value calendar in onSelect at datepicker using [this solution](http://stackoverflow.com/questions/492994/compare-dates-with-javascript) . If calendar is not the result what you want ( in this case 3 days advance ) then call return – Randyka Yudhistira Jan 27 '15 at 06:44
  • Hi, thank you for the reply but I'm not quite sure what you mean. – stoltenheim Jan 27 '15 at 13:06

0 Answers0