function cimy_save_options()
{
    global $wpdb, $cimy_uef_version, $wpdb_wp_fields_table, $max_length_fieldset_value, $cimy_uef_domain, $wp_hidden_fields, $max_length_extra_fields_title;
    if (!cimy_check_admin('manage_options')) {
        return;
    }
    if (isset($_POST['force_activation'])) {
        cimy_plugin_install();
        return;
    }
    if (!check_admin_referer('cimy_uef_options', 'cimy_uef_optionsnonce')) {
        return;
    }
    $results = array();
    $do_not_save_options = false;
    $options = cimy_get_options();
    $old_wp_hidden_fields = $options['wp_hidden_fields'];
    $options['aue_hidden_fields'] = array();
    $options['wp_hidden_fields'] = array();
    $options['welcome_email'] = stripslashes($_POST['welcome_email']);
    $options['extra_fields_title'] = stripslashes($_POST['extra_fields_title']);
    $options['extra_fields_title'] = substr($options['extra_fields_title'], 0, $max_length_extra_fields_title);
    $options['fieldset_title'] = stripslashes($_POST['fieldset_title']);
    $options['fieldset_title'] = substr($options['fieldset_title'], 0, $max_length_fieldset_value);
    $old_reg_log = $options['registration-logo'];
    $registration_logo = cimy_manage_upload("registration_logo", "", array(), empty($old_reg_log) ? false : basename($old_reg_log), isset($_POST['registration_logo_del']), "registration-logo");
    if (!empty($registration_logo) || isset($_POST['registration_logo_del'])) {
        $options['registration-logo'] = $registration_logo;
    }
    if (isset($_POST['db_wp_fields_check'])) {
        switch ($_POST['db_wp_fields']) {
            case 'empty':
                cimy_manage_db('empty_wp_fields');
                $results['empty_wp_fields'] = __("WordPress Fields table emptied", $cimy_uef_domain);
                break;
            case 'delete':
                cimy_manage_db('drop_wp_fields');
                $results['empty_wp_fields'] = __("WordPress Fields table deleted", $cimy_uef_domain);
                break;
        }
    }
    if (isset($_POST['db_extra_fields_check'])) {
        switch ($_POST['db_extra_fields']) {
            case 'empty':
                cimy_manage_db('empty_extra_fields');
                $results['empty_extra_fields'] = __("Extra Fields table emptied", $cimy_uef_domain);
                break;
            case 'delete':
                cimy_manage_db('drop_extra_fields');
                $results['empty_extra_fields'] = __("Extra Fields table deleted", $cimy_uef_domain);
                break;
        }
    }
    if (isset($_POST['db_data_check'])) {
        switch ($_POST['db_data']) {
            case 'empty':
                cimy_manage_db('empty_data');
                $results['empty_data'] = __("Users Data table emptied", $cimy_uef_domain);
                break;
            case 'delete':
                cimy_manage_db('drop_data');
                $results['empty_data'] = __("Users Data table deleted", $cimy_uef_domain);
                break;
        }
    }
    if (isset($_POST['db_options_check'])) {
        switch ($_POST['db_options']) {
            case 'default':
                cimy_manage_db('default_options');
                $do_not_save_options = true;
                $results['results'] = __("Options set to default values", $cimy_uef_domain);
                break;
            case 'delete':
                cimy_manage_db('drop_options');
                $do_not_save_options = true;
                $results['results'] = __("Options deleted", $cimy_uef_domain);
                break;
        }
    }
    if (isset($_POST['do_not_save_options'])) {
        $do_not_save_options = true;
    }
    if (isset($_POST['hide_username'])) {
        array_push($options['aue_hidden_fields'], 'username');
    }
    if (isset($_POST['hide_name'])) {
        array_push($options['aue_hidden_fields'], 'name');
    }
    if (isset($_POST['hide_posts'])) {
        array_push($options['aue_hidden_fields'], 'posts');
    }
    if (isset($_POST['hide_email'])) {
        array_push($options['aue_hidden_fields'], 'email');
    }
    if (isset($_POST['hide_website'])) {
        array_push($options['aue_hidden_fields'], 'website');
    }
    if (isset($_POST['hide_role'])) {
        array_push($options['aue_hidden_fields'], 'role');
    }
    $tot_wp_hidden_fields = count($old_wp_hidden_fields);
    $action = "add";
    isset($_POST['confirm_email']) ? $options['confirm_email'] = true : ($options['confirm_email'] = false);
    isset($_POST['confirm_form']) ? $options['confirm_form'] = true : ($options['confirm_form'] = false);
    if ($options['confirm_email']) {
        cimy_force_signup_table_creation();
    }
    isset($_POST['redirect_to']) ? $options['redirect_to'] = $_POST['redirect_to'] : ($options['redirect_to'] = "");
    isset($_POST['mail_include_fields']) ? $options['mail_include_fields'] = true : ($options['mail_include_fields'] = false);
    if (isset($_POST['captcha'])) {
        $options['captcha'] = $_POST['captcha'];
    }
    if (isset($_POST['recaptcha_public_key'])) {
        $options['recaptcha_public_key'] = trim($_POST['recaptcha_public_key']);
    }
    if (isset($_POST['recaptcha_private_key'])) {
        $options['recaptcha_private_key'] = trim($_POST['recaptcha_private_key']);
    }
    if (!isset($results['empty_wp_fields'])) {
        if (isset($_POST['show_wp_password'])) {
            array_push($options['wp_hidden_fields'], 'password');
            if (!in_array("password", $old_wp_hidden_fields)) {
                $data = $wp_hidden_fields['password'];
                $data['num_fields'] = $tot_wp_hidden_fields;
                $tot_wp_hidden_fields++;
                cimy_save_field($action, $wpdb_wp_fields_table, $data);
            }
            if (isset($_POST['show_wp_password2'])) {
                array_push($options['wp_hidden_fields'], 'password2');
                if (!in_array("password2", $old_wp_hidden_fields)) {
                    $data = $wp_hidden_fields['password2'];
                    $data['num_fields'] = $tot_wp_hidden_fields;
                    $tot_wp_hidden_fields++;
                    cimy_save_field($action, $wpdb_wp_fields_table, $data);
                }
            }
            isset($_POST['show_wp_password_meter']) ? $options['password_meter'] = true : ($options['password_meter'] = false);
        } else {
            $options['password_meter'] = false;
        }
        $db_wp_fields_independent = array("firstname", "lastname", "nickname", "website", "aim", "yahoo", "jgt", "bio-info");
        foreach ($db_wp_fields_independent as $wp_field_independent) {
            if (isset($_POST['show_wp_' . $wp_field_independent])) {
                array_push($options['wp_hidden_fields'], $wp_field_independent);
                if (!in_array($wp_field_independent, $old_wp_hidden_fields)) {
                    $data = $wp_hidden_fields[$wp_field_independent];
                    $data['num_fields'] = $tot_wp_hidden_fields;
                    $tot_wp_hidden_fields++;
                    cimy_save_field($action, $wpdb_wp_fields_table, $data);
                }
            }
        }
    }
    $all_wp_fields = get_cimyFields(true);
    $sql = "DELETE FROM " . $wpdb_wp_fields_table . " WHERE ";
    $k = -1;
    $j = -1;
    $msg = "";
    $not_del_old = "";
    $not_del_sql = "";
    foreach ($all_wp_fields as $wp_field) {
        $f_name = strtolower($wp_field['NAME']);
        $f_order = intval($wp_field['F_ORDER']);
        if (!in_array($f_name, $options['wp_hidden_fields'])) {
            if (in_array($f_name, $old_wp_hidden_fields)) {
                if ($k > -1) {
                    $sql .= " OR ";
                    $msg .= ", ";
                } else {
                    $k = $f_order;
                    $j = $f_order;
                }
                $sql .= "F_ORDER=" . $f_order;
                $msg .= $f_order;
            }
        } else {
            if ($j > -1) {
                if ($not_del_old != "") {
                    $not_del_old .= ", ";
                }
                $not_del_sql .= " WHEN " . $f_order . " THEN " . $j . " ";
                $not_del_old .= $f_order;
                $j++;
            }
        }
    }
    // if at least one field was selected
    if ($k > -1) {
        // $sql WILL BE: DELETE FROM <table> WHERE F_ORDER=<value1> [OR F_ORDER=<value2> ...]
        $wpdb->query($sql);
        if ($not_del_sql != "") {
            $not_del_sql = "UPDATE " . $wpdb_wp_fields_table . " SET F_ORDER=CASE F_ORDER" . $not_del_sql . "ELSE F_ORDER END WHERE F_ORDER IN(" . $not_del_old . ")";
            // $not_del_sql WILL BE: UPDATE <table> SET F_ORDER=CASE F_ORDER WHEN <oldvalue1> THEN <newvalue1> [WHEN ... THEN ...] ELSE F_ORDER END WHERE F_ORDER IN(<oldvalue1> [, <oldvalue2>...])
            $wpdb->query($not_del_sql);
        }
    }
    if (!$do_not_save_options) {
        cimy_set_options($options);
        $results['results'] = __("Options changed", $cimy_uef_domain);
    }
    return $results;
}
Exemplo n.º 2
0
function cimy_admin_define_extra_fields()
{
    global $wpdb, $wpdb_fields_table, $wpdb_wp_fields_table, $rule_canbeempty, $rule_email, $rule_maxlen, $rule_maxlen_needed, $available_types, $max_length_name, $max_length_label, $max_length_desc, $max_length_value, $max_size_file, $rule_equalto, $rule_equalto_case_sensitive, $cimy_uef_domain, $cuef_plugin_path, $cimy_uef_file_types, $rule_equalto_regex;
    if (!cimy_check_admin('manage_options')) {
        return;
    }
    // if (!empty($_POST))
    // 	print_r($_POST);
    $errors = array();
    $results = array();
    $add_caption = __("Add field", $cimy_uef_domain);
    $edit_caption = __("Update field", $cimy_uef_domain);
    $del_caption = __("Delete field", $cimy_uef_domain);
    $delSel_caption = __("Delete selected fields", $cimy_uef_domain);
    $order_caption = __("Change order", $cimy_uef_domain);
    $submit_msgs = array();
    $submit_msgs['add_caption'] = $add_caption;
    $submit_msgs['edit_caption'] = $edit_caption;
    $submit_msgs['del_caption'] = $del_caption;
    $submit_msgs['delSel_caption'] = $delSel_caption;
    $submit_msgs['order_caption'] = $order_caption;
    $min_length_caption = __("Min length", $cimy_uef_domain);
    $exact_length_caption = __("Exact length", $cimy_uef_domain);
    $max_length_caption = __("Max length", $cimy_uef_domain);
    $exact_or_max_length_capton = __("Exact or Max length", $cimy_uef_domain);
    $minLen = 1;
    $maxLen = $max_length_value;
    $submit = "";
    $action = "";
    // defaults to add
    $field_order = "0";
    // options form engine
    if (isset($_POST['cimy_options'])) {
        $action = "options";
        $res = cimy_save_options();
    } else {
        if (isset($_POST["submit_add"]) && !empty($_POST["submit_add"])) {
            $action = "add";
            $submit = $_POST["submit_add"];
        } else {
            if (isset($_POST["submit_edit"]) && !empty($_POST["submit_edit"])) {
                $action = "edit";
                $submit = $_POST["submit_edit"];
            } else {
                if (isset($_POST["submit_del"]) && !empty($_POST["submit_del"])) {
                    $action = "del";
                    $submit = $_POST["submit_del"];
                } else {
                    if (isset($_POST["submit_del_sel"]) && !empty($_POST["submit_del_sel"])) {
                        $action = "delSel";
                        $submit = $_POST["submit_del_sel"];
                    } else {
                        if (isset($_POST["submit_order"]) && !empty($_POST["submit_order"])) {
                            $action = "order";
                            $submit = $_POST["submit_order"];
                        }
                    }
                }
            }
        }
    }
    if (!isset($res)) {
        $res = array();
    }
    // call options draw function
    $opt_ret = cimy_show_options($res, true);
    // needed fields count here, after options and before $action manager! do not move!
    $num_fields = $opt_ret['db_extra_fields'];
    $num_wp_fields = $opt_ret['db_wp_fields'];
    if (isset($_POST['wp_fields'])) {
        $wp_fields_post = true;
        $fields_table = $wpdb_wp_fields_table;
        $tot_fields = $num_wp_fields;
    } else {
        $wp_fields_post = false;
        $fields_table = $wpdb_fields_table;
        $tot_fields = $num_fields;
    }
    // if pushed change order button
    if ($action == "order") {
        $sql = "UPDATE " . $fields_table . " SET F_ORDER=CASE F_ORDER";
        $k = -1;
        $msg = "";
        $msg_new = "";
        $arr1 = array();
        $arr2 = array();
        // check only selected fields
        foreach ($_POST['check'] as $i) {
            if ($k > -1) {
                $msg .= ", ";
                $msg_new .= ", ";
            } else {
                $k = $i;
            }
            $sql .= " WHEN " . $i . " THEN " . $_POST['order'][$i];
            $msg .= $i;
            $msg_new .= $_POST['order'][$i];
            array_push($arr1, $i);
            array_push($arr2, $_POST['order'][$i]);
        }
        if ($k > -1) {
            if (count(array_diff($arr1, $arr2)) == 0) {
                $sql .= " ELSE F_ORDER END WHERE F_ORDER IN (" . $msg . ")";
                // $sql WILL BE: UPDATE <table> SET F_ORDER=CASE F_ORDER WHEN <oldvalue1> THEN <newvalue1> [WHEN ... THEN ...] ELSE F_ORDER END WHERE F_ORDER IN(<oldvalue1> [, <oldvalue2>... ])
                $wpdb->query($sql);
                $results['order'] = __("Fields", $cimy_uef_domain) . " #" . $msg . " " . __("changed to", $cimy_uef_domain) . " #" . $msg_new;
            } else {
                $errors['order'] = __("You cannot give an order that misses some numbers", $cimy_uef_domain);
            }
        } else {
            $errors['order'] = __("Nothing selected", $cimy_uef_domain);
        }
    }
    // if pushed delete or update single button
    if ($action == "del" || $action == "edit") {
        $field_order = key($submit);
        // if pushed the single delete button then check the relative checkbox and let delSel code to delete it
        if ($action == "del") {
            $_POST['check'][$field_order] = $field_order;
            $action = "delSel";
        }
    }
    if ($action == "delSel") {
        $sql = "DELETE FROM " . $fields_table . " WHERE ";
        $sql_data_del = "";
        $k = -1;
        $j = -1;
        $msg = "";
        $not_del_old = "";
        $not_del_sql = "";
        // check which fields are selected for deletions
        for ($i = 1; $i <= $tot_fields; $i++) {
            if ($_POST['check'][$i]) {
                if ($k > -1) {
                    $sql .= " OR ";
                    $sql_data_del .= " OR ";
                    $msg .= ", ";
                } else {
                    $k = $i;
                    $j = $i;
                }
                $sql_data_del .= "FIELD_ID=" . $i;
                $sql .= "F_ORDER=" . $i;
                $msg .= $i;
            } else {
                // field to NOT be deleted, but order probably have to change, if j==(-1) then order is ok because deletions is after it!
                if ($j > -1) {
                    if ($not_del_old != "") {
                        $not_del_old .= ", ";
                    }
                    $not_del_sql .= " WHEN " . $i . " THEN " . $j . " ";
                    $not_del_old .= $i;
                    $j++;
                }
            }
        }
        // if at least one field was selected
        if ($k > -1) {
            // $sql WILL BE: DELETE FROM <table> WHERE F_ORDER=<value1> [OR F_ORDER=<value2> ...]
            $wpdb->query($sql);
            // delete also all data inserted by users in this/these field/s
            cimy_delete_users_info($sql_data_del);
            if ($not_del_sql != "") {
                $not_del_sql = "UPDATE " . $fields_table . " SET F_ORDER=CASE F_ORDER" . $not_del_sql . "ELSE F_ORDER END WHERE F_ORDER IN(" . $not_del_old . ")";
                // $not_del_sql WILL BE: UPDATE <table> SET F_ORDER=CASE F_ORDER WHEN <oldvalue1> THEN <newvalue1> [WHEN ... THEN ...] ELSE F_ORDER END WHERE F_ORDER IN(<oldvalue1> [, <oldvalue2>...])
                $wpdb->query($not_del_sql);
            }
            $results['delete'] = __("Field(s)", $cimy_uef_domain) . " #" . $msg . " " . __("deleted correctly", $cimy_uef_domain);
        } else {
            $errors['delete'] = __("Nothing selected", $cimy_uef_domain);
        }
    }
    if ($action == "add" || $action == "edit") {
        $store_rule = array();
        // RETRIEVE DATA FROM THE FORM
        $name = substr(stripslashes($_POST['name'][$field_order]), 0, $max_length_name);
        $value = substr(stripslashes($_POST['value'][$field_order]), 0, $max_length_value);
        $desc = substr(stripslashes($_POST['description'][$field_order]), 0, $max_length_desc);
        $label = substr(stripslashes($_POST['label'][$field_order]), 0, $max_length_label);
        $name = strtoupper($name);
        $oldname = strtoupper(stripslashes($_POST['oldname'][$field_order]));
        $type = $_POST['type'][$field_order];
        $fieldset = $_POST['fieldset'][$field_order];
        $minlen = $_POST['minlen'][$field_order];
        $exactlen = $_POST['exactlen'][$field_order];
        $maxlen = $_POST['maxlen'][$field_order];
        // min length available
        $minLen = 1;
        // max length or size for picture available
        if (in_array($type, $cimy_uef_file_types)) {
            $maxLen = $max_size_file;
            /* overwrite previous values */
            $min_length_caption = __("Min size", $cimy_uef_domain) . " (KB)";
            $exact_length_caption = __("Exact size", $cimy_uef_domain) . " (KB)";
            $max_length_caption = __("Max size", $cimy_uef_domain) . " (KB)";
            $exact_or_max_length_capton = __("Exact or Max size", $cimy_uef_domain) . " (KB)";
        } else {
            $maxLen = $max_length_value;
        }
        /* end overwrite previous values */
        if ($minlen != "") {
            $store_rule['min_length'] = intval($_POST['minlength'][$field_order]);
        }
        if ($exactlen != "") {
            $store_rule['exact_length'] = intval($_POST['exactlength'][$field_order]);
        }
        if ($maxlen != "") {
            $store_rule['max_length'] = intval($_POST['maxlength'][$field_order]);
        }
        $empty = $_POST['empty'][$field_order];
        $empty == "1" ? $store_rule['can_be_empty'] = true : ($store_rule['can_be_empty'] = false);
        $store_rule['edit'] = $_POST['edit'][$field_order];
        $email = $_POST['email'][$field_order];
        $email == "1" ? $store_rule['email'] = true : ($store_rule['email'] = false);
        $equal = $_POST['equal'][$field_order];
        if ($equal != "") {
            $store_rule['equal_to'] = stripslashes($_POST['equalto'][$field_order]);
            $equalto_casesens = $_POST['equalto_casesens'][$field_order];
            $equalto_regex = $_POST['equalto_regex'][$field_order];
        }
        $show_in_reg = $_POST['show_in_reg'][$field_order];
        $show_in_reg == "1" ? $store_rule['show_in_reg'] = true : ($store_rule['show_in_reg'] = false);
        $show_in_profile = $_POST['show_in_profile'][$field_order];
        $show_in_profile == "1" ? $store_rule['show_in_profile'] = true : ($store_rule['show_in_profile'] = false);
        $show_in_aeu = $_POST['show_in_aeu'][$field_order];
        $show_in_aeu == "1" ? $store_rule['show_in_aeu'] = true : ($store_rule['show_in_aeu'] = false);
        $show_in_search = $_POST['show_in_search'][$field_order];
        $show_in_search == "1" ? $store_rule['show_in_search'] = true : ($store_rule['show_in_search'] = false);
        $show_in_blog = $_POST['show_in_blog'][$field_order];
        $show_in_blog == "1" ? $store_rule['show_in_blog'] = true : ($store_rule['show_in_blog'] = false);
        $show_level = $_POST['show_level'][$field_order];
        $store_rule['show_level'] = $show_level;
        // START CHECKING FOR ERRORS
        if ($name == "") {
            $errors['name'] = __("Name not specified", $cimy_uef_domain);
        } else {
            if (!stristr($name, " ") === false) {
                $errors['name'] = __("Name cannot contains spaces", $cimy_uef_domain);
            }
        }
        if ($label == "") {
            $errors['label'] = __("Label not specified", $cimy_uef_domain);
        }
        // max or exact length rule is needed for this type
        if (in_array($type, $rule_maxlen_needed)) {
            if ($maxlen == "" && $exactlen == "") {
                $errors['maxlength1'] = $exact_or_max_length_capton . " " . __("not selected (with this type is necessary)", $cimy_uef_domain);
            }
        }
        // max or exact length rule is not needed but it's available for this type
        if (in_array($type, $rule_maxlen)) {
            if (($maxlen != "" || $minlen != "") && $exactlen != "") {
                $errors['exactlength1'] = __("If you select", $cimy_uef_domain) . " " . $exact_length_caption . " " . __("you cannot select Min or Max", $cimy_uef_domain);
            }
            // MIN LEN
            if ($minlen != "") {
                if ($store_rule['min_length'] < $minLen || $store_rule['min_length'] > $maxLen) {
                    $errors['minlength3'] = $min_length_caption . " " . __("should be in the range of", $cimy_uef_domain) . " " . $minLen . "-" . $maxLen;
                }
            }
            // EXACT LEN
            if ($exactlen != "") {
                if ($store_rule['exact_length'] < $minLen || $store_rule['exact_length'] > $maxLen) {
                    $errors['exactlength3'] = $exact_length_caption . " " . __("should be in the range of", $cimy_uef_domain) . " " . $minLen . "-" . $maxLen;
                }
            }
            // MAX LEN
            if ($maxlen != "") {
                if ($store_rule['max_length'] < $minLen || $store_rule['max_length'] > $maxLen) {
                    $errors['maxlength3'] = $max_length_caption . " " . __("should be in the range of", $cimy_uef_domain) . " " . $minLen . "-" . $maxLen;
                }
            }
        } else {
            $minlen = "";
            $exactlen = "";
            $maxlen = "";
        }
        if ($equal != "") {
            if (!isset($store_rule['equal_to'])) {
                $errors['equalTo'] = __("Equal TO not specified", $cimy_uef_domain);
            } else {
                if ($store_rule['equal_to'] == "") {
                    $errors['equalTo'] = __("Equal TO not specified", $cimy_uef_domain);
                } else {
                    if (strtoupper($store_rule['equal_to']) != "YES" && strtoupper($store_rule['equal_to']) != "NO") {
                        if ($type == "checkbox") {
                            $errors['equalTo2'] = __("With checkbox type Equal TO can only be", $cimy_uef_domain) . ": [Yes, No]";
                        }
                        if ($type == "radio") {
                            $errors['equalTo2'] = __("With radio type Equal TO can only be", $cimy_uef_domain) . ": [Yes, No]";
                        }
                    }
                }
            }
            if ($equalto_casesens != "" && in_array($type, $rule_equalto_case_sensitive)) {
                $store_rule['equal_to_case_sensitive'] = true;
            } else {
                $store_rule['equal_to_case_sensitive'] = false;
            }
            if ($equalto_regex != "" && in_array($type, $rule_equalto_regex)) {
                $store_rule['equal_to_regex'] = true;
            } else {
                $store_rule['equal_to_regex'] = false;
            }
        }
        if ($value != "" && strtoupper($value) != "YES" && strtoupper($value) != "NO") {
            if ($type == "checkbox") {
                $errors['value'] = __("With checkbox type Value can only be", $cimy_uef_domain) . ": [Yes, No]";
            }
            if ($type == "radio") {
                $errors['value'] = __("With radio type Value can only be", $cimy_uef_domain) . ": [Yes, No]";
            }
        }
        if (is_multisite()) {
            // uploading files not supported with WordPress MU
            if (in_array($type, $cimy_uef_file_types)) {
                $store_rule["show_in_reg"] = false;
            }
        }
        // IF THERE ARE NO ERRORS THEN GO ON
        if (count($errors) == 0) {
            $exist = array();
            if ($type != "radio") {
                $sql1 = "SELECT id FROM " . $fields_table . " WHERE name='" . $wpdb->escape($name) . "' LIMIT 1";
                $exist = $wpdb->get_row($sql1);
            }
            // SEARCH THE NAME IN THE DATABASE, GO ON ONLY IF DURING EDIT IT WAS THE SAME FIELD
            if (count($exist) == 0 || $action == "edit" && $oldname == $name) {
                // MIN LEN
                if (!in_array($type, $rule_maxlen)) {
                    unset($store_rule['min_length']);
                }
                // EXACT LEN
                if (!in_array($type, $rule_maxlen)) {
                    unset($store_rule['exact_length']);
                }
                // MAX LEN
                if (!in_array($type, $rule_maxlen)) {
                    unset($store_rule['max_length']);
                }
                if (!in_array($type, $rule_email)) {
                    $store_rule['email'] = false;
                }
                if (!in_array($type, $rule_canbeempty)) {
                    $store_rule['can_be_empty'] = true;
                }
                if ($type == "checkbox" || $type == "radio") {
                    $value = strtoupper($value);
                }
                $data = array();
                $data['name'] = $name;
                $data['value'] = $value;
                $data['desc'] = $desc;
                $data['label'] = $label;
                $data['type'] = $type;
                $data['store_rule'] = $store_rule;
                $data['field_order'] = $field_order;
                $data['num_fields'] = $num_fields;
                $data['fieldset'] = $fieldset;
                cimy_save_field($action, $fields_table, $data);
                if ($action == "add") {
                    $results['inserted'] = __("Field inserted correctly", $cimy_uef_domain);
                } else {
                    if ($action == "edit") {
                        $results['edit'] = __("Field #", $cimy_uef_domain) . $field_order . " " . __("updated correctly", $cimy_uef_domain);
                    }
                }
            } else {
                $errors['namedup'] = __("Name inserted is just in the database, change to another one", $cimy_uef_domain);
            }
        }
    }
    // if extra fields table is not present
    if ($num_fields == -1) {
        exit;
    }
    // do NOT move this line, it's here because should shows also fields just added to the database
    $allFields = get_cimyFields();
    ?>

	<div class="wrap" id="addfield">
	<h2><?php 
    _e("Add a new Field", $cimy_uef_domain);
    ?>
</h2>

	<?php 
    // print errors if there are some
    cimy_uef_print_messages($errors, $results);
    if ($store_rule['min_length'] == 0) {
        unset($store_rule['min_length']);
    }
    if ($store_rule['exact_length'] == 0) {
        unset($store_rule['exact_length']);
    }
    if ($store_rule['max_length'] == 0) {
        unset($store_rule['max_length']);
    }
    // SHOW LEVEL
    $show_anonymous = '';
    $show_subscriber = '';
    $show_contributor = '';
    $show_author = '';
    $show_editor = '';
    $show_admin = '';
    if ($action == "add") {
        // CAN BE MODIFIED OR NOT
        $selected_input[$store_rule['edit']] = ' selected="selected"';
        // NAME
        if ($name != "") {
            $selected_input["name"] = $name;
        } else {
            $selected_input["name"] = '';
        }
        // LABEL
        if ($label != "") {
            $selected_input["label"] = $label;
        } else {
            $selected_input["label"] = '';
        }
        // VALUE
        if ($value != "") {
            $selected_input["value"] = $value;
        } else {
            $selected_input["value"] = '';
        }
        // DESCRIPTION
        if ($desc != "") {
            $selected_input["desc"] = $desc;
        } else {
            $selected_input["desc"] = '';
        }
        // TYPE
        if ($type != "") {
            $selected_input[$type] = ' selected="selected"';
        } else {
            $selected_input["text"] = ' selected="selected"';
        }
        // MIN LEN
        if ($minlen != "") {
            $selected_input["minlen"] = ' checked="checked"';
        } else {
            $selected_input["minlen"] = '';
        }
        if (isset($store_rule['min_length'])) {
            $selected_input["min_length"] = $store_rule['min_length'];
        } else {
            $selected_input["min_length"] = '';
        }
        // EXACT LEN
        if ($exactlen != "") {
            $selected_input["exactlen"] = ' checked="checked"';
        } else {
            $selected_input["exactlen"] = '';
        }
        if (isset($store_rule['exact_length'])) {
            $selected_input["exact_length"] = $store_rule['exact_length'];
        } else {
            $selected_input["exact_length"] = '';
        }
        // MAX LEN
        if ($maxlen != "") {
            $selected_input["maxlen"] = ' checked="checked"';
        } else {
            $selected_input["maxlen"] = '';
        }
        if (isset($store_rule['max_length'])) {
            $selected_input["max_length"] = $store_rule['max_length'];
        } else {
            $selected_input["max_length"] = '';
        }
        // EQUAL TO
        if (isset($equal)) {
            $selected_input["equal"] = ' checked="checked"';
        } else {
            $selected_input["equal"] = '';
        }
        if (isset($store_rule['equal_to'])) {
            $selected_input["equal_to"] = $store_rule['equal_to'];
        } else {
            $selected_input["equal_to"] = '';
        }
        if (isset($equalto_casesens)) {
            $selected_input["equal_to_case_sensitive"] = ' checked="checked"';
        } else {
            $selected_input["equal_to_case_sensitive"] = '';
        }
        if (isset($equalto_regex)) {
            $selected_input["equal_to_regex"] = ' checked="checked"';
        } else {
            $selected_input["equal_to_regex"] = '';
        }
        // CHECK EMAIL SYNTAX
        if ($store_rule['email'] == true) {
            $selected_input["email"] = ' checked="checked"';
        } else {
            $selected_input["email"] = '';
        }
        // SHOW LEVEL
        switch ($store_rule['show_level']) {
            case '-1':
                $show_anonymous = ' selected="selected"';
                break;
            case '0':
                $show_subscriber = ' selected="selected"';
                break;
            case '1':
                $show_contributor = ' selected="selected"';
                break;
            case '2':
                $show_author = ' selected="selected"';
                break;
            case '5':
                $show_editor = ' selected="selected"';
                break;
            case '8':
                $show_admin = ' selected="selected"';
                break;
        }
    } else {
        $selected_input["ok_edit"] = ' selected="selected"';
        $show_anonymous = ' selected="selected"';
    }
    // CAN BE EMPTY
    if ($store_rule['can_be_empty'] == true || $action != "add") {
        $selected_input["empty"] = ' checked="checked"';
    } else {
        $selected_input["empty"] = '';
    }
    // SHOW IN REGISTRATION
    if (!isset($store_rule['show_in_reg']) || $store_rule['show_in_reg'] == true || $action != "add") {
        $selected_input["show_in_reg"] = ' checked="checked"';
    } else {
        $selected_input["show_in_reg"] = '';
    }
    // SHOW IN PROFILE
    if (!isset($store_rule['show_in_profile']) || $store_rule['show_in_profile'] == true || $action != "add") {
        $selected_input["show_in_profile"] = ' checked="checked"';
    } else {
        $selected_input["show_in_profile"] = '';
    }
    // SHOW IN AUTHORS AND USERS EXTENDED
    if (!isset($store_rule['show_in_aeu']) || $store_rule['show_in_aeu'] == true || $action != "add") {
        $selected_input["show_in_aeu"] = ' checked="checked"';
    } else {
        $selected_input["show_in_aeu"] = '';
    }
    // SHOW IN THE SEARCH
    if (!isset($store_rule['show_in_search']) || $store_rule['show_in_search'] == true || $action != "add") {
        $selected_input["show_in_search"] = ' checked="checked"';
    } else {
        $selected_input["show_in_search"] = '';
    }
    // SHOW IN THE BLOG
    if (!isset($store_rule['show_in_blog']) || $store_rule['show_in_blog'] == true || $action != "add") {
        $selected_input["show_in_blog"] = ' checked="checked"';
    } else {
        $selected_input["show_in_blog"] = '';
    }
    $selected_input["name"] = esc_attr($selected_input["name"]);
    $selected_input["value"] = esc_attr($selected_input["value"]);
    $selected_input["label"] = esc_attr($selected_input["label"]);
    $selected_input["desc"] = esc_attr($selected_input["desc"]);
    $selected_input["equal_to"] = esc_attr($selected_input["equal_to"]);
    ?>
	
	<form method="post" action="#addfield">
		<p><?php 
    _e("To add a new field you have to choose a name, type and label; optional are value and description. Rules are applied during user registration.", $cimy_uef_domain);
    ?>
</p>
		<ul>
			<li><?php 
    _e("With <strong>radio</strong> and <strong>checkbox</strong>: <em>Value</em> and <em>equal TO</em> can only be 'Yes' or 'No' that means 'selected' or 'not selected'", $cimy_uef_domain);
    ?>
</li>
			<li><?php 
    _e("With <strong>drop-down</strong>: you have to add all options into label for example: label/item1,item2,item3", $cimy_uef_domain);
    ?>
</li>
			<li><?php 
    _e("With <strong>picture</strong>: you can preload a default image putting url in <em>Value</em>; 'min,exact,max size' are in KB; <em>equal TO</em> means max pixel size (width or height) for thumbnail", $cimy_uef_domain);
    ?>
</li>
			<li><?php 
    _e("With <strong>picture-url</strong>: you can preload a default image putting url in <em>Value</em>; <em>equal TO</em> means max width pixel size (height will be proportional)", $cimy_uef_domain);
    ?>
</li>
			<li><?php 
    _e("With <strong>registration-date</strong>: <em>equal TO</em> means date and time format", $cimy_uef_domain);
    ?>
</li>
			<li><?php 
    _e("With <strong>avatar</strong>: you can preload a default image putting url in <em>Value</em>; 'min,exact,max size' are in KB; <em>equal TO</em> is automatically set to 512 pixels", $cimy_uef_domain);
    ?>
</li>
			<li><?php 
    _e("With <strong>file</strong>: you can preload a default file putting url in <em>Value</em>; 'min,exact,max size' are in KB; under <em>equal TO</em> can be specified allowed extensions separated by comma, example: zip,pdf,doc", $cimy_uef_domain);
    ?>
</li>
		</ul>
		<br />

		<table  class="widefat" cellpadding="10">
		<thead align="center">
		<tr>
			<th><h3 style="text-align: center;"><?php 
    _e("Name");
    ?>
 - <?php 
    _e("Value");
    ?>
</h3></th>
			<th><h3 style="text-align: center;"><?php 
    _e("Type", $cimy_uef_domain);
    ?>
</h3></th>
			<th><h3 style="text-align: center;"><?php 
    _e("Label", $cimy_uef_domain);
    ?>
 - <?php 
    _e("Description");
    ?>
</h3></th>
			<th><h3 style="text-align: center;"><?php 
    _e("Rules", $cimy_uef_domain);
    ?>
</h3></th>
			<th><h3 style="text-align: center;"><?php 
    _e("Actions");
    ?>
</h3></th>
		</tr>
		</thead>
		<tbody id="plugins" class="plugins">
		<tr class="active">
		<td style="vertical-align: middle;">
			<label><strong><?php 
    _e("Name");
    ?>
</strong><br /><input name="name[0]" type="text" value="<?php 
    echo $selected_input["name"];
    ?>
" maxlength="<?php 
    echo $max_length_name;
    ?>
" /></label><br /><br />
			<label><strong><?php 
    _e("Value");
    ?>
</strong><br /><textarea name="value[0]" rows="2" cols="17"><?php 
    echo $selected_input["value"];
    ?>
</textarea></label>
		</td>
		<td style="vertical-align: middle;">
			<label><strong><?php 
    _e("Type", $cimy_uef_domain);
    ?>
</strong><br />
			<select name="type[0]">
			<?php 
    foreach ($available_types as $this_type) {
        echo '<option value="' . $this_type . '"' . $selected_input[$this_type] . '>' . $this_type . '</option>';
        echo "\n";
        if (isset($selected_input[$this_type])) {
            unset($selected_input[$this_type]);
        }
    }
    ?>
			</select>
			</label>
			<br /><br />
			<label><strong><?php 
    _e("Fieldset", $cimy_uef_domain);
    ?>
</strong><br />
			<?php 
    echo cimy_fieldsetOptions($fieldset, "0");
    ?>
			</label>
		</td>
		<td style="vertical-align: middle;">
			<label><strong><?php 
    _e("Label", $cimy_uef_domain);
    ?>
</strong><br /><textarea name="label[0]" rows="2" cols="18"><?php 
    echo $selected_input["label"];
    ?>
</textarea></label><br /><br />
			<label><strong><?php 
    _e("Description");
    ?>
</strong><br /><textarea name="description[0]" rows="4" cols="18"><?php 
    echo $selected_input["desc"];
    ?>
</textarea></label>
		</td>
		<td style="vertical-align: middle;">
			<!-- MIN LENGTH -->
			<input type="checkbox" name="minlen[0]" value="1"<?php 
    echo $selected_input["minlen"];
    ?>
 /> <?php 
    echo $min_length_caption;
    ?>
 [1-<?php 
    echo $maxLen;
    ?>
]: &nbsp;&nbsp;&nbsp;<input type="text" name="minlength[0]" value="<?php 
    echo $selected_input["min_length"];
    ?>
" maxlength="5" size="5" /><br />
			
			<!-- EXACT LENGTH -->
			<input type="checkbox" name="exactlen[0]" value="1"<?php 
    echo $selected_input["exactlen"];
    ?>
 /> <?php 
    echo $exact_length_caption;
    ?>
 [1-<?php 
    echo $maxLen;
    ?>
]: <input type="text" name="exactlength[0]" value="<?php 
    echo $selected_input["exact_length"];
    ?>
" maxlength="5" size="5" /><br />

			<!-- MAX LENGTH -->
			<input type="checkbox" name="maxlen[0]" value="1"<?php 
    echo $selected_input["maxlen"];
    ?>
 /> <?php 
    echo $max_length_caption;
    ?>
 [1-<?php 
    echo $maxLen;
    ?>
]: &nbsp;&nbsp;<input type="text" name="maxlength[0]" value="<?php 
    echo $selected_input["max_length"];
    ?>
" maxlength="5" size="5" /><br />
			
			<input type="checkbox" name="empty[0]" value="1"<?php 
    echo $selected_input["empty"];
    ?>
 /> <?php 
    _e("Can be empty", $cimy_uef_domain);
    ?>
<br />
			<input type="checkbox" name="email[0]" value="1"<?php 
    echo $selected_input["email"];
    ?>
 /> <?php 
    _e("Check for E-mail syntax", $cimy_uef_domain);
    ?>
<br />
			
			<select name="edit[0]">
				<option value="ok_edit"<?php 
    echo $selected_input["ok_edit"];
    ?>
><?php 
    _e("Can be modified", $cimy_uef_domain);
    ?>
</option>
				<option value="edit_only_if_empty"<?php 
    echo $selected_input["edit_only_if_empty"];
    ?>
><?php 
    _e("Can be modified only if empty", $cimy_uef_domain);
    ?>
</option>
				<option value="edit_only_by_admin"<?php 
    echo $selected_input["edit_only_by_admin"];
    ?>
><?php 
    _e("Can be modified only by admin", $cimy_uef_domain);
    ?>
</option>
				<option value="edit_only_by_admin_or_if_empty"<?php 
    echo $selected_input["edit_only_by_admin_or_if_empty"];
    ?>
><?php 
    _e("Can be modified only by admin or if empty", $cimy_uef_domain);
    ?>
</option>
				<option value="no_edit"<?php 
    echo $selected_input["no_edit"];
    ?>
><?php 
    _e("Cannot be modified", $cimy_uef_domain);
    ?>
</option>
			<?php 
    if (isset($selected_input[$edit])) {
        unset($selected_input[$edit]);
    }
    ?>
			</select>
			<br />
			<!-- EQUAL TO -->
			<input type="checkbox" name="equal[0]" value="1"<?php 
    echo $selected_input["equal"];
    ?>
 /> <?php 
    _e("Should be equal TO", $cimy_uef_domain);
    ?>
: <input type="text" name="equalto[0]" maxlength="100" value="<?php 
    echo $selected_input["equal_to"];
    ?>
"/><br />
			<!-- CASE SENSITIVE -->
			&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="equalto_casesens[0]" value="1"<?php 
    echo $selected_input["equal_to_case_sensitive"];
    ?>
 /> <?php 
    _e("Case sensitive", $cimy_uef_domain);
    ?>
<br />

			<!-- REGEX -->
			&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" name="equalto_regex[0]" value="1"<?php 
    echo $selected_input["equal_to_regex"];
    ?>
 /> <?php 
    _e("Regular Expression", $cimy_uef_domain);
    ?>
<br />
			
			<!-- SHOW IN REGISTRATION -->
			<input type="checkbox" name="show_in_reg[0]" value="1"<?php 
    echo $selected_input["show_in_reg"];
    ?>
 /> <?php 
    _e("Show the field in the registration", $cimy_uef_domain);
    ?>
<br />
			
			<!-- SHOW IN PROFILE -->
			<input type="checkbox" name="show_in_profile[0]" value="1"<?php 
    echo $selected_input["show_in_profile"];
    ?>
 /> <?php 
    _e("Show the field in User's profile", $cimy_uef_domain);
    ?>
<br />
			
			<!-- SHOW IN A&U EXTENDED -->
			<input type="checkbox" name="show_in_aeu[0]" value="1"<?php 
    echo $selected_input["show_in_aeu"];
    ?>
 /> <?php 
    _e("Show the field in A&amp;U Extended menu", $cimy_uef_domain);
    ?>
<br />

			<!-- SHOW IN THE SEARCH ENGINE -->
			<input type="checkbox" name="show_in_search[0]" value="1"<?php 
    echo $selected_input["show_in_search"];
    ?>
 /> <?php 
    _e("Show the field in the search engine", $cimy_uef_domain);
    ?>
<br />

			<!-- SHOW IN THE BLOG -->
			<input type="checkbox" name="show_in_blog[0]" value="1"<?php 
    echo $selected_input["show_in_blog"];
    ?>
 /> <?php 
    _e("Show the field in the blog", $cimy_uef_domain);
    ?>
<br />

			<!-- SHOW SECURITY LEVEL -->
			<?php 
    _e("Show the field if the role is at least:", $cimy_uef_domain) . " ";
    ?>
			<select name="show_level[0]">
			<option value="-1"<?php 
    echo $show_anonymous;
    ?>
><?php 
    _e("Anonymous");
    ?>
</option>
			<option value="0"<?php 
    echo $show_subscriber;
    ?>
><?php 
    echo translate_user_role("Subscriber");
    ?>
</option>
			<option value="1"<?php 
    echo $show_contributor;
    ?>
><?php 
    echo translate_user_role("Contributor");
    ?>
</option>
			<option value="2"<?php 
    echo $show_author;
    ?>
><?php 
    echo translate_user_role("Author");
    ?>
</option>
			<option value="5"<?php 
    echo $show_editor;
    ?>
><?php 
    echo translate_user_role("Editor");
    ?>
</option>
			<option value="8"<?php 
    echo $show_admin;
    ?>
><?php 
    echo translate_user_role("Administrator");
    ?>
</option>
			</select>
			<br />
		</td>
		<td align="center" style="vertical-align: middle;">
			<p class="submit" style="border-width: 0px;">
			<input name="reset" type="reset" value="<?php 
    _e("Clear", $cimy_uef_domain);
    ?>
" /><br /><br />
			<input class="button-primary" name="submit_add[0]" type="submit" value="<?php 
    echo $add_caption;
    ?>
" />
			</p>
		</td>
		</tr>
		</tbody>
		</table>
		<br /><br />
	</form>

	</div>

<script type="text/javascript">
<!--//
function changeFormAction(form_id, tr_id) {
    var element = document.getElementById(form_id);
    element.action = '#'+tr_id;
}
//-->
</script>

<?php 
    wp_print_scripts("cimy_uef_invert_sel");
    $wp_fields = get_cimyFields(true);
    cimy_admin_show_extra_fields($wp_fields, $submit_msgs, true, $errors, $results, $wp_fields_post, $field_order);
    cimy_admin_show_extra_fields($allFields, $submit_msgs, false, $errors, $results, $wp_fields_post, $field_order);
}