Index: project_issue.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/project_issue/project_issue.module,v retrieving revision 1.174 diff -u -p -r1.174 project_issue.module --- project_issue.module 6 Aug 2009 23:35:34 -0000 1.174 +++ project_issue.module 11 Nov 2009 18:11:11 -0000 @@ -590,6 +590,66 @@ function project_issue_add_auto_followup } /** + * Add or update a project issue status option. + * + * @param $status + * An associative array specifying what needs to be saved. To update a status + * option, specify the sid and the value of at least one other key. To add + * a status option, all keys except 'sid' must be specified. + * - sid: The sid of an already existing status option, use when updating. + * - name: the human readable text for this status option. + * - weight: when this status will appear in relation to other status + * options. + * - author_has: whether the author of an issue will see this status option + * or not. + * - default_query: whether this status option will be shown in default + * queries. + * + * @return + * TRUE if the status option was successfully saved, FALSE if another status + * option has the name specifed in $status['name']. + */ +function project_issue_status_option_save($status) { + if (isset($status['sid'])) { + // An sid has been specified so we must be updating. + // Check to see whether the name has already been taken with another sid: + if (isset($status['name'])) { + $issue_state = db_result(db_query("SELECT COUNT(*) FROM {project_issue_state} WHERE name = '%s' AND sid <> %d", $status['name'], $status['sid'])); + if (!empty($issue_state)) { + return FALSE; + } + } + + foreach ($status as $key => $value) { + if ($key == 'name') { + $query[] = "$key = '%s'"; + $params[] = $value; + } + elseif ($key != 'sid') { + $query[] = "$key = %d"; + $params[] = $value; + } + } + $params[] = $status['sid']; + + db_query("UPDATE {project_issue_state} SET ". implode(' , ', $query) ." WHERE sid = %d", $params); + return TRUE; + } + else { + // Add a new state + // Check to see whether the state already exists: + $issue_state = db_result(db_query("SELECT COUNT(*) FROM {project_issue_state} WHERE name = '%s'", $status['name'])); + if (empty($issue_state)) { + db_query("INSERT INTO {project_issue_state} (name, weight, author_has, default_query) VALUES ('%s', %d, %d, %d)", $status['name'], $status['weight'], $status['author_has'], $status['default_query']); + return TRUE; + } + else { + return FALSE; + } + } +} + +/** * Saves a comment to the database. * * TODO: Ideally this should die as soon as core's comment_save() becomes more Index: includes/admin.issue_status.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/project_issue/includes/admin.issue_status.inc,v retrieving revision 1.1 diff -u -p -r1.1 admin.issue_status.inc --- includes/admin.issue_status.inc 4 Apr 2009 07:03:28 -0000 1.1 +++ includes/admin.issue_status.inc 11 Nov 2009 18:11:11 -0000 @@ -125,21 +125,17 @@ function project_issue_admin_states_form // Update existing status options. if($form_state['values']['status']) { foreach ($form_state['values']['status'] as $sid => $value) { - $state = db_fetch_object(db_query('SELECT name, weight, author_has, default_query FROM {project_issue_state} WHERE sid = %d', $sid)); - // Check to see whether the record needs updating. - if (($state->name != $value['name']) || ($state->weight != $value['weight']) || ($state->author_has != $value['author_has']) || ($state->default_query != $value['default_query'])) { - db_query("UPDATE {project_issue_state} SET name = '%s', weight = %d, author_has = %d, default_query = %d WHERE sid = %d", $value['name'], $value['weight'], $value['author_has'], $value['default_query'], $sid); + $value['sid'] = $sid; + $success = project_issue_status_option_save($value); + if($success === FALSE) { + drupal_set_message(t('Status %status already exists.', array ('%status' => $value['name'])), 'error'); } } } // Add any new status options. if (isset($form_state['values']['status_add']) && !empty($form_state['values']['status_add']['name'])) { - // Check to see whether the state already exists: - $issue_state = db_result(db_query("SELECT COUNT(*) FROM {project_issue_state} WHERE name = '%s'", $form_state['values']['status_add']['name'])); - if (empty($issue_state)) { - db_query("INSERT INTO {project_issue_state} (name, weight, author_has, default_query) VALUES ('%s', %d, %d, %d)", $form_state['values']['status_add']['name'], $form_state['values']['status_add']['weight'], $form_state['values']['status_add']['author_has'], $form_state['values']['status_add']['default_query']); - } - else { + $success = project_issue_status_option_save($form_state['values']['status_add']); + if($success === FALSE) { drupal_set_message(t('Status %status already exists.', array ('%status' => $form_state['values']['status_add']['name'])), 'error'); } }