Beispiel #1
0
function eme_convert_answer2tag($answer)
{
    $formfield = eme_get_formfield_byname($answer['field_name']);
    $field_info = $formfield['field_info'];
    $field_tags = $formfield['field_tags'];
    if (!empty($field_tags) && eme_is_multifield($formfield['field_type'])) {
        $answers = eme_convert_multi2array($answer['answer']);
        $values = eme_convert_multi2array($field_info);
        $tags = eme_convert_multi2array($field_tags);
        $my_arr = array();
        foreach ($answers as $ans) {
            foreach ($values as $key => $val) {
                if ($val == $ans) {
                    $my_arr[] = $tags[$key];
                }
            }
        }
        return eme_convert_array2multi($my_arr);
    } else {
        return $answer['answer'];
    }
}
function eme_formfields_page()
{
    global $wpdb;
    admin_show_warnings();
    if (!current_user_can(get_option('eme_cap_forms')) && (isset($_GET['eme_admin_action']) || isset($_POST['eme_admin_action']))) {
        $message = __('You have no right to update form fields!', 'eme');
        eme_formfields_table_layout($message);
        return;
    }
    if (isset($_GET['eme_admin_action']) && $_GET['eme_admin_action'] == "edit_formfield") {
        // edit formfield
        $field_id = intval($_GET['field_id']);
        eme_formfields_edit_layout($field_id);
        return;
    }
    // Insert/Update/Delete Record
    $formfields_table = $wpdb->prefix . FORMFIELDS_TBNAME;
    $validation_result = '';
    $message = '';
    if (isset($_POST['eme_admin_action'])) {
        if ($_POST['eme_admin_action'] == "do_editformfield") {
            $formfield = array();
            $field_id = intval($_POST['field_id']);
            $formfield['field_name'] = trim(stripslashes($_POST['field_name']));
            $formfield['field_type'] = intval($_POST['field_type']);
            $formfield['field_info'] = trim(stripslashes($_POST['field_info']));
            $formfield['field_tags'] = trim(stripslashes($_POST['field_tags']));
            $db_formfield = eme_get_formfield_byname($formfield['field_name']);
            if ($field_id && $db_formfield && $db_formfield['field_id'] != $field_id) {
                $message = __('Error: the field name must be unique.', 'eme');
                eme_formfields_edit_layout($field_id, $message);
                return;
            } elseif (eme_is_multifield($formfield['field_type']) && empty($formfield['field_info'])) {
                $message = __('Error: the field value can not be empty for this type of field.', 'eme');
                eme_formfields_edit_layout($field_id, $message);
                return;
            } elseif (eme_is_multifield($formfield['field_type']) && eme_is_multi($formfield['field_info']) && !empty($formfield['field_tags']) && count(eme_convert_multi2array($formfield['field_info'])) != count(eme_convert_multi2array($formfield['field_tags']))) {
                $message = __('Error: if you specify field tags, there need to be exact the same amount of tags as values.', 'eme');
                eme_formfields_edit_layout($field_id, $message);
                return;
            } else {
                $validation_result = $wpdb->update($formfields_table, $formfield, array('field_id' => $field_id));
                if ($validation_result !== false) {
                    $message = __("Successfully edited the field", "eme");
                }
            }
        } elseif ($_POST['eme_admin_action'] == "do_addformfield") {
            // Add a new formfield
            $formfield = array();
            $formfield['field_name'] = trim(stripslashes($_POST['field_name']));
            $formfield['field_type'] = intval($_POST['field_type']);
            $formfield['field_info'] = trim(stripslashes($_POST['field_info']));
            $formfield['field_tags'] = trim(stripslashes($_POST['field_tags']));
            if (eme_get_formfield_byname($formfield['field_name'])) {
                $message = __('Error: the field name must be unique.', 'eme');
                $validation_result = false;
            } elseif (eme_is_multifield($formfield['field_type']) && empty($formfield['field_info'])) {
                $message = __('Error: the field value can not be empty for this type of field.', 'eme');
                $validation_result = false;
            } elseif (eme_is_multifield($formfield['field_type']) && eme_is_multi($formfield['field_info']) && !empty($formfield['field_tags']) && count(eme_convert_multi2array($formfield['field_info'])) != count(eme_convert_multi2array($formfield['field_tags']))) {
                $message = __('Error: if you specify field tags, there need to be exact the same amount of tags as values.', 'eme');
                $validation_result = false;
            } else {
                $validation_result = $wpdb->insert($formfields_table, $formfield);
                if ($validation_result !== false) {
                    $message = __("Successfully added the field", "eme");
                }
            }
        } elseif ($_POST['eme_admin_action'] == "do_deleteformfield" && isset($_POST['formfields'])) {
            // Delete formfield or multiple
            $formfields = $_POST['formfields'];
            if (is_array($formfields)) {
                //Make sure the array is only numbers
                foreach ($formfields as $field_id) {
                    if (is_numeric($field_id)) {
                        $fields[] = $field_id;
                    }
                }
                //Run the query if we have an array of formfield ids
                if (count($fields > 0)) {
                    $validation_result = $wpdb->query("DELETE FROM {$formfields_table} WHERE field_id IN (" . implode(",", $fields) . ")");
                    if ($validation_result !== false) {
                        $message = __("Successfully deleted the field(s)", "eme");
                    }
                } else {
                    $validation_result = false;
                    $message = __("Couldn't delete the form fields. Incorrect field IDs supplied. Please try again.", "eme");
                }
            }
        }
        if ($validation_result !== false) {
            $message = isset($message) ? $message : __("Successfully {$_POST['eme_admin_action']}ed the field", "eme");
        } else {
            $message = isset($message) ? $message : __("There was a problem {$_POST['eme_admin_action']}ing the field, please try again.");
        }
    }
    eme_formfields_table_layout($message);
}