<?php

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Renders Custom Fields config form.
 * 
 * The page is rendered in wp-admin / Classifieds / Options / Custom Fields panel
 * 
 * @since 1.0
 * @return void
 */
function wpadverts_custom_fields_page_options() {
    
    include_once WP_PLUGIN_DIR . '/wpadverts-custom-fields/includes/functions.php';
    
    if( adverts_request( "cfpanel") == "data-sources" ) {
        if( adverts_request( "add" ) == "new" ) {
            _wpadverts_custom_fields_page_ds_add();
        } else if( adverts_request( "edit" ) ) {
            _wpadverts_custom_fields_page_ds_edit();
        } else if( adverts_request( "delete") ) {
            _wpadverts_custom_fields_page_ds_delete();
        } else {
            _wpadverts_custom_fields_page_ds_list();
        }
    } else {
        if( adverts_request( "add" ) == "new" ) {
            _wpadverts_custom_fields_page_add();
        } else if( adverts_request( "edit" ) ) {
            _wpadverts_custom_fields_page_edit();
        } else if( adverts_request( "delete") ) {
            _wpadverts_custom_fields_page_delete();
        } else {
            _wpadverts_custom_fields_page_list();
        }
    } 
    
    
}

function _wpadverts_custom_fields_page_add() {
    
    $can_clone = false;
    $options = array();
    
    if(adverts_request( "clone" ) ) {
        
        $clone = get_post( absint( adverts_request( "clone" ) ) );
        $post_statuses = array_keys( wpadverts_custom_fields_form_types() );
        
        if( $clone->post_type == 'wpadverts-form' && in_array( $clone->post_status, $post_statuses ) ) {
            $can_clone = true;
            $options["form_type"] = $clone->post_status;
        }
    }
    
    adverts_form_add_validator("wpadverts_custom_fields_name_exists", array(
        "callback" => "wpadverts_custom_fields_name_exists",
        "label" => "",
        "params" => array(),
        "default_error" => __( "Some other form is already using this name.", "wpadverts-custom-fields" ),
        "validate_empty" => false
    ));
    
    $form_scheme = array(
        "name" => "custom_fields_add",
        "action" => "",
        "field" => array(
            array(
                "name" => "form_type",
                "type" => "adverts_field_select",
                "order" => 5,
                "label" => __( "Form Type", "wpadverts-custom-fields" ),
                "is_required" => true,
                "max_choices" => 1,
                "empty_option" => true,
                "empty_option_text" => " ",
                "options" => array(
                    array( "value"=> "wpad-form-add", "text"=> __( "Create Advert ([adverts_add])", "wpadverts-custom-fields" ) ),
                    array( "value"=> "wpad-form-search", "text"=> __( "Search Ads ([adverts_list])", "wpadverts-custom-fields" ) ),
                    array( "value"=> "wpad-form-contact", "text"=> __( "Contact", "wpadverts-custom-fields" ) ),
                ),
                "validator" => array(
                    array( "name" => "is_required" ),
                )
            ),
            array(
                "name" => "form_title",
                "type" => "adverts_field_text",
                "order" => 5,
                "label" => __( "Form Title", "wpadverts-custom-fields" ),
                "is_required" => true,
                "validator" => array(
                    array( "name" => "is_required" )
                )
            ),
            array(
                "name" => "form_name",
                "type" => "adverts_field_text",
                "order" => 10,
                "label" => __( "Form Name (a-z, 0-9, - only)", "wpadverts-custom-fields" ),
                "is_required" => true,
                "validator" => array(
                    array( "name" => "is_required" ),
                    array( "name" => "wpadverts_custom_fields_name_exists" )
                )
            ),
            array(
                "name" => "is_default",
                "type" => "adverts_field_checkbox",
                "order" => 15,
                "label" => __( "Default", "wpadverts-custom-fields" ),
                "is_required" => false,
                "validator" => array( ),
                "max_choices" => 1,
                "options" => array(
                    array( "value"=> "1", "text"=> __( "Automatically apply this scheme to form.", "wpadverts-fee-per-category" ) ),
                )
            ),
        )
    );
    
    $h2_title = __( "Create a New Form", "wpadverts-custom-fields" );
    $button_text = __("Create New Form", "wpadverts-custom-fields");
    
    if( $can_clone ) {
        
        $form_scheme["field"][] = array(
            "name" => "form_clone",
            "type" => "adverts_field_hidden",
            "order" => 14,
            "label" => "",
            "is_required" => false,
        );
        
        $options["form_clone"] = $clone->post_status ."--". $clone->ID;
        
        foreach( $form_scheme["field"] as $key => $field ) {
            if( $field["name"] == "form_type" ) {
                $form_scheme["field"][$key]["attr"] = array( 
                    "title" => __( "Cannot change form type while cloning a form.", "wpadverts-custom-fields" ),
                    "style" => "cursor:not-allowed"
                );
            }
        }
        
        $h2_title = __( "Clone Form", "wpadverts-custom-fields" );
        $button_text = __( "Clone", "wpadverts-custom-fields" );
    }
    
    wp_enqueue_style( 'adverts-admin' );
    wp_enqueue_script( 'wpadverts-custom-fields-form-add' );
    
    $flash = Adverts_Flash::instance();
    $soft_redirect = false;
    $error = array();
    
    $form = new Adverts_Form( $form_scheme );
    $form->bind( $options );

    if(isset($_POST) && !empty($_POST)) {
        $form->bind( $_POST );
        $valid = $form->validate();

        if($valid) {
            
            $type = $form->get_value("form_type");
            $name = $form->get_value("form_name");
            
            $post_content = '';
            
            if( adverts_request( "form_clone" ) ) {
                list( $clone_type, $clone_id ) = explode( "--", adverts_request( "form_clone" ) );
                $clone = get_post( $clone_id );
                
                if( $clone instanceof WP_Post && $clone->post_status == $form->get_value( "form_type" ) ) {
                    $post_content = $clone->post_content;
                }
            }
            
            $id = wp_insert_post( array( 
                'post_name' => $form->get_value( "form_name" ),
                'post_title' => $form->get_value( "form_title" ),
                'post_content' => $post_content,
                'post_status' => $form->get_value( "form_type" ),
                'menu_order' => absint( $form->get_value("is_default") ),
                'post_type' => "wpadverts-form"
            ), true );
            
            $m = __('Form Created. Loading Form Editor. If it is taking to long <a href="%s">click here</a>.', "wpadverts-custom-fields");
            
            $soft_redirect = add_query_arg( 'edit', $id, remove_query_arg( 'add' ) );
            $flash->add_info( sprintf( $m, $soft_redirect ) );
        } else {
            $flash->add_error( __("There are errors in your form.", "adverts") );
        }
    }
    
    
    
    include dirname( ADVERTS_PATH ) . '/wpadverts-custom-fields/admin/add.php';
}

function _wpadverts_custom_fields_page_edit() {
    $id = adverts_request( "edit" );
    $scheme = get_post( $id );
    
    if( $scheme->post_type != 'wpadverts-form' ) {
        echo __( "Inocrrect Form ID", "wpadverts-custom-fields" );
        return;
    }
    
    $status = $scheme->post_status;
    $forms = wpadverts_custom_fields_form_types();
    
    if( ! isset( $forms[ $scheme->post_status ] ) ) {
        echo __( "Inocrrect Form Type", "wpadverts-custom-fields" );
        return;
    }
    
    $data = $forms[$scheme->post_status];
    $data["scheme"] = $scheme;
    
    wp_enqueue_script( 'wpadverts-custom-fields-editor' );
    wp_enqueue_style( 'wpadverts-custom-fields-editor' );
    
    
    include $data["template"];
}


function _wpadverts_custom_fields_page_list() {
    
    $loop_args = array(
        'post_type' => 'wpadverts-form',
        'post_status' => null,
        'posts_per_page' => -1,
        'orderby' => array( 'title' => 'ASC' )
    );

    include dirname( ADVERTS_PATH ) . '/wpadverts-custom-fields/admin/options.php';
}

function _wpadverts_custom_fields_page_delete() {
    $post = get_post( adverts_request( 'delete' ) );

    if( !$post || $post->post_type != 'wpadverts-form' ) {
        wp_die(__('Form with given ID does not exist.', 'wpadverts-custom-fields'));
    }

    foreach( get_children( $post->ID ) as $child) {
        wp_delete_post( $child->ID, true );
    }

    $flash = Adverts_Flash::instance();
    $flash->add_info( __("Form deleted.", "wpadverts-custom-fields"));

    wp_delete_post( $post->ID, true );
    wp_redirect( remove_query_arg( array( 'delete', 'noheader', 'pg' ) ) );
    exit;
}

function wpadverts_custom_fields_name_exists( $value ) {
    
    $query = new WP_Query(array(
        "post_type" => "wpadverts-form",
        'post_status' => array_keys( wpadverts_custom_fields_form_types() ),
        "name" => $value
    ));
    
    if( $query->have_posts() ) {
        return "invalid";
    } else {
        return true;
    }
}

function _wpadverts_custom_fields_page_ds_list() {
    
    $options = get_option( "wpacf_data_source" );

    if( ! is_array( $options ) ) {
        $options = array();
    }
    
    include dirname( ADVERTS_PATH ) . '/wpadverts-custom-fields/admin/data-sources.php';
}

function _wpadverts_custom_fields_page_ds_add( $edit = false, $bind = array() ) {
    
    adverts_form_add_validator("wpadverts_custom_fields_data_source_exists", array(
        "callback" => "wpadverts_custom_fields_data_source_exists",
        "label" => "",
        "params" => array(),
        "default_error" => __( "Some other data source is already using this name.", "wpadverts-custom-fields" ),
        "validate_empty" => false
    ));
    
    adverts_form_add_field("wpadverts_custom_fields_ds_options", array(
        "renderer" => "wpadverts_custom_fields_ds_options",
        "callback_save" => null,
        "callback_bind" => null,
    ));
    
    $cpt_list = wpadverts_get_post_types();
    $cpt_list[] = "advert-author";
    $ds_taxonomy_options = array();
    
    foreach( $cpt_list as $cpt ) {
        $taxes = get_object_taxonomies( $cpt, "objects" );
        $post_data = get_post_type_object( $cpt );
        $ds_taxonomy_options[] = array(
            "value" => $cpt,
            "text" => $post_data->labels->name,
            "disabled" => 1,
            "depth" => 0
        );
        foreach( $taxes as $tax_key => $tax ) {
            $ds_taxonomy_options[] = array(
                "value" => $tax_key,
                "text" => $tax->label,
                "depth" => 1
            );
        }
    }

    $form_scheme = array(
        "name" => "custom_fields_ds_add",
        "action" => "",
        "field" => array(
            array(
                "name" => "ds_title",
                "type" => "adverts_field_text",
                "order" => 1,
                "label" => __( "Title", "wpadverts-custom-fields" ),
                "is_required" => true,
                "validator" => array(
                    array( "name" => "is_required" )
                )
            ),
            array(
                "name" => "ds_name",
                "type" => "adverts_field_text",
                "order" => 5,
                "label" => __( "Name ", "wpadverts-custom-fields" ),
                "is_required" => true,
                "validator" => array(
                    array( "name" => "is_required" ),
                    array( "name" => "wpadverts_custom_fields_data_source_exists" )
                ),
                "attr" => array(
                    "placeholder" => __("use: a-z, 0-9 and '-' only", "wpadverts-custom-fields")
                )
            ),
            array(
                "name" => "ds_type",
                "type" => "adverts_field_select",
                "order" => 10,
                "label" => __( "Type", "wpadverts-custom-fields" ),
                "is_required" => true,
                "max_choices" => 1,
                "empty_option" => true,
                "empty_option_text" => " ",
                "options" => array(
                    array( "value"=> "options", "text"=> __( "Predefined Options", "wpadverts-custom-fields" ) ),
                    array( "value"=> "callback", "text"=> __( "Callback Function", "wpadverts-custom-fields" ) ),
                    array( "value"=> "taxonomy", "text"=> __( "Taxonomy", "wpadverts-custom-fields" ) ),
                ),
                "validator" => array(
                    array( "name" => "is_required" ),
                )
            ),
            array(
                "name" => "ds_callback",
                "type" => "adverts_field_text",
                "order" => 15,
                "label" => __( "Callback", "wpadverts-custom-fields" ),
            ),
            array(
                "name" => "ds_taxonomy",
                "type" => "adverts_field_select",
                "order" => 20,
                "label" => __( "Taxonomy", "wpadverts-custom-fields" ),
                "empty_option" => true,
                "empty_option_text" => __( "Select Taxonomy ...", "wpadverts-custom-fields" ),
                "options" => $ds_taxonomy_options
            ),
            array(
                "name" => "ds_args",
                "type" => "adverts_field_text",
                "order" => 25,
                "label" => __( "Args", "wpadverts-custom-fields" )
            ),
            array(
                "name" => "ds_options",
                "type" => "wpadverts_custom_fields_ds_options",
                "order" => 30,
                "label" => __( "Options", "wpadverts-custom-fields" ),
                "is_required" => false,
                "validator" => array( ),
            ),
        )
    );
    
    wp_enqueue_style( 'adverts-admin' );
    wp_enqueue_script( 'wpadverts-custom-fields-data-source' );
    
    $flash = Adverts_Flash::instance();
    $soft_redirect = false;
    $error = array();
    
    $form = new Adverts_Form( $form_scheme );
    $form->bind( $bind );

    if(isset($_POST) && !empty($_POST)) {
        $form->bind( $_POST );
        $valid = $form->validate();

        if($valid) {
            
            $options = get_option( "wpacf_data_source" );
            
            if( ! is_array( $options ) ) {
                $options = array();
            }
            
            $ds_name = $form->get_value("ds_name");
            $ds_title = $form->get_value( "ds_title");
            $ds_type = $form->get_value( "ds_type" );
            $ds_callback = "";
            $ds_taxonomy = "";
            $ds_args = "";
            $ds_options = array();
            $ds_meta = array();
            
            if( $ds_type == "options" ) {
                if( is_array( adverts_request( "ds_option") ) ) {
                    $ds_options = array_values( adverts_request( "ds_option") );
                }
                if(adverts_request( "ds_options_use_values" ) == "1" ) {
                    $ds_meta["use_values"] = 1;
                } else {
                    $ds_meta["use_values"] = 0;
                }
            } else if( $ds_type == "callback" ) {
                $ds_callback = $form->get_value( "ds_callback" );
                $ds_args = $form->get_value( "ds_args" );
            } else if( $ds_type == "taxonomy" ) {
                $ds_taxonomy = $form->get_value( "ds_taxonomy" );
                $ds_args = $form->get_value( "ds_args" );
            }
            
            $insert = array(
                "name" => $ds_name,
                "title" => $ds_title,
                "type" => $ds_type,
                "callback" => $ds_callback,
                "taxonomy" => $ds_taxonomy,
                "options" => $ds_options,
                "args" => $ds_args,
                "meta" => $ds_meta
            );
            
            $options[ $ds_name ] = $insert;
            
            //echo "<pre>";print_r($options);echo "</pre>";
            
            update_option( "wpacf_data_source", $options );
            
            $m = __('Data Source Created. Loading Editor. If it is taking to long <a href="%s">click here</a>.', "wpadverts-custom-fields");
            
            $soft_redirect = add_query_arg( 'edit', $ds_name, remove_query_arg( 'add' ) );
            $flash->add_info( sprintf( $m, $soft_redirect ) );
        } else {
            $flash->add_error( __("There are errors in your form.", "wpadverts") );
        }
    }

    $button_text = __("Create Data Source", "wpadverts-custom-fields");
    
    include dirname( ADVERTS_PATH ) . '/wpadverts-custom-fields/admin/data-sources-add.php';
}


function _wpadverts_custom_fields_page_ds_edit() {
    
    $edit = adverts_request( "edit" );
    $options = get_option( "wpacf_data_source" );
    
    if( ! isset( $options[ $edit ] ) ) {
        $flash = Adverts_Flash::instance();
        $flash->add_error( __("Data source does not exist.", "wpadverts") );
        adverts_admin_flash( $flash );
        return;
    }
    
    $taxes = get_object_taxonomies( "advert", "objects" );
    $ds_taxonomy_options = array();
    foreach( $taxes as $tax_key => $tax ) {
        $ds_taxonomy_options[] = array(
            "value" => $tax_key,
            "text" => $tax->label
        );
    }
    
    $o = $options[ $edit ];
    
    $bind = array(
        "ds_name" => $o["name"],
        "ds_title" => $o["title"],
        "ds_type" => $o["type"],
        "ds_callback" => $o["callback"],
        "ds_taxonomy" => $o["taxonomy"],
        "ds_options" => $o["options"],
        "ds_args" => $o["args"],
        "ds_meta" => $o["meta"]
    );
    
    adverts_form_add_validator("wpadverts_custom_fields_data_source_exists", array(
        "callback" => "wpadverts_custom_fields_data_source_exists",
        "label" => "",
        "params" => array(),
        "default_error" => __( "Some other data source is already using this name.", "wpadverts-custom-fields" ),
        "validate_empty" => false
    ));
    
    adverts_form_add_field("wpadverts_custom_fields_ds_options", array(
        "renderer" => "wpadverts_custom_fields_ds_options",
        "callback_save" => null,
        "callback_bind" => null,
    ));
    
    $form_scheme = array(
        "name" => "custom_fields_ds_add",
        "action" => "",
        "field" => array(
            array(
                "name" => "ds_name",
                "type" => "adverts_field_text",
                "order" => 1,
                "label" => __( "Name ", "wpadverts-custom-fields" ),
                "is_required" => true,
                "validator" => array(
                    array( "name" => "is_required" ),
                ),
                "attr" => array(
                    "placeholder" => __("use: a-z, 0-9 and '-' only", "wpadverts-custom-fields"),
                    "readonly" => "readonly"
                )
            ),
            array(
                "name" => "ds_title",
                "type" => "adverts_field_text",
                "order" => 5,
                "label" => __( "Title", "wpadverts-custom-fields" ),
                "is_required" => true,
                "validator" => array(
                    array( "name" => "is_required" )
                )
            ),
            array(
                "name" => "ds_type",
                "type" => "adverts_field_select",
                "order" => 10,
                "label" => __( "Type", "wpadverts-custom-fields" ),
                "is_required" => true,
                "max_choices" => 1,
                "empty_option" => true,
                "empty_option_text" => " ",
                "options" => array(
                    array( "value"=> "options", "text"=> __( "Predefined Options", "wpadverts-custom-fields" ) ),
                    array( "value"=> "callback", "text"=> __( "Callback Function", "wpadverts-custom-fields" ) ),
                    array( "value"=> "taxonomy", "text"=> __( "Taxonomy", "wpadverts-custom-fields" ) ),
                ),
                "validator" => array(
                    array( "name" => "is_required" ),
                )
            ),
            array(
                "name" => "ds_callback",
                "type" => "adverts_field_text",
                "order" => 15,
                "label" => __( "Callback", "wpadverts-custom-fields" ),
            ),
            array(
                "name" => "ds_taxonomy",
                "type" => "adverts_field_select",
                "order" => 20,
                "label" => __( "Taxonomy", "wpadverts-custom-fields" ),
                "options" => $ds_taxonomy_options
            ),
            array(
                "name" => "ds_args",
                "type" => "adverts_field_text",
                "order" => 25,
                "label" => __( "Args", "wpadverts-custom-fields" )
            ),
            array(
                "name" => "ds_options",
                "type" => "wpadverts_custom_fields_ds_options",
                "order" => 30,
                "label" => __( "Options", "wpadverts-custom-fields" ),
                "is_required" => false,
                "validator" => array( ),
                "meta" => array(
                    "use_values" => isset( $o["meta"]["use_values"] ) ? $o["meta"]["use_values"] : 0,
                    "options" => isset( $o["options"] ) ? $o["options"] : array()
                )
            ),
        )
    );
    
    wp_enqueue_style( 'adverts-admin' );
    wp_enqueue_script( 'wpadverts-custom-fields-data-source' );
    
    $flash = Adverts_Flash::instance();
    $soft_redirect = false;
    $error = array();
    
    $form = new Adverts_Form( $form_scheme );
    $form->bind( $bind );

    if(isset($_POST) && !empty($_POST)) {
        $form->bind( $_POST );
        $valid = $form->validate();

        if($valid) {
            
            $options = get_option( "wpacf_data_source" );
            
            if( ! is_array( $options ) ) {
                $options = array();
            }
            
            $ds_name = $edit;
            $ds_title = $form->get_value( "ds_title");
            $ds_type = $form->get_value( "ds_type" );
            $ds_callback = "";
            $ds_taxonomy = "";
            $ds_args = "";
            $ds_options = array();
            $ds_meta = array();
            
            if( $ds_type == "options" ) {
                if( is_array( adverts_request( "ds_option") ) ) {
                    $ds_options = array_values( adverts_request( "ds_option") );
                }
                if(adverts_request( "ds_options_use_values" ) == "1" ) {
                    $ds_meta["use_values"] = 1;
                } else {
                    $ds_meta["use_values"] = 0;
                }
            } else if( $ds_type == "callback" ) {
                $ds_callback = $form->get_value( "ds_callback" );
                $ds_args = $form->get_value( "ds_args" );
            } else if( $ds_type == "taxonomy" ) {
                $ds_taxonomy = $form->get_value( "ds_taxonomy" );
                $ds_args = $form->get_value( "ds_args" );
            }
            
            $insert = array(
                "name" => $ds_name,
                "title" => $ds_title,
                "type" => $ds_type,
                "callback" => $ds_callback,
                "taxonomy" => $ds_taxonomy,
                "options" => $ds_options,
                "args" => $ds_args,
                "meta" => $ds_meta
            );
            
            $options[ $ds_name ] = $insert;
            
            //echo "<pre>";print_r($options);echo "</pre>";
            
            update_option( "wpacf_data_source", $options );
            
            $m = __('Data Source Update.', "wpadverts-custom-fields");
            
            $soft_redirect = add_query_arg( 'edit', $ds_name, remove_query_arg( 'add' ) );
            $flash->add_info( sprintf( $m, $soft_redirect ) );
        } else {
            $flash->add_error( __("There are errors in your form.", "wpadverts") );
        }
    }

    $button_text = __("Update Data Source", "wpadverts-custom-fields");
    
    include dirname( ADVERTS_PATH ) . '/wpadverts-custom-fields/admin/data-sources-edit.php';
}

function wpadverts_custom_fields_ds_options( $field ) {
    
    if( adverts_request( "ds_option" ) ) {
        $use_values = absint( adverts_request( "ds_options_use_values" ) );
    } else if( isset( $field["meta"]["use_values"] ) ) {
        $use_values = $field["meta"]["use_values"];
    } else {
        $use_values = 0;
    }
    
    if( adverts_request( "ds_option" ) && is_array( adverts_request( "ds_option" ) ) ) {
        $options = array_values( adverts_request( "ds_option" ) );
    } else if( isset( $field["meta"]["options"] ) ) {
        $options = $field["meta"]["options"];
    } else {
        $options = array();
    }
    
    ?>
    <div id="ds_options">
        <ul id="ds_options_list">
            
        </ul>
        <a href="#" id="ds_options_list_add" class="button-secondary"><?php _e( "Add Option", "wpadverts-custom-fields" ) ?></a>
        
        &nbsp;
        
        <label style="line-height: 29px;">
            <input type="checkbox" value="1" id="ds_options_use_values" name="ds_options_use_values" <?php checked( $use_values ) ?> /> <?php _e( "use values", "wpadverts-custom-fields" ) ?>
        </label>
    </div>

    <script type="text/javascript">
        var ds_options = <?php echo json_encode( $options );  ?>;
    </script>
    

    <?php
    
    add_action( "admin_footer", "wpadverts_custom_fields_ds_options_footer" );
}

function wpadverts_custom_fields_ds_options_footer() {
    ?>
    <div style="display: none">
        <li class="tmpl-cf-data-source-item">
            <input type="text" name="ds_option_value[]" class="wpadverts-multiselect-option-value" placeholder="<?php _e("value", "wpadverts-custom-fields") ?>" />
            <input type="text" name="ds_option_text[]" class="wpadverts-multiselect-option-text" placeholder="<?php _e("text", "wpadverts-custom-fields") ?>" />
            <input type="number" name="ds_option_depth[]" class="wpadverts-multiselect-option-depth" min="0" max="9" placeholder="<?php _e("depth", "wpadverts-custom-fields") ?>" title="<?php _e("Depth", "wpadverts-custom-fields") ?>" />
            <label>
                <input type="checkbox" name="ds_option_disabled[]" class="wpadverts-multiselect-option-disabled" value="1" />
                disabled &nbsp; 
            </label>
            <span class="wpadverts-multiselect-option-drag dashicons dashicons-move"></span>
            <span class="wpadverts-multiselect-option-trash dashicons dashicons-trash"></span>
        </li>
    </div>



    <style type="text/css">
        #ds_options_list {
            margin:0;
            padding:0;
        }
        
        .wpadverts-multiselect-option-drag {
            line-height: 34px;
        }
        .wpadverts-multiselect-option-trash {
            line-height: 34px;
        }
        .wpadverts-multiselect-option-drag:hover {
            cursor: move;
            color: #007cba;
        }
        .wpadverts-multiselect-option-trash:hover {
            cursor: pointer;
            color: #007cba;
        }
        .wpadverts-multiselect-option-depth {
            width:70px;
        }
    </style>
    <?php
}

function wpadverts_custom_fields_data_source_exists( $value ) {
    
    $ds_list = get_option( "wpacf_data_source" );
            
    if( ! is_array( $ds_list ) ) {
        return true;
    }
            
    if( isset( $ds_list[$value] ) ) {
        return "invalid";
    } else {
        return true;
    }
}

function _wpadverts_custom_fields_page_ds_delete() {
    
    $delete = adverts_request( "delete" );
    $ds_list = get_option( "wpacf_data_source" );

    if( ! is_array( $ds_list ) || ! isset( $ds_list[$delete] ) ) {
        wp_die(__('Data source with given ID does not exist.', 'wpadverts-custom-fields'));
    }

    foreach( get_children( $post->ID ) as $child) {
        wp_delete_post( $child->ID, true );
    }

    unset( $ds_list[ $delete ] );
    update_option( "wpacf_data_source", $ds_list );
    
    $flash = Adverts_Flash::instance();
    $flash->add_info( __("Data Source deleted.", "wpadverts-custom-fields"));

    wp_redirect( remove_query_arg( array( 'delete', 'noheader', 'pg' ) ) );
    exit;
}