@@ -909,3 +909,72 @@ function file_get_content_type_override( $p_filename ) {
909
909
910
910
return $ g_file_download_content_type_overrides [$ t_extension ];
911
911
}
912
+
913
+ /**
914
+ * Move any attachments as needed when a bug is moved from project to project.
915
+ *
916
+ * @param int $p_bug_id ID of bug containing attachments to be moved
917
+ * @param int $p_project_id_to destination project ID for the bug
918
+ * @return null
919
+ */
920
+ function file_move_bug_attachments ( $ p_bug_id , $ p_project_id_to ) {
921
+ $ t_project_id_from = bug_get_field ( $ p_bug_id , 'project_id ' );
922
+ if ( $ t_project_id_from == $ p_project_id_to ) {
923
+ return ;
924
+ }
925
+
926
+ $ t_method = config_get ( 'file_upload_method ' );
927
+ if ( $ t_method != DISK ) {
928
+ return ;
929
+ }
930
+
931
+ if ( !file_bug_has_attachments ( $ p_bug_id ) ) {
932
+ return ;
933
+ }
934
+
935
+ $ t_path_from = project_get_field ( $ t_project_id_from , 'file_path ' );
936
+ if ( is_blank ( $ t_path_from ) ) {
937
+ $ t_path_from = config_get ( 'absolute_path_default_upload_folder ' , null , null , $ t_project_id_from );
938
+ }
939
+ file_ensure_valid_upload_path ( $ t_path_from );
940
+ $ t_path_to = project_get_field ( $ p_project_id_to , 'file_path ' );
941
+ if ( is_blank ( $ t_path_to ) ) {
942
+ $ t_path_to = config_get ( 'absolute_path_default_upload_folder ' , null , null , $ p_project_id_to );
943
+ }
944
+ file_ensure_valid_upload_path ( $ t_path_to );
945
+ if ( $ t_path_from == $ t_path_to ) {
946
+ return ;
947
+ }
948
+
949
+ # Initialize the update query to update a single row
950
+ $ t_bug_file_table = db_get_table ( 'mantis_bug_file_table ' );
951
+ $ c_bug_id = db_prepare_int ( $ p_bug_id );
952
+ $ query_disk_attachment_update = "UPDATE $ t_bug_file_table
953
+ SET folder= " . db_param () . "
954
+ WHERE bug_id= " . db_param () . "
955
+ AND id = " . db_param ();
956
+
957
+ $ t_attachment_rows = bug_get_attachments ( $ p_bug_id );
958
+ $ t_attachments_count = count ( $ t_attachment_rows );
959
+ for ( $ i = 0 ; $ i < $ t_attachments_count ; $ i ++ ) {
960
+ $ t_row = $ t_attachment_rows [$ i ];
961
+ $ t_basename = basename ( $ t_row ['diskfile ' ] );
962
+
963
+ $ t_disk_file_name_from = file_path_combine ( $ t_path_from , $ t_basename );
964
+ $ t_disk_file_name_to = file_path_combine ( $ t_path_to , $ t_basename );
965
+
966
+ if ( !file_exists ( $ t_disk_file_name_to ) ) {
967
+ chmod ( $ t_disk_file_name_from , 0775 );
968
+ if ( !rename ( $ t_disk_file_name_from , $ t_disk_file_name_to ) ) {
969
+ if ( !copy ( $ t_disk_file_name_from , $ t_disk_file_name_to ) ) {
970
+ trigger_error ( FILE_MOVE_FAILED , ERROR );
971
+ }
972
+ file_delete_local ( $ t_disk_file_name_from );
973
+ }
974
+ chmod ( $ t_disk_file_name_to , config_get ( 'attachments_file_permissions ' ) );
975
+ db_query_bound ( $ query_disk_attachment_update , Array ( db_prepare_string ( $ t_path_to ), $ c_bug_id , db_prepare_int ( $ t_row ['id ' ] ) ) );
976
+ } else {
977
+ trigger_error ( ERROR_FILE_DUPLICATE , ERROR );
978
+ }
979
+ }
980
+ }
0 commit comments