$GLOBALS['phpgw']->db->query("insert into phpgw_tts_transitions (transition_name,transition_description,transition_source_state,transition_target_state) values ('" . addslashes($transition['name']) . "','" . addslashes($transition['description']) . "'," . intval($transition['source_state']) . ", " . intval($transition['target_state']) . ")", __LINE__, __FILE__);
    } else {
        $GLOBALS['phpgw']->db->query("update phpgw_tts_transitions " . " set transition_name='" . addslashes($transition['name']) . "', " . " transition_description='" . addslashes($transition['description']) . "', " . " transition_source_state=" . intval($transition['source_state']) . ", " . " transition_target_state=" . intval($transition['target_state']) . " WHERE transition_id=" . $transition_id, __LINE__, __FILE__);
    }
    $GLOBALS['phpgw']->redirect_link('/tts/transitions.php');
} else {
    $GLOBALS['phpgw_info']['flags']['app_header'] = $GLOBALS['phpgw_info']['apps']['tts']['title'] . ' - ' . ($transition_id ? lang('Create new transition') : lang('Edit the transition'));
    $GLOBALS['phpgw']->common->phpgw_header();
    // select the ticket that you selected
    $GLOBALS['phpgw']->db->query("select * from phpgw_tts_transitions where transition_id='{$transition_id}'", __LINE__, __FILE__);
    $GLOBALS['phpgw']->db->next_record();
    $transition['name'] = try_lang($GLOBALS['phpgw']->db->f('transition_name'));
    $transition['source_state'] = try_lang($GLOBALS['phpgw']->db->f('transition_source_state'));
    $transition['target_state'] = try_lang($GLOBALS['phpgw']->db->f('transition_target_state'));
    $transition['description'] = try_lang($GLOBALS['phpgw']->db->f('transition_description'), $transition['target_state']);
    $GLOBALS['phpgw']->template->set_file(array('edit_transition' => 'edit_transition.tpl'));
    $GLOBALS['phpgw']->template->set_block('edit_transition', 'form');
    $GLOBALS['phpgw']->template->set_var('form_action', $GLOBALS['phpgw']->link('/tts/edit_transition.php', '&transition_id=' . $transition_id));
    $GLOBALS['phpgw']->template->set_var('lang_transition_name', lang('Transition name'));
    $GLOBALS['phpgw']->template->set_var('lang_transition_description', lang('Description'));
    $GLOBALS['phpgw']->template->set_var('lang_source_state', lang('Source State'));
    $GLOBALS['phpgw']->template->set_var('lang_target_state', lang('Target State'));
    $GLOBALS['phpgw']->template->set_var('lang_save', lang('Save'));
    $GLOBALS['phpgw']->template->set_var('lang_cancel', lang('Cancel'));
    $GLOBALS['phpgw']->template->set_var('value_name', $transition['name']);
    $GLOBALS['phpgw']->template->set_var('value_description', $transition['description']);
    $GLOBALS['phpgw']->template->set_var('options_source_state', listid_field('phpgw_tts_states', 'state_name', 'state_id', $transition['source_state']));
    $GLOBALS['phpgw']->template->set_var('options_target_state', listid_field('phpgw_tts_states', 'state_name', 'state_id', $transition['target_state']));
    $GLOBALS['phpgw']->template->pfp('out', 'form');
    $GLOBALS['phpgw']->common->phpgw_footer();
}
$GLOBALS['phpgw']->template->set_var('tts_head_status', lang('Status'));
$GLOBALS['phpgw']->template->set_var('tts_notickets', '');
$GLOBALS['phpgw']->template->set_var('lang_category', lang('Category'));
$GLOBALS['phpgw']->template->set_var('delete_state_link', $GLOBALS['phpgw']->link('/tts/delete_state.php', 'state_id=' . $state_id));
$GLOBALS['phpgw']->template->set_var('lang_delete_the_tickets', lang('Delete the listed tickets.'));
$GLOBALS['phpgw']->template->set_var('lang_irregular_move_into_state', lang('Perform irregular transition into the following state'));
$GLOBALS['phpgw']->template->set_var('lang_delete', lang('Delete'));
$GLOBALS['phpgw']->template->set_var('lang_cancel', lang('Cancel'));
$GLOBALS['phpgw']->db->query("select * from phpgw_tts_transitions where transition_source_state=" . $state_id, __LINE__, __FILE__);
while ($GLOBALS['phpgw']->db->next_record()) {
    $GLOBALS['phpgw']->template->set_var('update_state_value', try_lang($GLOBALS['phpgw']->db->f('transition_target_state')));
    $GLOBALS['phpgw']->template->set_var('update_state_text', try_lang($GLOBALS['phpgw']->db->f('transition_description')));
    $GLOBALS['phpgw']->template->parse('update_state_group', 'update_state_items', True);
}
// Choose the initial state to display
$GLOBALS['phpgw']->template->set_var('options_state', listid_field('phpgw_tts_states', 'state_name', 'state_id', '', "state_id<>" . $state_id));
if (!$sort) {
    $sortmethod = 'order by ticket_priority desc';
} else {
    $sortmethod = "order by {$order} {$sort}";
}
$GLOBALS['phpgw']->db->query("select count(*) from phpgw_tts_tickets where ticket_state=" . $state_id, __LINE__, __FILE__);
$GLOBALS['phpgw']->db->next_record();
$numtotal = $GLOBALS['phpgw']->db->f('0');
$GLOBALS['phpgw']->db->query("select count(*) from phpgw_tts_tickets where ticket_status='O' and ticket_state=" . $state_id, __LINE__, __FILE__);
$GLOBALS['phpgw']->db->next_record();
$numopen = $GLOBALS['phpgw']->db->f('0');
$GLOBALS['phpgw']->template->set_var('tts_numtotal', lang('Tickets total %1', $numtotal));
$GLOBALS['phpgw']->template->set_var('tts_numopen', lang('Tickets open %1', $numopen));
$db2 = clone $GLOBALS['phpgw']->db;
$GLOBALS['phpgw']->db->query("select * from phpgw_tts_tickets where ticket_state=" . $state_id . " " . $sortmethod, __LINE__, __FILE__);
    $GLOBALS['phpgw']->template->set_var('optionselected', $entry['account_id'] == $_POST['ticket_group'] ? ' SELECTED ' : '');
    $GLOBALS['phpgw']->template->parse('options_group', 'options_select', true);
}
//produce the list of categories
$s = '<select name="ticket_category">' . $GLOBALS['phpgw']->categories->formated_list('select', 'all', $_POST['ticket_category'], True) . '</select>';
$GLOBALS['phpgw']->template->set_var('value_category', $s);
//produce the list of accounts for assigned to
$s = '<option value="0">' . lang('None') . '</option>';
$accounts = $groups;
$accounts->account_id = $group_id;
$account_list = $accounts->get_list('accounts');
while (list($key, $entry) = each($account_list)) {
    $s .= '<option value="' . $entry['account_id'] . '" ' . ($entry['account_id'] == $_POST['ticket_assignedto'] ? ' SELECTED ' : '') . '>' . $entry['account_lid'] . '</option>';
}
$GLOBALS['phpgw']->template->set_var('value_assignedto', '<select name="ticket_assignedto">' . $s . '</select>');
// Choose the correct priority to display
$prority_selected[$ticket_priority] = ' selected';
$priority_comment[1] = ' - ' . lang('Lowest');
$priority_comment[5] = ' - ' . lang('Medium');
$priority_comment[10] = ' - ' . lang('Highest');
for ($i = 1; $i <= 10; $i++) {
    $priority_select .= '<option value="' . $i . '"' . ($i == $_POST['ticket_priority'] ? ' SELECTED ' : '') . '>' . $i . $priority_comment[$i] . '</option>';
}
$GLOBALS['phpgw']->template->set_var('value_priority', '<select name="ticket_priority">' . $priority_select . '</select>');
// Choose the initial state to display
$GLOBALS['phpgw']->template->set_var('options_state', listid_field('phpgw_tts_states', 'state_name', 'state_id', $_POST['ticket_state'], "state_initial=1"));
$GLOBALS['phpgw']->template->set_var('tts_select_options', '');
$GLOBALS['phpgw']->template->set_var('tts_new_lstcategory', '');
$GLOBALS['phpgw']->template->set_var('tts_new_lstassignto', '');
$GLOBALS['phpgw']->template->pfp('out', 'form');
$GLOBALS['phpgw']->common->phpgw_footer();