/**
  * Method used to update the details for a specific custom field.
  *
  * @access  public
  * @return  integer 1 if the update worked, -1 otherwise
  */
 function update()
 {
     global $HTTP_POST_VARS;
     if (empty($HTTP_POST_VARS["report_form"])) {
         $HTTP_POST_VARS["report_form"] = 0;
     }
     if (empty($HTTP_POST_VARS["report_form_required"])) {
         $HTTP_POST_VARS["report_form_required"] = 0;
     }
     if (empty($HTTP_POST_VARS["anon_form"])) {
         $HTTP_POST_VARS["anon_form"] = 0;
     }
     if (empty($HTTP_POST_VARS["anon_form_required"])) {
         $HTTP_POST_VARS["anon_form_required"] = 0;
     }
     if (empty($HTTP_POST_VARS["list_display"])) {
         $HTTP_POST_VARS["list_display"] = 0;
     }
     if (empty($HTTP_POST_VARS["min_role"])) {
         $HTTP_POST_VARS["min_role"] = 1;
     }
     if (!isset($HTTP_POST_VARS["rank"])) {
         $HTTP_POST_VARS["rank"] = Custom_Field::getMaxRank() + 1;
     }
     $old_details = Custom_Field::getDetails($HTTP_POST_VARS["id"]);
     $stmt = "UPDATE\n                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "custom_field\n                 SET\n                    fld_title='" . Misc::escapeString($HTTP_POST_VARS["title"]) . "',\n                    fld_description='" . Misc::escapeString($HTTP_POST_VARS["description"]) . "',\n                    fld_type='" . Misc::escapeString($HTTP_POST_VARS["field_type"]) . "',\n                    fld_report_form=" . Misc::escapeInteger($HTTP_POST_VARS["report_form"]) . ",\n                    fld_report_form_required=" . Misc::escapeInteger($HTTP_POST_VARS["report_form_required"]) . ",\n                    fld_anonymous_form=" . Misc::escapeInteger($HTTP_POST_VARS["anon_form"]) . ",\n                    fld_anonymous_form_required=" . Misc::escapeInteger($HTTP_POST_VARS["anon_form_required"]) . ",\n                    fld_list_display=" . Misc::escapeInteger($HTTP_POST_VARS["list_display"]) . ",\n                    fld_min_role=" . Misc::escapeInteger($HTTP_POST_VARS['min_role']) . ",\n                    fld_rank = " . Misc::escapeInteger($HTTP_POST_VARS['rank']) . ",\n                    fld_backend = '" . Misc::escapeString(@$HTTP_POST_VARS['custom_field_backend']) . "'\n                 WHERE\n                    fld_id=" . Misc::escapeInteger($HTTP_POST_VARS["id"]);
     $res = $GLOBALS["db_api"]->dbh->query($stmt);
     if (PEAR::isError($res)) {
         Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
         return -1;
     } else {
         // if the current custom field is a combo box, get all of the current options
         if (in_array($HTTP_POST_VARS["field_type"], array('combo', 'multiple'))) {
             $stmt = "SELECT\n                            cfo_id\n                         FROM\n                            " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "custom_field_option\n                         WHERE\n                            cfo_fld_id=" . Misc::escapeInteger($HTTP_POST_VARS["id"]);
             $current_options = $GLOBALS["db_api"]->dbh->getCol($stmt);
         }
         // gotta remove all custom field options if the field is being changed from a combo box to a text field
         if ($old_details["fld_type"] != $HTTP_POST_VARS["field_type"] && !in_array($old_details['fld_type'], array('text', 'textarea')) && !in_array($HTTP_POST_VARS["field_type"], array('combo', 'multiple'))) {
             Custom_Field::removeOptionsByFields($HTTP_POST_VARS["id"]);
         }
         // update the custom field options, if any
         if ($HTTP_POST_VARS["field_type"] == "combo" || $HTTP_POST_VARS["field_type"] == "multiple") {
             $updated_options = array();
             if (empty($HTTP_POST_VARS['custom_field_backend'])) {
                 foreach ($HTTP_POST_VARS["field_options"] as $option_value) {
                     $params = Custom_Field::parseParameters($option_value);
                     if ($params["type"] == 'new') {
                         Custom_Field::addOptions($HTTP_POST_VARS["id"], $params["value"]);
                     } else {
                         $updated_options[] = $params["id"];
                         // check if the user is trying to update the value of this option
                         if ($params["value"] != Custom_Field::getOptionValue($HTTP_POST_VARS["id"], $params["id"])) {
                             Custom_Field::updateOption($params["id"], $params["value"]);
                         }
                     }
                 }
             }
         }
         // get the diff between the current options and the ones posted by the form
         // and then remove the options not found in the form submissions
         if (in_array($HTTP_POST_VARS["field_type"], array('combo', 'multiple'))) {
             $diff_ids = @array_diff($current_options, $updated_options);
             if (@count($diff_ids) > 0) {
                 Custom_Field::removeOptions($HTTP_POST_VARS['id'], array_values($diff_ids));
             }
         }
         // now we need to check for any changes in the project association of this custom field
         // and update the mapping table accordingly
         $old_proj_ids = @array_keys(Custom_Field::getAssociatedProjects($HTTP_POST_VARS["id"]));
         // COMPAT: this next line requires PHP > 4.0.4
         $diff_ids = array_diff($old_proj_ids, $HTTP_POST_VARS["projects"]);
         if (count($diff_ids) > 0) {
             foreach ($diff_ids as $removed_prj_id) {
                 Custom_Field::removeIssueAssociation($HTTP_POST_VARS["id"], false, $removed_prj_id);
             }
         }
         // update the project associations now
         $stmt = "DELETE FROM\n                        " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project_custom_field\n                     WHERE\n                        pcf_fld_id=" . Misc::escapeInteger($HTTP_POST_VARS["id"]);
         $res = $GLOBALS["db_api"]->dbh->query($stmt);
         if (PEAR::isError($res)) {
             Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
             return -1;
         } else {
             for ($i = 0; $i < count($HTTP_POST_VARS["projects"]); $i++) {
                 Custom_Field::associateProject($HTTP_POST_VARS["projects"][$i], $HTTP_POST_VARS["id"]);
             }
         }
         return 1;
     }
 }