1

我正在为使用日历以及开始和结束时间的搬家企业构建预订表格。我使用 Formidable Pro 构建了时间选择器,它允许我检查时间字段上的“唯一”,这会在所选日期自动删除它们。但是,它不会自动从开始时间和结束时间之间的范围内删除时间(即:如果有人选择从凌晨 1 点到凌晨 3 点租一辆卡车,我需要将凌晨 1 点、凌晨 2 点和凌晨 3 点从未来的选项中删除,但现在它仅删除凌晨 1 点和凌晨 3 点)。我需要编写 ajax 以从选项中删除中间时间。我不知道从哪里开始。这是当前的 ajax_time_ 选项函数。任何朝着正确方向的推动将不胜感激。

function ajax_time_options(){
    global $frmpro_settings, $frmdb, $wpdb;

    //posted vars = $time_field, $date_field, $step, $start, $end, $date, $clock
    extract($_POST);

    $time_key = str_replace('field_', '', $time_field);
    $date_key = str_replace('field_', '', $date_field);
    if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', trim($date)))
        $date = FrmProAppHelper::convert_date($date, $frmpro_settings->date_format, 'Y-m-d');
    $date_entries = FrmEntryMeta::getEntryIds("fi.field_key='$date_key' and meta_value='$date'");

    $opts = array('' => '');
    $time = strtotime($start);
    $end = strtotime($end);
    $step = explode(':', $step);
    $step = (isset($step[1])) ? ($step[0] * 3600 + $step[1] * 60) : ($step[0] * 60);
    $format = ($clock) ? 'H:i' : 'h:i A';

    while($time <= $end){
        $opts[date($format, $time)] = date($format, $time);
        $time += $step;
    }

    if($date_entries and !empty($date_entries)){
        $used_times = $wpdb->get_col("SELECT meta_value FROM $frmdb->entry_metas it LEFT JOIN $frmdb->fields fi ON (it.field_id = fi.id) WHERE fi.field_key='$time_key' and it.item_id in (". implode(',', $date_entries).")");

        if($used_times and !empty($used_times)){
            $number_allowed = apply_filters('frm_allowed_time_count', 1, $time_key, $date_key);
            $count = array();
            foreach($used_times as $used){
                if(!isset($opts[$used]))
                    continue;

                if(!isset($count[$used]))
                    $count[$used] = 0;
                $count[$used]++;

                if((int)$count[$used] >= $number_allowed)
                    unset($opts[$used]);
            }
            unset($count);
        }
    }

    echo json_encode($opts);
    die();
}    
4

0 回答 0