<select name='formbuilderfields[<?php 
        echo $fields['id'];
        ?>
][field_type]' 
										id='field_type' 
										alt='<?php 
        _e('Select the type of field that you wish to have shown in this location.', 'formbuilder');
        ?>
' 
										title='<?php 
        _e('Select the type of field that you wish to have shown in this location.', 'formbuilder');
        ?>
'>
										<?php 
        $help_text_html = "";
        $field_types = formbuilder_get_field_types();
        foreach ($field_types as $field_type => $help_text) {
            if ($fields[$key] == $field_type) {
                $selected = "selected";
            } else {
                $selected = "";
            }
            echo "\n<option value='{$field_type}' {$selected}>{$field_type}</option>";
            $help_text_html .= "<strong>{$field_type}:</strong> {$help_text}<br/>\n";
        }
        ?>
									</select>
								</span>
								<a href='javascript:;' class='formBuilderHelpTextToggle' 
									onClick='toggleVis("formBuilderHelpTextField<?php 
        echo $fields['id'];
 private function showTheEditField(FBField $field)
 {
     static $t;
     if (!$t) {
         $t = new FBTemplatizer('field_edit.phtml');
     }
     $t->clear();
     $t->set('FIELD_ID', $field->getId());
     $t->set('FIELD_NAME', htmlentities($field->getName(), ENT_QUOTES, get_option('blog_charset')));
     $t->set('FIELD_VALUE', htmlentities($field->getValue(), ENT_QUOTES, get_option('blog_charset')));
     $t->set('FIELD_LABEL', htmlentities($field->getLabel(), ENT_QUOTES, get_option('blog_charset')));
     $t->set('FIELD_ERROR', htmlentities($field->getError(), ENT_QUOTES, get_option('blog_charset')));
     $t->set('FIELD_HELP', htmlentities($field->getHelp(), ENT_QUOTES, get_option('blog_charset')));
     $all_field_types = formbuilder_get_field_types();
     $html = '';
     $help = '';
     foreach ($all_field_types as $key => $value) {
         if ($key == $field->getType()) {
             $selected = "selected = 'selected'";
         } else {
             $selected = "";
         }
         $html .= "<option value='{$key}' {$selected}>{$key}</option>";
         $help .= "{$key}: {$value}\\n";
     }
     $t->set('FIELD_TYPE_SELECT', $html);
     $t->set('FIELD_TYPE_HELP', htmlentities($help, ENT_QUOTES, get_option('blog_charset')));
     $all_required_types = formbuilder_get_required_types();
     $html = '';
     $help = '';
     foreach ($all_required_types as $key => $value) {
         if ($key == $field->getRequired()) {
             $selected = "selected = 'selected'";
         } else {
             $selected = "";
         }
         $html .= "<option value='{$key}' {$selected}>{$key}</option>";
         $help .= "{$key}: {$value}\\n";
     }
     $t->set('FIELD_REQUIRED_SELECT', $html);
     $t->set('FIELD_REQUIRED_HELP', htmlentities($help, ENT_QUOTES, get_option('blog_charset')));
     return $t->parse();
 }
/**
 * Old form editing controls.
 * @param unknown_type $form_id
 */
function formbuilder_options_editForm($form_id)
{
    global $wpdb, $formbuilder_admin_nav_options;
    /*
     * Permissions control.  Block users who can't create new forms.
     */
    if (!formbuilder_user_can('create')) {
        formbuilder_admin_alert('You do not have permission to access this area.');
        return;
    }
    /*
     * Process submitted form results.
     */
    if (isset($_POST['formbuilder']) and is_array($_POST['formbuilder'])) {
        $_POST['formbuilder'] = formbuilder_array_stripslashes($_POST['formbuilder']);
        $_POST['formbuilderfields'] = formbuilder_array_stripslashes($_POST['formbuilderfields']);
        // Verify the data that was posted.
        // No verification currently done on the main form fields.
        // Check for tags.  Add them separately if necessary.
        if (isset($_POST['formbuilder']['tags'])) {
            $newTags = array();
            $tags = explode(',', $_POST['formbuilder']['tags']);
            foreach ($tags as $tag) {
                $tag = trim($tag);
                $tag = preg_replace("/[^A-Za-z0-9 _-]/isU", "", $tag);
                if ($tag != '') {
                    $newTags[] = $tag;
                }
            }
            // Remove tags no longer in the list.  And trim the new tags list to only include true new tags.
            $originalTags = array();
            $sql = "SELECT * FROM " . FORMBUILDER_TABLE_TAGS . " WHERE form_id = '{$form_id}' ORDER BY tag ASC;";
            $results = $wpdb->get_results($sql, ARRAY_A);
            foreach ($results as $tag) {
                $newTagKey = array_search($tag['tag'], $newTags);
                if ($newTagKey === false) {
                    $sql = "DELETE FROM " . FORMBUILDER_TABLE_TAGS . " WHERE id='{$tag['id']}';";
                    if ($wpdb->query($sql) === false) {
                        $errors[] = __('ERROR.  Your tags failed to update.', 'formbuilder') . ' ' . sprintf(__('The error was: %s', 'formbuilder'), $wpdb->last_error);
                    }
                } else {
                    unset($newTags[$newTagKey]);
                }
            }
            // Add new tags from the list.
            foreach ($newTags as $tag) {
                $request = array('form_id' => $form_id, 'tag' => $tag);
                if ($wpdb->insert(FORMBUILDER_TABLE_TAGS, $request) === false) {
                    $errors[] = sprintf(__("Failed inserting tag '%s'.", 'formbuilder'), $tag) . ' ' . sprintf(__('The error was: %s', 'formbuilder'), $wpdb->last_error);
                }
            }
            unset($_POST['formbuilder']['tags']);
        }
        // Check to ensure that we can save the form data.  List an error message if not.
        if (false === $wpdb->update(FORMBUILDER_TABLE_FORMS, $_POST['formbuilder'], array('id' => $form_id))) {
            $errors[] = __('ERROR.  Your form failed to save.', 'formbuilder') . ' ' . sprintf(__('The error was: %s', 'formbuilder'), $wpdb->last_error);
        }
        // Check to see if we have any form fields to save, while making sure there are no existing error messages.
        if (isset($_POST['formbuilderfields']) and is_array($_POST['formbuilderfields']) and !isset($errors)) {
            // Iterate through the form fields, do verification and save them to the database.
            foreach ($_POST['formbuilderfields'] as $key => $value) {
                // Verify that the field has appropriate data
                $value['field_name'] = clean_field_name($value['field_name']);
                // Verify that the field has a field name at all
                if (!$value['field_name']) {
                    if ($value['field_type'] != 'comments area' and $value['field_type'] != 'page break') {
                        $errors[] = sprintf(__("ERROR.  You have a field on your form with an empty field name.  The field is a '%s'  All fields MUST have a unique field name.", 'formbuilder'), $value['field_type']);
                    }
                }
                // Check to ensure that the field name hasn't already been used.
                if ($value['field_name']) {
                    if (!isset($tmp_field_names[$value['field_name']])) {
                        $tmp_field_names[$value['field_name']] = true;
                    } else {
                        $errors[] = __("ERROR.  You have a duplicate field '" . $value['field_name'] . "' on your form.  All field names must be unique.", 'formbuilder');
                    }
                }
                $result = $wpdb->update(FORMBUILDER_TABLE_FIELDS, $value, array('id' => $key));
                if (false === $result) {
                    $errors[] = __("ERROR.  Problems were detected while saving your form fields.", 'formbuilder') . ' ' . sprintf(__('The error was: %s', 'formbuilder'), $wpdb->last_error);
                }
            }
        }
        if (isset($_POST['fieldAction']) and is_array($_POST['fieldAction'])) {
            $fieldAction = $_POST['fieldAction'];
            $fieldKey = key($fieldAction);
            $fieldValue = current($fieldAction);
            if ($fieldValue == __('Add New Field', 'formbuilder')) {
                if ($fieldKey == "newField") {
                    // Create a new field at the end of the form.
                    $sql = "SELECT * FROM " . FORMBUILDER_TABLE_FIELDS . " WHERE form_id = {$form_id} ORDER BY display_order DESC;";
                    $relatedRows = $wpdb->get_results($sql, ARRAY_A);
                    #						$relatedRows = $tableFields->search_rows("$form_id", "form_id", "display_order DESC");
                    $actionRow = $relatedRows[0];
                    $display_order = $actionRow['display_order'] + 1;
                    $wpdb->insert(FORMBUILDER_TABLE_FIELDS, array('form_id' => $form_id, 'display_order' => $display_order, 'field_value' => '', 'field_label' => '', 'error_message' => '', 'help_text' => ''));
                    $rowID = $wpdb->insert_id;
                    #						$tableFields->save_row($rowID, array("form_id"=>"$form_id", "display_order"=>$display_order));
                }
                if (!isset($errors)) {
                    echo "<meta http-equiv='refresh' content='0;url=#field_{$rowID}' />";
                }
            }
            if ($fieldValue == __("Add Another", 'formbuilder')) {
                $sql = "SELECT * FROM " . FORMBUILDER_TABLE_FIELDS . " WHERE id = {$fieldKey} ORDER BY display_order DESC;";
                $results = $wpdb->get_results($sql, ARRAY_A);
                $actionRow = $results[0];
                #$actionRow = $tableFields->load_row($fieldKey);
                $sql = "SELECT * FROM " . FORMBUILDER_TABLE_FIELDS . " WHERE form_id = {$form_id} ORDER BY display_order DESC;";
                $relatedRows = $wpdb->get_results($sql, ARRAY_A);
                #$relatedRows = $tableFields->search_rows("$form_id", "form_id");
                foreach ($relatedRows as $row) {
                    #$row = $tableFields->load_row($tableRowID);
                    $tableRowID = $row['id'];
                    if ($row['display_order'] >= $actionRow['display_order']) {
                        $row['display_order'] = $row['display_order'] + 1;
                        $wpdb->update(FORMBUILDER_TABLE_FIELDS, $row, array('id' => $tableRowID));
                        #$tableFields->save_row($tableRowID, $row);
                    }
                }
                $wpdb->insert(FORMBUILDER_TABLE_FIELDS, array('form_id' => $form_id, 'display_order' => $actionRow['display_order'], 'field_value' => '', 'field_label' => '', 'error_message' => '', 'help_text' => ''));
                $rowID = $wpdb->insert_id;
                #$rowID = $tableFields->create_row();
                #$tableFields->save_row($rowID, array("form_id"=>"$form_id", "display_order"=>$actionRow['display_order']));
                if (!isset($errors)) {
                    echo "<meta http-equiv='refresh' content='0;url=#field_{$rowID}' />";
                }
            }
            if ($fieldValue == __("Delete", 'formbuilder')) {
                #					$actionRow = $tableFields->load_row($fieldKey);
                #					$relatedRows = $tableFields->search_rows("$form_id", "form_id", "display_order ASC");
                #					$tableFields->remove_row($fieldKey);
                $sql = "SELECT * FROM " . FORMBUILDER_TABLE_FIELDS . " WHERE id = {$fieldKey} ORDER BY display_order DESC;";
                $results = $wpdb->get_results($sql, ARRAY_A);
                $actionRow = $results[0];
                $sql = "SELECT * FROM " . FORMBUILDER_TABLE_FIELDS . " WHERE form_id = {$form_id} ORDER BY display_order ASC;";
                $relatedRows = $wpdb->get_results($sql, ARRAY_A);
                $sql = "DELETE FROM " . FORMBUILDER_TABLE_FIELDS . " WHERE id = '{$fieldKey}';";
                $wpdb->query($sql);
                foreach ($relatedRows as $row) {
                    #						$row = $tableFields->load_row($tableRowID);
                    $tableRowID = $row['id'];
                    if ($row['display_order'] > $actionRow['display_order']) {
                        $row['display_order'] = $row['display_order'] - 1;
                        #							$tableFields->save_row($tableRowID, $row);
                        $wpdb->update(FORMBUILDER_TABLE_FIELDS, $row, array('id' => $tableRowID));
                    }
                }
                if (!isset($errors)) {
                    echo "<meta http-equiv='refresh' content='0;url=#field_" . $relatedRows[0]['id'] . "' />";
                }
            }
            if ($fieldValue == __("Move Up", 'formbuilder')) {
                #					$actionRow = $tableFields->load_row($fieldKey);
                #					$relatedRows = $tableFields->search_rows("$form_id", "form_id", "display_order ASC");
                $sql = "SELECT * FROM " . FORMBUILDER_TABLE_FIELDS . " WHERE id = {$fieldKey} ORDER BY display_order DESC;";
                $results = $wpdb->get_results($sql, ARRAY_A);
                $actionRow = $results[0];
                $sql = "SELECT * FROM " . FORMBUILDER_TABLE_FIELDS . " WHERE form_id = {$form_id} ORDER BY display_order ASC;";
                $relatedRows = $wpdb->get_results($sql, ARRAY_A);
                #					$firstRow = $tableFields->load_row(reset($relatedRows));
                $sql = "SELECT * FROM " . FORMBUILDER_TABLE_FIELDS . " WHERE id = '" . $relatedRows[0]['id'] . "' ORDER BY display_order DESC;";
                $results = $wpdb->get_results($sql, ARRAY_A);
                $firstRow = $results[0];
                $firstPos = $firstRow['display_order'];
                $current_pos = $actionRow['display_order'];
                if ($current_pos > $firstPos) {
                    $current_pos -= 1;
                    $actionRow['display_order'] = $current_pos;
                    foreach ($relatedRows as $row) {
                        #							$row = $tableFields->load_row($tableRowID);
                        $tableRowID = $row['id'];
                        if ($row['display_order'] == $current_pos) {
                            $row['display_order'] = $row['display_order'] + 1;
                            #								$tableFields->save_row($tableRowID, $row);
                            $wpdb->update(FORMBUILDER_TABLE_FIELDS, $row, array('id' => $tableRowID));
                        }
                    }
                    #						$tableFields->save_row($fieldKey, $actionRow);
                    $wpdb->update(FORMBUILDER_TABLE_FIELDS, $actionRow, array('id' => $fieldKey));
                }
                if (!isset($errors)) {
                    echo "<meta http-equiv='refresh' content='0;url=#field_{$fieldKey}' />";
                }
            }
            if ($fieldValue == __("Move Down", 'formbuilder')) {
                #					$actionRow = $tableFields->load_row($fieldKey);
                $sql = "SELECT * FROM " . FORMBUILDER_TABLE_FIELDS . " WHERE id = {$fieldKey} ORDER BY display_order DESC;";
                $results = $wpdb->get_results($sql, ARRAY_A);
                $actionRow = $results[0];
                #					$relatedRows = $tableFields->search_rows("$form_id", "form_id", "display_order DESC");
                $sql = "SELECT * FROM " . FORMBUILDER_TABLE_FIELDS . " WHERE form_id = {$form_id} ORDER BY display_order DESC;";
                $relatedRows = $wpdb->get_results($sql, ARRAY_A);
                #					$firstRow = $tableFields->load_row(reset($relatedRows));
                $sql = "SELECT * FROM " . FORMBUILDER_TABLE_FIELDS . " WHERE id = '" . $relatedRows[0]['id'] . "' ORDER BY display_order DESC;";
                $results = $wpdb->get_results($sql, ARRAY_A);
                $firstRow = $results[0];
                $lastPos = $firstRow['display_order'];
                $current_pos = $actionRow['display_order'];
                if ($current_pos < $lastPos) {
                    $current_pos += 1;
                    $actionRow['display_order'] = $current_pos;
                    foreach ($relatedRows as $row) {
                        #							$row = $tableFields->load_row($tableRowID);
                        $tableRowID = $row['id'];
                        if ($row['display_order'] == $current_pos) {
                            $row['display_order'] = $row['display_order'] - 1;
                            #								$tableFields->save_row($tableRowID, $row);
                            $wpdb->update(FORMBUILDER_TABLE_FIELDS, $row, array('id' => $tableRowID));
                        }
                    }
                    #						$tableFields->save_row($fieldKey, $actionRow);
                    $wpdb->update(FORMBUILDER_TABLE_FIELDS, $actionRow, array('id' => $fieldKey));
                }
                if (!isset($errors)) {
                    echo "<meta http-equiv='refresh' content='0;url=#field_{$fieldKey}' />";
                }
            }
        }
        if (isset($_POST['Save']) and !isset($errors)) {
            $message = sprintf(__("Your form has been saved.  %sYou may click here to return to the main FormBuilder options page.%s", 'formbuilder'), "<a href='" . FB_ADMIN_PLUGIN_PATH . "'>", "</a>");
        }
    }
    // End process submitted form results.
    /*
     * Show any error messages that we need to show.
     */
    if (isset($message)) {
        echo "<div class='updated'><p><strong>{$message}</strong></p></div>";
    }
    if (isset($errors)) {
        foreach ($errors as $error) {
            echo "<div class='updated' style='background-color: #FFBBBB; border: 1px solid red; color: red;'><p><strong>{$error}</strong></p></div>";
        }
    }
    /*
     * Load the form fields from the database.
     */
    $sql = "SELECT * FROM " . FORMBUILDER_TABLE_FORMS . " WHERE id = '{$form_id}';";
    $results = $wpdb->get_results($sql, ARRAY_A);
    $form_fields = $results[0];
    /*
     * Iterate through each field in the forms database for this form.  
     * These are the generic form control fields such as name, subject, recipient.
     */
    foreach ($form_fields as $key => $value) {
        // Create a new field array.
        $field = array();
        // Assign the key to the Field key
        $field['Field'] = $key;
        // If there is a POST value for the field, us it, otherwise use the $value variable which contains the value from the DB.
        if (!isset($_POST['formbuilder'][$key])) {
            $field['Value'] = $value;
        } else {
            $field['Value'] = $_POST['formbuilder'][$key];
        }
        // Add a brief explanation to specific fields of how to enter the data.
        if ($field['Field'] == "name") {
            $field['Title'] = __('What do you want to call this contact form?', 'formbuilder');
            $field['HelpText'] = __('What do you want to call this contact form?', 'formbuilder');
            $field['Type'] = "varchar(255)";
        }
        if ($field['Field'] == "subject") {
            $field['Title'] = __('The subject line for the email you receive from the form.', 'formbuilder');
            $field['HelpText'] = __('The subject line for the email you receive from the form.', 'formbuilder');
            $field['Type'] = "varchar(255)";
        }
        if ($field['Field'] == "recipient") {
            $field['Title'] = __('What email address should the data from this contact form be mailed to?', 'formbuilder');
            $field['HelpText'] = __('What email address should the data from this contact form be mailed to?', 'formbuilder');
            $field['Type'] = "varchar(255)";
        }
        if ($field['Field'] == "method") {
            $field['Title'] = __('How should this form post data?  If you are unsure, leave it on POST', 'formbuilder');
            $field['HelpText'] = __('How should this form post data?  If you are unsure, leave it on POST', 'formbuilder');
            $field['Type'] = "enum(POST,GET)";
        }
        if ($field['Field'] == "action") {
            $field['Title'] = __('You may specify an alternate form processing system if necessary.  If you are unsure, leave it alone.', 'formbuilder');
            $field['HelpText'] = __('You may specify an alternate form processing system if necessary.  If you are unsure, leave it alone.', 'formbuilder');
            $field['Type'] = "enum('|" . __('Form to Email - Convert the form results to an email.', 'formbuilder') . "'";
            if (file_exists(FORMBUILDER_PLUGIN_PATH . "/modules")) {
                $d = dir(FORMBUILDER_PLUGIN_PATH . "/modules");
                while (false !== ($entry = $d->read())) {
                    if ($entry != "." and $entry != "..") {
                        $module_filename = FORMBUILDER_PLUGIN_PATH . "/modules/{$entry}";
                        if (!is_file($module_filename)) {
                            continue;
                        }
                        $module_data = implode("", file($module_filename));
                        if (eregi("\n\\w*name\\: ([^\r\n]+)", $module_data, $regs)) {
                            $module_name = $regs[1];
                        } else {
                            $module_name = $entry;
                        }
                        $field['Type'] .= ",'{$entry}|{$module_name}'";
                        if (eregi("\n\\w*instructions\\: ([^\r\n]+)", $module_data, $regs)) {
                            $module_instructions = "\\n\\n" . addslashes($regs[1]);
                        } else {
                            $module_instructions = "";
                        }
                        $field['HelpText'] .= $module_instructions;
                    }
                }
                $d->close();
            }
            $field['Type'] .= ")";
        }
        if ($field['Field'] == "thankyoutext") {
            $field['Title'] = __('What message would you like to show your visitors?', 'formbuilder');
            $field['HelpText'] = __('What message would you like to show your visitors when the successfully complete the form?', 'formbuilder');
            $field['Type'] = "text";
        }
        if ($field['Field'] == "autoresponse") {
            $field['Title'] = __('You may specify an autoresponse to send back if necessary.', 'formbuilder');
            $field['HelpText'] = __('You may specify an autoresponse to send back if necessary.  You should have alread created them on the main FormBuilder Management page.', 'formbuilder');
            $field['Type'] = "enum('|'";
            $sql = "SELECT * FROM " . FORMBUILDER_TABLE_RESPONSES . ";";
            $response_ids = $wpdb->get_results($sql, ARRAY_A);
            #				$response_ids = $tableResponses->list_rows();
            if ($response_ids) {
                foreach ($response_ids as $response_data) {
                    #					$response_data = $tableResponses->load_row($response_id);
                    $field['Type'] .= ",'" . $response_data['id'] . "|" . $response_data['name'] . "'";
                }
            }
            $field['Type'] .= ")";
        }
        $fields[$key] = $field;
    }
    $all_field_types = formbuilder_get_field_types();
    $all_required_types = formbuilder_get_required_types();
    include FORMBUILDER_PLUGIN_PATH . "html/options_edit_form.inc.php";
}