/** * Method used to update the values stored in the database. * * @access public * @return integer 1 if the update worked properly, any other value otherwise */ function updateValues() { global $HTTP_POST_VARS; $prj_id = Auth::getCurrentProject(); $issue_id = Misc::escapeInteger($HTTP_POST_VARS["issue_id"]); $old_values = Custom_Field::getValuesByIssue($prj_id, $issue_id); // get the types for all of the custom fields being submitted $stmt = "SELECT\n fld_id,\n fld_type\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "custom_field\n WHERE\n fld_id IN (" . implode(", ", Misc::escapeInteger(@array_keys($HTTP_POST_VARS['custom_fields']))) . ")"; $field_types = $GLOBALS["db_api"]->dbh->getAssoc($stmt); // get the titles for all of the custom fields being submitted $stmt = "SELECT\n fld_id,\n fld_title\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "custom_field\n WHERE\n fld_id IN (" . implode(", ", Misc::escapeInteger(@array_keys($HTTP_POST_VARS['custom_fields']))) . ")"; $field_titles = $GLOBALS["db_api"]->dbh->getAssoc($stmt); $updated_fields = array(); foreach ($HTTP_POST_VARS["custom_fields"] as $fld_id => $value) { $fld_id = Misc::escapeInteger($fld_id); // security check $sql = "SELECT\n fld_min_role\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "custom_field\n WHERE\n fld_id = {$fld_id}"; $min_role = $GLOBALS["db_api"]->dbh->getOne($sql); if ($min_role > Auth::getCurrentRole()) { continue; } $option_types = array('multiple', 'combo'); if (!in_array($field_types[$fld_id], $option_types)) { // check if this is a date field if ($field_types[$fld_id] == 'date') { $value = $value['Year'] . "-" . $value['Month'] . "-" . $value['Day']; if ($value == '--') { $value = ''; } } // first check if there is actually a record for this field for the issue $stmt = "SELECT\n icf_id,\n icf_value\n FROM\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_custom_field\n WHERE\n icf_iss_id=" . $issue_id . " AND\n icf_fld_id={$fld_id}"; $res = $GLOBALS["db_api"]->dbh->getRow($stmt, DB_FETCHMODE_ASSOC); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return -1; } $icf_id = $res['icf_id']; $icf_value = $res['icf_value']; if ($icf_value == $value) { continue; } if (empty($icf_id)) { // record doesn't exist, insert new record $stmt = "INSERT IGNORE INTO\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_custom_field\n (\n icf_iss_id,\n icf_fld_id,\n icf_value\n ) VALUES (\n " . $issue_id . ",\n {$fld_id},\n '" . Misc::escapeString($value) . "'\n )"; $res = $GLOBALS["db_api"]->dbh->query($stmt); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return -1; } } else { // record exists, update it $stmt = "UPDATE\n " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue_custom_field\n SET\n icf_value='" . Misc::escapeString($value) . "'\n WHERE\n icf_id={$icf_id}"; $res = $GLOBALS["db_api"]->dbh->query($stmt); if (PEAR::isError($res)) { Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__); return -1; } } if ($field_types[$fld_id] == 'textarea') { $updated_fields[$field_titles[$fld_id]] = ''; } else { $updated_fields[$field_titles[$fld_id]] = History::formatChanges($icf_value, $value); } } else { $old_value = Custom_Field::getDisplayValue($HTTP_POST_VARS['issue_id'], $fld_id, true); if (!is_array($old_value)) { $old_value = array($old_value); } if (!is_array($value)) { $value = array($value); } if (count(array_diff($old_value, $value)) > 0 || count(array_diff($value, $old_value)) > 0) { $old_display_value = Custom_Field::getDisplayValue($HTTP_POST_VARS['issue_id'], $fld_id); // need to remove all associated options from issue_custom_field and then // add the selected options coming from the form Custom_Field::removeIssueAssociation($fld_id, $HTTP_POST_VARS["issue_id"]); if (@count($value) > 0) { Custom_Field::associateIssue($HTTP_POST_VARS["issue_id"], $fld_id, $value); } $new_display_value = Custom_Field::getDisplayValue($HTTP_POST_VARS['issue_id'], $fld_id); $updated_fields[$field_titles[$fld_id]] = History::formatChanges($old_display_value, $new_display_value); } } } Workflow::handleCustomFieldsUpdated($prj_id, $issue_id, $old_values, Custom_Field::getValuesByIssue($prj_id, $issue_id)); Issue::markAsUpdated($HTTP_POST_VARS["issue_id"]); // need to save a history entry for this if (count($updated_fields) > 0) { // log the changes $changes = ''; $i = 0; foreach ($updated_fields as $key => $value) { if ($i > 0) { $changes .= "; "; } if (!empty($value)) { $changes .= "{$key}: {$value}"; } else { $changes .= "{$key}"; } $i++; } History::add($HTTP_POST_VARS["issue_id"], Auth::getUserID(), History::getTypeID('custom_field_updated'), "Custom field updated ({$changes}) by " . User::getFullName(Auth::getUserID())); } return 1; }
/** * Generates the specialized headers for an email. * * @param integer $issue_id The issue ID * @param string $type The type of message this is * @param string $headers The existing headers of this message. * @param integer $sender_usr_id The id of the user sending this email. * @return array An array of specialized headers */ public static function getSpecializedHeaders($issue_id, $type, $headers, $sender_usr_id) { $new_headers = array(); if (!empty($issue_id)) { $prj_id = Issue::getProjectID($issue_id); if (count(Group::getAssocList($prj_id)) > 0) { // group issue is currently assigned too $new_headers['X-Eventum-Group-Issue'] = Group::getName(Issue::getGroupID($issue_id)); // group of whoever is sending this message. if (empty($sender_usr_id)) { $new_headers['X-Eventum-Group-Replier'] = $new_headers['X-Eventum-Group-Issue']; } else { $new_headers['X-Eventum-Group-Replier'] = Group::getName(User::getGroupID($sender_usr_id)); } // group of current assignee $assignees = Issue::getAssignedUserIDs($issue_id); if (empty($assignees[0])) { $new_headers['X-Eventum-Group-Assignee'] = ''; } else { $new_headers['X-Eventum-Group-Assignee'] = @Group::getName(User::getGroupID($assignees[0])); } } if (CRM::hasCustomerIntegration($prj_id)) { $crm = CRM::getInstance($prj_id); try { $customer = $crm->getCustomer(Issue::getCustomerID($issue_id)); $new_headers['X-Eventum-Customer'] = $customer->getName(); } catch (CustomerNotFoundException $e) { } try { $contract = $crm->getContract(Issue::getContractID($issue_id)); $support_level = $contract->getSupportLevel(); if (is_object($support_level)) { $new_headers['X-Eventum-Level'] = $support_level->getName(); } } catch (ContractNotFoundException $e) { } } // add assignee header $new_headers['X-Eventum-Assignee'] = implode(',', User::getEmail(Issue::getAssignedUserIDs($issue_id))); $new_headers['X-Eventum-Category'] = Category::getTitle(Issue::getCategory($issue_id)); $new_headers['X-Eventum-Project'] = Project::getName($prj_id); $new_headers['X-Eventum-Priority'] = Priority::getTitle(Issue::getPriority($issue_id)); // handle custom fields $cf_values = Custom_Field::getValuesByIssue($prj_id, $issue_id); $cf_titles = Custom_Field::getFieldsToBeListed($prj_id); foreach ($cf_values as $fld_id => $values) { // skip empty titles // TODO: why they are empty? if (!isset($cf_titles[$fld_id])) { continue; } // skip empty values if (empty($values)) { continue; } $cf_value = implode(', ', (array) $values); // value could be empty after multivalued field join if (empty($cf_value)) { continue; } // convert spaces for header fields $cf_title = str_replace(' ', '_', $cf_titles[$fld_id]); $new_headers['X-Eventum-CustomField-' . $cf_title] = $cf_value; } } $new_headers['X-Eventum-Type'] = $type; return $new_headers; }