/**
 * Single condition form elements.
 * 
 * @param type $data
 * @param type $condition
 * @param type $key
 * @return string 
 */
function wpcf_cd_admin_form_single_filter($data, $condition, $key = null, $group = false, $force_multi = false)
{
    if ($group) {
        $name = 'wpcf[group][conditional_display]';
    } else {
        $name = 'wpcf[fields][' . $data['id'] . '][conditional_display]';
    }
    $group_id = isset($_GET['group_id']) ? intval($_GET['group_id']) : false;
    if ($group_id && !$group) {
        // Allow group to use other fields
        $fields = wpcf_admin_fields_get_fields_by_group($group_id);
    } else {
        $fields = wpcf_admin_fields_get_fields();
    }
    $options = array();
    foreach ($fields as $field_id => $field) {
        if (!$group && $data['id'] == $field_id) {
            continue;
        }
        if (wpcf_admin_is_repetitive($field)) {
            continue;
        }
        $options[$field_id] = array('#value' => $field_id, '#title' => $field['name'], '#attributes' => array('class' => 'wpcf-conditional-select-' . $field['type']));
    }
    if (!$group && empty($options)) {
        return array('cd' => array('#type' => 'markup', '#markup' => '<p>' . __('You will be able to set conditional field display when you save more fields.', 'wpcf') . '</p>'));
    }
    $id = !is_null($key) ? $key : strval('condition_' . mt_rand());
    $form = array();
    $before = '<div class="wpcf-cd-entry"><br />';
    $form['cd']['field_' . $id] = array('#type' => 'select', '#name' => $name . '[conditions][' . $id . '][field]', '#options' => $options, '#inline' => true, '#before' => $before, '#default_value' => isset($condition['field']) ? $condition['field'] : null, '#attributes' => array('class' => 'wpcf-cd-field'));
    $form['cd']['operation_' . $id] = array('#type' => 'select', '#name' => $name . '[conditions][' . $id . '][operation]', '#options' => array_flip(wpcf_cd_admin_operations()), '#inline' => true, '#default_value' => isset($condition['operation']) ? $condition['operation'] : null, '#attributes' => array('class' => 'wpcf-cd-operation'));
    $form['cd']['value_' . $id] = array('#type' => 'textfield', '#name' => $name . '[conditions][' . $id . '][value]', '#options' => wpcf_cd_admin_operations(), '#inline' => true, '#value' => isset($condition['value']) ? $condition['value'] : '', '#attributes' => array('class' => 'wpcf-cd-value'));
    $form['cd']['remove_' . $id] = array('#type' => 'button', '#name' => 'remove', '#value' => __('Remove condition', 'wpcf'), '#attributes' => array('onclick' => 'wpcfCdRemoveCondition(jQuery(this));', 'class' => 'wpcf-add-condition'), '#before' => '<br />', '#after' => '</div>');
    return $form['cd'];
}
/**
 * Single condition form elements.
 *
 * @param type $data
 * @param type $condition
 * @param type $key
 * @return string
 */
function wpcf_cd_admin_form_single_filter($data, $condition, $key = null, $group = false, $force_multi = false)
{
    global $wpcf;
    if ($group) {
        $name = 'wpcf[group][conditional_display]';
    } else {
        $name = 'wpcf[fields][' . $data['id'] . '][conditional_display]';
    }
    $group_id = isset($_GET['group_id']) ? intval($_GET['group_id']) : false;
    /*
     *
     *
     * TODO Review this allowing fields from same group as conditional (self loop)
     * I do not remember allowing fields from same group as conditional (self loop)
     * on Group Fields edit screen.
     */
    //    if ( $group_id && !$group ) {// Allow group to use other fields
    //        $fields = wpcf_admin_fields_get_fields_by_group( $group_id );
    //    } else {
    $fields = wpcf_admin_fields_get_fields(true, false, true);
    ksort($fields, SORT_STRING);
    //    }
    if ($group) {
        $_distinct = wpcf_admin_fields_get_fields_by_group($group_id);
        foreach ($_distinct as $_field_id => $_field) {
            if (isset($fields[$_field_id])) {
                unset($fields[$_field_id]);
            }
        }
    }
    $options = array();
    $ignore_field_type_array = array('audio', 'checkboxes', 'embed', 'file', 'image', 'video', 'wysiwyg');
    $flag_repetitive = false;
    foreach ($fields as $field_id => $field) {
        if (!$group && $data['id'] == $field_id) {
            continue;
        }
        // WE DO NOT ALLOW repetitive fields to be compared.
        if (wpcf_admin_is_repetitive($field)) {
            $flag_repetitive = true;
            continue;
        }
        /**
         * Skip some files
         */
        if (in_array($field['type'], $ignore_field_type_array)) {
            continue;
        }
        /**
         * build options
         */
        $options[$field_id] = array('#value' => $field_id, '#title' => stripslashes($field['name']), '#attributes' => array('class' => 'wpcf-conditional-select-' . $field['type']));
    }
    /*
     * Special case
     * https://icanlocalize.basecamphq.com/projects/7393061-wp-views/todo_items/153565054/comments
     *
     * When field is new and only one diff field in list - that
     * means one field is saved but other not yet.
     */
    $is_new = isset($data['id']) && isset($fields[$data['id']]) ? false : true;
    $special_stop = false;
    if ($is_new) {
        if (count($options) == 1) {
            $special_stop = true;
        }
    }
    /*
     *
     * This means all fields are repetitive and no one left to compare with.
     * WE DO NOT ALLOW repetitive fields to be compared.
     */
    if (!$group && empty($options) && $flag_repetitive) {
        return array('cd' => array('#type' => 'markup', '#markup' => '<p class="wpcf-error">' . __('Conditional display is only working based on non-repeating fields. All fields in this group are repeating, so you cannot set their display based on other fields.', 'wpcf') . '</p>' . wpcf_conditional_disable_add_js($data['id'])));
    } else {
        if (!$group && (empty($options) || $special_stop)) {
            return array('cd' => array('#type' => 'markup', '#markup' => '<p>' . __('You will be able to set conditional field display when you save more fields.', 'wpcf') . '</p>'));
        }
    }
    $id = !is_null($key) ? $key : strval('condition_' . wpcf_unique_id(serialize($data) . serialize($condition) . $key . $group));
    $form = array();
    $before = '<div class="wpcf-cd-entry"><br />';
    $form['cd']['field_' . $id] = array('#type' => 'select', '#name' => $name . '[conditions][' . $id . '][field]', '#options' => $options, '#inline' => true, '#before' => $before, '#default_value' => isset($condition['field']) ? $condition['field'] : null, '#attributes' => array('class' => 'wpcf-cd-field'));
    $form['cd']['operation_' . $id] = array('#type' => 'select', '#name' => $name . '[conditions][' . $id . '][operation]', '#options' => array_flip(wpcf_cd_admin_operations()), '#inline' => true, '#default_value' => isset($condition['operation']) ? $condition['operation'] : null, '#attributes' => array('class' => 'wpcf-cd-operation'));
    $form['cd']['value_' . $id] = array('#type' => 'textfield', '#name' => $name . '[conditions][' . $id . '][value]', '#inline' => true, '#value' => isset($condition['value']) ? $condition['value'] : '', '#attributes' => array('class' => 'wpcf-cd-value'));
    /*
     *
     * Adjust for date
     */
    if (!empty($condition['value'])) {
        WPCF_Loader::loadInclude('fields/date/functions.php');
        $timestamp = wpcf_fields_date_convert_datepicker_to_timestamp($condition['value']);
        if ($timestamp !== false) {
            $date_value = adodb_date('d', $timestamp) . ',' . adodb_date('m', $timestamp) . ',' . adodb_date('Y', $timestamp);
            $date_function = 'date';
        } else {
            if (wpcf_fields_date_timestamp_is_valid($condition['value'])) {
                $date_value = adodb_date('d', $condition['value']) . ',' . adodb_date('m', $condition['value']) . ',' . adodb_date('Y', $condition['value']);
                $date_function = 'date';
            }
        }
    }
    if (empty($date_value)) {
        $date_value = '';
        $date_function = false;
    }
    $form['cd']['value_date_' . $id] = array('#type' => 'markup', '#markup' => '<br />' . wpcf_conditional_add_date_controls($date_function, $date_value, $name . '[conditions][' . $id . ']'), '#attributes' => array('class' => 'wpcf-cd-value-date'));
    $form['cd']['remove_' . $id] = array('#type' => 'button', '#name' => 'remove', '#value' => __('Remove condition', 'wpcf'), '#attributes' => array('onclick' => 'wpcfCdRemoveCondition(jQuery(this));', 'class' => 'wpcf-add-condition'), '#after' => '</div><br />');
    return $form['cd'];
}
function wpcf_conditional_get_curent($data)
{
    /**
     * state of conditional display custom use
     */
    if (true && array_key_exists('data', $data) && is_array($data['data']) && array_key_exists('conditional_display', $data['data']) && is_array($data['data']['conditional_display']) && array_key_exists('custom_use', $data['data']['conditional_display']) && !empty($data['data']['conditional_display']['custom_use'])) {
        return '<ul><li>' . __('Custom logic', 'wpcf') . '</li></ul>';
    }
    $current = '';
    if (true && isset($data['data']['conditional_display']) && isset($data['data']['conditional_display']['conditions']) && !empty($data['data']['conditional_display']['conditions'])) {
        $convert = wpcf_cd_admin_operations();
        $all_types_fields = get_option('wpcf-fields', array());
        $current .= '<ul>';
        foreach ($data['data']['conditional_display']['conditions'] as $condition) {
            if (false || !isset($condition['field']) || !isset($all_types_fields[$condition['field']])) {
                continue;
            }
            $operation = __('unknown', 'wpcf');
            if (true && isset($condition['operation']) && isset($convert[$condition['operation']])) {
                $operation = $convert[$condition['operation']];
            }
            $value = isset($condition['value']) ? $condition['value'] : '';
            /**
             * handle date field
             */
            if (true && empty($value) && isset($all_types_fields[$condition['field']]) && isset($all_types_fields[$condition['field']]['type']) && 'date' == $all_types_fields[$condition['field']]['type']) {
                $value = sprintf(__('%1$s%2$s, %3$s', 'wpcf'), $condition['month'], $condition['date'], $condition['year']);
                $value = date(get_option('date_format'), mktime(0, 0, 0, $condition['date'], $condition['month'], $condition['year']));
            }
            /* remove operators description */
            $operation = preg_replace('#\\([^)]+\\)#', '', $operation);
            $current .= sprintf('<li><span>%s %s %s</span></li>', esc_html($all_types_fields[$condition['field']]['name']), esc_html($operation), esc_html($value));
        }
        $current .= '</ul>';
    }
    return $current;
}