<?php

function display_copy_project_approval_path_selection_parameters() {
    start_table(TABLESTYLE_NOBORDER);
        project_list_row(_("Copy To Project:"), 'to_project_id', null, _(''), true, check_value('show_inactive'));
        project_list_row(_("Copy From Project:"), 'from_project_id', null, _(''), true, check_value('show_inactive'));
        start_row();
            label_cell('');
            submit_cells('Refresh', _("Refresh"));
        end_row();
    end_table(1);
}

function display_copy_project_approval_path_details($to_project_id, $from_project_id) {
    $from_project_name = get_project_formatted_name($from_project_id);
    $to_project_name = get_project_formatted_name($to_project_id);
    
    div_start('copy_project_approval_paths');

        start_table(TABLESTYLE, "width=88%");

            $th = array("Id", "Transaction Type", "Approval Path of $from_project_name", "Approval Path of $to_project_name", "Add", "Update");
            table_header($th);

            $k = 0;
            $ap_path_types_array = create_project_approval_path_types_array($to_project_id, $from_project_id);
                foreach ($ap_path_types_array as $path_tp_id => $path_tp_name) {
                    alt_table_row_color($k);

                        label_cell($path_tp_id, 'align=center');
                        label_cell($path_tp_name);
                        
                        $from_row = get_copy_project_approval_path_details($path_tp_id, $from_project_id);
                            is_array($from_row) ? label_cell($from_row["approval_path_name"]) : label_cell('');
                            
                        $to_row = get_copy_project_approval_path_details($path_tp_id, $to_project_id);
                            if (is_array($to_row)) {
                                label_cell($to_row["approval_path_name"]);
                            } else {
                                label_cell('');
                            }
                            
                            if (is_array($from_row) && ! is_array($to_row)) {
                                check_cells('', "add_$path_tp_id", null, false, 'Update', 'align=center');
                            } else {
                                label_cell('');
                            }
                            
                            if (is_array($from_row) && is_array($to_row)) {
                                check_cells('', "update_$path_tp_id", null, false, 'Update', 'align=center');
                            } else {
                                label_cell('');
                            }

                    end_row();
                }

        end_table(2);

    div_end();
}

function can_save_copy_project_approval_paths($to_project_id, $from_project_id) {
    if (! ($to_project_id > 0)) {
        return array('success' => false, 'message' => 'Please select a to project.', 'set_focus' => 'to_project_id');
    }
    
    if (! ($from_project_id > 0)) {
        return array('success' => false, 'message' => 'Please select a from project.', 'set_focus' => 'from_project_id');
    }
    
    if ($to_project_id == $from_project_id) {
        return array('success' => false, 'message' => 'To Project and From Project cannot be the same.', 'set_focus' => 'from_project_id');
    }
    
    $checked_recs = 0;
        $ap_path_types_array = create_project_approval_path_types_array($to_project_id, $from_project_id);
            foreach ($ap_path_types_array as $path_tp_id => $path_tp_name) {
                $checked_add = $checked_update = false;
                
                if (isset($_POST["add_$path_tp_id"]) && $_POST["add_$path_tp_id"] == 1) {
                    $checked_add = true;
                    $checked_recs++;
                }
                
                if (isset($_POST["update_$path_tp_id"]) && $_POST["update_$path_tp_id"] == 1) {
                    $checked_update = true;
                    $checked_recs++;
                }
                
                if ($checked_add === true && $checked_update === true) {
                    return array('success' => false, 'message' => 'Cannot check both checked boxes in a single line.');
                }
            }
            
        if ($checked_recs == 0) {
            return array('success' => false, 'message' => 'Please select one record at least.');
        }
        
            return array('success' => true, 'message' => '');
}

function save_copy_project_approval_paths($to_project_id, $from_project_id) {
    global $systypes_array;
    
//    $st_tp_name = $systypes_array[ST_COPY_PROJECT_APPROVAL_PATH];
    $st_tp_name = 'Copy Project Approval Path';
    
    $cn_msg = can_save_copy_project_approval_paths($to_project_id, $from_project_id);
        if ($cn_msg['success'] === false) {
            return $cn_msg;
        }
        
        $ap_path_types_array = create_project_approval_path_types_array($to_project_id, $from_project_id);
            foreach ($ap_path_types_array as $path_tp_id => $path_tp_name) {
                
                $from_row = get_copy_project_approval_path_details($path_tp_id, $from_project_id);
                
                    $from_approval_path_id = $from_row['approval_path_id'];
                    $from_approval_path_type = $from_row['approval_path_type'];
                
                    if (isset($_POST["add_$path_tp_id"]) && $_POST["add_$path_tp_id"] == 1) {
                        $in_id = add_project_approval_path($to_project_id, $from_approval_path_type, $from_approval_path_id);
                            if (! ($in_id > 0)) {
                                return array('success' => false, 'message' => "Could not add $st_tp_name Id #$path_tp_id.");
                            }
                    }
                    
                    if (isset($_POST["update_$path_tp_id"]) && $_POST["update_$path_tp_id"] == 1) {
                        $to_row = get_copy_project_approval_path_details($path_tp_id, $to_project_id);
                        $af_row = update_project_approval_path($to_row['id'], $from_approval_path_id);
                            if ($af_row != 1) {
                                return array('success' => false, 'message' => "Could not update $st_tp_name Id #$path_tp_id.");
                            }
                    }
            }
            
            return array('success' => true, 'message' => "$st_tp_name have been saved.");    
}

function clear_copy_project_approval_paths($to_project_id, $from_project_id) {
    $ap_path_types_array = create_project_approval_path_types_array($to_project_id, $from_project_id);
        foreach ($ap_path_types_array as $path_tp_id => $path_tp_name) {
            unset($_POST["add_$path_tp_id"]);
            unset($_POST["update_$path_tp_id"]);
        }
}
    
?>