00001 <?php
00002 # This file is part of the Savane project
00003 # <http://gna.org/projects/savane/>
00004 #
00005 # $Id: transition.php 4905 2005-11-11 10:55:31Z yeupou $
00006 #
00007 # Copyright 2004-2004 (c) Mathieu Roy <yeupou--at--gnu.org>
00008 # Yves Perrin <yves.perrin--at--cern.ch>
00009 #
00010 # The Savane project is free software; you can redistribute it and/or
00011 # modify it under the terms of the GNU General Public License
00012 # as published by the Free Software Foundation; either version 2
00013 # of the License, or (at your option) any later version.
00014 #
00015 # The Savane project is distributed in the hope that it will be useful,
00016 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00018 # GNU General Public License for more details.
00019 #
00020 # You should have received a copy of the GNU General Public License
00021 # along with the Savane project; if not, write to the Free Software
00022 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00023
00024
00025 # Get an array of fields that should be updated on field transitions
00026 function trackers_transition_get_update($group_id)
00027 {
00028 $field_transition = array();
00029 $field_transition_sql = "SELECT transition_id,field_id,from_value_id,to_value_id,is_allowed,notification_list ".
00030 "FROM trackers_field_transition ".
00031 "WHERE group_id='".$group_id."' AND artifact='".ARTIFACT."' ";
00032
00033 $field_transition_result = db_query($field_transition_sql);
00034 if ($field_transition_result && db_numrows($field_transition_result) > 0)
00035 {
00036 while ($this_transition = db_fetch_array($field_transition_result))
00037 {
00038 $field_id = $this_transition['field_id'];
00039
00040 if (!array_key_exists($field_id, $field_transition))
00041 { $field_transition[$field_id] = array(); }
00042
00043 $from = $this_transition['from_value_id'];
00044 if ($from == "0")
00045 { $from = "any"; }
00046 if (!array_key_exists($from, $field_transition[$field_id]))
00047 { $field_transition[$field_id][$from] = array(); }
00048
00049 $to = $this_transition['to_value_id'];
00050 if (!array_key_exists($to, $field_transition[$field_id][$from]))
00051 { $field_transition[$field_id][$from][$to] = array(); }
00052
00053 $field_transition[$field_id][$from][$to]['transition_id'] = $this_transition['transition_id'];
00054 $field_transition[$field_id][$from][$to]['allowed'] = $this_transition['is_allowed'];
00055 $field_transition[$field_id][$from][$to]['notification_list'] = $this_transition['notification_list'];
00056 }
00057 }
00058 return $field_transition;
00059 }
00060
00061
00062
00063 # Return as an array the other field update pair field/value for a given transition.
00064 # No such transition, no update planned? Return false
00065 function trackers_transition_get_other_field_update ($transition_id)
00066 {
00067 $result = db_query("SELECT update_field_name,update_value_id FROM trackers_field_transition_other_field_update WHERE transition_id='".$transition_id."'");
00068 if (!db_numrows($result))
00069 { return false; }
00070
00071 # returning an array does not work afterward in while statements. the current workaround is
00072 # to return the result as it is. It is ugly, feel free to improve.
00073 return $result;
00074 #return db_fetch_array($result);
00075 }
00076
00077 # For a given transition, add/remove/update and "other field update", if necessary.
00078 function trackers_transition_update_other_field ($transition_id, $field_name, $value_id)
00079 {
00080 # If value_id is equal to 0, we are in the delete case
00081 if ($value_id == 0)
00082 {
00083 # We do not set LIMIT 1: if there were several entries for the same transition and
00084 # field name, it was a bug anyway.
00085 if (db_affected_rows(db_query("DELETE FROM trackers_field_transition_other_field_update WHERE transition_id='$transition_id' AND update_field_name='$field_name'")) > 0)
00086 {
00087 fb(_("Other Field update deleted"));
00088 return true;
00089 }
00090
00091 fb_dberror();
00092 return false;
00093 }
00094
00095 fb("other $transition_id, $field_name, $value_id", 1);
00096 # Otherwise, we first check if there is such "other field update configured" and do
00097 # INSERT or UPDATE accordingly
00098 $id = db_result(db_query("SELECT other_field_update_id FROM trackers_field_transition_other_field_update WHERE transition_id='$transition_id' AND update_field_name='$field_name' LIMIT 1"),
00099 0,
00100 'other_field_update_id');
00101 if ($id)
00102 {
00103 $sql = "UPDATE trackers_field_transition_other_field_update SET update_value_id='$value_id' WHERE other_field_update_id='$id'";
00104 }
00105 else
00106 {
00107 $sql = "INSERT INTO trackers_field_transition_other_field_update (transition_id,update_field_name,update_value_id) VALUES ('$transition_id','$field_name','$value_id')";
00108 }
00109 if (db_affected_rows(db_query($sql)))
00110 {
00111 fb_dbsuccess();
00112 return true;
00113 }
00114
00115 fb_dberror();
00116 return false;
00117 }
00118
00119 # For a given array of transitions and one item id, update other fields.
00120 # It must check, before updating a field, that no other update was made before.
00121 # It will also follow the first update configured found, if there are configuration conflicts.
00122 function trackers_transition_update_item ($item_id, $transition_id_array, $changes)
00123 {
00124 # Array in which we ll store field to updates
00125 $toupdate = array();
00126
00127 # Extract transitions updates
00128 if (is_array($transition_id_array))
00129 {
00130 while (list(,$transition_id) = each($transition_id_array))
00131 {
00132 # Make sure we have a valid entry
00133 if (!$transition_id)
00134 { continue; }
00135
00136 # Get list of register updated for this transition
00137 $registered = trackers_transition_get_other_field_update($transition_id);
00138
00139 # No result? skip it
00140 if (!$registered)
00141 { continue; }
00142 else
00143 {
00144 # Run the list of registered updates for this transition
00145 while ($update = db_fetch_array($registered))
00146 {
00147 # Skip it if it already on the list to be changed
00148 if ((is_array($changes) && !array_key_exists($update['update_field_name'], $changes)) &&
00149 !array_key_exists($update['update_field_name'], $toupdate))
00150 {
00151 # Add to the list of planned updates
00152 $toupdate[$update['update_field_name']] = $update['update_value_id'];
00153 # If we close the item, update the closed_date field
00154 if ($update['update_field_name'] == 'status_id' &&
00155 $update['update_value_id'] == '3')
00156 {
00157 $toupdate['close_date'] = time();
00158 }
00159
00160 }
00161 }
00162 }
00163
00164 }
00165
00166 # Now update fields
00167 unset($upd_list);
00168 while (list($field,$value) = each($toupdate))
00169 {
00170 if ($value)
00171 {
00172 trackers_data_add_history($field,
00173 'transition-other-field-update',
00174 $value,
00175 $item_id);
00176 fb(sprintf(_("Automatic update of %s due to transitions settings"),trackers_data_get_label($field)));
00177 $upd_list .= "$field='$value',";
00178 $exists = 1;
00179 }
00180 }
00181
00182 if ($exists)
00183 {
00184 # Update database silently, we may have no rows to update
00185 db_query("UPDATE ".ARTIFACT." SET ".trim($upd_list, ",")." WHERE bug_id='$item_id'");
00186
00187 }
00188
00189
00190 }
00191
00192 return true;
00193 }
00194
00195 ?>